Try OpenEdge Now
skip to main content
Managed Adapters Guide
File Managed Adapter : Writing custom format plug-ins : Writing the Plug-in run time
 

Writing the Plug-in run time

For writing the Plug-in Run time, the class must implement the interface com.savvion.sbm.adapters.file.FormatPlugin
public interface FormatPlugin {
    public void write(Hashtable input, OutputStream os,
                    Object formatConfig) throws Exception;
    public Hashtable read(Hashtable input, InputStream is,
                    Object formatConfig) throws Exception;
}
The write() method is used in both the Write and Append modes. Here, the "input" hashtable contains all the adapter inputs, defined by the plug-in Configurator’s getParameters() method. In our case, it contains only the "MSG" parameter - the message to be logged in the file. The "os" output stream is already opened by the File Adapter, so the plug-in has to just write the message in the correct format to this stream. The formatConfig object is the custom plug-in configuration. In our case, this is the message format string ([D] M). The write() method can throw an exception if something goes wrong. This exception is logged in the adapter logfile and the workstep execution is suspended.
The read() method accepts a similar set of parameters and is used in the Read mode. The "input" Hashtable contains any optional input parameters. The "is" input stream is already opened from the source file. The "formatConfig" object is the optional custom configuration for the plug-in. The read() method is expected to return a Hashtable, containing all the outputs, as defined by the plug-in configurator's getParameters() method.
Note: Usually the "input" Hashtable parameter of the "read()" method is empty, because in Read mode the plug-in does not require any inputs - in this mode it reads data from the file and returns the processed output. However, some plug-ins with more complex configuration may still require to be supplied with additional inputs, even for this mode - usually to fine tune the way the File Adapter is executed. Those inputs can be mapped to dataslots, and provide a way to control the plug-in behavior dynamically.
The Log Plug-in will support only Write and Append modes, so the only method to implement is write(). The source of the Log Plug-in Run time is described below.
package com.savvion.sbm.adapters.file.plugins;
import com.savvion.sbm.adapters.file.*;
import java.io.*;
import java.util.*;
p public void write(Hashtable input, OutputStream os, Object formatConfig)
        throws Exception
    {
        // Prepare format string, message, and date
        String line = (String)formatConfig;
        String msg = ""+input.get("MSG");
        String date = new java.util.Date().toString();
        
        // Prepare message
        line=line.replaceAll("%D",date);
        line=line.replaceAll("%M",msg);
        line=line+"\n";
        
        // Write message in the log
        os.write(line.getBytes());
    }
    
    public Hashtable read(Hashtable input, InputStream is, Object             formatConfig)
        throws Exception
        {
        return null; // not supported
    }
public class Log implements FormatPlugin {
}
In the write() method, we first obtain the message format ([D] M, or any custom value) - this is our configuration object. Next, we load the message itself, as the "MSG" element of the input Hashtable, as well as get the current date and time.
After this, we prepare the message according to the custom format, and write the message to the log.
You can leave the read() method empty, since this mode is not supported.