| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.tracing; |
| 34 | | | |
| 35 | | | import java.lang.reflect.InvocationHandler; |
| 36 | | | import java.lang.reflect.InvocationTargetException; |
| 37 | | | import java.lang.reflect.Method; |
| 38 | | | import java.lang.reflect.Modifier; |
| 39 | | | import java.lang.reflect.Proxy; |
| 40 | | | import java.util.Arrays; |
| 41 | | | import java.util.HashSet; |
| 42 | | | import java.util.Set; |
| 43 | | | |
| 44 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 45 | | | import org.jcoderz.commons.tracing.Tracer.TracingToken; |
| 46 | | | |
| 47 | | | |
| 48 | | | <p> |
| 49 | | | |
| 50 | | | <i></i> |
| 51 | | | </p> |
| 52 | | | <p> |
| 53 | | | <b></b><i></i> |
| 54 | | | {@link } |
| 55 | | | |
| 56 | | | |
| 57 | | | </p> |
| 58 | | | |
| 59 | | | @author |
| 60 | | | |
| 61 | | (1) | public final class TracingProxy |
| 62 | | | implements InvocationHandler |
| 63 | | | { |
| 64 | | | private final Object mRealObject; |
| 65 | | | private final String mRealObjectClassName; |
| 66 | | | private final Tracer mObjectTracer; |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | @param |
| 73 | | | @param |
| 74 | | | |
| 75 | | | private TracingProxy (Object realObject, Tracer tracer) |
| 76 | 0 | | { |
| 77 | 0 | | mRealObject = realObject; |
| 78 | 0 | | mRealObjectClassName = mRealObject.getClass().getName(); |
| 79 | 0 | | mObjectTracer = tracer; |
| 80 | 0 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | @param |
| 87 | | | @return |
| 88 | | | |
| 89 | | | |
| 90 | | (2) | public static Object getProxy (Object obj, Class tracerClass) |
| 91 | | | { |
| 92 | 0 | | final String classname = obj.getClass().getName(); |
| 93 | 0 | | final Tracer tracer = getTracer(tracerClass, classname); |
| 94 | | | |
| 95 | | | final Object proxy; |
| 96 | 0 | | if (tracer.isTracing()) |
| 97 | | | { |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | 0 | | final Set interfaces = new HashSet(); |
| 102 | 0 | | Class currentClass = obj.getClass(); |
| 103 | 0 | | while (currentClass != null) |
| 104 | | | { |
| 105 | 0 | | interfaces.addAll(Arrays.asList(currentClass.getInterfaces())); |
| 106 | 0 | | currentClass = currentClass.getSuperclass(); |
| 107 | | | } |
| 108 | | | |
| 109 | 0 | | proxy = Proxy.newProxyInstance( |
| 110 | | | obj.getClass().getClassLoader(), |
| 111 | | | (Class[]) interfaces.toArray(new Class[interfaces.size()]), |
| 112 | | | new TracingProxy(obj, tracer)); |
| 113 | 0 | | } |
| 114 | | | else |
| 115 | | | { |
| 116 | 0 | | proxy = obj; |
| 117 | | | } |
| 118 | 0 | | return proxy; |
| 119 | | | } |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | @see |
| 125 | | | |
| 126 | | | |
| 127 | | (3)(4)(5)(6) | public Object invoke (Object proxy, Method method, Object[] args) |
| 128 | | (7) | throws Throwable |
| 129 | | | { |
| 130 | | | final TracingToken tt; |
| 131 | 0 | | if (mObjectTracer.isTracing()) |
| 132 | | | { |
| 133 | 0 | | if (args == null || !mObjectTracer.isTracingArguments()) |
| 134 | | | { |
| 135 | 0 | | tt = mObjectTracer.entering( |
| 136 | | | mRealObjectClassName, method.getName()); |
| 137 | | | } |
| 138 | | | else |
| 139 | | | { |
| 140 | 0 | (8) | tt = mObjectTracer.entering( |
| 141 | | | mRealObjectClassName, method.getName(), args); |
| 142 | | | } |
| 143 | | | } |
| 144 | | | else |
| 145 | | | { |
| 146 | 0 | | tt = null; |
| 147 | | | } |
| 148 | | | |
| 149 | 0 | | final Object result = invokeMethod(method, args, tt); |
| 150 | | | |
| 151 | 0 | | if (tt != null) |
| 152 | | | { |
| 153 | 0 | | if (result != null |
| 154 | | | || method.getReturnType() != Void.TYPE |
| 155 | | | || !mObjectTracer.isTracingArguments()) |
| 156 | | | { |
| 157 | 0 | | mObjectTracer.exiting(tt, result); |
| 158 | | | } |
| 159 | | | else |
| 160 | | | { |
| 161 | 0 | | mObjectTracer.exiting(tt); |
| 162 | | | } |
| 163 | | | } |
| 164 | 0 | | return result; |
| 165 | | | } |
| 166 | | | |
| 167 | | (9) | private static Tracer getTracer(Class tracer, String className) |
| 168 | | | { |
| 169 | | | final Tracer result; |
| 170 | | | try |
| 171 | | | { |
| 172 | 0 | | final Method method |
| 173 | | | = tracer.getMethod("getTracer", new Class[] {String.class}); |
| 174 | 0 | | if (!Modifier.isStatic(method.getModifiers())) |
| 175 | | | { |
| 176 | 0 | (10) | throw new ArgumentMalformedException( |
| 177 | | (11) | "tracer", tracer, |
| 178 | | | "Factory method 'getTracer' must be static."); |
| 179 | | | } |
| 180 | 0 | | if (!Modifier.isPublic(method.getModifiers())) |
| 181 | | | { |
| 182 | 0 | (12) | throw new ArgumentMalformedException( |
| 183 | | | "tracer", tracer, |
| 184 | | | "Factory method 'getTracer' must be public."); |
| 185 | | | } |
| 186 | 0 | | result = (Tracer) method.invoke(null, new Object[] {className}); |
| 187 | | | } |
| 188 | 0 | | catch (IllegalArgumentException e) |
| 189 | | | { |
| 190 | 0 | (13) | throw new ArgumentMalformedException( |
| 191 | | | "tracer", tracer, |
| 192 | | | "The static tracer factory did not accept the String argument.", |
| 193 | | | e); |
| 194 | | | } |
| 195 | 0 | | catch (IllegalAccessException e) |
| 196 | | | { |
| 197 | 0 | (14) | throw new ArgumentMalformedException( |
| 198 | | | "tracer", tracer, |
| 199 | | | "The static tracer factory did deny access.", |
| 200 | | | e); |
| 201 | | | } |
| 202 | 0 | | catch (InvocationTargetException e) |
| 203 | | | { |
| 204 | 0 | | throw new ArgumentMalformedException( |
| 205 | | | "tracer", tracer, |
| 206 | | | "The static tracer factory threw an exception with detail: " |
| 207 | | | + e.getMessage() + ".", |
| 208 | | | e); |
| 209 | | | } |
| 210 | 0 | | catch (SecurityException e) |
| 211 | | | { |
| 212 | 0 | (15) | throw new ArgumentMalformedException( |
| 213 | | | "tracer", tracer, |
| 214 | | | "Failed to look up static factory method.", |
| 215 | | | e); |
| 216 | | | } |
| 217 | 0 | | catch (NoSuchMethodException e) |
| 218 | | | { |
| 219 | 0 | (16) | throw new ArgumentMalformedException( |
| 220 | | | "tracer", tracer, |
| 221 | | | "The Tracer implementation must implement a static factory " |
| 222 | | | + "method 'public Tracer getTracer(String)'.", |
| 223 | | | e); |
| 224 | 0 | | } |
| 225 | 0 | | return result; |
| 226 | | | } |
| 227 | | | |
| 228 | | | private Object invokeMethod ( |
| 229 | | | Method method, Object[] args, TracingToken tt) |
| 230 | | | throws Throwable |
| 231 | | | { |
| 232 | | | final Object result; |
| 233 | | | try |
| 234 | | | { |
| 235 | 0 | | result = method.invoke(mRealObject, args); |
| 236 | | | } |
| 237 | 0 | | catch (InvocationTargetException x) |
| 238 | | | { |
| 239 | 0 | | if (tt != null) |
| 240 | | | { |
| 241 | 0 | | mObjectTracer.throwing(tt, x.getCause()); |
| 242 | | | } |
| 243 | 0 | (17) | throw x.getCause(); |
| 244 | | | } |
| 245 | 0 | | catch (Exception x) |
| 246 | | | { |
| 247 | 0 | | if (tt != null) |
| 248 | | | { |
| 249 | 0 | | mObjectTracer.throwing(tt, x); |
| 250 | | | } |
| 251 | 0 | | throw x; |
| 252 | 0 | | } |
| 253 | 0 | | return result; |
| 254 | | | } |
| 255 | | | } |