KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > ejb > WSIFPort_EJB


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 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 "WSIF" 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) 2001, 2002, 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.wsif.providers.ejb;
59
60 import java.lang.reflect.Method JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import javax.ejb.EJBHome JavaDoc;
67 import javax.ejb.EJBObject JavaDoc;
68 import javax.naming.InitialContext JavaDoc;
69 import javax.rmi.PortableRemoteObject JavaDoc;
70 import javax.wsdl.BindingOperation;
71 import javax.wsdl.Definition;
72 import javax.wsdl.Port;
73 import javax.wsdl.extensions.ExtensibilityElement;
74
75 import org.apache.wsif.WSIFException;
76 import org.apache.wsif.WSIFOperation;
77 import org.apache.wsif.logging.Trc;
78 import org.apache.wsif.providers.WSIFDynamicTypeMap;
79 import org.apache.wsif.util.WSIFUtils;
80 import org.apache.wsif.wsdl.extensions.ejb.EJBAddress;
81
82 /**
83  * EJB WSIF Port.
84  * @author <a HREF="mailto:gpfau@de.ibm.com">Gerhard Pfau</a>
85  * Partially based on WSIFPort_ApacheSOAP from Alekander Slominski,
86  * Paul Fremantle, Sanjiva Weerawarana and Matthew J. Duftler
87  * @author Owen Burroughs <owenb@apache.org>
88  * @author Jeremy Hughes <hughesj@apache.org>
89  */

