KickJava   Java API By Example, From Geeks To Geeks.

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


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

25
26 package org.objectweb.jonas_ws.deployment.api;
27
28 import java.io.File JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 import org.objectweb.jonas_lib.I18n;
41
42 import org.objectweb.jonas_ws.deployment.lib.MappingFileManager;
43 import org.objectweb.jonas_ws.deployment.lib.wrapper.MappingFileManagerWrapper;
44 import org.objectweb.jonas_ws.deployment.xml.JonasPortComponent;
45 import org.objectweb.jonas_ws.deployment.xml.JonasWebserviceDescription;
46 import org.objectweb.jonas_ws.deployment.xml.PortComponent;
47 import org.objectweb.jonas_ws.deployment.xml.WebserviceDescription;
48
49 import org.objectweb.jonas.common.Log;
50
51 import org.objectweb.util.monolog.api.BasicLevel;
52 import org.objectweb.util.monolog.api.Logger;
53
54 /**
55  * This class corresponds to 1 <code>webservices-description</code> XML
56  * element. It's used to get a MappingFile instance if it's defined, idem for
57  * WSDLFile instance and give access to the Web service Port component list.
58  *
59  * @author Guillaume Sauthier
60  * @author Xavier Delplanque
61  */

62 public class ServiceDesc {
63
64     /**
65      * Internationalization
66      */

67     private static I18n i18n = I18n.getInstance(ServiceDesc.class);
68
69     /**
70      * logger
71      */

72     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
73
74     /**
75      * The webservice-description-name
76      */

77     private String JavaDoc name;
78
79     /**
80      * The list of PortComponentDesc
81      */

82     private Hashtable JavaDoc namePCDescBindings = new Hashtable JavaDoc();
83
84     /**
85      * The WSDL of the WebService
86      */

87     private WSDLFile wsdl = null;
88
89     /**
90      * The JaxRpc Mapping of the WebService
91      */

92     private MappingFile mapping = null;
93
94     /**
95      * Default endpoint URI for this group of WebServices
96      */

97     private String JavaDoc endpointURI = null;
98
99     /**
100      * Place where WSDL(s) will be published (may be null)
101      */

102     private File JavaDoc publicationDirectory = null;
103
104     /**
105      * URL of the mapping file
106      */

107     private URL JavaDoc mappingFileURL;
108
109     /**
110      * URL of the WSDL
111      */

112     private URL JavaDoc localWSDLURL;
113
114     /**
115      * JAX-RPC Mapping file name
116      */

117     private String JavaDoc mappingFilename = null;
118
119     /**
120      * WSDL file name
121      */

122     private String JavaDoc wsdlFilename = null;
123
124     /**
125      * Constructor : creates a ServiceDesc object.
126      * @param jarCL ejbjar or war classLoader
127      * @param wsd Zeus object containing WebserviceDescription informations
128      * @param jwsd Zeus object containing JonasWebserviceDescription informations
129      * @throws WSDeploymentDescException When contruction fails.
130      */

131     public ServiceDesc(ClassLoader JavaDoc jarCL, WebserviceDescription wsd, JonasWebserviceDescription jwsd) throws WSDeploymentDescException {
132
133         // set name
134
name = wsd.getWebserviceDescriptionName();
135         if ("".equals(name)) { //$NON-NLS-1$
136
throw new WSDeploymentDescException(getI18n().getMessage("ServiceDesc.noServiceName")); //$NON-NLS-1$
137
}
138
139         if (jwsd != null) {
140             // set endpointURI
141
String JavaDoc uri = jwsd.getDefaultEndpointURI();
142             if (uri != null && !"".equals(uri)) {
143                 endpointURI = uri;
144             }
145
146             // Set wsdl-publication-directory
147
String JavaDoc wsdlPubDir = jwsd.getWsdlPublishDirectory();
148             if (wsdlPubDir != null) {
149                 URL JavaDoc pub;
150                 try {
151                     pub = new URL JavaDoc(wsdlPubDir);
152                 } catch (MalformedURLException JavaDoc e) {
153                     throw new WSDeploymentDescException("Cannot create URL : " + wsdlPubDir, e);
154                 }
155                 publicationDirectory = new File JavaDoc(pub.getPath());
156             }
157         }
158
159
160         // get ServiceRef WSDLFile name
161
wsdlFilename = wsd.getWsdlFile();
162
163         // wsdl-file element must have a value
164
if ("".equals(wsdlFilename)) { //$NON-NLS-1$
165
String JavaDoc err = getI18n().getMessage("ServiceDesc.noWSDL", name); //$NON-NLS-1$
166
throw new WSDeploymentDescException(err);
167         }
168
169         // build the WSDLFile
170
wsdl = new WSDLFile(jarCL, wsdlFilename);
171         localWSDLURL = jarCL.getResource(wsdlFilename);
172
173         // set mapping file
174
mappingFilename = wsd.getJaxrpcMappingFile();
175
176         // jaxrpc-mapping-file element must have a value
177
if (mappingFilename.equals("")) { //$NON-NLS-1$
178
String JavaDoc err = getI18n().getMessage("ServiceDesc.noJAXRPCMapping", name); //$NON-NLS-1$
179
throw new WSDeploymentDescException(err);
180         }
181
182         InputStream JavaDoc isMapping = jarCL.getResourceAsStream(mappingFilename);
183
184         // isMapping must not be null (CL cannot find resource)
185
if (isMapping == null) {
186             String JavaDoc err = getI18n().getMessage("ServiceDesc.MappingNotFound", mappingFilename, name); //$NON-NLS-1$
187
throw new WSDeploymentDescException(err);
188         }
189
190         mappingFileURL = jarCL.getResource(mappingFilename);
191
192         // Build Mapping file
193
if (isRunningInClientContainer()) {
194             // running in Client Container
195
mapping = MappingFileManager.getInstance(isMapping, mappingFilename);
196         } else {
197             // running in server
198
mapping = MappingFileManagerWrapper.getMappingFile(isMapping, mappingFilename);
199         }
200
201         // links port-components and jonas-port-components
202
Map JavaDoc links = associatePCAndJPC(wsd, jwsd);
203
204         // set namePCDescBindings
205
List JavaDoc pcl = wsd.getPortComponentList();
206
207         for (int i = 0; i < pcl.size(); i++) {
208             // for each port component, build and add a PortComponentDesc Object
209
PortComponent pc = (PortComponent) pcl.get(i);
210             JonasPortComponent jpc = (JonasPortComponent) links.get(pc.getPortComponentName());
211
212             PortComponentDesc pcd = PortComponentDescFactory.newInstance(jarCL, pc, jpc, this);
213
214             if (!wsdl.hasPort(pcd.getQName())) {
215                 throw new WSDeploymentDescException(getI18n().getMessage(
216                         "ServiceDesc.unknownWSDLPort", pcd.getName(), pcd.getQName())); //$NON-NLS-1$
217
}
218
219             if (namePCDescBindings.put(pcd.getName(), pcd) != null) {
220                 throw new WSDeploymentDescException(getI18n().getMessage(
221                         "ServiceDesc.portNameAlreadyUsed", pcd.getName())); //$NON-NLS-1$
222
}
223         }
224
225         // validate informations :
226
List JavaDoc ports = getPortComponents();
227
228         // Ports use Soap bindings
229
for (int i = 0; i < ports.size(); i++) {
230             if (ports.get(i) != null) {
231                 PortComponentDesc pcd = (PortComponentDesc) ports.get(i);
232
233                 if (!wsdl.hasSOAPBinding(pcd.getQName())) {
234                     throw new WSDeploymentDescException(getI18n().getMessage(
235                             "ServiceDesc.noSOAPBinding", pcd.getName(), pcd.getQName())); //$NON-NLS-1$
236
}
237             }
238         }
239     }
240
241     /**
242      * @return Returns true if current execution takes place inside a
243      * ClientContainer
244      */

245     private boolean isRunningInClientContainer() {
246         return (System.getProperty("jonas.base") == null);
247     }
248
249     /**
250      * @param wsd WebservicesDescription element
251      * @param jwsd JonasWebservicesDescription element
252      * @return a Map of PortComponent.name to JonasPortComponent
253      */

254     private Map JavaDoc associatePCAndJPC(WebserviceDescription wsd, JonasWebserviceDescription jwsd) {
255         Map JavaDoc res = new HashMap JavaDoc();
256         // for each port-component
257
for (Iterator JavaDoc i = wsd.getPortComponentList().iterator(); i.hasNext();) {
258             PortComponent pc = (PortComponent) i.next();
259             res.put(pc.getPortComponentName(), null);
260         }
261         // jonas-port-component(s)
262
if (jwsd != null) {
263
264             // get all port-component.name
265
Set JavaDoc keys = res.keySet();
266
267             // for each jonas-port-component
268
for (Iterator JavaDoc i = jwsd.getJonasPortComponentList().iterator(); i.hasNext();) {
269                 JonasPortComponent jpc = (JonasPortComponent) i.next();
270                 String JavaDoc pcName = jpc.getPortComponentName();
271
272                 if (keys.contains(pcName)) {
273                     // jonas-port-component linked to port-component
274
res.put(pcName, jpc);
275                 } else {
276                     String JavaDoc err = "jonas-port-component '" + pcName + "' is not linked to any port-component. It will be ignored."; //getI18n().getMessage("WSDeploymentDesc.wsdlDeclareUnknownPort", wsdlf.getName());
277
logger.log(BasicLevel.WARN, err);
278                 }
279             }
280         }
281         return res;
282     }
283
284     /**
285      * Return the name of the WebService.
286      * @return the name of the WebService.
287      */

288     public String JavaDoc getName() {
289         return name;
290     }
291
292     /**
293      * Return the Mapping File.
294      * @return the MappingFile.
295      */

296     public MappingFile getMapping() {
297         return mapping;
298     }
299
300     /**
301      * Return the WSDL File.
302      * @return the WSDL File.
303      */

304     public WSDLFile getWSDL() {
305         return wsdl;
306     }
307
308     /**
309      * @return Returns the endpointURI.
310      */

311     public String JavaDoc getEndpointURI() {
312         return endpointURI;
313     }
314
315     /**
316      * Return the port component list of this service desc.
317      * @return the port component list of this service desc.
318      */

319     public List JavaDoc getPortComponents() {
320         return new Vector JavaDoc(namePCDescBindings.values());
321     }
322
323     /**
324      * Return the port component desc with the given name if it exists, null
325      * else.
326      * @param pcName the name of the portComponent to retrieve.
327      * @return the port component desc with the given name if it exists, null
328      * else.
329      */

330     public PortComponentDesc getPortComponent(String JavaDoc pcName) {
331         return (PortComponentDesc) namePCDescBindings.get(pcName);
332     }
333
334     /**
335      * @return Returns a String representation of the ServiceDesc.
336      */

337     public String JavaDoc toString() {
338         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
339
340         sb.append("\n" + getClass().getName()); //$NON-NLS-1$
341
sb.append("\ngetName()=" + getName()); //$NON-NLS-1$
342
sb.append("\ngetMapping()=" + getMapping()); //$NON-NLS-1$
343
sb.append("\ngetWSDL()=" + getWSDL()); //$NON-NLS-1$
344

345         for (Iterator JavaDoc i = getPortComponents().iterator(); i.hasNext();) {
346             sb.append("\ngetPortComponents()=" + ((PortComponentDesc) i.next()).toString()); //$NON-NLS-1$
347
}
348
349         return sb.toString();
350     }
351
352     /**
353      * @return Returns the getI18n().
354      */

355     protected static I18n getI18n() {
356         return i18n;
357     }
358
359     /**
360      * @return Returns the publicationDirectory.
361      */

362     public File JavaDoc getPublicationDirectory() {
363         return publicationDirectory;
364     }
365
366     /**
367      * @return Returns URL where mapping file can be loaded.
368      */

369     public URL JavaDoc getMappingFileURL() {
370         return mappingFileURL;
371     }
372
373     /**
374      * @return Returns URL where the WSDL can be loaded locally.
375      */

376     public URL JavaDoc getLocalWSDLURL() {
377         return localWSDLURL;
378     }
379
380     /**
381      * @return Returns the JAX-RPC Mapping filename
382      */

383     public String JavaDoc getMappingFilename() {
384         return mappingFilename;
385     }
386
387     /**
388      * @return Returns the WSDL filename
389      */

390     public String JavaDoc getWsdlFilename() {
391         return wsdlFilename;
392     }
393 }
Popular Tags