In this article you will find out more about importing a SL4J logger and using the JNDI resources from a plugin.


When writing either an enricher or validation plugin in Java, you will almost certainly require logging and you may require database connections. Rather than trying to re-invent the wheel, it is simple and straightforward to piggy back on the JNDI resources stored in the semarchy.xml and also leverage the PDE log from within the code. 

Firstly, to use the JNDI resources you use the fact that Tomcat has loaded these as resources during start-up.  From this, you can get a connection with 3 lines of code (see full code attached for imports):

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/SEMARCHY_CUSTOMER_B2C_MDM");
Connection con = ds.getConnection();

This connection should be managed like any other connection through try-catch blocks and closed once it is finished. The one gotcha is that in some examples the Context object is closed. This will remove the resources from Tomcat, and so the enricher may work the first time, but then once the context is closed it will no longer surface the JNDI data sources. Just don't close the Context object, but definitely still close the any connection, statements or result sets you get from it.

To use the logging is slightly trickier, only because it requires an additional set of libraries that aren't standard Java.  You need to add an "Imported Packages" to import org.slf4j (1.7.0) (note that it must be this version - the most recent version does not work of Semarchy xDM).  See the screenshot below for how to do this.

After adding the dependency, you will now be able to use the slf4j logger.  First you should import the Logger and LoggerFactory:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

And then create a logger.  You really only need 1, so this can be static:

private static Logger log = LoggerFactory.getLogger(TestLoggerEnricher.class);

where TestLoggerEnrichershould be the class of your enricher.  From here, you can simply use the log object to start logging with the standard trace(), debug(), info(), warn() and error() methods.

For a full (but not particularly useful in terms of functionality) code example, please see below:

package TestLogger;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.semarchy.engine.extensionpoint.DataRow;
import com.semarchy.engine.extensionpoint.IDataRow;
import com.semarchy.engine.extensionpoint.IRowTransformer;
import com.semarchy.engine.extensionpoint.IRowTransformerSetupInfo;

public class TestLoggerEnricher implements IRowTransformer {

private static Logger log = LoggerFactory.getLogger(TestLoggerEnricher.class);
public static final String OUTPUT = "OUTPUT";
public static final String INPUT = "INPUT";

public TestLoggerEnricher() {
    // TODO Auto-generated constructor stub
}

@Override
public void setUp(IRowTransformerSetupInfo arg0) {
    // TODO Auto-generated method stub
    log.info("setUp method");
}

@Override
public void tearDown() {
    // TODO Auto-generated method stub
    log.info("tearDown method");
}

@Override
public List<IDataRow> transform(List<IDataRow> pDataRowsBatch) {
    ArrayList<IDataRow> outputDataRowList = new ArrayList<IDataRow>();
    for (IDataRow inputRow : pDataRowsBatch) {
        outputDataRowList.add(transformOneRow(inputRow));
    }
    testDB("java:/comp/env/jdbc/SEMARCHY_CUSTOMER_B2C_MDM");
    return outputDataRowList;
}

public void testDB(String jdbc) {
Context ctx = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
ctx = new InitialContext();
  DataSource ds = (DataSource) ctx.lookup(jdbc);
  con = ds.getConnection();
  stmt = con.createStatement();
  rs = stmt.executeQuery("select * from GD_PERSON");

  while (rs.next()) {
   log.info("first_name=" + rs.getString("last_name"));
  }

 } catch (NamingException e) {
  log.error(e.getMessage());
  e.printStackTrace();
 } catch (SQLException e) {
  log.error(e.getMessage());
  e.printStackTrace();
 } finally {
  try {
   rs.close();
   stmt.close();
   con.close();
   //ctx.close();
  } catch (SQLException e) {
   log.error(e.getMessage());
  } 
 }
}
public IDataRow transformOneRow(IDataRow inputRow) {
    String input = inputRow.getValueAsString(INPUT);
    log.info("input=" + input);
    DataRow outputDataRow = new DataRow();
    outputDataRow.setValue(OUTPUT, "Test");
    return outputDataRow;
    }
}