Class ScriptConnector

  • All Implemented Interfaces:
    ConnectorInterface, VersionInfoInterface

    public class ScriptConnector
    extends Connector
    implements ConnectorInterface
    The Script Connector enables you to write your own Connector in JavaScript. A Script Connector must implement a few functions to operate. If you plan to use it for iteration purposes only (for example, reading, not searching or updating), you can operate with two functions only. If you plan to use it as a fully qualified Connector, you must implement all functions. The functions do not use parameters. Passing data between the hosting Connector and the script is enabled by using predefined objects. One of these predefined objects is the result object, which is used to communicate status information. Upon entry in either function, the status field is set to normal, which causes the hosting Connector to continue calls. Signaling end-of-input or error is done by setting the status and message fields in this object. Two other script objects are defined upon function entry, the entry object and the search object.
    • Constructor Detail

      • ScriptConnector

        public ScriptConnector()
        Class constructor.
    • Method Detail

      • initialize

        public void initialize​(java.lang.Object obj)
                        throws java.lang.Exception
        Initialize the connector. The connector may be passed a parameter of any kind by the user. It is up to the connector to determine whether this object can be used or not. The parameter is typically provided by a user script. When an AssemblyLine initializes it's Connectors, they are passed a ConnectorMode object.
        Specified by:
        initialize in interface ConnectorInterface
        Overrides:
        initialize in class Connector
        Parameters:
        obj - User provided parameter
        Throws:
        java.lang.Exception - if the initialization of this connector fails.
      • selectEntries

        public void selectEntries()
                           throws java.lang.Exception
        Prepare the Connector for sequential read. If necessary, create a result set to be used for getNextEntry(). When the Connector is used as an Iterator in an AssemblyLine, this method will be called. Default is an empty method.
        Specified by:
        selectEntries in interface ConnectorInterface
        Overrides:
        selectEntries in class Connector
        Throws:
        java.lang.Exception - if an error occurs.
      • getNextEntry

        public Entry getNextEntry()
                           throws java.lang.Exception
        Returns the next Entry from the connector. The entry is populated with attributes and values from the next entry in the input set.

        Example:

         var ctor = input.getConnector();
         var entry = ctor.getNextEntry();
         
         for (; entry != null; entry = ctor.getNextEntry()) {
                main.logmsg("Read entry...");
                main.dumpEntry(entry);
         }
         
        Specified by:
        getNextEntry in interface ConnectorInterface
        Overrides:
        getNextEntry in class Connector
        Returns:
        - the next Entry, or null if no more data
        Throws:
        java.lang.Exception - if an error occurs.
        See Also:
        ConnectorInterface.selectEntries()
      • modEntry

        public void modEntry​(Entry entry,
                             SearchCriteria search)
                      throws java.lang.Exception
        Modifies an existing entry. The new entry data is given by the entry parameter and the search criteria specifies which entry to modify.
        Specified by:
        modEntry in interface ConnectorInterface
        Overrides:
        modEntry in class Connector
        Parameters:
        entry - The entry data
        search - The search criteria used to locate the entry to be modified
        Throws:
        java.lang.Exception - if an error occurs.
      • modEntry

        public void modEntry​(Entry entry,
                             SearchCriteria search,
                             Entry old)
                      throws java.lang.Exception
        Modifies an existing entry. The new entry data is given by the entry parameter and the search criteria specifies which entry to modify.
        Specified by:
        modEntry in interface ConnectorInterface
        Overrides:
        modEntry in class Connector
        Parameters:
        entry - The entry data
        search - The search criteria used to locate the entry to be modified
        old - The old entry found by the search criteria
        Throws:
        java.lang.Exception - if an error occurs.
      • deleteEntry

        public void deleteEntry​(Entry entry,
                                SearchCriteria search)
                         throws java.lang.Exception
        Deletes an existing entry. The search criteria specifies which entry to modify. Some connectors may silently ignore the search criteria. For example, the LDAP connector will use the distinguished name ($dn) from the entry parameter (if it exists) rather than expanding the search criteria and search for the entry. Each connector's inner semantics governs whether the search parameter is used or not.
        Specified by:
        deleteEntry in interface ConnectorInterface
        Overrides:
        deleteEntry in class Connector
        Parameters:
        entry - The entry data
        search - The search criteria used to locate the entry to be deleted
        Throws:
        java.lang.Exception - if an error occurs.
      • findEntry

        public Entry findEntry​(SearchCriteria search)
                        throws java.lang.Exception
        Finds an existing entry. The search criteria specifies which entry to locate

        Here is an example of how to find all people with names starting with 'J' which are from IBM organization in US.

        Example:

         var ctor = input.getConnector();
         var crit = new com.ibm.di.SearchCriteria("$dn",
                        com.ibm.di.SearchCriteria.SUBSTRING, "c=US,o=IBM");
         crit.addCriteria("name", com.ibm.di.SearchCriteria.INITIAL_STRING, "J");
         crit.addCriteria("objectclass", com.ibm.di.SearchCriteria.SUBSTRING, "person");
         
         var res = ctor.findEntry(crit);
         if (res != null) {
                main.logmsg("Found entry:");
                main.dumpEntry(res);
         } else {
          if( getFindEntryCount()> 1 ){
           main.logmsg("Found these entries:");
           while ( (entry = ctor.getNextFindEntry()) != null ) {
             main.dumpEntry(entry);
           }
          }
          else {
                 main.logmsg("Entry not found!");
          }
         }
         
        Specified by:
        findEntry in interface ConnectorInterface
        Overrides:
        findEntry in class Connector
        Parameters:
        search - The search criteria used to locate the entry to be modified
        Returns:
        The entry found, or null if no or multiple entries found
        Throws:
        java.lang.Exception - if an error occurs.
      • putEntry

        public void putEntry​(Entry entry)
                      throws java.lang.Exception
        Adds a new entry to the data source

        Example:

         var ctor = write.getConnector();
         
         for (i = 0; i < 10; i++) {
                var entry = system.newEntry();
                entry.setAttribute("linenumber", i);
                entry.setAttribute("line", i + " line of text...");
         
                main.logmsg("Writes entry to output...");
                main.dumpEntry(entry);
                ctor.putEntry(entry);
         }
         
        Specified by:
        putEntry in interface ConnectorInterface
        Overrides:
        putEntry in class Connector
        Parameters:
        entry - The entry data to add
        Throws:
        java.lang.Exception - if an error occurs.
      • queryReply

        public Entry queryReply​(Entry entry)
                         throws java.lang.Exception
        Performs a query/reply operations.
        Specified by:
        queryReply in interface ConnectorInterface
        Overrides:
        queryReply in class Connector
        Parameters:
        entry - The data used in outgoing call
        Returns:
        The entry returned by the peer
        Throws:
        java.lang.Exception - if an error occurs.
      • terminate

        public void terminate()
                       throws java.lang.Exception
        Terminate the connector. This function closes all connection and releases all resources used by the connector. This function also calls the parser's closeParser() method if a parser is active.
        Specified by:
        terminate in interface ConnectorInterface
        Overrides:
        terminate in class Connector
        Throws:
        java.lang.Exception - if an error occurs.
      • querySchema

        public java.lang.Object querySchema​(java.lang.Object source)
                                     throws java.lang.Exception
        This function translates to whatever means a connector has to discover schema for a connection. The connector may implement this, in which case a Vector of Entry objects is returned for each column/attribute it discovered. For a database connector this would typically be column names and their attributes.

        Each Entry in the Vector returned should contain the following attributes:

        Name Value
        name The name of the column/attribute/field ....
        syntax The syntax or expected value type
        size If specified this will give the user a hint as to how long the field may be

        Specified by:
        querySchema in interface ConnectorInterface
        Overrides:
        querySchema in class Connector
        Parameters:
        source - The object on which to discover schema. This may be an Entry or a string value
        Returns:
        A Vector of com.ibm.di.entry.Entry objects describing each entity
        Throws:
        java.lang.Exception - if an error while retrieving the schema occurs.
        See Also:
        Entry, Vector
      • getVersion

        public java.lang.String getVersion()
        Return version information
        Specified by:
        getVersion in interface VersionInfoInterface
        Returns:
        version info