KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > providers > StatelessEJBProvider


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.providers;
59
60 import java.io.* ;
61 import java.util.* ;
62 import javax.servlet.* ;
63 import javax.servlet.http.* ;
64 import org.apache.soap.* ;
65 import org.apache.soap.rpc.* ;
66 import org.apache.soap.server.* ;
67 import org.apache.soap.util.* ;
68
69 import java.lang.reflect.*;
70 import javax.rmi.*;
71 import javax.ejb.*;
72 import javax.naming.*;
73 import javax.naming.Context JavaDoc.*;
74
75 public class StatelessEJBProvider implements org.apache.soap.util.Provider {
76
77         private DeploymentDescriptor dd ;
78         private Envelope envelope ;
79         private Call call ;
80         private String JavaDoc methodName ;
81         private String JavaDoc targetObjectURI ;
82         private HttpServlet servlet ;
83         private HttpSession session ;
84
85         private javax.naming.Context JavaDoc contxt = null;
86         private EJBObject remoteObjRef = null;
87         public static java.lang.String JavaDoc CNTXT_PROVIDER_URL = "iiop://localhost:900";
88         public static java.lang.String JavaDoc CNTXT_FACTORY_NAME = "com.ibm.ejs.ns.jndi.CNInitialContextFactory";
89         private Vector methodParameters = null;
90         private String JavaDoc respEncStyle = null;
91 /**
92  * StatelessEJBProvider constructor comment.
93  */

94 public StatelessEJBProvider() {
95         super();
96 }
97
98 private void initialize() throws SOAPException {
99
100         if(contxt == null) {
101
102                 java.util.Properties JavaDoc properties = new java.util.Properties JavaDoc();
103                 properties.put(javax.naming.Context.PROVIDER_URL, CNTXT_PROVIDER_URL);
104                 properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, CNTXT_FACTORY_NAME);
105                 try {
106                         contxt = new javax.naming.InitialContext JavaDoc(properties);
107                 } catch (NamingException ne) {
108                         // ErrorListener?
109
System.out.println("Naming Exception caught during InitialContext creation @ " + CNTXT_PROVIDER_URL);
110                         throw new SOAPException(Constants.FAULT_CODE_SERVER, "Unable to initialize context" );
111                 }
112         }
113
114 }
115
116   private void initialize(String JavaDoc url, String JavaDoc factory)
117     throws SOAPException
118   {
119     if(contxt == null)
120     {
121       java.util.Properties JavaDoc properties = new java.util.Properties JavaDoc();
122       
123       if ((url != null) && (!url.trim().equals("")))
124       {
125     properties.put(javax.naming.Context.PROVIDER_URL, url);
126       }
127       if ((factory != null) && (!factory.trim().equals("")))
128       {
129     properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, factory);
130       }
131       
132       try
133       {
134     contxt = new javax.naming.InitialContext JavaDoc(properties);
135       }
136       catch (NamingException ne)
137       {
138     // ErrorListener?
139
System.out.println("Naming Exception caught during InitialContext creation @ " + url);
140     throw new SOAPException(Constants.FAULT_CODE_SERVER, "Unable to initialize context");
141       }
142     }
143   }
144
145
146 /**
147  * invoke method comment.
148  */

149 public void invoke(SOAPContext reqContext, SOAPContext resContext)
150                     throws SOAPException {
151                   System.err.println( "=============================================" );
152           System.err.println("In TemplateProvider.invoke()" );
153
154         Parameter ret = null;
155         Object JavaDoc[] args = null;
156         Class JavaDoc[] argTypes = null;
157
158         respEncStyle = call.getEncodingStyleURI();
159
160         if (methodParameters != null) {
161                 int parametersCount = methodParameters.size ();
162                 args = new Object JavaDoc[parametersCount];
163                 argTypes = new Class JavaDoc[parametersCount];
164
165                 for (int i = 0; i < parametersCount; i++) {
166                         Parameter param = (Parameter) methodParameters.elementAt (i);
167                         args[i] = param.getValue ();
168                         argTypes[i] = param.getType ();
169
170                         if (respEncStyle == null) {
171                                 respEncStyle = param.getEncodingStyleURI ();
172                         }
173                 }
174         }
175
176         if (respEncStyle == null) {
177           respEncStyle = Constants.NS_URI_SOAP_ENC;
178         }
179
180         try {
181
182                 Method m = MethodUtils.getMethod (remoteObjRef, methodName, argTypes);
183                 Bean result = new Bean (m.getReturnType (), m.invoke (remoteObjRef, args));
184
185
186                 if (result.type != void.class) {
187                   ret = new Parameter (RPCConstants.ELEM_RETURN, result.type,
188                                                    result.value, null);
189                 }
190
191
192         } catch (InvocationTargetException e) {
193                 System.err.println("Exception Caught upon method invocation attempt: " + e.getMessage());
194                 Throwable JavaDoc t = e.getTargetException ();
195           throw new SOAPException (Constants.FAULT_CODE_SERVER, t.getMessage(), t);
196         }
197          catch (Throwable JavaDoc t) {
198                 System.err.println("Exception Caught upon method invocation attempt: " + t.toString());
199                 throw new SOAPException (Constants.FAULT_CODE_SERVER, t.getMessage(), t);
200         }
201
202         try {
203           Response resp = new Response( targetObjectURI, // URI
204
call.getMethodName(), // Method
205
(Parameter) ret, // ReturnValue
206
null, // Params
207
null, // Header
208
respEncStyle, // encoding
209
resContext ); // response soapcontext - not supported yet
210
Envelope env = resp.buildEnvelope();
211                   StringWriter sw = new StringWriter();
212                   env.marshall( sw, call.getSOAPMappingRegistry(), resContext );
213                   resContext.setRootPart( sw.toString(), Constants.HEADERVAL_CONTENT_TYPE_UTF8);
214                 }
215                 catch( Exception JavaDoc e ) {
216                   if ( e instanceof SOAPException ) throw (SOAPException ) e ;
217                   throw new SOAPException( Constants.FAULT_CODE_SERVER, e.toString() );
218                 }
219 }
220
221 /**
222  * locate method comment.
223  */

