Class ImportHandler

java.lang.Object
org.spiderwiz.core.ImportHandler

public class ImportHandler
extends Object
This class provides a mechanism for importing data from external resources into the framework and exporting data from the framework to external resources. The mechanism involves the following steps:
  • Configuring an import channel through the import-n property defined in the application's configuration file.
  • Retrieving imported objects from the channel.
  • For each object, creating an instance of each data object that the application produces and letting it have a chance to interpret the data by calling its importObject() method.
  • Whenever a data object is committed its exportObject() method is called, and if it returns a non-null object, the object is exported to the import channel.

The mechanism provided by this class supports importing and exporting of serialized data through one of the communication channels supported by the framework (or a communication plugin). It may happen that you want to hook into the basic mechanism and provide some extra processing in one or more of its steps. You can do it by extending the class. If you do, you should register your implementation with Main.createImportHandler() to make it a substitute to the default class. This is called a serialize Import handler.

You can also implement a general object Import handler, in which case you would write the code for retrieving and delivering import/export objects directly.

Override configure() to customize your handler configuration.

To modify the handler behavior in connection and disconnection events override onConnect(), onDisconnect() and onConnectFailed().

To extend the handling of imported serialized data override processLine().

General object handlers should call processObject() to feed the framework with imported objects.

Override exportObject() to handle exported objects in the case of a general object handler, or to extend the default implementation in the case of serialize handlers.

To do custom cleanup code when the handler is discarded override cleanup().

Note that each instance of this class manages one Channel object (in the case of serialize handler). You can get the object by calling getChannel().

See Also:
Channel, Main.ObjectCodes.RawImport, Main.ObjectCodes.RawExport, Main.createImportHandler()
  • Constructor Details

  • Method Details

    • configure

      protected boolean configure​(Map<String,​String> configParams, int n)
      Configures and initializes the handler.

      configures and initializes the handler from from parameters specified by import-n properties in the application's configuration file. The values of these properties must be a list of pairs key=value concatenated by a semicolon.

      The default implementation of the method initializes a serialized data channel as documented there. Implementations of serialize Import handlers can override this method to add custom initialization code after calling super.configure(). Implementations of general object handlers can override it to provide their own configuration and initialization code.

      Parameters:
      configParams - a map of key=value configuration parameters.
      n - the n value of the import-n property.
      Returns:
      true on success.
    • onConnect

      protected void onConnect()
      Called when the channel managed by this handler is connected.

      Override this method to do extra processing when the channel managed by this handler is connected. Your implementation must call super.onConnect().

    • onDisconnect

      protected void onDisconnect​(String reason)
      Called when the channel managed by this handler is disconnected.

      Override this method to do extra processing when the channel managed by this handler is disconnected. Your implementation must call super.onDisconnect(reason).

      Parameters:
      reason - text describing the disconnection reason as implemented by Channel.
    • onConnectFailed

      protected void onConnectFailed​(Exception ex)
      Called when the channel managed by this handler could not connect.

      Override this method to do extra processing when the channel managed by this handler could not connect. Your implementation must call super.onConnectFailed(ex).

      Parameters:
      ex - the exception object that was thrown on the attempt to connect, if any, or null if not applicable.
    • processLine

      protected void processLine​(String line, ZDate ts)
      Processes an imported serialized data line.

      Override this method if you implement a serialize importer and you want to do extra processing on imported lines before passing them over to the framework. To pass the line to the framework call super.processLine(line, ts).

      Parameters:
      line - record content.
      ts - timestamp attached to the line if provided by the channel managed by this handler.
    • processObject

      protected void processObject​(Object importObject, ZDate ts, int size) throws Exception
      Processes an imported object.

      You can call this method when you implement a general object import handler in order to process one import object.

      Parameters:
      importObject - the imported object
      ts - timestamp of the import. If null, the current time is used.
      size - size of the object in bytes, for statistic purposes (logging and SpiderAdmin).
      Throws:
      Exception
    • exportObject

      protected boolean exportObject​(Object data)
      Exports an object.

      Override this method to do extra processing on exported records before transmitting them over the channel managed by this handler (if you implement a serialize importer), or to export it in a form other than a serialized string (if you implement a general object importer). In the first case, call super.exportObject(data) to transmit the data as a string.

      Parameters:
      data - the exported record.
      Returns:
      true if and only if record was transmitted successfully.
    • getChannel

      protected final Channel getChannel()
      Returns the Channel object managed by this handler.
      Returns:
      the Channel object managed by this handler.
    • cleanup

      protected void cleanup()
      Cleans up object resources.

      Override this method to do custom resource cleanup code when the handler is discarded.

    • getServerVersion

      protected String getServerVersion()
      Returns the version of the server application the channel managed by this handler is connected to, or null if not applicable.

      Override this method to return a string representing the version of the server application this handler is connected to.

      Returns:
      the version of the server application the channel managed by this handler is connected to, or null if not applicable or by default.
    • getName

      public String getName()
      Returns the connection name.

      Returns the name attached to the connection managed by this handler in the application's configuration file. If a name is not attached, the value of n used in the import-n property defined in that file is returned.

      Returns:
      the connection name.
    • getRemoteAddress

      public String getRemoteAddress()
      Returns the remote address of the connected entity.
      Returns:
      the remote address of the connected entity.