001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aspects.logging;
023:
024: import org.jboss.aop.joinpoint.ConstructorInvocation;
025: import org.jboss.aop.joinpoint.FieldReadInvocation;
026: import org.jboss.aop.joinpoint.FieldWriteInvocation;
027: import org.jboss.aop.joinpoint.MethodInvocation;
028: import org.jboss.logging.Logger;
029:
030: import java.util.Arrays;
031:
032: /**
033: * Logs invocations.
034: *
035: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>.
036: * @version $Revision: 57186 $
037: */
038: public final class CallLoggingInterceptor implements
039: org.jboss.aop.advice.Interceptor, LoggingConstants {
040: protected Logger log = Logger.getLogger(this .getClass());
041:
042: public String getName() {
043: return "CallLoggingInterceptor";
044: }
045:
046: public Object invoke(org.jboss.aop.joinpoint.Invocation invocation)
047: throws Throwable {
048: boolean callLogging = log.isDebugEnabled();
049: if (callLogging)
050: callLogging = Boolean.valueOf(
051: (String) invocation.getMetaData(LOGGING,
052: CALL_LOGGING)).booleanValue();
053:
054: if (callLogging)
055: log.debug("Invoking: " + dumpInvocation(invocation));
056:
057: Object response = null;
058: try {
059: response = invocation.invokeNext();
060: return response;
061: } catch (Throwable t) {
062: if (callLogging)
063: log.debug("Throwing: " + dumpInvocation(invocation), t);
064: throw t;
065: } finally {
066: if (callLogging)
067: log.debug("Response: "
068: + dumpInvocationResponse(response) + " for "
069: + dumpInvocation(invocation));
070: }
071: }
072:
073: /**
074: * Display useful information about the invocation
075: * @todo improve this, probably controlled by meta data
076: * @param invocation the invocation
077: */
078: public String dumpInvocation(
079: org.jboss.aop.joinpoint.Invocation invocation) {
080: StringBuffer buffer = new StringBuffer();
081: if (invocation instanceof MethodInvocation) {
082: org.jboss.aop.joinpoint.MethodInvocation methodInvocation = (org.jboss.aop.joinpoint.MethodInvocation) invocation;
083: buffer.append("Method@").append(
084: System.identityHashCode(invocation)).append("{");
085: buffer.append("method=").append(
086: methodInvocation.getMethod());
087: // TODO fix this, null arguments screws this up
088: // buffer.append(" args=").append(Arrays.asList(methodInvocation.arguments));
089: } else if (invocation instanceof FieldReadInvocation) {
090: org.jboss.aop.joinpoint.FieldReadInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldReadInvocation) invocation;
091: buffer.append("FieldRead@").append(
092: System.identityHashCode(invocation)).append("{");
093: buffer.append("field=").append(fieldInvocation.getField());
094: } else if (invocation instanceof FieldWriteInvocation) {
095: org.jboss.aop.joinpoint.FieldWriteInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldWriteInvocation) invocation;
096: buffer.append("FieldWrite@").append(
097: System.identityHashCode(invocation)).append("{");
098: buffer.append("field=").append(fieldInvocation.getField());
099: buffer.append(" value=").append(fieldInvocation.getValue());
100: } else if (invocation instanceof ConstructorInvocation) {
101: org.jboss.aop.joinpoint.ConstructorInvocation cInvocation = (org.jboss.aop.joinpoint.ConstructorInvocation) invocation;
102: buffer.append("Construct@").append(
103: System.identityHashCode(invocation)).append("{");
104: buffer.append("constructor=").append(
105: cInvocation.getConstructor());
106: buffer.append(" args=").append(
107: Arrays.asList(cInvocation.getArguments()));
108: } else {
109: return "Unknown " + invocation;
110: }
111: buffer.append("}");
112: return buffer.toString();
113: }
114:
115: /**
116: * Display useful information about the invocation response
117: * @todo improve this, probably controlled by meta data
118: * @param invocation the invocation
119: */
120: public String dumpInvocationResponse(Object response) {
121: if (response == null)
122: return "null";
123: StringBuffer buffer = new StringBuffer();
124: buffer.append(response);
125: return buffer.toString();
126: }
127: }
|