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.mq.test;
023:
024: import java.lang.reflect.InvocationHandler;
025: import java.lang.reflect.InvocationTargetException;
026: import java.lang.reflect.Method;
027: import java.lang.reflect.Proxy;
028: import java.util.ArrayList;
029:
030: import javax.jms.JMSException;
031: import javax.jms.Message;
032:
033: import org.jboss.logging.Logger;
034: import org.jboss.mq.TransactionRequest;
035: import org.jboss.mq.server.JMSServerInterceptor;
036: import org.jboss.mq.server.jmx.InterceptorMBeanSupport;
037:
038: /**
039: * The log interceptor.
040: *
041: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
042: * @version $Revision: 57198 $
043: */
044: public class LogInterceptor extends InterceptorMBeanSupport implements
045: InvocationHandler, LogInterceptorMBean {
046: // Constants -----------------------------------------------------
047:
048: private static final Logger log = Logger
049: .getLogger(LogInterceptor.class);
050:
051: // Attributes ----------------------------------------------------
052:
053: private JMSServerInterceptor nextInterceptor;
054:
055: // Static --------------------------------------------------------
056:
057: // Constructors --------------------------------------------------
058:
059: // Public --------------------------------------------------------
060:
061: // InvocationHandler implementation ------------------------------
062:
063: public Object invoke(Object proxy, Method method, Object[] args)
064: throws Throwable {
065: if (method.getName().equals("setNext")) {
066: nextInterceptor = (JMSServerInterceptor) args[0];
067: return null;
068: }
069:
070: StringBuffer buffer = new StringBuffer("invoke ");
071: describe(buffer, method, args);
072: log.debug(buffer);
073: try {
074: Object result = method.invoke(nextInterceptor, args);
075: buffer = new StringBuffer("return (");
076: dump(buffer, result);
077: buffer.append(") ");
078: describe(buffer, method, args);
079: log.debug(buffer);
080: return result;
081: } catch (Throwable t) {
082: if (t instanceof InvocationTargetException)
083: t = ((InvocationTargetException) t)
084: .getTargetException();
085: buffer = new StringBuffer("error ");
086: describe(buffer, method, args);
087: log.debug(buffer, t);
088: throw t;
089: }
090: }
091:
092: public String getInterceptorClass() throws Exception {
093: return null;
094: }
095:
096: public void setInterceptorClass(String c) throws Exception {
097: }
098:
099: public JMSServerInterceptor getInterceptor() {
100: Class[] interfaces = new Class[] { JMSServerInterceptor.class };
101: return (JMSServerInterceptor) Proxy
102: .newProxyInstance(
103: LogInterceptor.class.getClassLoader(),
104: interfaces, this );
105: }
106:
107: // Protected -----------------------------------------------------
108:
109: protected void describe(StringBuffer buffer, Method method,
110: Object[] args) throws JMSException {
111: buffer.append(method.getName());
112: if (args == null)
113: buffer.append("()");
114: else {
115: buffer.append("(");
116: for (int i = 0; i < args.length; ++i) {
117: if (i > 0)
118: buffer.append(", ");
119: dump(buffer, args[i]);
120: }
121: buffer.append(")");
122: }
123: }
124:
125: protected void dump(StringBuffer buffer, Object obj)
126: throws JMSException {
127: if (obj instanceof Message) {
128: buffer.append("Message:");
129: buffer.append(((Message) obj).getJMSMessageID());
130: } else if (obj instanceof TransactionRequest) {
131: TransactionRequest tr = (TransactionRequest) obj;
132: buffer.append("Transaction Request xid=").append(tr.xid);
133: buffer.append(" type=");
134: switch (tr.requestType) {
135: case TransactionRequest.ONE_PHASE_COMMIT_REQUEST:
136: buffer.append("1Pcommit");
137: break;
138: case TransactionRequest.TWO_PHASE_COMMIT_PREPARE_REQUEST:
139: buffer.append("2Pprepare");
140: break;
141: case TransactionRequest.TWO_PHASE_COMMIT_COMMIT_REQUEST:
142: buffer.append("2Pcommit");
143: break;
144: case TransactionRequest.TWO_PHASE_COMMIT_ROLLBACK_REQUEST:
145: buffer.append("2Prollback");
146: break;
147: default:
148: buffer.append("UNKNOWN");
149: }
150: if (tr.messages != null && tr.messages.length != 0) {
151: buffer.append(" msgs=");
152: ArrayList msgs = new ArrayList(tr.messages.length);
153: for (int i = 0; i < tr.messages.length; ++i)
154: msgs.add(tr.messages[i].getJMSMessageID());
155: buffer.append(msgs);
156: }
157: if (tr.acks != null && tr.acks.length != 0) {
158: buffer.append(" acks=");
159: ArrayList acks = new ArrayList(tr.acks.length);
160: for (int i = 0; i < tr.acks.length; ++i)
161: acks.add(tr.acks[i]);
162: buffer.append(acks);
163: }
164: } else
165: buffer.append(obj);
166: }
167:
168: // Package Private -----------------------------------------------
169:
170: // Private -------------------------------------------------------
171:
172: // Inner classes -------------------------------------------------
173:
174: }
|