KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > base > WSIFServiceFactoryImpl


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.base;
59
60 import java.util.Hashtable JavaDoc;
61 import java.util.Map JavaDoc;
62
63 import javax.wsdl.Definition;
64 import javax.wsdl.PortType;
65 import javax.wsdl.Service;
66 import javax.xml.namespace.QName JavaDoc;
67
68 import org.apache.wsif.WSIFException;
69 import org.apache.wsif.WSIFService;
70 import org.apache.wsif.WSIFServiceFactory;
71 import org.apache.wsif.logging.Trc;
72
73 /**
74  * Factory class used to create instances of WSIFService
75  *
76  * @author Mark Whitlock
77  * @author Owen Burroughs <owenb@apache.org>
78  */

79 public class WSIFServiceFactoryImpl extends WSIFServiceFactory {
80
81     private boolean useCache = false;
82     private Map JavaDoc cache = new Hashtable JavaDoc();
83
84     /**
85      * Create a WSIFService from WSDL document URL.
86      * <br> If serviceName or serviceNS is null,
87      * then WSDL document must have exactly one service in it.
88      * <br> If portTypeName or portTypeNS is null,
89      * then WSDL document must have exactly one portType in it
90      * and all ports of the selected service must
91      * implement the same portType.
92      * @param wsdlLoc The URL for the wsdl's location
93      * @param serviceNS The namespace of the service
94      * @param serviceName The name of the service
95      * @param portTypeNS The namespace of the port type
96      * @param portTypeName The name of the port type
97      * @return The service
98      * @exception A WSIFException if an error occurs when creating the service
99      */

100     public WSIFService getService(
101         String JavaDoc wsdlLoc,
102         String JavaDoc serviceNS,
103         String JavaDoc serviceName,
104         String JavaDoc portTypeNS,
105         String JavaDoc portTypeName)
106         throws WSIFException {
107         Trc.entry(
108             this,
109             wsdlLoc,
110             serviceNS,
111             serviceName,
112             portTypeNS,
113             portTypeName);
114
115         String JavaDoc key = "";
116         if (useCache) {
117             key =
118                 genCacheKey(
119                     wsdlLoc,
120                     serviceNS,
121                     serviceName,
122                     portTypeNS,
123                     portTypeName);
124             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
125             if (cachedWSI != null) {
126                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
127                 Trc.exit(wsi);
128                 return wsi;
129             }
130         }
131
132         WSIFServiceImpl wsi =
133             new WSIFServiceImpl(
134                 wsdlLoc,
135                 serviceNS,
136                 serviceName,
137                 portTypeNS,
138                 portTypeName);
139
140         if (useCache && !key.equals("")) {
141             cache.put(key, wsi);
142         }
143
144         Trc.exit(wsi);
145         return wsi;
146     }
147
148     /**
149      * Create a WSIF service instance from WSDL document URL
150      * using a ClassLoader to find local resources.
151      * <br> If serviceName or serviceNS is null,
152      * then WSDL document must have exactly one service in it.
153      * <br> If portTypeName or portTypeNS is null,
154      * then WSDL document must have exactly one portType in it
155      * and all ports of the selected service must
156      * implement the same portType.
157      * @param wsdlLoc The URL for the wsdl's location
158      * @param cl A ClassLoader to use in locating the wsdl
159      * @param serviceNS The namespace of the service
160      * @param serviceName The name of the service
161      * @param portTypeNS The namespace of the port type
162      * @param portTypeName The name of the port type
163      * @return The service
164      * @exception A WSIFException if an error occurs when creating the service
165      */

166     public WSIFService getService(
167         String JavaDoc wsdlLoc,
168         ClassLoader JavaDoc cl,
169         String JavaDoc serviceNS,
170         String JavaDoc serviceName,
171         String JavaDoc portTypeNS,
172         String JavaDoc portTypeName)
173         throws WSIFException {
174         Trc.entry(
175             this,
176             wsdlLoc,
177             cl,
178             serviceNS,
179             serviceName,
180             portTypeNS,
181             portTypeName);
182
183         String JavaDoc key = "";
184         if (useCache) {
185             key =
186                 genCacheKey(
187                     wsdlLoc,
188                     serviceNS,
189                     serviceName,
190                     portTypeNS,
191                     portTypeName);
192             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
193             if (cachedWSI != null) {
194                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
195                 Trc.exit(wsi);
196                 return wsi;
197             }
198         }
199
200         WSIFServiceImpl wsi =
201             new WSIFServiceImpl(
202                 wsdlLoc,
203                 cl,
204                 serviceNS,
205                 serviceName,
206                 portTypeNS,
207                 portTypeName);
208
209         if (useCache && !key.equals("")) {
210             cache.put(key, wsi);
211         }
212
213         Trc.exit(wsi);
214         return wsi;
215     }
216
217     /**
218      * Returns a new WSIFService.
219      * @param def The Definition object representing the wsdl
220      * @return The service
221      * @exception A WSIFException if an error occurs when creating the service
222      */

223     public WSIFService getService(Definition def) throws WSIFException {
224         Trc.entryExpandWsdl(this, new Object JavaDoc[] { def });
225         String JavaDoc key = "";
226         if (useCache) {
227             key = genCacheKey(def, null, null);
228             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
229             if (cachedWSI != null) {
230                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
231                 Trc.exit(wsi);
232                 return wsi;
233             }
234         }
235
236         WSIFServiceImpl wsi = new WSIFServiceImpl(def);
237         if (useCache && !key.equals("")) {
238             cache.put(key, wsi);
239         }
240
241         Trc.exit(wsi);
242         return wsi;
243     }
244
245     /**
246      * Returns a new WSIFService.
247      * @param def The Definition object representing the wsdl
248      * @param service The Service object representing the service to use
249      * @return The service
250      * @exception A WSIFException if an error occurs when creating the service
251      */

252     public WSIFService getService(Definition def, Service service)
253         throws WSIFException {
254         Trc.entryExpandWsdl(this, new Object JavaDoc[] { def, service });
255         String JavaDoc key = "";
256         if (useCache) {
257             key = genCacheKey(def, service, null);
258             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
259             if (cachedWSI != null) {
260                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
261                 Trc.exit(wsi);
262                 return wsi;
263             }
264         }
265
266         WSIFServiceImpl wsi = new WSIFServiceImpl(def, service);
267         if (useCache && !key.equals("")) {
268             cache.put(key, wsi);
269         }
270         Trc.exit(wsi);
271         return wsi;
272     }
273
274     /**
275      * Returns a new WSIFService.
276      * @param def The Definition object representing the wsdl
277      * @param service The Service object representing the service to use
278      * @param portType The PortType object representing the port type to use
279      * @return The service
280      * @exception A WSIFException if an error occurs when creating the service
281      */

282     public WSIFService getService(
283         Definition def,
284         Service service,
285         PortType portType)
286         throws WSIFException {
287         Trc.entryExpandWsdl(this, new Object JavaDoc[]{def, service, portType});
288         String JavaDoc key = "";
289         if (useCache) {
290             key = genCacheKey(def, service, portType);
291             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
292             if (cachedWSI != null) {
293                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
294                 Trc.exit(wsi);
295                 return wsi;
296             }
297         }
298
299         WSIFServiceImpl wsi = new WSIFServiceImpl(def, service, portType);
300         if (useCache && !key.equals("")) {
301             cache.put(key, wsi);
302         }
303
304         Trc.exit(wsi);
305         return wsi;
306     }
307
308     /**
309      * Returns a new WSIFService.
310      * @param def The Definition object representing the wsdl
311      * @param serviceNS The namespace of the service
312      * @param serviceName The name of the service
313      * @param portTypeNS The namespace of the port type
314      * @param portTypeName The name of the port type
315      * @return The service
316      * @exception A WSIFException if an error occurs when creating the service
317      */

318     public WSIFService getService(
319         Definition def,
320         String JavaDoc serviceNS,
321         String JavaDoc serviceName,
322         String JavaDoc portTypeNS,
323         String JavaDoc portTypeName)
324         throws WSIFException {
325         Trc.entryExpandWsdl(
326             this,
327             new Object JavaDoc[] {
328                 def,
329                 serviceNS,
330                 serviceName,
331                 portTypeNS,
332                 portTypeName });
333
334         String JavaDoc key = "";
335         if (useCache) {
336             key =
337                 genCacheKey(
338                     def,
339                     serviceNS,
340                     serviceName,
341                     portTypeNS,
342                     portTypeName);
343             WSIFServiceImpl cachedWSI = (WSIFServiceImpl) cache.get(key);
344             if (cachedWSI != null) {
345                 WSIFServiceImpl wsi = new WSIFServiceImpl(cachedWSI);
346                 Trc.exit(wsi);
347                 return wsi;
348             }
349         }
350
351         WSIFServiceImpl wsi =
352             new WSIFServiceImpl(
353                 def,
354                 serviceNS,
355                 serviceName,
356                 portTypeNS,
357                 portTypeName);
358         if (useCache && !key.equals("")) {
359             cache.put(key, wsi);
360         }
361
362         Trc.exit(wsi);
363         return wsi;
364     }
365
366     /**
367      * Set caching of servies on/off. The default is off. If caching is on then
368      * a call to getService will first check if a service matching the parameters
369      * specified has already been created and if so a reference to that instance
370      * of WSIFService is returned.
371      * @param on Flag to indicate whether or not caching of services should be used
372      */

373     public void cachingOn(boolean on) {
374         Trc.entry(this,on);
375         useCache = on;
376         Trc.exit();
377     }
378
379     private String JavaDoc genCacheKey(
380         Definition def,
381         Service service,
382         PortType portType) {
383         Trc.entry(this, def, service, portType);
384
385         String JavaDoc db =
386             (def != null && def.getDocumentBaseURI() != null)
387                 ? def.getDocumentBaseURI()
388                 : "null";
389         QName JavaDoc serviceName = (service != null) ? service.getQName() : null;
390         String JavaDoc sn = (serviceName != null) ? serviceName.toString() : "null";
391         QName JavaDoc portTypeName = (portType != null) ? portType.getQName() : null;
392         String JavaDoc ptn = (portTypeName != null) ? portTypeName.toString() : "null";
393         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
394         key.append("D=");
395         key.append(db);
396         key.append("S=");
397         key.append(sn);
398         key.append("P=");
399         key.append(ptn);
400
401         String JavaDoc ret = key.toString();
402         // If no distinguishable information is available then don't add to cache
403
if (ret.equals("D=nullS=nullP=null"))
404             ret = "";
405         Trc.exit(ret);
406         return ret;
407     }
408
409     private String JavaDoc genCacheKey(
410         String JavaDoc wsdlLoc,
411         String JavaDoc serviceNS,
412         String JavaDoc serviceName,
413         String JavaDoc portTypeNS,
414         String JavaDoc portTypeName) {
415         Trc.entry(
416             this,
417             wsdlLoc,
418             serviceNS,
419             serviceName,
420             portTypeNS,
421             portTypeName);
422
423         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
424         if (wsdlLoc == null)
425             wsdlLoc = "";
426         if (serviceNS == null)
427             serviceNS = "";
428         if (serviceName == null)
429             serviceName = "";
430         if (portTypeNS == null)
431             portTypeNS = "";
432         if (portTypeName == null)
433             portTypeName = "";
434         key.append("W=");
435         key.append(wsdlLoc);
436         key.append("SN=");
437         key.append(serviceNS);
438         key.append("SS=");
439         key.append(serviceName);
440         key.append("PN=");
441         key.append(portTypeNS);
442         key.append("PS=");
443         key.append(portTypeName);
444
445         String JavaDoc ret = key.toString();
446         // If no distinguishable information is available then don't add to cache
447
if (ret.equals("W=SN=SS=PN=PS="))
448             ret = "";
449         Trc.exit(ret);
450         return ret;
451     }
452
453     private String JavaDoc genCacheKey(
454         Definition def,
455         String JavaDoc serviceNS,
456         String JavaDoc serviceName,
457         String JavaDoc portTypeNS,
458         String JavaDoc portTypeName) {
459         Trc.entry(this, def, serviceNS, serviceName, portTypeNS, portTypeName);
460
461         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
462         String JavaDoc db =
463             (def != null && def.getDocumentBaseURI() != null)
464                 ? def.getDocumentBaseURI()
465                 : "null";
466         if (serviceNS == null)
467             serviceNS = "";
468         if (serviceName == null)
469             serviceName = "";
470         if (portTypeNS == null)
471             portTypeNS = "";
472         if (portTypeName == null)
473             portTypeName = "";
474         key.append("D=");
475         key.append(db);
476         key.append("SN=");
477         key.append(serviceNS);
478         key.append("SS=");
479         key.append(serviceName);
480         key.append("PN=");
481         key.append(portTypeNS);
482         key.append("PS=");
483         key.append(portTypeName);
484
485         String JavaDoc ret = key.toString();
486         // If no distinguishable information is available then don't add to cache
487
if (ret.equals("D=nullSN=SS=PN=PS="))
488             ret = "";
489         Trc.exit(ret);
490         return ret;
491     }
492 }
493
Popular Tags