90 public class WSIFPort_EJB
91     extends org.apache.wsif.base.WSIFDefaultPort
92     implements java.io.Serializable JavaDoc {
93
94     private static final long serialVersionUID = 1L;
95
96     private javax.wsdl.Definition fieldDefinition = null;
97     private javax.wsdl.Port fieldPortModel = null;
98     private EJBHome JavaDoc fieldEjbHome = null; // 'factory for physical connection'
99
private EJBObject JavaDoc fieldEjbObject = null; // 'physical connection'
100

101     protected Map JavaDoc operationInstances = new HashMap JavaDoc();
102
103     public WSIFPort_EJB(Definition def, Port port, WSIFDynamicTypeMap typeMap) {
104         Trc.entry(this, def, port, typeMap);
105
106         fieldDefinition = def;
107         fieldPortModel = port;
108
109         Trc.exit();
110     }
111
112     public Definition getDefinition() {
113         return fieldDefinition;
114     }
115
116     public WSIFOperation_EJB getDynamicWSIFOperation(
117         String JavaDoc name,
118         String JavaDoc inputName,
119         String JavaDoc outputName)
120         throws WSIFException {
121         Trc.entry(this, name, inputName, outputName);
122
123         WSIFOperation_EJB tempOp =
124             (WSIFOperation_EJB) operationInstances.get(getKey(name, inputName, outputName));
125
126         WSIFOperation_EJB operation = null;
127         if (tempOp != null) {
128             operation = tempOp.copy();
129         }
130
131         if (operation == null) {
132             BindingOperation bindingOperationModel =
133                WSIFUtils.getBindingOperation(
134                   fieldPortModel.getBinding(), name, inputName, outputName );
135
136             if (bindingOperationModel != null) {
137                 operation = new WSIFOperation_EJB(fieldPortModel, bindingOperationModel, this);
138                 setDynamicWSIFOperation(name, inputName, outputName, operation);
139             }
140         }
141
142         Trc.exit(operation);
143         return operation;
144     }
145
146     public EJBHome JavaDoc getEjbHome() throws WSIFException {
147         Trc.entry(this);
148
149         if (fieldEjbHome == null) {
150             EJBAddress address = null;
151
152             try {
153                 ExtensibilityElement portExtension =
154                     (ExtensibilityElement) fieldPortModel.getExtensibilityElements().get(0);
155
156                 if (portExtension == null) {
157                     throw new WSIFException("missing port extension");
158                 }
159
160                 address = (EJBAddress) portExtension;
161
162                 Hashtable JavaDoc hash = new Hashtable JavaDoc();
163
164                 // If the initial context factory is specified in the wsdl use it
165
String JavaDoc icf = address.getInitialContextFactory();
166                 if (icf != null) {
167                     hash.put(InitialContext.INITIAL_CONTEXT_FACTORY, icf);
168                 }
169
170                 // If the jndi provider url is specified in the wsdl use it
171
String JavaDoc providerURL = address.getJndiProviderURL();
172                 if (providerURL != null) {
173                     hash.put(InitialContext.PROVIDER_URL, providerURL);
174                 }
175
176                 // Lookup from an authoritative source
177
hash.put(InitialContext.AUTHORITATIVE, "true");
178
179                 InitialContext JavaDoc initContext;
180                 initContext = new InitialContext JavaDoc(hash);
181                 
182                 Class JavaDoc homeClass = null;
183                 try {
184                     if (address.getClassName() != null) {
185                         homeClass =
186                             Class.forName(
187                                 address.getClassName(),
188                                 true,
189                                 Thread.currentThread().getContextClassLoader());
190                         if (!(EJBHome JavaDoc.class.isAssignableFrom(homeClass))) {
191                             homeClass = null;
192                         }
193                     }
194                 } catch (ClassNotFoundException JavaDoc cnf) {
195                     Trc.ignoredException(cnf);
196                 }
197                 
198                 if (homeClass != null) {
199                     fieldEjbHome = (EJBHome JavaDoc) PortableRemoteObject.narrow(initContext.lookup(address.getJndiName()),homeClass);
200                 } else {
201                     fieldEjbHome = (EJBHome JavaDoc) initContext.lookup(address.getJndiName());
202                 }
203
204             } catch (Exception JavaDoc ex) {
205                 Trc.exception(ex);
206                 throw new WSIFException(
207                     "Failed to lookup EJB home using JNDI name '" + address.getJndiName() + "'",
208                     ex);
209             }
210         }
211         Trc.exit(fieldEjbHome);
212         return fieldEjbHome;
213     }
214
215     public EJBObject JavaDoc getEjbObject() throws WSIFException {
216         Trc.entry(this);
217
218         if (fieldEjbObject == null) {
219             EJBHome JavaDoc ejbHome = getEjbHome();
220             try {
221                 Method JavaDoc createMethod = ejbHome.getClass().getDeclaredMethod("create", null);
222                 fieldEjbObject = (EJBObject JavaDoc) createMethod.invoke(ejbHome, null);
223             } catch (Exception JavaDoc ex) {
224                 Trc.exception(ex);
225                 throw new WSIFException(
226                     "Could not create instance for home '" + ejbHome + "'",
227                     ex);
228             }
229         }
230         Trc.exit(fieldEjbObject);
231         return fieldEjbObject;
232     }
233
234     public Port getPortModel() {
235         Trc.entry(this);
236         Trc.exit(fieldPortModel);
237         return fieldPortModel;
238     }
239
240     public void setDefinition(Definition value) {
241         Trc.entry(this, value);
242         fieldDefinition = value;
243         Trc.exit();
244     }
245
246     // WSIF: keep list of operations available in this port
247
public void setDynamicWSIFOperation(
248         String JavaDoc name,
249         String JavaDoc inputName,
250         String JavaDoc outputName,
251         WSIFOperation_EJB value) {
252         Trc.entry(this, name, inputName, outputName);
253
254         operationInstances.put(getKey(name, inputName, outputName), value);
255         Trc.exit();
256     }
257
258     public void setEjbHome(EJBHome JavaDoc newEjbHome) {
259         Trc.entry(this, newEjbHome);
260         fieldEjbHome = newEjbHome;
261         Trc.exit();
262     }
263
264     public void setEjbObject(EJBObject JavaDoc newEjbObject) {
265         Trc.entry(this, newEjbObject);
266         fieldEjbObject = newEjbObject;
267         Trc.exit();
268     }
269
270     public void setPortModel(Port value) {
271         Trc.entry(this, value);
272         fieldPortModel = value;
273         Trc.exit();
274     }
275
276     public WSIFOperation createOperation(String JavaDoc operationName)
277         throws WSIFException {
278         Trc.entry(this, operationName);
279         WSIFOperation wo = createOperation(operationName, null, null);
280         Trc.exit(wo);
281         return wo;
282     }
283
284     public WSIFOperation createOperation(
285         String JavaDoc operationName,
286         String JavaDoc inputName,
287         String JavaDoc outputName)
288         throws WSIFException {
289         Trc.entry(this, operationName, inputName, outputName);
290         WSIFOperation_EJB op =
291             getDynamicWSIFOperation(operationName, inputName, outputName);
292         if (op == null) {
293             throw new WSIFException(
294                 "Could not create operation: "
295                     + operationName
296                     + ":"
297                     + inputName
298                     + ":"
299                     + outputName);
300         }
301         WSIFOperation wo = op.copy();
302         Trc.exit(wo);
303         return wo;
304     }
305
306     public String JavaDoc deep() {
307         String JavaDoc buff = "";
308         try {
309             buff = new String JavaDoc(super.toString() + ":\n");
310
311             buff += "definition:" + Trc.brief(fieldDefinition);
312             buff += " portModel:" + Trc.brief(fieldPortModel);
313             buff += " ejbHome:" + fieldEjbHome;
314             buff += " ejbObject" + fieldEjbObject;
315
316             buff += " operationInstances:";
317             if (operationInstances == null)
318                 buff += "null";
319             else {
320                 buff += " size:" + operationInstances.size();
321                 Iterator JavaDoc it = operationInstances.keySet().iterator();
322                 int i = 0;
323                 while (it.hasNext()) {
324                     String JavaDoc key = (String JavaDoc) it.next();
325                     WSIFOperation_EJB woejb = (WSIFOperation_EJB) operationInstances.get(key);
326                     buff += "\noperationInstances[" + i + "]:" + key + " " + woejb + " ";
327                     i++;
328                 }
329             }
330         } catch (Exception JavaDoc e) {
331             Trc.exceptionInTrace(e);
332         }
333
334         return buff;
335     }
336 }
Popular Tags