Wednesday, May 9, 2012

Log4J implementation for WPS

LoggerCache.java


import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Maintains a cache of loggers to be used by custom mediation components.
 */
public class LoggerCache {

private Hashtable<String, Logger> theStore;
private static LoggerCache theInstance;

/**
* Constructs a <code>LoggerCache</code>
*/
protected LoggerCache() {
theStore = new Hashtable<String, Logger>();
}

/**
* @return a singleton instance of <code>LoggerCache</code>
*/
public static synchronized LoggerCache getInstance() {
if (theInstance == null) {
theInstance = new LoggerCache();
}
return theInstance;
}

/**
* Retrieves a logger from the cache for the provided name.
* If the logger is not already in the cache, it will be created
* and placed inside the cache.
* @param name the logger name
* @return the <code>Logger</code> from the cache
*/
public synchronized Logger getLogger(final String name) {

if (name == null) {
throw new NullPointerException("name == null");
}

Logger theLogger = this.theStore.get(name);
if (theLogger == null) {

String logPropertiesFilePath = null;
theLogger = Logger.getLogger(TCLogger.class);
try {
Context ctx=new InitialContext();
logPropertiesFilePath = (String)ctx.lookup("cell/persistent/logPropertiesFilePath");
} catch (NamingException e) {
e.printStackTrace();
}
PropertyConfigurator.configure(logPropertiesFilePath);

this.theStore.put(name, theLogger);
}

return theLogger;
}

}
TCLogger.java


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

import com.ibm.websphere.bo.BOXMLSerializer;
import com.ibm.websphere.sca.ServiceManager;
import commonj.sdo.DataObject;

/**
 * LoggerHelper provides logging facility for all levels of logging. 
 * @author Ajay A Suman
 * @since 22 Sep 2011
 */
public class TCLogger {
private TCLogger() {
// intended
}
/**
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Name of the component being entered/invoked
* Note: Entry is only logged if Level is set to INFO
*/
public static void entering(String flowName, String componentName) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isInfoEnabled()) {
logger.info(flowName + " " + componentName + " Entering");
}
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}
/**
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Name of the component being exited/returned from
* Note: Exit is only logged if Level is set to INFO
*/
public static void exiting(String flowName, String componentName){
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isInfoEnabled()) {
logger.info(flowName + " " + componentName + " Exiting");
}
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}
/**
* Method to log messages intended for Info level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is
*            raised String msg - Custom message to be logged
* @param DataObject dataObject - The business object to be logged along with the message.
*/
public static void logInfo(String flowName, String componentName, String msg,
DataObject dataObject) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isInfoEnabled()) {
logger.info(flowName + " " + componentName + " " + msg + "\n" + dataObjectToString(dataObject));
}
}
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}

/**
* Method to log messages intended for Info level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
*/
public static void logInfo(String flowName, String componentName, String msg) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isInfoEnabled()) {
logger.info(flowName + " " + componentName + " " + msg);
}
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}
/**
* Method to log messages intended for Warning level. These need to be used in cases where
* exceptions have been handled appropriately.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
* @param commonj.sdo.DataObject dataObject - The business object to be logged along with the message.
*/
public static void logWarning(String flowName, String componentName, String msg,
DataObject dataObject) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
logger.warn(flowName + " " + componentName + " " + msg + "\n" + dataObjectToString(dataObject));
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}

/**
* Method to log messages intended for Warning level. These need to be used in cases where
* exceptions have been handled appropriately.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
*/
public static void logWarning(String flowName, String componentName, String msg) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
logger.warn(flowName + " " + componentName + " " + msg);
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}

/**
* Method to log messages intended for Severe level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
* @param DataObject dataObject - The business object to be logged along with the message.
*/
public static void logSevere(String flowName, String componentName, String msg,
DataObject dataObject) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
logger.error(flowName + " " + componentName + " " + msg + "\n" + dataObjectToString(dataObject));
catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}
/**
* Method to log messages intended for Severe level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
*/
public static void logSevere(String flowName, String componentName, String msg) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
logger.error(flowName + " " + componentName + " " + msg);
} catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}

/**
* Method to log messages intended for Finest level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
* @param DataObject dataObject - The business object to be logged along with the message.
*/
public static void logFinest(String flowName, String componentName, String msg,
DataObject dataObject) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isDebugEnabled()) {
logger.debug(flowName + " " + componentName + " " + msg + "\n" + dataObjectToString(dataObject));
}  catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}

/**
* Method to log messages intended for Finest level.
* @param String flowName - Name of the BPEL/Mediation Flow Component
* @param String componentName - Logical name of the component where the log is raised 
* @param String msg - Custom message to be logged
*/
public static void logFinest(String flowName, String componentName, String msg) {
LoggerCache loggerStore = LoggerCache.getInstance();
try{
if(null == flowName){
flowName = TCLogger.class.getName();
}
Logger logger = loggerStore.getLogger(flowName);
if (logger.isDebugEnabled()) {
logger.debug(flowName + " " + componentName + " " + msg);
}  catch(Exception e){
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String serialForm = stringWriter.toString();
System.out.println("Exception occured while logging!!!! \n" +serialForm);
}
}
/**
* Method to serialise a data object.
* @param dataObject
* @return serialised dataObject String
*/
private static String dataObjectToString(DataObject dataObject) {
String serialForm = null;
BOXMLSerializer boXMLSe = (BOXMLSerializer) ServiceManager.INSTANCE
.locateService("com/ibm/websphere/bo/BOXMLSerializer");
if (dataObject != null) {
// Serialise the BO to XML. The serializer  produces UTF-8 byte encoding.
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
boXMLSe.writeDataObject(dataObject, dataObject.getType()
.getURI(), dataObject.getType().getName(), output);
serialForm = output.toString("UTF-8");
} catch (IOException e) {
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
serialForm = stringWriter.toString();
}
} else {
serialForm = "Null DataObject";
}
return serialForm;
}
}

No comments:

Post a Comment