KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > test > LogInterceptor


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.mq.test;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import javax.jms.JMSException JavaDoc;
31 import javax.jms.Message JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.mq.TransactionRequest;
35 import org.jboss.mq.server.JMSServerInterceptor;
36 import org.jboss.mq.server.jmx.InterceptorMBeanSupport;
37
38 /**
39  * The log interceptor.
40  *
41  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
42  * @version $Revision: 37459 $
43  */

44 public class LogInterceptor
45    extends InterceptorMBeanSupport
46    implements InvocationHandler JavaDoc, LogInterceptorMBean
47 {
48    // Constants -----------------------------------------------------
49

50    private static final Logger log = Logger.getLogger(LogInterceptor.class);
51
52    // Attributes ----------------------------------------------------
53

54    private JMSServerInterceptor nextInterceptor;
55
56    // Static --------------------------------------------------------
57

58    // Constructors --------------------------------------------------
59

60    // Public --------------------------------------------------------
61

62    // InvocationHandler implementation ------------------------------
63

64    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
65    {
66       if (method.getName().equals("setNext"))
67       {
68          nextInterceptor = (JMSServerInterceptor) args[0];
69          return null;
70       }
71       
72       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("invoke ");
73       describe(buffer, method, args);
74       log.debug(buffer);
75       try
76       {
77          Object JavaDoc result = method.invoke(nextInterceptor, args);
78          buffer = new StringBuffer JavaDoc("return (");
79          dump(buffer, result);
80          buffer.append(") ");
81          describe(buffer, method, args);
82          log.debug(buffer);
83          return result;
84       }
85       catch (Throwable JavaDoc t)
86       {
87          if (t instanceof InvocationTargetException JavaDoc)
88             t = ((InvocationTargetException JavaDoc) t).getTargetException();
89          buffer = new StringBuffer JavaDoc("error ");
90          describe(buffer, method, args);
91          log.debug(buffer, t);
92          throw t;
93       }
94    }
95
96    public String JavaDoc getInterceptorClass() throws Exception JavaDoc
97    {
98       return null;
99    }
100
101    public void setInterceptorClass(String JavaDoc c) throws Exception JavaDoc
102    {
103    }
104
105    public JMSServerInterceptor getInterceptor()
106    {
107       Class JavaDoc[] interfaces = new Class JavaDoc[] { JMSServerInterceptor.class };
108       return (JMSServerInterceptor) Proxy.newProxyInstance(LogInterceptor.class.getClassLoader(), interfaces, this);
109    }
110
111    // Protected -----------------------------------------------------
112

113    protected void describe(StringBuffer JavaDoc buffer, Method JavaDoc method, Object JavaDoc[] args)
114       throws JMSException JavaDoc
115    {
116       buffer.append(method.getName());
117       if (args == null)
118          buffer.append("()");
119       else
120       {
121          buffer.append("(");
122          for (int i = 0; i < args.length; ++i)
123          {
124             if (i > 0)
125                buffer.append(", ");
126             dump(buffer, args[i]);
127          }
128          buffer.append(")");
129       }
130    }
131    
132    protected void dump(StringBuffer JavaDoc buffer, Object JavaDoc obj)
133       throws JMSException JavaDoc
134    {
135       if (obj instanceof Message JavaDoc)
136       {
137          buffer.append("Message:");
138          buffer.append(((Message JavaDoc) obj).getJMSMessageID());
139       }
140       else if (obj instanceof TransactionRequest)
141       {
142          TransactionRequest tr = (TransactionRequest) obj;
143          buffer.append("Transaction Request xid=").append(tr.xid);
144          buffer.append(" type=");
145          switch (tr.requestType)
146          {
147             case TransactionRequest.ONE_PHASE_COMMIT_REQUEST: buffer.append("1Pcommit"); break;
148             case TransactionRequest.TWO_PHASE_COMMIT_PREPARE_REQUEST: buffer.append("2Pprepare"); break;
149             case TransactionRequest.TWO_PHASE_COMMIT_COMMIT_REQUEST: buffer.append("2Pcommit"); break;
150             case TransactionRequest.TWO_PHASE_COMMIT_ROLLBACK_REQUEST: buffer.append("2Prollback"); break;
151             default: buffer.append("UNKNOWN");
152          }
153          if (tr.messages != null && tr.messages.length != 0)
154          {
155             buffer.append(" msgs=");
156             ArrayList JavaDoc msgs = new ArrayList JavaDoc(tr.messages.length);
157             for (int i = 0; i < tr.messages.length; ++i)
158                 msgs.add(tr.messages[i].getJMSMessageID());
159             buffer.append(msgs);
160          }
161          if (tr.acks != null && tr.acks.length != 0)
162          {
163             buffer.append(" acks=");
164             ArrayList JavaDoc acks = new ArrayList JavaDoc(tr.acks.length);
165             for (int i = 0; i < tr.acks.length; ++i)
166                 acks.add(tr.acks[i]);
167             buffer.append(acks);
168          }
169       }
170       else
171          buffer.append(obj);
172    }
173    
174    // Package Private -----------------------------------------------
175

176    // Private -------------------------------------------------------
177

178    // Inner classes -------------------------------------------------
179

180 }
181
Popular Tags