KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > logging > CallLoggingInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.logging;
23
24 import org.jboss.aop.joinpoint.ConstructorInvocation;
25 import org.jboss.aop.joinpoint.FieldReadInvocation;
26 import org.jboss.aop.joinpoint.FieldWriteInvocation;
27 import org.jboss.aop.joinpoint.MethodInvocation;
28 import org.jboss.logging.Logger;
29
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * Logs invocations.
34  *
35  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>.
36  * @version $Revision: 37406 $
37  */

38 public final class CallLoggingInterceptor
39    implements org.jboss.aop.advice.Interceptor, LoggingConstants
40 {
41    protected Logger log = Logger.getLogger(this.getClass());
42
43    public String JavaDoc getName() { return "CallLoggingInterceptor"; }
44
45    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
46    {
47       boolean callLogging = log.isDebugEnabled();
48       if (callLogging)
49          callLogging = Boolean.valueOf((String JavaDoc)invocation.getMetaData(LOGGING, CALL_LOGGING)).booleanValue();
50
51       if (callLogging)
52          log.debug("Invoking: " + dumpInvocation(invocation));
53
54       Object JavaDoc response = null;
55       try
56       {
57          response = invocation.invokeNext();
58          return response;
59       }
60       catch (Throwable JavaDoc t)
61       {
62          if (callLogging)
63             log.debug("Throwing: " + dumpInvocation(invocation), t);
64          throw t;
65       }
66       finally
67       {
68          if (callLogging)
69             log.debug("Response: " + dumpInvocationResponse(response) + " for " + dumpInvocation(invocation));
70       }
71    }
72
73    /**
74     * Display useful information about the invocation
75     * @todo improve this, probably controlled by meta data
76     * @param invocation the invocation
77     */

78    public String JavaDoc dumpInvocation(org.jboss.aop.joinpoint.Invocation invocation)
79    {
80       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81       if (invocation instanceof MethodInvocation)
82       {
83          org.jboss.aop.joinpoint.MethodInvocation methodInvocation = (org.jboss.aop.joinpoint.MethodInvocation)invocation;
84          buffer.append("Method@").append(System.identityHashCode(invocation)).append("{");
85          buffer.append("method=").append(methodInvocation.getMethod());
86          // TODO fix this, null arguments screws this up
87
// buffer.append(" args=").append(Arrays.asList(methodInvocation.arguments));
88
}
89       else if (invocation instanceof FieldReadInvocation)
90       {
91          org.jboss.aop.joinpoint.FieldReadInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldReadInvocation)invocation;
92          buffer.append("FieldRead@").append(System.identityHashCode(invocation)).append("{");
93          buffer.append("field=").append(fieldInvocation.getField());
94       }
95       else if (invocation instanceof FieldWriteInvocation)
96       {
97          org.jboss.aop.joinpoint.FieldWriteInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldWriteInvocation)invocation;
98          buffer.append("FieldWrite@").append(System.identityHashCode(invocation)).append("{");
99          buffer.append("field=").append(fieldInvocation.getField());
100          buffer.append(" value=").append(fieldInvocation.getValue());
101       }
102       else if (invocation instanceof ConstructorInvocation)
103       {
104          org.jboss.aop.joinpoint.ConstructorInvocation cInvocation = (org.jboss.aop.joinpoint.ConstructorInvocation)invocation;
105          buffer.append("Construct@").append(System.identityHashCode(invocation)).append("{");
106          buffer.append("constructor=").append(cInvocation.getConstructor());
107          buffer.append(" args=").append(Arrays.asList(cInvocation.getArguments()));
108       }
109       else
110       {
111          return "Unknown " + invocation;
112       }
113       buffer.append("}");
114       return buffer.toString();
115    }
116
117    /**
118     * Display useful information about the invocation response
119     * @todo improve this, probably controlled by meta data
120     * @param invocation the invocation
121     */

122    public String JavaDoc dumpInvocationResponse(Object JavaDoc response)
123    {
124       if (response == null)
125          return "null";
126       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
127       buffer.append(response);
128       return buffer.toString();
129    }
130 }
131
Popular Tags