1 22 package org.jboss.mq.test; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Proxy ; 28 import java.util.ArrayList ; 29 30 import javax.jms.JMSException ; 31 import javax.jms.Message ; 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 44 public class LogInterceptor 45 extends InterceptorMBeanSupport 46 implements InvocationHandler , LogInterceptorMBean 47 { 48 50 private static final Logger log = Logger.getLogger(LogInterceptor.class); 51 52 54 private JMSServerInterceptor nextInterceptor; 55 56 58 60 62 64 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 65 { 66 if (method.getName().equals("setNext")) 67 { 68 nextInterceptor = (JMSServerInterceptor) args[0]; 69 return null; 70 } 71 72 StringBuffer buffer = new StringBuffer ("invoke "); 73 describe(buffer, method, args); 74 log.debug(buffer); 75 try 76 { 77 Object result = method.invoke(nextInterceptor, args); 78 buffer = new StringBuffer ("return ("); 79 dump(buffer, result); 80 buffer.append(") "); 81 describe(buffer, method, args); 82 log.debug(buffer); 83 return result; 84 } 85 catch (Throwable t) 86 { 87 if (t instanceof InvocationTargetException ) 88 t = ((InvocationTargetException ) t).getTargetException(); 89 buffer = new StringBuffer ("error "); 90 describe(buffer, method, args); 91 log.debug(buffer, t); 92 throw t; 93 } 94 } 95 96 public String getInterceptorClass() throws Exception 97 { 98 return null; 99 } 100 101 public void setInterceptorClass(String c) throws Exception 102 { 103 } 104 105 public JMSServerInterceptor getInterceptor() 106 { 107 Class [] interfaces = new Class [] { JMSServerInterceptor.class }; 108 return (JMSServerInterceptor) Proxy.newProxyInstance(LogInterceptor.class.getClassLoader(), interfaces, this); 109 } 110 111 113 protected void describe(StringBuffer buffer, Method method, Object [] args) 114 throws JMSException 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 buffer, Object obj) 133 throws JMSException 134 { 135 if (obj instanceof Message ) 136 { 137 buffer.append("Message:"); 138 buffer.append(((Message ) 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 msgs = new ArrayList (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 acks = new ArrayList (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 176 178 180 } 181 | Popular Tags |