KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > ServletImplInvocationHandler


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.enterprise.webservice;
24
25 import java.lang.UnsupportedOperationException JavaDoc;
26
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.lang.reflect.InvocationHandler JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.logging.LogDomains;
35
36 /**
37  * InvocationHandler used to delegate calls to JAXRPC servlet impls
38  * that aren't subtypes of their associated Service Endpoint Interface.
39  *
40  * @author Kenneth Saks
41  */

42 public class ServletImplInvocationHandler implements InvocationHandler JavaDoc {
43
44     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
45
46     private Object JavaDoc servletImplDelegate;
47     private Class JavaDoc servletImplClass;
48     
49     public ServletImplInvocationHandler(Object JavaDoc delegate) {
50         servletImplDelegate = delegate;
51         servletImplClass = delegate.getClass();
52     }
53     
54     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
55         throws Throwable JavaDoc {
56
57         // NOTE : be careful with "args" parameter. It is null
58
// if method signature has 0 arguments.
59

60         Class JavaDoc methodClass = method.getDeclaringClass();
61         if( methodClass == java.lang.Object JavaDoc.class ) {
62             return invokeJavaObjectMethod(this, method, args);
63         }
64
65         Object JavaDoc returnValue = null;
66
67         try {
68             // Since impl class isn't subtype of SEI, we need to do a
69
// method lookup to get method object to use for invocation.
70
Method JavaDoc implMethod = servletImplClass.getMethod
71                 (method.getName(), method.getParameterTypes());
72             returnValue = implMethod.invoke(servletImplDelegate, args);
73         } catch(InvocationTargetException JavaDoc ite) {
74             logger.log(Level.FINE, "", ite);
75             throw ite.getCause();
76         } catch(Throwable JavaDoc t) {
77             logger.log(Level.INFO, "Error invoking servlet impl", t);
78             throw t;
79         }
80
81     return returnValue;
82     }
83
84     private Object JavaDoc invokeJavaObjectMethod(InvocationHandler JavaDoc handler,
85                                           Method JavaDoc method, Object JavaDoc[] args)
86         throws Throwable JavaDoc {
87
88         Object JavaDoc returnValue = null;
89
90         // Can only be one of :
91
// boolean java.lang.Object.equals(Object)
92
// int java.lang.Object.hashCode()
93
// String java.lang.Object.toString()
94
//
95
// Optimize by comparing as few characters as possible.
96

97         switch( method.getName().charAt(0) ) {
98             case 'e' :
99                 Object JavaDoc other = Proxy.isProxyClass(args[0].getClass()) ?
100                     Proxy.getInvocationHandler(args[0]) : args[0];
101                 returnValue = new Boolean JavaDoc(handler.equals(other));
102                 break;
103             case 'h' :
104                 returnValue = new Integer JavaDoc(handler.hashCode());
105                 break;
106             case 't' :
107                 returnValue = handler.toString();
108                 break;
109             default :
110                 throw new Throwable JavaDoc("Object method " + method.getName() +
111                                     "not found");
112         }
113
114         return returnValue;
115     }
116
117 }
118
Popular Tags