You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by wh...@apache.org on 2002/08/14 18:01:50 UTC

cvs commit: xml-axis-wsif/java/src/org/apache/wsif WSIFException.java

whitlock    2002/08/14 09:01:50

  Modified:    java/src/org/apache/wsif/base WSIFServiceImpl.java
               java/src/org/apache/wsif/logging Trc.java
               java/src/org/apache/wsif WSIFException.java
  Log:
  Improve trace
  
  Revision  Changes    Path
  1.24      +12 -12    xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceImpl.java
  
  Index: WSIFServiceImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/base/WSIFServiceImpl.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- WSIFServiceImpl.java	8 Aug 2002 15:24:31 -0000	1.23
  +++ WSIFServiceImpl.java	14 Aug 2002 16:01:49 -0000	1.24
  @@ -1013,19 +1013,19 @@
       public String deep() {
           String buff = "";
           try {
  -            buff = new String(this.toString() + "\n");
  -            buff += " providersExtRegs:"
  +            buff = new String(this.toString());
  +            buff += "\nprovidersExtRegs:"
                   + (providersExtRegs == null ? "null" : providersExtRegs.toString());
  -            buff += " msgFactory:" + (msgFactory == null ? "null" : msgFactory.toString());
  -            buff += " def:" + Trc.brief(def);
  -            buff += " service:" + Trc.brief(service);
  -            buff += " portType:" + Trc.brief(portType);
  -            buff += " myPortsArr:" + (myPortsArr == null ? "null" : myPortsArr.toString());
  -            buff += " myPortsMap:" + (myPortsMap == null ? "null" : myPortsMap.toString());
  -            buff += " typeMap:" + (typeMap == null ? "null" : typeMap.toString());
  -            buff += " typeMapInitialised:" + typeMapInitialised;
  -            buff += " preferredPort:" + (preferredPort == null ? "null" : preferredPort);
  -            buff += " chosenPort:" + Trc.brief(chosenPort);
  +            buff += "\nmsgFactory:" + (msgFactory == null ? "null" : msgFactory.toString());
  +            buff += "\ndef:" + Trc.brief(def);
  +            buff += "\nservice:" + Trc.brief(service);
  +            buff += "\nportType:" + Trc.brief(portType);
  +            buff += "\nmyPortsArr:" + (myPortsArr == null ? "null" : myPortsArr.toString());
  +            buff += "\nmyPortsMap:" + Trc.brief(myPortsMap);
  +            buff += "\ntypeMap:" + (typeMap == null ? "null" : typeMap.toString());
  +            buff += "\ntypeMapInitialised:" + typeMapInitialised;
  +            buff += "\npreferredPort:" + (preferredPort == null ? "null" : preferredPort);
  +            buff += "\nchosenPort:" + Trc.brief(chosenPort);
           } catch (Exception e) {
               Trc.exceptionInTrace(e);
           }
  
  
  
  1.4       +149 -33   xml-axis-wsif/java/src/org/apache/wsif/logging/Trc.java
  
  Index: Trc.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/logging/Trc.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Trc.java	26 Jul 2002 09:37:18 -0000	1.3
  +++ Trc.java	14 Aug 2002 16:01:49 -0000	1.4
  @@ -60,6 +60,11 @@
   import java.io.PrintWriter;
   import java.io.StringWriter;
   import java.util.Collection;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.Map;
  +import java.util.StringTokenizer;
  +
   import javax.wsdl.Binding;
   import javax.wsdl.BindingOperation;
   import javax.wsdl.Definition;
  @@ -72,10 +77,23 @@
   import org.apache.commons.logging.LogFactory;
   
   /**
  + * Trc provides trace support for WSIF and is a wrapper around
  + * commons-logging. It adds value to commons-logging by providing
  + * entry/exit/exception trace; never throwing an exception back 
  + * even if asked to trace something really stupid; and tracing
  + * references to large WSDL objects, unless specifically asked to
  + * trace out the entire object. Trc also provides brief() methods
  + * (called from deep() methods) which trace out WSDL objects (and
  + * others) in a consistent and helpful way. The goals of Trc are
  + * to not impact performance if trace is off; to provide a rich
  + * and easy-to-use API for WSIF; to enable creation of trace files
  + * that will help developers to easily diagnose problems in WSIF.
  + * 
    * @author Mark Whitlock <wh...@apache.org>
    */
   public class Trc {
  -    private static Log log = LogFactory.getLog("wsif");
  +	private static final String wsifPackageName = "org.apache.wsif";
  +    private static Log log = LogFactory.getLog(wsifPackageName+".*");
       public static boolean ON = log.isDebugEnabled();
   
       private final static String ENTRY = "ENTRY";
  @@ -83,8 +101,6 @@
       private final static String EXCEPTION = "EXCEPTION";
       private final static String EVENT = "EVENT";
       
  -    private static int depth = 0;
  -
       /**
        * Private constructor ensures no one can instantiate this class.
        */
  @@ -738,6 +754,46 @@
           return "";
       }
   
  +    /**
  +     * Emulates map.toString() except does a checkWsdl() on all 
  +     * the elements of the map.
  +     */
  +    public static String brief(Map map) {
  +        try {
  +            if (!ON)
  +                return "";
  +            if (map == null)
  +                return "<null>";
  +            if (map.isEmpty())
  +                return "size(0)";
  +
  +            StringBuffer result = new StringBuffer("{");
  +            boolean first = true;
  +            Iterator it = map.keySet().iterator();
  +
  +            while (it.hasNext()) {
  +                String n = (String) it.next();
  +                Object value = map.get(n);
  +                if (value == null)
  +                    value = "<null>";
  +                Object v2 = checkWsdl(value);
  +
  +                if (!first)
  +                    result.append(", ");
  +                first = false;
  +                result.append(n);
  +                result.append("=");
  +                result.append(v2.toString());
  +            }
  +
  +            result.append("}");
  +            return result.toString();
  +        } catch (Exception e) {
  +            exceptionInTrace(e);
  +        }
  +        return "";
  +    }
  +    
       private static void checkWsdl(Object[] parms) {
           if (parms == null)
               return;
  @@ -803,6 +859,27 @@
                   str = "binding(UNNAMED";
               else
                   str = "binding(" + b.getQName();
  +        } else if (o instanceof Map) {
  +            Map map = (Map) o;
  +            HashMap newMap = null;
  +            Iterator it = map.keySet().iterator();
  +            while (it.hasNext()) {
  +                Object key = it.next();
  +                if (key == null)
  +                    continue;
  +                Object value = map.get(key);
  +                if (value == null)
  +                    continue;
  +                Object alt = checkWsdl(value);
  +                if (!value.equals(alt)) {
  +                    if (newMap == null)
  +                        newMap = new HashMap(map);
  +                    newMap.put(key, alt);
  +                }
  +            }
  +            if (newMap != null)
  +                return newMap;
  +            return map;
           }
   
           if (!found)
  @@ -812,35 +889,82 @@
           return str;
       }
   
  -    private static String getMethodName() {
  +    /**
  +     * This method calculates the name of the WSIF method that is
  +     * being traced. This could be passed as a parameter to the Trc
  +     * call, but making Trc simpler to invoke encourages developers 
  +     * to add trace to the code and avoids confusing the code with 
  +     * lengthy trace statements. We are able to calculate the method
  +     * name here by parsing a stack trace. This is slow but only happens
  +     * when trace is on. Hopefully this will be a fraction of the 
  +     * time it takes to write the trace to disk.
  +     */
  +    private static void appendMethodName(StringBuffer buff) {
           Exception e = new Exception();
           StringWriter sw = new StringWriter();
           PrintWriter pw = new PrintWriter(sw);
           e.printStackTrace(pw);
           String stack = sw.getBuffer().toString();
   
  -        int idx = stack.indexOf("org.apache.wsif.logging");
  -        while (idx != -1) {
  -            stack = stack.substring(idx + 12);
  -            idx = stack.indexOf("org.apache.wsif.logging");
  -        }
  -
  -        idx = stack.indexOf("org.apache.wsif");
  -        stack = stack.substring(idx);
  -
  -        idx = stack.indexOf("(");
  -        stack = stack.substring(0, idx);
  -
  -        String previous = stack;
  -        idx = stack.indexOf(".");
  -        while (idx != -1) {
  -            previous = stack;
  -            stack = stack.substring(idx + 1);
  -            idx = stack.indexOf(".");
  +        // The next while loop tries to find the method that called
  +        // Trc. The method name will have (...parmeters...) after it
  +        // and will be after the last call to Trc. 
  +        StringTokenizer st1 = new StringTokenizer(stack);
  +        boolean foundWsifLogging = false;
  +        String tok = null;
  +        while (st1.hasMoreTokens()) {
  +            tok = st1.nextToken();
  +            if (tok.indexOf("(") == -1)
  +                continue;
  +
  +            if (tok.startsWith(wsifPackageName + ".logging")) {
  +                foundWsifLogging = true;
  +                continue;
  +            }
  +
  +            if (foundWsifLogging)
  +                break;
  +        }
  +
  +        // Indent the method name by the number of WSIF calls 
  +        // higher up the stack. This improves readability. There
  +        // were various other ways of calculating the indentation
  +        // but this seemed the most reliable (and simplest).
  +        buff.append(" ");
  +        while (st1.hasMoreTokens()) {
  +            if (st1.nextToken().startsWith(wsifPackageName))
  +                buff.append(" ");
           }
  -        return previous;
  +
  +        // Strip off the (... parameters...). I expect there will
  +        // always be a ( in the token, but this code copes even if 
  +        // there isn't.
  +        int idx = tok.indexOf("(");
  +        if (idx != -1)
  +            tok = tok.substring(0, idx);
  +
  +        // Now strip off the WSIF package name off the front of 
  +        // the class name. All WSIF class names are unique, so 
  +        // the package name just takes up more space in the trace
  +        // and provides no added value. If Trc was not called from
  +        // WSIF (unlikely) then output the whole 
  +        // packagename.classname.methodname.
  +        String result = null;
  +        if (tok.startsWith(wsifPackageName)) {
  +            StringTokenizer st2 = new StringTokenizer(tok, ".");
  +            String previous = null;
  +            while (st2.hasMoreTokens()) {
  +                previous = result;
  +                result = st2.nextToken();
  +            }
  +            if (previous != null)
  +                result = previous + "." + result;
  +        } else
  +            result = tok;
  +
  +        buff.append(result);
       }
  -    
  +        
       private static void traceIt(
           Object that,
           String type,
  @@ -851,15 +975,8 @@
               checkWsdl(parms);
   
           StringBuffer sb = new StringBuffer(type);
  +        appendMethodName(sb);
   
  -        if (type.equals(EXIT))
  -            depth--;
  -        for (int i = 0; i <= depth; i++)
  -            sb.append(" ");
  -        if (type.equals(ENTRY))
  -            depth++;
  -
  -        sb.append(getMethodName());
           if (that != null) {
               sb.append("<");
               sb.append(Integer.toHexString(that.hashCode()));
  @@ -877,5 +994,4 @@
   
           log.debug(sb);
       }
  -
   }
  
  
  
  1.2       +9 -0      xml-axis-wsif/java/src/org/apache/wsif/WSIFException.java
  
  Index: WSIFException.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/WSIFException.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WSIFException.java	6 Jun 2002 08:28:48 -0000	1.1
  +++ WSIFException.java	14 Aug 2002 16:01:50 -0000	1.2
  @@ -58,6 +58,7 @@
   package org.apache.wsif;
   
   import java.rmi.RemoteException;
  +import org.apache.wsif.logging.Trc;
   
   /**
    * A WSIFException is used to indicate something going wrong
  @@ -74,18 +75,26 @@
   
       public WSIFException(String msg) {
           super(msg);
  +        Trc.entry(this,msg);
  +        Trc.exit();
       }
   
       public WSIFException(String msg, Throwable targetException) {
           this(msg);
  +        Trc.entry(this,msg,targetException);
           this.detail = targetException;
  +        Trc.exit();
       }
   
       public void setTargetException(Throwable targetException) {
  +    	Trc.entry(this,targetException);
           this.detail = targetException;
  +        Trc.exit();
       }
   
       public Throwable getTargetException() {
  +    	Trc.entry(this);
  +    	Trc.exit(detail);
           return detail;
       }