CallbackUtil.java :  » Workflow-Engines » shark » org » enhydra » shark » Java Open Source

Java Open Source » Workflow Engines » shark 
shark » org » enhydra » shark » CallbackUtil.java
package org.enhydra.shark;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
import org.enhydra.shark.api.internal.working.CallbackUtilities;

/**
 * Implementation of Callback Utilities interface.
 * 
 * @author Sasa Bojanic
 * @author Tanja Jovanovic
 */
public class CallbackUtil implements CallbackUtilities, Serializable {

   protected Properties properties;

   protected Map methods;

   protected long everything;

   protected boolean logObjectToo;

   protected boolean logStackTrace;

   protected Map classes;

   protected CallbackUtil() {
   }

   // ////////////////////////////////////////////////////////////////
   // Callback API implementation
   // ////////////////////////////////////////////////////////////////

   /**
    * Returns value of property <i>propertyName </i> used for Shark configuration.
    * 
    * @param name property name.
    * @return Value of property <i>name </i>.
    */
   public String getProperty(String name) {
      return properties.getProperty(name);
   }

   /**
    * Returns value of property <i>name </i> used for Shark configuration. If doesn't
    * exist the default value is returned.
    * 
    * @param name property name.
    * @param defaultValue default property value.
    * @return Value of property <i>name </i> or default value.
    */
   public String getProperty(String name, String defaultValue) {
      return properties.getProperty(name, defaultValue);
   }

   /**
    * Returns all properties used for Shark configuration.
    * 
    * @return Properties object.
    */
   public Properties getProperties() {
      return properties;
   }

   /**
    * Sets properties for Shark configuration.
    * 
    * @param props Properties object.
    */
   public void setProperties(Properties props) {
      this.properties = props;
      methods = new HashMap();
      classes = new HashMap();
      everything = -1;
      logObjectToo = true;
      logStackTrace = false;
      for (Iterator iter = getProperties().entrySet().iterator(); iter.hasNext();) {
         Map.Entry element = (Map.Entry) iter.next();
         if (((String) element.getKey()).startsWith("CallbackUtil.TimeProfiler.")) {
            String tmp = ((String) element.getKey()).substring(26);
            if (tmp.equals("level")) {
               String v = (String) element.getValue();
               if (v.equalsIgnoreCase("warn")) {
                  logObjectToo = logStackTrace = false;
               } else if (v.equalsIgnoreCase("debug")) {
                  logObjectToo = logStackTrace = true;
               }
               continue;
            } else if (tmp.equals("default")) {
               everything = Long.parseLong((String) element.getValue());
               continue;
            }
            if (tmp.indexOf('.') > -1) {
               methods.put(tmp, Long.decode((String) element.getValue()));
            } else {
               classes.put(tmp, Long.decode((String) element.getValue()));
            }
         }
      }
   }