224 public void locate(DeploymentDescriptor dd,
225                    Envelope env,
226                    Call call,
227                    String JavaDoc methodName,
228                    String JavaDoc targetObjectURI,
229                    SOAPContext reqContext)
230               throws org.apache.soap.SOAPException {
231
232           HttpServlet servlet = (HttpServlet) reqContext.getProperty( Constants.BAG_HTTPSERVLET );
233           HttpSession session = (HttpSession) reqContext.getProperty( Constants.BAG_HTTPSESSION );
234
235           System.err.println( "=============================================" );
236           System.err.println( "In TemplateProvider.locate()" );
237           System.err.println( "URI: " + targetObjectURI );
238           System.err.println( "DD.ServiceClass: " + dd.getServiceClass() );
239           System.err.println( "DD.ProviderClass: " + dd.getProviderClass() );
240           System.err.println( "Call.MethodName: " + call.getMethodName() );
241
242           this.dd = dd ;
243           this.envelope = env ;
244           this.call = call ;
245           this.methodName = methodName ;
246           this.targetObjectURI = targetObjectURI ;
247           this.servlet = servlet ;
248           this.session = session ;
249
250           Hashtable props = dd.getProps();
251
252
253         String JavaDoc ContxtProviderURL = (String JavaDoc) props.get("ContextProviderURL");
254         String JavaDoc ContxtFactoryName = (String JavaDoc) props.get("FullContextFactoryName");
255
256         if ((ContxtProviderURL != null) || (ContxtFactoryName != null))
257                 initialize(ContxtProviderURL, ContxtFactoryName);
258         else
259                 initialize();
260
261         String JavaDoc homeInterfaceName = (String JavaDoc) props.get("FullHomeInterfaceName");
262         if (homeInterfaceName == null)
263                 throw new SOAPException(Constants.FAULT_CODE_SERVER, "Error in Deployment Descriptor Property Settings");
264
265         // From the Deployment Descriptor get the JNDI lookup name that is inside the "java" element...
266
String JavaDoc jndiName = (String JavaDoc) props.get("JNDIName");
267         if ( jndiName == null ) jndiName = dd.getProviderClass();
268
269         if ((jndiName != null) && (contxt != null)) {
270
271                 try {
272
273                         // Use service name to locate EJB home object via the contxt
274
// EJBHome home = (EJBHome) contxt.lookup(jndiName);
275
EJBHome home = (EJBHome) PortableRemoteObject.narrow(contxt.lookup(jndiName), Class.forName(homeInterfaceName));
276                         // call the 'create' method on the EJB home object, and store the
277
// ref to the EJB object.
278
Method createMethod = home.getClass().getMethod("create", new Class JavaDoc[0]);
279                         remoteObjRef = (EJBObject) createMethod.invoke((Object JavaDoc) home, new Object JavaDoc[0]);
280
281                 } catch (Exception JavaDoc e) {
282
283                         System.out.println("Exception caught: " + e.toString());
284                         throw new SOAPException(Constants.FAULT_CODE_SERVER,"Error in connecting to EJB", e);
285                 }
286         }
287
288         // Once previous steps have been successful, then we take the Call Object
289
// and extract the method name from it, and any parameters, and store them.
290
methodName = call.getMethodName();
291         methodParameters = call.getParams();
292
293
294 }
295 }
296
Popular Tags