1 23 24 28 29 package com.sun.enterprise.admin.util.proxy; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationHandler ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 40 public class ProxyClass implements InvocationHandler { 41 42 private static InheritableThreadLocal 43 callStackHolder = new InheritableThreadLocal () { 44 protected synchronized Object initialValue() { 45 return new CallStack(); 46 } 47 }; 48 49 private static Logger _logger = getLogger(); 50 51 private Object delegate; 52 private Interceptor interceptor; 53 54 55 public ProxyClass(Object handler, Interceptor interceptor) { 56 delegate = handler; 57 this.interceptor = interceptor; 58 } 59 60 public Object invoke(Object proxy, Method method, Object [] args) 61 throws Throwable { 62 Call call = new Call(method, args); 63 CallStack callStack = (CallStack)callStackHolder.get(); 64 callStack.beginCall(call); 65 try { 66 interceptor.preInvoke(callStack); 67 } catch (Throwable t) { 68 _logger.log(Level.FINE, "core.interceptor_preinvoke_fail", 69 t.getMessage()); 70 _logger.log(Level.FINEST, 71 "core.interceptor_preinvoke_exception", t); 72 } 73 Object result = null; 74 boolean success = true; 75 Throwable failReason = null; 76 try { 77 result = method.invoke(delegate, args); 78 } catch (InvocationTargetException ite) { 79 success = false; 80 failReason = ite.getTargetException(); 81 throw failReason; 82 } catch (Throwable t) { 83 success = false; 84 failReason = t; 85 throw failReason; 86 } finally { 87 if (!success) { 88 call.setState(CallState.FAILED); 89 call.setFailureReason(failReason); 90 } 91 call.setResult(result); 92 93 if(!(call.getState().isFailed())) 94 call.setState(CallState.SUCCESS); 95 96 try { 97 interceptor.postInvoke(callStack); 98 } catch (Throwable t) { 99 _logger.log(Level.FINE, "core.interceptor_postinvoke_fail", 100 t.getMessage()); 101 _logger.log(Level.FINEST, 102 "core.interceptor_postinvoke_exception", t); 103 } 104 callStack.endCall(); 105 } 106 return result; 107 } 108 109 private static Logger getLogger() { 110 String loggerName = System.getProperty("com.sun.aas.admin.logger.name"); 111 if (loggerName == null) { 112 loggerName = "global"; 113 } 114 return Logger.getLogger(loggerName); 115 } 116 } 117 | Popular Tags |