KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > deployment > api > ServiceRefDesc


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * --------------------------------------------------------------------------
20  * $Id: ServiceRefDesc.java,v 1.17 2005/04/28 08:43:27 benoitf Exp $
21  * --------------------------------------------------------------------------
22  */

23
24 package org.objectweb.jonas_ws.deployment.api;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.Vector JavaDoc;
37 import javax.xml.namespace.QName JavaDoc;
38 import org.objectweb.jonas_ejb.lib.BeanNaming;
39 import org.objectweb.jonas_lib.I18n;
40 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
41 import org.objectweb.jonas_lib.deployment.api.HandlerDesc;
42 import org.objectweb.jonas_lib.deployment.xml.Handler;
43 import org.objectweb.jonas_lib.deployment.xml.JonasInitParam;
44 import org.objectweb.jonas_lib.deployment.xml.JonasPortComponentRef;
45 import org.objectweb.jonas_lib.deployment.xml.JonasServiceRef;
46 import org.objectweb.jonas_lib.deployment.xml.PortComponentRef;
47 import org.objectweb.jonas_lib.deployment.xml.ServiceRef;
48 import org.objectweb.jonas_ws.deployment.lib.MappingFileManager;
49 import org.objectweb.jonas_ws.deployment.lib.wrapper.MappingFileManagerWrapper;
50 import org.objectweb.jonas.common.Log;
51 import org.objectweb.jonas.server.Server;
52 import org.objectweb.util.monolog.api.BasicLevel;
53 import org.objectweb.util.monolog.api.Logger;
54
55 /**
56  * A ServiceRefDesc describe a dependancy from a J2EE component onto a
57  * WebService (it is a Web Service Client). The component will have the hability
58  * to lookup the Service Implementation and use it.
59  *
60  * @author Guillaume Sauthier
61  * @author Xavier Delplanque
62  */

