KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > proxy > ProxyClass


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.enterprise.admin.util.proxy;
30
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.lang.reflect.InvocationHandler JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * A proxy class
39  */

40 public class ProxyClass implements InvocationHandler JavaDoc {
41
42     private static InheritableThreadLocal JavaDoc
43             callStackHolder = new InheritableThreadLocal JavaDoc() {
44         protected synchronized Object JavaDoc initialValue() {
45             return new CallStack();
46         }
47     };
48
49     private static Logger JavaDoc _logger = getLogger();
50
51     private Object JavaDoc delegate;
52     private Interceptor interceptor;
53
54     /** Creates a new instance of Proxy */
55     public ProxyClass(Object JavaDoc handler, Interceptor interceptor) {
56         delegate = handler;
57         this.interceptor = interceptor;
58     }
59     
60     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
61             throws Throwable JavaDoc {
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 JavaDoc 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 JavaDoc result = null;
74         boolean success = true;
75         Throwable JavaDoc failReason = null;
76         try {
77             result = method.invoke(delegate, args);
78         } catch (InvocationTargetException JavaDoc ite) {
79             success = false;
80             failReason = ite.getTargetException();
81             throw failReason;
82         } catch (Throwable JavaDoc 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 JavaDoc 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 JavaDoc getLogger() {
110         String JavaDoc 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