   public boolean isEnabled (WMSessionHandle shandle, int level) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            return SharkEngineManager.getInstance().getLoggingManager().isEnabled(shandle, level);
         } catch (Exception e) {
            System.err.println("Error checking if level is enabled - errMsg=" + e.getMessage());
         }
      }
      return false;
   }
   
   public boolean isEnabled (WMSessionHandle shandle, String channel,int level) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            return SharkEngineManager.getInstance().getLoggingManager().isEnabled(shandle, channel, level);
         } catch (Exception e) {
            System.err.println("Error checking if level is enabled - errMsg=" + e.getMessage());
         }
      }
      return false;
   }

   /**
    * Log a message object with the <i>ERROR </i> Level.
    * 
    * @param msg the message to log.
    */
   public void error(WMSessionHandle shandle, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().error(shandle, msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>ERROR </i> Level.
    * 
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void error(WMSessionHandle shandle, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().error(shandle, msg, ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>ERROR </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    */
   public void error(WMSessionHandle shandle, String channel, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().error(shandle,
                                                                       channel,
                                                                       msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>ERROR </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void error(WMSessionHandle shandle, String channel, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().error(shandle,
                                                                       channel,
                                                                       msg,
                                                                       ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>WARN </i> Level.
    * 
    * @param msg the message to log.
    */
   public void warn(WMSessionHandle shandle, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().warn(shandle, msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>WARN </i> Level.
    * 
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void warn(WMSessionHandle shandle, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().warn(shandle, msg, ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>WARN </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    */
   public void warn(WMSessionHandle shandle, String channel, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().warn(shandle,
                                                                      channel,
                                                                      msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>WARN </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void warn(WMSessionHandle shandle, String channel, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().warn(shandle,
                                                                      channel,
                                                                      msg,
                                                                      ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>INFO </i> Level.
    * 
    * @param msg the message to log.
    */
   public void info(WMSessionHandle shandle, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().info(shandle, msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>INFO </i> Level.
    * 
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void info(WMSessionHandle shandle, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().info(shandle, msg, ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>INFO </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    */
   public void info(WMSessionHandle shandle, String channel, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().info(shandle,
                                                                      channel,
                                                                      msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>INFO </i> Level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void info(WMSessionHandle shandle, String channel, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().info(shandle,
                                                                      channel,
                                                                      msg,
                                                                      ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>DEBUG </i> level.
    * 
    * @param msg the message to log.
    */
   public void debug(WMSessionHandle shandle, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().debug(shandle, msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>DEBUG </i> level.
    * 
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void debug(WMSessionHandle shandle, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().debug(shandle, msg, ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>DEBUG </i> level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    */
   public void debug(WMSessionHandle shandle, String channel, String msg) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().debug(shandle,
                                                                       channel,
                                                                       msg);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   /**
    * Log a message object with the <i>DEBUG </i> level.
    * 
    * @param channel the log channel to be used for logging.
    * @param msg the message to log.
    * @param ex the exception to log, including its stack trace.
    */
   public void debug(WMSessionHandle shandle, String channel, String msg, Exception ex) {
      if (SharkEngineManager.getInstance().getLoggingManager() != null) {
         try {
            SharkEngineManager.getInstance().getLoggingManager().debug(shandle,
                                                                       channel,
                                                                       msg,
                                                                       ex);
         } catch (Exception e) {
            System.err.println("Error in logging - errMsg=" + e.getMessage());
         }
      }
   }

   public String getUserId (WMSessionHandle shandle) {
      String ret=null;
      if (shandle!=null) {
         return shandle.getVendorData().toString();
      }
      return ret;
   }
   
   /**
    * 
    */
   public long methodStart(WMSessionHandle shandle, String location) {
      return lenght4location(location) > -1 ? System.currentTimeMillis() : -1;
   }

   public void methodEnd(WMSessionHandle shandle, long stamp, String location, Object o) {
      methodEnd(shandle, stamp, location, o, "TimeProfiler");
   }

   public void methodEnd(WMSessionHandle shandle,
                         long stamp,
                         String location,
                         Object o,
                         String channel) {
      if (stamp > -1) {
         long tmp = (System.currentTimeMillis() - stamp);
         if (tmp >= lenght4location(location)) {
            String logEntry = location + " took " + tmp + " millis";
            if (logObjectToo) {
               logEntry += " for "
                           + ((o != null) ? o.toString() : "null") + ","
                           + ((shandle != null) ? shandle.toString() : "");
            }
            if (!logStackTrace) {
               info(shandle, channel, logEntry);
            } else {
               info(shandle, channel, logEntry, new Exception(channel));
            }
         }
      }
   }

   protected long lenght4location(String location) {
      if (methods.containsKey(location)) {
         return ((Long) methods.get(location)).longValue();
      }
      location = location.substring(0, location.lastIndexOf('.'));
      if (classes.containsKey(location)) {
         return ((Long) classes.get(location)).longValue();
      }
      return everything;
   }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.