com.continuent.tungsten.replicator.filter
Class JavaScriptFilter

java.lang.Object
  extended by com.continuent.tungsten.replicator.filter.JavaScriptFilter
All Implemented Interfaces:
Filter, FilterManualProperties, ReplicatorPlugin

public class JavaScriptFilter
extends java.lang.Object
implements FilterManualProperties

This filter allows to develop JavaScript filters without coding any Java at all.

User's script will be precompiled once and then called on every filtered event.

Script must define the following function:
function filter(event) - called on every filtered ReplDBMSEvent.

filter(event) function's return value is handled as follows:
a. null and ReplDBMSEvent - passed through to the JavaScriptFilter's caller,
b. Undefined (no return statement in the JS) - ignored,
c. Everything else - logged into INFO stream.

Further functions may be defined, but are optional:
function prepare() - called when this filter is being prepared.
function release() - called when this filter is being released.

Any of these functions are free to return a value. If they do, value is logged into trep.log INFO stream.

Also there are the following variables exported via JavaScript reflection for user's usage:
properties - TungstenProperties of the current replicator instance;
filterProperties - TungstenProperties subset with the current filter's properties (eg. "script");
logger - this classes Logger. Eg. of usage: logger.info("I'm a script!");
thread - current Thread. Eg. of usage: thread.sleep(1000);

Note: if you wish to call more than one JS file, use multiple instances of this filter with different names defined in replicator.properties

Author:
Linas Virbalas

Constructor Summary
JavaScriptFilter()
           
 
Method Summary
 void configure(PluginContext context)
          Complete plug-in configuration.
 ReplDBMSEvent filter(ReplDBMSEvent event)
          Calls filter(event) function in user's script.
 void prepare(PluginContext context)
          Reads and compiles user's script.
 void release(PluginContext context)
          Calls script's release() function if it exists.
 void setConfigPrefix(java.lang.String configPrefix)
          Set filter's configuration prefix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaScriptFilter

public JavaScriptFilter()
Method Detail

setConfigPrefix

public void setConfigPrefix(java.lang.String configPrefix)
Set filter's configuration prefix. This is important in order for the filter to be able to know where its properties are in the configuration file.
Eg. of how filter's properties could be read:
TungstenProperties filterProperties = properties.subset(configPrefix + ".", true);

Specified by:
setConfigPrefix in interface FilterManualProperties
Parameters:
configPrefix - Configuration prefix.
See Also:
FilterManualProperties.setConfigPrefix(java.lang.String)

filter

public ReplDBMSEvent filter(ReplDBMSEvent event)
                     throws ReplicatorException,
                            java.lang.InterruptedException
Calls filter(event) function in user's script. Blocks until it returns. Filter the event. Filters may transform the event or return null if the event should be discarded. Filters must be prepared to be interrupted, which mechanism is used to cancel processing.

Specified by:
filter in interface Filter
Parameters:
event - An event to be filtered
Returns:
Filtered ReplDBMSEvent or null
Throws:
ReplicatorException - Thrown if there is a processing error
java.lang.InterruptedException - Must be thrown if the filter is interrupted or the replicator may hang
See Also:
Filter.filter(com.continuent.tungsten.replicator.event.ReplDBMSEvent)

configure

public void configure(PluginContext context)
               throws ReplicatorException
Complete plug-in configuration. This is called after setters are invoked at the time that the replicator goes through configuration.

Specified by:
configure in interface ReplicatorPlugin
Throws:
ReplicatorException - Thrown if configuration is incomplete or fails
See Also:
ReplicatorPlugin.configure(com.continuent.tungsten.replicator.plugin.PluginContext)

prepare

public void prepare(PluginContext context)
             throws ReplicatorException
Reads and compiles user's script. Initializes function reflection objects. Calls script's prepare() function if it exists. Prepare plug-in for use. This method is assumed to allocate all required resources. It is called before the plug-in performs any operations.

Specified by:
prepare in interface ReplicatorPlugin
Throws:
ReplicatorException - Thrown if resource allocation fails
See Also:
ReplicatorPlugin.prepare(com.continuent.tungsten.replicator.plugin.PluginContext)

release

public void release(PluginContext context)
             throws ReplicatorException
Calls script's release() function if it exists. Closes JavaScript context. Release all resources used by plug-in. This is called before the plug-in is deallocated.

Specified by:
release in interface ReplicatorPlugin
Throws:
ReplicatorException - Thrown if resource deallocation fails
See Also:
ReplicatorPlugin.release(com.continuent.tungsten.replicator.plugin.PluginContext)