KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.lang.reflect.InvocationHandler JavaDoc;
30
31 import java.rmi.Remote JavaDoc;
32
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
35
36 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
37
38 import com.sun.enterprise.Switch;
39 import com.sun.enterprise.ComponentInvocation;
40 import com.sun.enterprise.InvocationManager;
41
42 // JAXRPC-RI classes
43
import com.sun.xml.rpc.spi.JaxRpcObjectFactory;
44 import com.sun.xml.rpc.spi.runtime.Implementor;
45 import com.sun.xml.rpc.spi.runtime.ImplementorCache;
46 import com.sun.xml.rpc.spi.runtime.ImplementorCacheDelegate;
47 import com.sun.xml.rpc.spi.runtime.RuntimeEndpointInfo;
48 import com.sun.xml.rpc.spi.runtime.Tie;
49
50 /**
51  * This class extends the behavior of ImplementorCache in order to
52  * interpose on lifecycle events for the creation and destruction of
53  * ties/servants for servlet web service endpoints.
54  *
55  * @author Kenneth Saks
56  */

57 public class ImplementorCacheDelegateImpl extends ImplementorCacheDelegate {
58
59     private Hashtable JavaDoc implementorCache_;
60     private ServletContext JavaDoc servletContext_;
61     private JaxRpcObjectFactory rpcFactory_;
62
63     public ImplementorCacheDelegateImpl(ServletConfig JavaDoc servletConfig) {
64         servletContext_ = servletConfig.getServletContext();
65         implementorCache_ = new Hashtable JavaDoc();
66         rpcFactory_ = JaxRpcObjectFactory.newInstance();
67     }
68
69     public Implementor getImplementorFor(RuntimeEndpointInfo targetEndpoint) {
70
71         Implementor implementor = null;
72         try {
73             synchronized(targetEndpoint) {
74                 implementor = (Implementor)
75                     implementorCache_.get(targetEndpoint);
76                 if( implementor == null ) {
77                     implementor = createImplementor(targetEndpoint);
78                     implementorCache_.put(targetEndpoint, implementor);
79                 }
80             }
81
82             InvocationManager invManager =
83                 Switch.getSwitch().getInvocationManager();
84             ComponentInvocation inv = invManager.getCurrentInvocation();
85             inv.setWebServiceTie(implementor.getTie());
86
87         } catch(Throwable JavaDoc t) {
88 /* XXX FIXME
89             JAXRPCServletException jse = new JAXRPCServletException
90                 ("error.implementorFactory.newInstanceFailed",
91                  targetEndpoint.getName());
92             jse.initCause(t);
93             throw jse;
94 */

95             RuntimeException JavaDoc re = new RuntimeException JavaDoc();
96             re.initCause(t);
97             throw re;
98         }
99
100         return implementor;
101     }
102
103     public void releaseImplementor(RuntimeEndpointInfo targetEndpoint,
104                                    Implementor implementor) {
105         // do nothing
106
}
107
108     public void destroy() {
109         for (Iterator JavaDoc iter = implementorCache_.values().iterator();
110              iter.hasNext();) {
111             Implementor implementor = (Implementor) iter.next();
112             try {
113                 implementor.destroy();
114             } catch(Throwable JavaDoc t) {
115                 // @@@ log
116
}
117         }
118         implementorCache_.clear();
119     }
120
121     private Implementor createImplementor(RuntimeEndpointInfo targetEndpoint)
122         throws Exception JavaDoc {
123
124         Tie tie = (Tie) targetEndpoint.getTieClass().newInstance();
125
126         Class JavaDoc seiClass = targetEndpoint.getRemoteInterface();
127         Class JavaDoc implClass = targetEndpoint.getImplementationClass();
128
129         Remote JavaDoc servant = null;
130         if( seiClass.isAssignableFrom(implClass) ) {
131             // if servlet endpoint impl is a subtype of SEI, use an
132
// instance as the servant.
133
servant = (Remote JavaDoc) implClass.newInstance();
134         } else {
135             // Create a dynamic proxy that implements SEI (and optionally
136
// ServiceLifecycle) and delegates to an instance of the
137
// endpoint impl.
138
Object JavaDoc implInstance = implClass.newInstance();
139
140             InvocationHandler JavaDoc handler =
141                 new ServletImplInvocationHandler(implInstance);
142             boolean implementsLifecycle =
143                 ServiceLifecycle JavaDoc.class.isAssignableFrom(implClass);
144             Class JavaDoc[] proxyInterfaces = implementsLifecycle ?
145                 new Class JavaDoc[] { seiClass, ServiceLifecycle JavaDoc.class } :
146                 new Class JavaDoc[] { seiClass };
147
148             servant = (Remote JavaDoc) Proxy.newProxyInstance
149                 (implClass.getClassLoader(), proxyInterfaces, handler);
150         }
151         tie.setTarget(servant);
152         
153         Implementor implementor = rpcFactory_.createImplementor(servletContext_, tie);
154         implementor.init();
155
156         return implementor;
157     }
158 }
159
Popular Tags