KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > util > 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.util;
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
32 public final class InvocationHandlerUtil {
33
34     InvocationHandlerUtil() {}
35
36     public static Object JavaDoc invokeJavaObjectMethod(InvocationHandler JavaDoc handler,
37                                                 Method JavaDoc method, Object JavaDoc[] args)
38         throws EJBException JavaDoc {
39
40         Object JavaDoc returnValue = null;
41
42         // Can only be one of :
43
// boolean java.lang.Object.equals(Object)
44
// int java.lang.Object.hashCode()
45
// String java.lang.Object.toString()
46
//
47
// Optimize by comparing as few characters as possible.
48

49         switch( method.getName().charAt(0) ) {
50             case 'e' :
51                 Object JavaDoc other = Proxy.isProxyClass(args[0].getClass()) ?
52                     Proxy.getInvocationHandler(args[0]) : args[0];
53                 returnValue = new Boolean JavaDoc(handler.equals(other));
54                 break;
55             case 'h' :
56                 returnValue = new Integer JavaDoc(handler.hashCode());
57                 break;
58             case 't' :
59                 returnValue = handler.toString();
60                 break;
61             default :
62                 throw new EJBException JavaDoc(method.getName());
63         }
64
65         return returnValue;
66     }
67
68     public static Throwable JavaDoc handleInvocationException(Throwable JavaDoc invException) {
69
70         Throwable JavaDoc toThrow = invException;
71         
72         if (invException instanceof java.lang.RuntimeException JavaDoc) {
73             toThrow = invException;
74         } else if (invException instanceof Exception JavaDoc) {
75             toThrow = invException;
76         } else {
77             toThrow = new EJBException JavaDoc(invException.getMessage());
78         }
79
80         return toThrow;
81     }
82 }
83
Popular Tags