63 public class ServiceRefDesc {
64
65     /**
66      * javax.xml.rpc.Service classname
67      */

68     private static final String JavaDoc JAVAX_XML_RPC_SERVICE = "javax.xml.rpc.Service";
69
70     /**
71      * logger
72      */

73     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
74
75     /**
76      * Internationalization
77      */

78     private static I18n i18n = I18n.getInstance(ServiceRefDesc.class);
79
80     /** All the params of the ServiceRefDesc */
81     private Hashtable JavaDoc params = new Hashtable JavaDoc();
82
83     /** The list of PortComponentRefs */
84     private Vector JavaDoc pcRefs = new Vector JavaDoc();
85
86     /** The list of Handlers */
87     private Vector JavaDoc hRefs = new Vector JavaDoc();
88
89     /** The WSDLFile of the ServiceRefDesc */
90     private WSDLFile wsdl;
91
92     /** The alternate WSDLFile of the ServiceRefDesc */
93     private WSDLFile altWsdl;
94
95     /** The MappingFile of the ServiceRefDesc */
96     private MappingFile mapping;
97
98     /** The name of the ServiceRefDesc (used in lookup) */
99     private String JavaDoc name;
100
101     /** The service interface */
102     private Class JavaDoc serviceInterface;
103
104     /** The Service QName */
105     private QName JavaDoc serviceQName = null;
106
107     /** The Module file (directory or jar) */
108     private File JavaDoc moduleFile = null;
109
110     /** WSDL FileName */
111     private String JavaDoc wsdlFileName = null;
112
113     /** jaxrpc mapping file name */
114     private String JavaDoc mappingFileName = null;
115
116     /** alt-wsdl URL */
117     private URL JavaDoc alternateWSDL = null;
118
119     /** local URL of the uptodate WSDL */
120     private URL JavaDoc localWSDLURL = null;
121
122     /** local URL os the mapping file */
123     private URL JavaDoc mappingFileURL = null;
124
125     /**
126      * Creates a new ServiceRefDesc object.
127      * @param classLoader web class loader
128      * @param sref generated object containing service-ref informations.
129      * @param jsref JOnAS specific service-ref informations.
130      * @param filename the name of the archive where retrieve WSDL and mapping
131      * files
132      * @throws WSDeploymentDescException if the wsdl file is undefined or if
133      * informations in wsdl and handler doesn't match.
134      */

135     public ServiceRefDesc(ClassLoader JavaDoc classLoader, ServiceRef sref, JonasServiceRef jsref, String JavaDoc filename)
136             throws WSDeploymentDescException {
137
138         if (filename != null) {
139             moduleFile = new File JavaDoc(filename);
140         }
141
142         // setup the classloader
143
ClassLoader JavaDoc loader = classLoader;
144
145         // set ServiceRefDesc name
146
name = sref.getServiceRefName();
147
148         // set Service QName (if exists)
149
if (sref.getServiceQname() != null) {
150             serviceQName = sref.getServiceQname().getQName();
151         }
152
153         // set Service Interface
154
String JavaDoc si = sref.getServiceInterface().trim();
155
156         try {
157             serviceInterface = loader.loadClass(si);
158         } catch (ClassNotFoundException JavaDoc cnf) {
159             throw new WSDeploymentDescException(getI18n().getMessage("ServiceRefDesc.serviceIntfNotFound", si), cnf); //$NON-NLS-1$
160
}
161
162         // this interface must implements javax.xml.rpc.Service
163
if (!javax.xml.rpc.Service JavaDoc.class.isAssignableFrom(serviceInterface)) {
164             String JavaDoc err = getI18n().getMessage("ServiceRefDesc.mustExtService", serviceInterface.getName()); //$NON-NLS-1$
165
throw new WSDeploymentDescException(err);
166         }
167
168         // ServiceRefDesc init parameters : jonas specific deployment desc
169
if (jsref != null) {
170             List JavaDoc jipl = jsref.getJonasInitParamList();
171
172             for (int i = 0; i < jipl.size(); i++) {
173                 // add in params table each init parameter name and associated value
174
JonasInitParam p = (JonasInitParam) jipl.get(i);
175                 params.put(p.getParamName().trim(), p.getParamValue().trim());
176             }
177         }
178
179         // fill portComponentRef list
180
Map JavaDoc links = linkPCR2JPCR(sref, jsref);
181         List JavaDoc pcrl = sref.getPortComponentRefList();
182
183         for (int i = 0; i < pcrl.size(); i++) {
184             // for each reference, build and add a PortComponentRef Object
185
PortComponentRef ref = (PortComponentRef) pcrl.get(i);
186             JonasPortComponentRef jref = (JonasPortComponentRef) links.get(ref.getServiceEndpointInterface());
187             pcRefs.add(new PortComponentRefDesc(loader, ref, jref));
188         }
189
190         // fill handlers list
191
List JavaDoc hl = sref.getHandlerList();
192         Handler h = null;
193
194         for (int i = 0; i < hl.size(); i++) {
195             // for each reference, build and add a HandlerRef Object
196
// get the standard dd handler
197
h = (Handler) hl.get(i);
198             // build and add a new handler
199
try {
200                 hRefs.add(new HandlerDesc(loader, h));
201             } catch (DeploymentDescException dde) {
202                 throw new WSDeploymentDescException(dde);
203             }
204         }
205
206         // if alt-wsdl defined AND running online
207
if (jsref != null && jsref.getAltWsdl() != null && isJonasRuntime()) {
208             if (logger.isLoggable(BasicLevel.DEBUG)) {
209                 logger.log(BasicLevel.DEBUG, "loading alt-wsdl : " + jsref.getAltWsdl());
210             }
211             try {
212                 alternateWSDL = new URL JavaDoc(jsref.getAltWsdl());
213                 altWsdl = new WSDLFile(alternateWSDL, jsref.getAltWsdl());
214             } catch (IOException JavaDoc e) {
215                 String JavaDoc err = "Cannot load alternate WSDL Stream from " + jsref.getAltWsdl();
216                 throw new WSDeploymentDescException(err);
217             }
218         }
219
220         // get ServiceRefDesc WSDLFile name
221
String JavaDoc wf = sref.getWsdlFile();
222         if (wf != null) {
223             this.wsdlFileName = wf.trim();
224             wsdl = new WSDLFile(loader, wsdlFileName);
225
226             localWSDLURL = loader.getResource(wsdlFileName);
227         }
228
229         if (getWSDLFile() != null) {
230             if ((getWSDLFile().getNbServices() > 1) && (serviceQName == null)) {
231                 String JavaDoc err = getI18n().getMessage("ServiceRefDesc.serviceQnameNotDef", wsdlFileName, name); //$NON-NLS-1$
232
throw new WSDeploymentDescException(err);
233             } else if ((getWSDLFile().getNbServices() == 1) && (serviceQName == null)) {
234                 // When the WSDL defines only 1 service, we can retrieve
235
// automatically the service QName for service-ref
236
serviceQName = getWSDLFile().getServiceQname();
237             } else {
238                 // serviceQName not null
239
// check service QName existence in WSDL
240
if (isJonasRuntime()) {
241                     if (!getWSDLFile().hasService(serviceQName)) {
242                         throw new WSDeploymentDescException(getI18n().getMessage(
243                                 "ServiceRefDesc.serviceNotFoundInWSDL", serviceQName, wsdlFileName)); //$NON-NLS-1$
244
}
245                 }
246             }
247
248         }
249
250         // get ServiceRefDesc MappingFile name
251
String JavaDoc mf = sref.getJaxrpcMappingFile();
252
253         if (mf != null) {
254             this.mappingFileName = mf.trim();
255             this.mappingFileURL = loader.getResource(mappingFileName);
256             setMappingFile(loader);
257         }
258
259         // mapping is required when generated service interface is specified
260
if (!serviceInterface.getName().equals(JAVAX_XML_RPC_SERVICE) && (mapping == null)) {
261             throw new WSDeploymentDescException(getI18n().getMessage(
262                     "ServiceRefDesc.mappingRequired", serviceInterface.getName())); //$NON-NLS-1$
263
}
264
265         validate();
266     }
267
268     /**
269      * @return Returns true if called in a JOnAS runtime (in a container, ...).
270      */

271     private boolean isJonasRuntime() {
272
273         if (Server.isStarted()) {
274             // we're in jonas runtime
275
// but if we're doing deploy stuff, return false
276
Throwable JavaDoc t = new Exception JavaDoc();
277             StackTraceElement JavaDoc[] elem = t.getStackTrace();
278             for (int i = 0; i < elem.length; i++) {
279                 if ("org.objectweb.jonas_ws.wsgen.wrapper.WsGenWrapper".equals(elem[i].getClassName())) {
280                     return false;
281                 }
282                 if ("org.objectweb.jonas_lib.genclientstub.wrapper.ClientGenStubWrapper".equals(elem[i].getClassName())) {
283                     return false;
284                 }
285                 if ("org.objectweb.jonas_ejb.genic.wrapper.GenicServiceWrapper".equals(elem[i].getClassName())) {
286                     return false;
287                 }
288             }
289             return true;
290
291         } else {
292             // offline mode or client container
293
ClassLoader JavaDoc cl = this.getClass().getClassLoader();
294             try {
295                 cl.loadClass("org.objectweb.jonas.server.Bootstrap");
296             } catch (ClassNotFoundException JavaDoc e) {
297                 // we're in client container
298
// => jonas runtime
299
return true;
300             }
301             // loading successful => we're not in client container
302
return false;
303         }
304     }
305
306     /**
307      * @param sref ServiceRef instance
308      * @param jsref linked JonasServiceRef instance
309      * @return Returns a map associating the PortComponentRef.sei with the JonasPortComponentRef
310      */

311     private Map JavaDoc linkPCR2JPCR(ServiceRef sref, JonasServiceRef jsref) {
312         Map JavaDoc res = new HashMap JavaDoc();
313         // for each port-component-ref
314
for (Iterator JavaDoc i = sref.getPortComponentRefList().iterator(); i.hasNext();) {
315             PortComponentRef pcr = (PortComponentRef) i.next();
316             res.put(pcr.getServiceEndpointInterface(), null);
317         }
318         // jonas-port-component-ref(s)
319
if (jsref != null) {
320
321             // get all jonas-port-component.sei
322
Set JavaDoc keys = res.keySet();
323
324             // for each jonas-port-component
325
for (Iterator JavaDoc i = jsref.getJonasPortComponentRefList().iterator(); i.hasNext();) {
326                 JonasPortComponentRef jpcr = (JonasPortComponentRef) i.next();
327                 String JavaDoc sei = jpcr.getServiceEndpointInterface();
328
329                 if ((sei != null) && (keys.contains(sei))) {
330                     // jonas-port-component-ref linked to port-component-ref
331
res.put(sei, jpcr);
332                 } else {
333                     String JavaDoc err = "jonas-port-component-ref '" + sei + "' is not linked to any port-component-ref. It will be ignored.";
334                     logger.log(BasicLevel.WARN, err);
335                 }
336             }
337         }
338         return res;
339     }
340
341     /**
342      * Return the list of PortComponentRef.
343      * @return the list of PortComponentRef
344      */

345     public List JavaDoc getPortComponentRefs() {
346         return pcRefs;
347     }
348
349     /**
350      * Return the list of Handler.
351      * @return the list of Handler
352      */

353     public List JavaDoc getHandlerRefs() {
354         return hRefs;
355     }
356
357     /**
358      * Return the name used for Service interface lookup.
359      * @return the service-ref-name value
360      */

361     public String JavaDoc getServiceRefName() {
362         return name;
363     }
364
365     /**
366      * Return the Class object representing the service-interface.
367      * @return the Class object representing the service-interface.
368      */

369     public Class JavaDoc getServiceInterface() {
370         return serviceInterface;
371     }
372
373     /**
374      * Return the WSDLFile object describing the WebService.
375      * @return the WSDLFile object describing the WebService.
376      */

377     public WSDLFile getWSDLFile() {
378         // if alt-wsdl is not set, use standard wsdl-file
379
WSDLFile file = altWsdl;
380         if (file == null) {
381             file = wsdl;
382         }
383         return file;
384     }
385
386     /**
387      * Return the MappingFile object.
388      * @return the MappingFile object.
389      */

390     public MappingFile getMappingFile() {
391         return mapping;
392     }
393
394     /**
395      * Return all the params of the ServiceRefDesc as an Hashtable.
396      * @return all the params of the ServiceRefDesc as an Hashtable.
397      */

398     public Hashtable JavaDoc getParams() {
399         return params;
400     }
401
402     /**
403      * Return the value of the specified parameter
404      * @param name the parameter to retrieve
405      * @return the value of the specified parameter
406      */

407     public String JavaDoc getParam(String JavaDoc name) {
408         return (String JavaDoc) params.get(name);
409     }
410
411     /**
412      * Return the name of WSDL inside of the module.
413      * @return the name of WSDL inside of the module
414      */

415     public String JavaDoc getWsdlFileName() {
416         return wsdlFileName;
417     }
418
419     /**
420      * Return the QName identifying the service in the WSDL. can return null if
421      * WSDL not defined.
422      * @return Return the QName identifying the service in the WSDL (can be
423      * null).
424      */

425     public QName JavaDoc getServiceQName() {
426         return serviceQName;
427     }
428
429
430
431     /**
432      * @see java.lang.String#hashCode()
433      */

434     public int hashCode() {
435         return this.name.hashCode();
436     }
437     /**
438      * Return <code>true</code> if the parameter is a ServiceRefDesc and if it
439      * equals this object. Return <code>false</code> else.
440      * @param other the object to compare
441      * @return true if objects are equals
442      */

443     public boolean equals(Object JavaDoc other) {
444         if (other == null) {
445             return false;
446         }
447
448         if (!(other instanceof ServiceRefDesc)) {
449             return false;
450         }
451
452         ServiceRefDesc sr = (ServiceRefDesc) other;
453
454         if (!mapping.equals(sr.getMappingFile())) {
455             return false;
456         }
457
458         if (!wsdl.equals(sr.getWSDLFile())) {
459             return false;
460         }
461
462         if (!params.equals(sr.getParams())) {
463             return false;
464         }
465
466         for (Iterator JavaDoc i = sr.getPortComponentRefs().iterator(); i.hasNext();) {
467             PortComponentRefDesc pcr = (PortComponentRefDesc) i.next();
468
469             if (!pcRefs.contains(pcr)) {
470                 return false;
471             }
472         }
473
474         for (Iterator JavaDoc i = sr.getHandlerRefs().iterator(); i.hasNext();) {
475             HandlerDesc hr = (HandlerDesc) i.next();
476
477             if (!hRefs.contains(hr)) {
478                 return false;
479             }
480         }
481
482         return true;
483     }
484
485     /**
486      * Create the MappingFile.
487      * @param loader ClassLoader containing mapping file
488      * @throws WSDeploymentDescException When MappingFile creation fails.
489      */

490     private void setMappingFile(ClassLoader JavaDoc loader) throws WSDeploymentDescException {
491
492         // build the MappingFile
493
// Build Mapping file
494
if (moduleFile != null) {
495             if (isRunningInClientContainer()) {
496                 mapping = MappingFileManager.getInstance(moduleFile, mappingFileName);
497             } else {
498                 mapping = MappingFileManagerWrapper.getMappingFile(moduleFile, mappingFileName);
499             }
500         } else {
501             // Try to get the mapping from the ClassLoader
502
InputStream JavaDoc is = null;
503             if (loader != null) {
504                 is = loader.getResourceAsStream(mappingFileName);
505                 if (is != null) {
506                     // build the MappingFile
507
if (isRunningInClientContainer()) {
508                         mapping = MappingFileManager.getInstance(is, mappingFileName);
509                     } else {
510                         mapping = MappingFileManagerWrapper.getMappingFile(is, mappingFileName);
511                     }
512                 } else {
513                     throw new WSDeploymentDescException(getI18n().getMessage(
514                             "ServiceRefDesc.mappingFileNotFoundInLoader", mappingFileName)); //$NON-NLS-1$
515
}
516             } else {
517                 throw new WSDeploymentDescException(getI18n().getMessage("ServiceRefDesc.mappingFileNotFound")); //$NON-NLS-1$
518
}
519         }
520     }
521
522     /**
523      * @return Returns true if current execution takes place inside a
524      * ClientContainer
525      */

526     private boolean isRunningInClientContainer() {
527         return (System.getProperty("jonas.base") == null);
528     }
529
530     /**
531      * Validate the ServiceRefDesc
532      * @throws WSDeploymentDescException DOCUMENT ME!
533      */

534     private void validate() throws WSDeploymentDescException {
535         // validate informations :
536
// TODO pcLinkExist
537

538         if ((wsdlFileName != null) && ((mappingFileName == null) || ("".equals(mappingFileName)))) {
539             // Must have a mapping file along with WSDL
540
throw new WSDeploymentDescException(getI18n().getMessage(
541                     "ServiceRefDesc.missingMappingFile", this.name)); //$NON-NLS-1$
542
}
543
544         // Port validity between handlers and wsdl
545
for (int i = 0; i < hRefs.size(); i++) {
546             HandlerDesc myHRef = (HandlerDesc) hRefs.get(i);
547             List JavaDoc pnl = myHRef.getPortNames();
548
549             for (int j = 0; j < pnl.size(); j++) {
550                 if (!getWSDLFile().hasPort((String JavaDoc) pnl.get(j))) {
551                     // the handler use a port undefined in the wsdl
552
throw new WSDeploymentDescException(getI18n().getMessage(
553                             "ServiceRefDesc.undefinedPort", myHRef.getName(), pnl.get(j), wsdlFileName)); //$NON-NLS-1$
554
}
555             }
556         }
557
558         // Check service-interface
559
// if service interface is not generic, Full WSDL Knowledge is required
560
if (!serviceInterface.equals(javax.xml.rpc.Service JavaDoc.class)) {
561             if ((getWSDLFile() == null) || ((getWSDLFile() != null) && (getWSDLFile().getDefinition().getServices().values().size() == 0))) {
562                 throw new WSDeploymentDescException(getI18n().getMessage(
563                         "ServiceRefDesc.wsdlMissingInformation", serviceInterface.getName(), name)); //$NON-NLS-1$
564
}
565
566             // Check mapping for GeneratedServiceInterface
567
String JavaDoc namespaceURI = serviceQName.getNamespaceURI();
568             String JavaDoc realPackage = BeanNaming.getPackageName(serviceInterface.getName());
569
570             // real != mapping provided package
571
if (!realPackage.equals(mapping.getMapping(namespaceURI))) {
572                 throw new WSDeploymentDescException(getI18n().getMessage(
573                         "ServiceRefDesc.needPackageMapping", mappingFileName, realPackage)); //$NON-NLS-1$
574
}
575
576             /*
577              * TODO : Uncomment this part will require substancial modification
578              * to number of tests (integ + unit) // -> We need to create
579              * generated clients interfaces for each endpoint ... // keep
580              * service QName namespaceURI (targetNamespace) // check mapping for
581              * ServiceEndpointInterface when having a generated
582              * service-interface for (Iterator p = pcRefs.iterator() ;
583              * p.hasNext() ; ) { PortComponentRef pcr = (PortComponentRef)
584              * p.next(); String sei = pcr.getSei().getName(); realPackage =
585              * BeanNaming.getPackageName(sei); if
586              * (!realPackage.equals(mapping.getMapping(namespaceURI))) throw new
587              * WSDeploymentDescException("jaxrpc-mapping-file '" +
588              * mappingFileName + "' need a mapping for package '" + realPackage +
589              * "'"); }
590              */

591         }
592     }
593
594     /**
595      * @return Returns the i18n.
596      */

597     protected static I18n getI18n() {
598         return i18n;
599     }
600
601     /**
602      * @return Returns the alternate WSDL URL (may be null).
603      */

604     public URL JavaDoc getAlternateWsdlURL() {
605         return alternateWSDL;
606     }
607
608     /**
609      * @return Returns the URL where the uptodate WSDL can be found.
610      */

611     public URL JavaDoc getLocalWSDLURL() {
612         return localWSDLURL;
613     }
614
615     /**
616      * @return Returns the URL where the mapping file can be found.
617      */

618     public URL JavaDoc getMappingFileURL() {
619         return mappingFileURL;
620     }
621 }
Popular Tags