LibraryLink ToToggle FramesPrintFeedback

Chapter 4. File Marshalers

File component endpoints use a marshaler for processing messages. Poller endpoints rely on the marshaler for reading data off of the file system and normalizing it so it can be passed to the NMR. Sender endpoints rely on the marshaler for determining the name of the file to be written and for converting the normalized messages into the format to be written to the file system.

The default marshaler used by file component endpoints reads and writes valid XML files. It queries the message exchange, and the message, received from the NMR for the name of the outgoing message. The default marshaler expects the file name to be stored in a property called org.apache.servicemix.file.name.

If your application requires different functionality from the marshaler, you can provide a custom marshaler by implementing the org.apache.servicemix.components.util.FileMarshaler interface. You can easily configure your endpoints to use your custom marshaler instead of the default one.

In addition to the default file marshaler, FUSE ESB provides two other file marshalers that file component endpoints can use:

Binary File Marshaler

The binary file marshaler is provided by the class org.apache.servicemix.components.util.BinaryFileMarshaler. It reads in binary data and adds it to the normalized message as an attachment. You can set the name of the attachment and specify a content type for the attachment using the properties shown in Table 4.1.


Flat File Marshaler

The flat file marshaler is provided by the class org.apache.servicemix.components.util.SimpleFlatFileMarshaler. It reads in flat text files and converts them into XML messages.

By default the file is wrapped in a File element. Each line in the file is wrapped in a Line attribute. Each line also has a number attribute that represents the position of the line in the original file.

You can control some aspects of the generated XML file using the properties described in Table 4.2.


To develop a custom file marshaler you need to implement the org.apache.servicemix.components.util.FileMarshaler interface. Example 4.1 shows the interface.


The FileMarshaler interface has three methods that need to be implemented:

readMessage()

The readMessage() method is responsible for reading a file from the file system and converting the data into a normalized message. Table 4.3 describes the parameters used by the method.


getOutputName()

The getOutputName() method returns the name of the file to be written to the file system. The message exchange and the message received by the sender endpoint are passed to the method.

[Important]Important

The returned file name does not contain a directory path. The sender endpoint uses the directory it was configured to use.

writeMessage()

The writeMessage() method is responsible for writing messages received from the NMR to the file system as files. Table 4.4 describes the parameters used by the method.


Example 4.2 shows a simple file mashaler.


You configure a file component endpoint to use a file marshaler using its marshaler attribute. The marshaler attribute's value is a reference to a bean element specifying the class of the file filter implementation.

Example 4.3 shows configuration for a poller endpoint that uses the file marshaler implemented in Example 4.2.


[Note]Note

You can also configure a file component endpoint to use a file marshaler by adding a child marshaler element to the endpoint's configuration. The marshaler element simply wraps the bean element that configures the file marshaler.