Parsers are used in conjunction with a transport Connector to interpret or generate the content that travels over the Connector's byte stream. However, sometimes you may want to parse data that is presented in a very specific format; for this purpose you will need to implement your own Parser.
All Tivoli® Directory Integrator Parsers implement the com.ibm.di.parser.ParserInterface Java interface. This interface provides a number of methods to implement that are common to all parsers. Usually the parsers that you write will not require implementing all methods provided by the interface but only a subset of them. For this purpose you can use the com.ibm.di.parser.ParserImpl abstract class that implements the ParserInterface. The ParserImpl class contains the core Parser functionality so you can subclass it when implementing your own Parser.
There are two types of parsers: ones that read from a stream and return an Entry; and others that take an Entry and write it to a stream.
Once the Parser is constructed you have to configure it. This includes setting the input/output streams and configuring some additional parameters if needed. This is usually made by the hosting component (for example, a Connector). When finished with this job, the next step is initialization of the Parser where resources for future needs are allocated and any other initialization takes place. Generally the hosting component takes care of both configuring and calling the initialization method of the Parser. Next comes the most significant moment of using the Parser - writing or reading the entries. This is the place where the actual parsing happens. Finally, when the Connector has finished transporting the entries, the Parser must be closed. When closed, the Parser releases the resources that were used in the previous stages as well as closing the input and output streams.
For an example of a Parser implementation, look at the ExampleParser.java Parser included in Tivoli Directory Integrator. These are some of the important methods you will usually need to implement:
Note that when you open the input stream, it is your responsibility to close it. This is usually done in the closeParser() method. The com.ibm.di.parser.ParserImpl abstract class provides default implementation for closing the Parser input and output streams.
Note that when you open the output stream, it is your responsibility to close it. This is usually done in the closeParser() method. The com.ibm.di.parser.ParserImpl abstract class provides a default implementation for closing the Parser input and output streams.
You can allocate resources you may need in future, as well as setting any parameters or additional chained parsers. This method may not be required for all implemented parsers.
Here is an example of how you can access parameters. This set of code is part of the included example "ExampleParser.java".
str = getParam("attributeName");
if (str != null && str.trim().length() != 0) {
attrName = str;
}
Note that this method is called after setting of input and output streams is done.
Make sure you have initialized the input stream properly. In order to set the input stream you can use the setInputStream(...) method. You can use the getReader() method to get the reader object.
Generally input streams are initialized by the hosting component (for example, a Connector).
In order to get the writer you can use the getWriter() method which returns a "java.io.BufferedWriter".
Generally the output stream is initialized by the hosting component (for example, a Connector).
The com.ibm.di.parser.ParserImpl abstract class provides an implementation for this method but if you implement the interface you will have to write it by yourself.
When building the source code of your Parser, include in your CLASSPATH the jar files from the "jars" folder of the IBM® Tivoli Directory Integrator installation. As a minimum, you would need to include "miserver.jar" and "miconfig.jar". Keep in mind that the source code must be compiled for Java 5 or older.
The Parser GUI is implemented in the same way as for a Connector. Use a "tdi.xml" file to describe the Parser configuration form by using the same syntax as used for Connectors.
Packaging and deploying a Parser is just like packaging and deploying a Connector:
You need a jar file that contains:
After you have created the jar file of the new Parser, you only need to drop that jar file in the "jars" folder in the Tivoli Directory Integrator installation. You can create your own folder and put the jar there but the general place where parsers are stored is the "jars/parsers" folder. The next time the Tivoli Directory Integrator is started it will automatically load the new Parser and it will be ready for use.