Some 'more, in-depth' information on Oracle BPEL PM, ESB and other SOA, day2day things

Thursday, April 01, 2004

JDK 1.3 - fast quick and dirty object logging? An example ..

When developing an application -
even more when you just develop some parts of it- for me when having other people testing, it's always interesting - what information they fill into the objects they pass along to the methods ..
This becomes even more relevant - if the applications that are using your methods (webservices ...), are tested and developed by a 3rd party.

So during one of my flights a built up a quick example .. for an object-logger, with using java.lang.reflect. package - as I got really fascinated by this runtime-API :) and JDK1.3 doesn't support the XMLEncode ( decode package, that came along with 1.4 ..

The following code is just intended - as an example, of the usage of reflection, but why not using some funnny techs/api*s for such things too?!

package log;

 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;

 import java.util.logging.*;

/**
 * Fast and simple ObjectLogger that prints out the values of
 * public fields and executes all <b>public</b>
 * getXXX methods.
 * This is done by usage of java.lang.reflection API
 * @author Clemens Utschig - Utschig
 * @version 1.0
 * @date   23.03.2004
 */
public class ObjectLogger  {

  /**
   * The apache Logger
   */
  private static Logger mLogger;

  /**
   * GoF Singleton Instance, excutes one time the
   * private constructor
   *
  private static ObjectLogger mObjectLogger = new ObjectLogger();

  /**
   * GoF Singleton - therfor private Constructor - excuted only once,
   * just initalized the logger
   */
  private ObjectLogger() {
   mLogger = Logger.getLogger("ObjectLogger");
  }

  /**
   * Returns the singleton - instance, created as by the
   * member var.
   * @return the instance of the ObjectLogger
   */
  public static ObjectLogger getInstance () {
   return mObjectLogger;
  }

  /**
   * Logs the public fields of a given
   * Object !
   * @param pObject the Object to log
   */
  public static void logObject (Object pObject) {
   if (pObject != null) {
    mLogger.info(" --------- start logging Object ----------");
    mLogger.info("ClassName: " + pObject.getClass().getName());

    // get the publid fields with reflection
    Field [] objectFields = pObject.getClass().getFields();
    if ((objectFields != null) && (objectFields.length > 0)) {
      mLogger.info(" --------- public members ----------");
      for (int fieldIndex = 0; fieldIndex < objectFields.length; fieldIndex++) {
        try {
           mLogger.info(" - Name [" +objectFields[fieldIndex].getName()+ "] " +
                       " type [" +objectFields[fieldIndex].getType()+ "] " +
                      " value [" +objectFields[fieldIndex].get(pObject)+ "] ");
        } catch (IllegalAccessException illegalAccessExc) {
          mLogger.info("IllegalArgumentException");
        }
      }
    }

    // get the pulic methods
    Method [] objectMethods = pObject.getClass().getMethods();
    if ((objectMethods != null) && (objectMethods.length > 0)) {
      mLogger.info(" --------- Invocation of methods ----------");
      for (int objectMethodIndex = 0; objectMethodIndex < objectMethods.length; objectMethodIndex++) {
        try {
          if ((objectMethods[objectMethodIndex].getParameterTypes().length == 0) &&
              (objectMethods[objectMethodIndex].getName().startsWith("get"))){
            mLogger.info(" - MethodName [" + objectMethods[objectMethodIndex].getName()+ "] " +
                              " type ["  + objectMethods[objectMethodIndex].getReturnType().getName() + "]" +
                              " value [" + objectMethods[objectMethodIndex].invoke(pObject, new Object [] {}) + "] ");
          }  
        } catch (InvocationTargetException invocationTargetExc) {
          // do nothing
        } catch (IllegalAccessException illegalAccessExc) {
          // do nothing
        } catch (Exception allOthers) {
          // do nothing too
        }
      }
    }

     mLogger.info(" --------- End logging Object ----------");
   } else {
     mLogger.warning("Object given is NULL !");
   }
    
  }

  /**
   * Logs the public fields/methods of a given Object Array !
   * @param pObject[] the ObjectArray to log
   */
  public static void logObject (Object pObject []) {
    if (pObject != null) {
      mLogger.info(" -- start logging ObjectArray, size: " + pObject.length + " --");
      for (int objectIndex = 0; objectIndex < pObject.length;  objectIndex++) {
        logObject(pObject[objectIndex]);
      }
      mLogger.info(" -- end logging ObjectArray --" );
    } else {
      mLogger.warning("ObjectArray given is null !");
    }
  }

0 Comments:

Post a Comment

<< Home