KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > InvocationHandlerUtil


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 package com.sun.ejb.containers;
24
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import javax.ejb.EJBException JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32
33 public final class InvocationHandlerUtil {
34
35     InvocationHandlerUtil() {}
36
37     static Object JavaDoc invokeJavaObjectMethod(InvocationHandler JavaDoc handler,
38                                          Method JavaDoc method, Object JavaDoc[] args)
39         throws EJBException JavaDoc {
40
41         Object JavaDoc returnValue = null;
42
43         // Can only be one of :
44
// boolean java.lang.Object.equals(Object)
45
// int java.lang.Object.hashCode()
46
// String java.lang.Object.toString()
47
//
48
// Optimize by comparing as few characters as possible.
49

50         switch( method.getName().charAt(0) ) {
51             case 'e' :
52                 boolean result = false;
53                 if (args[0] != null) {
54                     Object JavaDoc other = Proxy.isProxyClass(args[0].getClass()) ?
55                             Proxy.getInvocationHandler(args[0]) : args[0];
56                             result = handler.equals(other);
57                 }
58                 returnValue = new Boolean JavaDoc(result);
59                 break;
60             case 'h' :
61                 returnValue = new Integer JavaDoc(handler.hashCode());
62                 break;
63             case 't' :
64                 returnValue = handler.toString();
65                 break;
66             default :
67                 throw new EJBException JavaDoc(method.getName());
68         }
69
70         return returnValue;
71     }
72
73     static boolean isDeclaredException(Throwable JavaDoc t,
74                                        Class JavaDoc[] declaredExceptions)
75     {
76         boolean declaredException = false;
77
78         for(int i = 0; i < declaredExceptions.length; i++) {
79             Class JavaDoc next = declaredExceptions[i];
80             if( next.isAssignableFrom(t.getClass()) ) {
81                 declaredException = true;
82                 break;
83             }
84         }
85
86         return declaredException;
87     }
88
89     static void throwLocalException(Throwable JavaDoc t,
90                                     Class JavaDoc[] declaredExceptions)
91         throws Throwable JavaDoc
92     {
93         Throwable JavaDoc toThrow = t;
94
95         if( (t instanceof java.lang.RuntimeException JavaDoc) ||
96             (isDeclaredException(t, declaredExceptions)) ) {
97             toThrow = t;
98         } else {
99             toThrow = new EJBException JavaDoc();
100             toThrow.initCause(t);
101         }
102         
103         throw toThrow;
104
105     }
106
107     static void throwRemoteException(Throwable JavaDoc t,
108                                      Class JavaDoc[] declaredExceptions)
109         throws Throwable JavaDoc
110     {
111         Throwable JavaDoc toThrow = t;
112
113         if( (t instanceof java.lang.RuntimeException JavaDoc) ||
114             (t instanceof java.rmi.RemoteException JavaDoc) ||
115             (isDeclaredException(t, declaredExceptions)) ) {
116             toThrow = t;
117         } else {
118             toThrow = new RemoteException JavaDoc("", t);
119         }
120
121         throw toThrow;
122     }
123 }
124
Popular Tags