KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > java > WSIFPort_Java


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.java;
59
60 import java.io.Serializable JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Map JavaDoc;
64 import java.util.Vector JavaDoc;
65
66 import javax.wsdl.BindingOperation;
67 import javax.wsdl.Definition;
68 import javax.wsdl.Port;
69 import javax.wsdl.extensions.ExtensibilityElement;
70 import javax.xml.namespace.QName JavaDoc;
71
72 import org.apache.wsif.WSIFException;
73 import org.apache.wsif.WSIFOperation;
74 import org.apache.wsif.base.WSIFDefaultPort;
75 import org.apache.wsif.logging.Trc;
76 import org.apache.wsif.providers.WSIFDynamicTypeMap;
77 import org.apache.wsif.util.WSIFUtils;
78 import org.apache.wsif.wsdl.extensions.format.TypeMap;
79 import org.apache.wsif.wsdl.extensions.format.TypeMapping;
80 import org.apache.wsif.wsdl.extensions.java.JavaAddress;
81
82 /**
83  * Java 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_Java extends WSIFDefaultPort implements Serializable JavaDoc {
91     
92     private static final long serialVersionUID = 1L;
93     
94     private javax.wsdl.Definition fieldDefinition = null;
95     private javax.wsdl.Port fieldPortModel = null;
96     private java.lang.Object JavaDoc fieldObjectReference = null; // 'physical connection'
97
private Map JavaDoc fieldTypeMaps = new HashMap JavaDoc();
98
99     protected Map JavaDoc operationInstances = new HashMap JavaDoc();
100
101     public WSIFPort_Java(Definition def, Port port, WSIFDynamicTypeMap typeMap)
102         throws WSIFException {
103         Trc.entry(this, def, port, typeMap);
104         fieldDefinition = def;
105         fieldPortModel = port;
106         buildTypeMap();
107         if (Trc.ON)
108             Trc.exit(deep());
109     }
110
111     public Definition getDefinition() {
112         Trc.entry(this);
113         Trc.exit(fieldDefinition);
114         return fieldDefinition;
115     }
116
117     public WSIFOperation createOperation(String JavaDoc operationName)
118         throws WSIFException {
119         Trc.entry(this);
120         WSIFOperation wo = createOperation(operationName, null, null);
121         Trc.exit(wo);
122         return wo;
123     }
124
125     public WSIFOperation createOperation(
126         String JavaDoc operationName,
127         String JavaDoc inputName,
128         String JavaDoc outputName)
129         throws WSIFException {
130         Trc.entry(this, operationName, inputName, outputName);
131
132         WSIFOperation_Java op =
133             getDynamicWSIFOperation(operationName, inputName, outputName);
134         if (op == null) {
135             throw new WSIFException(
136                 "Could not create operation: "
137                     + operationName
138                     + ":"
139                     + inputName
140                     + ":"
141                     + outputName);
142         }
143         WSIFOperation wo = op.copy();
144         Trc.exit(wo);
145         return wo;
146     }
147
148     protected WSIFOperation_Java getDynamicWSIFOperation(
149         String JavaDoc name,
150         String JavaDoc inputName,
151         String JavaDoc outputName)
152         throws WSIFException {
153         Trc.entry(this, name, inputName, outputName);
154
155         WSIFOperation_Java operation =
156             (WSIFOperation_Java) operationInstances.get(
157                 getKey(name, inputName, outputName));
158
159         if (operation == null) {
160             BindingOperation bindingOperationModel =
161             WSIFUtils.getBindingOperation(
162                fieldPortModel.getBinding(), name, inputName, outputName );
163
164             if (bindingOperationModel != null) {
165                 operation =
166                     new WSIFOperation_Java(
167                         fieldPortModel,
168                         bindingOperationModel,
169                         this,
170                         fieldTypeMaps);
171                 setDynamicWSIFOperation(name, inputName, outputName, operation);
172             }
173         }
174         Trc.exit(operation);
175         return operation;
176     }
177
178     public java.lang.Object JavaDoc getObjectReference() throws WSIFException {
179         Trc.entry(this);
180         if (fieldObjectReference == null) {
181             JavaAddress address = null;
182
183             try {
184                 ExtensibilityElement portExtension =
185                     (ExtensibilityElement) fieldPortModel.getExtensibilityElements().get(0);
186
187                 if (portExtension == null) {
188                     throw new WSIFException("missing port extension");
189                 }
190
191                 address = (JavaAddress) portExtension;
192
193                 fieldObjectReference =
194                     Class
195                         .forName(
196                             address.getClassName(),
197                             true,
198                             Thread.currentThread().getContextClassLoader())
199                         .newInstance();
200             } catch (Exception JavaDoc ex) {
201                 Trc.exception(ex);
202                 throw new WSIFException(
203                     "Could not create object of class '" + address.getClassName() + "'",
204                     ex);
205             }
206         }
207         Trc.exit(fieldObjectReference);
208         return fieldObjectReference;
209     }
210
211     public Port getPortModel() {
212         Trc.entry(this);
213         Trc.exit(fieldPortModel);
214         return fieldPortModel;
215     }
216
217     public void setDefinition(Definition value) {
218         Trc.entry(this, value);
219         fieldDefinition = value;
220         Trc.exit();
221     }
222
223     // WSIF: keep list of operations available in this port
224
public void setDynamicWSIFOperation(
225         String JavaDoc name,
226         String JavaDoc inputName,
227         String JavaDoc outputName,
228         WSIFOperation_Java value) {
229         Trc.entry(this, name, inputName, outputName, value);
230         operationInstances.put(getKey(name, inputName, outputName), value);
231         Trc.exit();
232     }
233
234     public void setObjectReference(java.lang.Object JavaDoc newObjectReference) {
235         Trc.entry(this, newObjectReference);
236         fieldObjectReference = newObjectReference;
237         Trc.exit();
238     }
239
240     public void setPortModel(Port value) {
241         Trc.entry(this, value);
242         fieldPortModel = value;
243         Trc.exit();
244     }
245
246     private void buildTypeMap() throws WSIFException {
247         Trc.entry(this);
248         TypeMapping typeMapping = null;
249
250         // Get the TypeMappings from the binding
251
Iterator JavaDoc bindingIterator =
252             this.fieldPortModel.getBinding().getExtensibilityElements().iterator();
253             
254         // Choose the first typeMap that has encoding=Java and style=Java.
255
// Ignore any other typeMap's that have other encodings and styles.
256
while (bindingIterator.hasNext()) {
257             Object JavaDoc next = bindingIterator.next();
258             if (next instanceof TypeMapping) {
259                 typeMapping = (TypeMapping) next;
260                 if ("Java".equals(typeMapping.getEncoding()) &&
261                     "Java".equals(typeMapping.getStyle()))
262                     break;
263                 typeMapping = null;
264             }
265         }
266
267         if (typeMapping == null) {
268             QName JavaDoc bindingName = fieldPortModel.getBinding().getQName();
269             throw new WSIFException(
270                 "Binding "
271                     + (bindingName == null ? "<null>" : bindingName.toString())
272                     + " does not contain a typeMap with encoding=Java and style=Java");
273         }
274
275         // Build the hashmap
276
bindingIterator = typeMapping.getMaps().iterator();
277         while (bindingIterator.hasNext()) {
278             TypeMap typeMap = (TypeMap) bindingIterator.next();
279             ///////////////////////////////////
280
QName JavaDoc typeName = typeMap.getTypeName();
281             if (typeName == null) typeName = typeMap.getElementName();
282             String JavaDoc type = typeMap.getFormatType();
283             if (typeName != null && type != null) {
284                 if (fieldTypeMaps.containsKey(typeName)) {
285                     Vector JavaDoc v = null;
286                     Object JavaDoc obj = fieldTypeMaps.get(typeName);
287                     if (obj instanceof Vector JavaDoc) {
288                         v = (Vector JavaDoc) obj;
289                     } else {
290                         v = new Vector JavaDoc();
291                         v.addElement(obj);
292                     }
293                     v.addElement(type);
294                     this.fieldTypeMaps.put(typeName, v);
295                 } else {
296                     this.fieldTypeMaps.put(typeName, type);
297                 }
298             } else {
299                 throw new WSIFException("Error in binding TypeMap. Key or Value is null");
300             }
301         }
302         Trc.exit();
303     }
304
305     public String JavaDoc deep() {
306         String JavaDoc buff = "";
307         try {
308             buff = new String JavaDoc(super.toString() + ":\n");
309
310             buff += "definition:" + fieldDefinition == null
311                 ? "null"
312                 : fieldDefinition.getQName() == null
313                 ? "unknown"
314                 : fieldDefinition.getQName().toString();
315             buff += " portModel:" + fieldPortModel == null
316                 ? "null"
317                 : fieldPortModel.getName() == null
318                 ? "unknown"
319                 : fieldPortModel.getName().toString();
320             buff += " objectReference:" + fieldObjectReference;
321
322             buff += " operationInstances:";
323             if (operationInstances == null)
324                 buff += "null";
325             else {
326                 buff += " size:" + operationInstances.size();
327                 Iterator JavaDoc it = operationInstances.keySet().iterator();
328                 int i = 0;
329                 while (it.hasNext()) {
330                     String JavaDoc key = (String JavaDoc) it.next();
331                     WSIFOperation_Java woj = (WSIFOperation_Java) operationInstances.get(key);
332                     buff += "\noperationInstances[" + i + "]:" + key + " " + woj + " ";
333                     i++;
334                 }
335             }
336         } catch (Exception JavaDoc e) {
337             Trc.exceptionInTrace(e);
338         }
339
340         return buff;
341     }
342 }
Popular Tags