01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.apache.log4j.Logger;
25: import org.apache.log4j.spi.LoggingEvent;
26: import org.jboss.aop.util.PayloadKey;
27:
28: import java.io.Serializable;
29: import java.util.ArrayList;
30: import java.util.Iterator;
31:
32: /**
33: * An interceptor that tests dumps any logging into the
34: * invocation response
35: *
36: * @author <a href="mailto:adrian@jboss">Adrian Brock</a>.
37: * @version $Revision: 57186 $
38: */
39: public class InvocationLogClientInterceptor implements
40: org.jboss.aop.advice.Interceptor, Serializable {
41: // Constants -----------------------------------------------------
42:
43: private static final long serialVersionUID = 9191894622629072033L;
44:
45: // Attributes ----------------------------------------------------
46:
47: // Static --------------------------------------------------------
48:
49: // Constructors --------------------------------------------------
50:
51: // Public --------------------------------------------------------
52:
53: // Interceptor Implementation ------------------------------------
54:
55: public String getName() {
56: return "InvocationLogClientInterceptor";
57: }
58:
59: /**
60: * @todo remove the hardwiring of the meta data (needs next invocation metadata)
61: */
62: public Object invoke(org.jboss.aop.joinpoint.Invocation invocation)
63: throws Throwable {
64: // TODO: This should be set by the caller when required
65: invocation.getMetaData().addMetaData(ThreadLocalAppender.LOG,
66: ThreadLocalAppender.LOG, "Log", PayloadKey.AS_IS);
67:
68: // Perform the invocation and dump any attached log
69: Object response = invocation.invokeNext();
70: if (response != null)
71: dumpLog(invocation);
72: return response;
73: }
74:
75: public void dumpLog(org.jboss.aop.joinpoint.Invocation invocation) {
76: Logger root = Logger.getRootLogger();
77: ArrayList list = (ArrayList) invocation
78: .getResponseAttachment(ThreadLocalAppender.LOG);
79: for (Iterator i = list.iterator(); i.hasNext();)
80: root.callAppenders((LoggingEvent) i.next());
81: }
82:
83: // Y Overrides ---------------------------------------------------
84:
85: // Protected -----------------------------------------------------
86:
87: // Private -------------------------------------------------------
88:
89: // Inner Classes -------------------------------------------------
90: }
|