001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ServiceDesc.java 6906 2005-06-09 09:55:23Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.deployment.api;
025:
026: import java.io.File;
027: import java.io.InputStream;
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.util.HashMap;
031: import java.util.Hashtable;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Set;
036: import java.util.Vector;
037:
038: import org.objectweb.jonas_lib.I18n;
039:
040: import org.objectweb.jonas_ws.deployment.lib.MappingFileManager;
041: import org.objectweb.jonas_ws.deployment.lib.wrapper.MappingFileManagerWrapper;
042: import org.objectweb.jonas_ws.deployment.xml.JonasPortComponent;
043: import org.objectweb.jonas_ws.deployment.xml.JonasWebserviceDescription;
044: import org.objectweb.jonas_ws.deployment.xml.PortComponent;
045: import org.objectweb.jonas_ws.deployment.xml.WebserviceDescription;
046:
047: import org.objectweb.jonas.common.Log;
048:
049: import org.objectweb.util.monolog.api.BasicLevel;
050: import org.objectweb.util.monolog.api.Logger;
051:
052: /**
053: * This class corresponds to 1 <code>webservices-description</code> XML
054: * element. It's used to get a MappingFile instance if it's defined, idem for
055: * WSDLFile instance and give access to the Web service Port component list.
056: *
057: * @author Guillaume Sauthier
058: * @author Xavier Delplanque
059: */
060: public class ServiceDesc {
061:
062: /**
063: * Internationalization
064: */
065: private static I18n i18n = I18n.getInstance(ServiceDesc.class);
066:
067: /**
068: * logger
069: */
070: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
071:
072: /**
073: * The webservice-description-name
074: */
075: private String name;
076:
077: /**
078: * The list of PortComponentDesc
079: */
080: private Hashtable namePCDescBindings = new Hashtable();
081:
082: /**
083: * The WSDL of the WebService
084: */
085: private WSDLFile wsdl = null;
086:
087: /**
088: * The JaxRpc Mapping of the WebService
089: */
090: private MappingFile mapping = null;
091:
092: /**
093: * Default endpoint URI for this group of WebServices
094: */
095: private String endpointURI = null;
096:
097: /**
098: * Place where WSDL(s) will be published (may be null)
099: */
100: private File publicationDirectory = null;
101:
102: /**
103: * URL of the mapping file
104: */
105: private URL mappingFileURL;
106:
107: /**
108: * URL of the WSDL
109: */
110: private URL localWSDLURL;
111:
112: /**
113: * JAX-RPC Mapping file name
114: */
115: private String mappingFilename = null;
116:
117: /**
118: * WSDL file name
119: */
120: private String wsdlFilename = null;
121:
122: /**
123: * Constructor : creates a ServiceDesc object.
124: * @param jarCL ejbjar or war classLoader
125: * @param wsd Zeus object containing WebserviceDescription informations
126: * @param jwsd Zeus object containing JonasWebserviceDescription informations
127: * @throws WSDeploymentDescException When contruction fails.
128: */
129: public ServiceDesc(ClassLoader jarCL, WebserviceDescription wsd,
130: JonasWebserviceDescription jwsd)
131: throws WSDeploymentDescException {
132:
133: // set name
134: name = wsd.getWebserviceDescriptionName();
135: if ("".equals(name)) { //$NON-NLS-1$
136: throw new WSDeploymentDescException(getI18n().getMessage(
137: "ServiceDesc.noServiceName")); //$NON-NLS-1$
138: }
139:
140: if (jwsd != null) {
141: // set endpointURI
142: String uri = jwsd.getDefaultEndpointURI();
143: if (uri != null && !"".equals(uri)) {
144: endpointURI = uri;
145: }
146:
147: // Set wsdl-publication-directory
148: String wsdlPubDir = jwsd.getWsdlPublishDirectory();
149: if (wsdlPubDir != null) {
150: URL pub;
151: try {
152: pub = new URL(wsdlPubDir);
153: } catch (MalformedURLException e) {
154: throw new WSDeploymentDescException(
155: "Cannot create URL : " + wsdlPubDir, e);
156: }
157: publicationDirectory = new File(pub.getPath());
158: }
159: }
160:
161: // get ServiceRef WSDLFile name
162: wsdlFilename = wsd.getWsdlFile();
163:
164: // wsdl-file element must have a value
165: if ("".equals(wsdlFilename)) { //$NON-NLS-1$
166: String err = getI18n().getMessage(
167: "ServiceDesc.noWSDL", name); //$NON-NLS-1$
168: throw new WSDeploymentDescException(err);
169: }
170:
171: // build the WSDLFile
172: wsdl = new WSDLFile(jarCL, wsdlFilename);
173: localWSDLURL = jarCL.getResource(wsdlFilename);
174:
175: // set mapping file
176: mappingFilename = wsd.getJaxrpcMappingFile();
177:
178: // jaxrpc-mapping-file element must have a value
179: if (mappingFilename.equals("")) { //$NON-NLS-1$
180: String err = getI18n().getMessage(
181: "ServiceDesc.noJAXRPCMapping", name); //$NON-NLS-1$
182: throw new WSDeploymentDescException(err);
183: }
184:
185: InputStream isMapping = jarCL
186: .getResourceAsStream(mappingFilename);
187:
188: // isMapping must not be null (CL cannot find resource)
189: if (isMapping == null) {
190: String err = getI18n()
191: .getMessage(
192: "ServiceDesc.MappingNotFound", mappingFilename, name); //$NON-NLS-1$
193: throw new WSDeploymentDescException(err);
194: }
195:
196: mappingFileURL = jarCL.getResource(mappingFilename);
197:
198: // Build Mapping file
199: if (isRunningInClientContainer()) {
200: // running in Client Container
201: mapping = MappingFileManager.getInstance(isMapping,
202: mappingFilename);
203: } else {
204: // running in server
205: mapping = MappingFileManagerWrapper.getMappingFile(
206: isMapping, mappingFilename);
207: }
208:
209: // links port-components and jonas-port-components
210: Map links = associatePCAndJPC(wsd, jwsd);
211:
212: // set namePCDescBindings
213: List pcl = wsd.getPortComponentList();
214:
215: for (int i = 0; i < pcl.size(); i++) {
216: // for each port component, build and add a PortComponentDesc Object
217: PortComponent pc = (PortComponent) pcl.get(i);
218: JonasPortComponent jpc = (JonasPortComponent) links.get(pc
219: .getPortComponentName());
220:
221: PortComponentDesc pcd = PortComponentDescFactory
222: .newInstance(jarCL, pc, jpc, this );
223:
224: if (!wsdl.hasPort(pcd.getQName())) {
225: throw new WSDeploymentDescException(
226: getI18n()
227: .getMessage(
228: "ServiceDesc.unknownWSDLPort", pcd.getName(), pcd.getQName())); //$NON-NLS-1$
229: }
230:
231: if (namePCDescBindings.put(pcd.getName(), pcd) != null) {
232: throw new WSDeploymentDescException(
233: getI18n()
234: .getMessage(
235: "ServiceDesc.portNameAlreadyUsed", pcd.getName())); //$NON-NLS-1$
236: }
237: }
238:
239: // validate informations :
240: List ports = getPortComponents();
241:
242: // Ports use Soap bindings
243: for (int i = 0; i < ports.size(); i++) {
244: if (ports.get(i) != null) {
245: PortComponentDesc pcd = (PortComponentDesc) ports
246: .get(i);
247:
248: if (!wsdl.hasSOAPBinding(pcd.getQName())) {
249: throw new WSDeploymentDescException(
250: getI18n()
251: .getMessage(
252: "ServiceDesc.noSOAPBinding", pcd.getName(), pcd.getQName())); //$NON-NLS-1$
253: }
254: }
255: }
256: }
257:
258: /**
259: * @return Returns true if current execution takes place inside a
260: * ClientContainer
261: */
262: private boolean isRunningInClientContainer() {
263: return (System.getProperty("jonas.base") == null);
264: }
265:
266: /**
267: * @param wsd WebservicesDescription element
268: * @param jwsd JonasWebservicesDescription element
269: * @return a Map of PortComponent.name to JonasPortComponent
270: */
271: private Map associatePCAndJPC(WebserviceDescription wsd,
272: JonasWebserviceDescription jwsd) {
273: Map res = new HashMap();
274: // for each port-component
275: for (Iterator i = wsd.getPortComponentList().iterator(); i
276: .hasNext();) {
277: PortComponent pc = (PortComponent) i.next();
278: res.put(pc.getPortComponentName(), null);
279: }
280: // jonas-port-component(s)
281: if (jwsd != null) {
282:
283: // get all port-component.name
284: Set keys = res.keySet();
285:
286: // for each jonas-port-component
287: for (Iterator i = jwsd.getJonasPortComponentList()
288: .iterator(); i.hasNext();) {
289: JonasPortComponent jpc = (JonasPortComponent) i.next();
290: String pcName = jpc.getPortComponentName();
291:
292: if (keys.contains(pcName)) {
293: // jonas-port-component linked to port-component
294: res.put(pcName, jpc);
295: } else {
296: String err = "jonas-port-component '"
297: + pcName
298: + "' is not linked to any port-component. It will be ignored."; //getI18n().getMessage("WSDeploymentDesc.wsdlDeclareUnknownPort", wsdlf.getName());
299: logger.log(BasicLevel.WARN, err);
300: }
301: }
302: }
303: return res;
304: }
305:
306: /**
307: * Return the name of the WebService.
308: * @return the name of the WebService.
309: */
310: public String getName() {
311: return name;
312: }
313:
314: /**
315: * Return the Mapping File.
316: * @return the MappingFile.
317: */
318: public MappingFile getMapping() {
319: return mapping;
320: }
321:
322: /**
323: * Return the WSDL File.
324: * @return the WSDL File.
325: */
326: public WSDLFile getWSDL() {
327: return wsdl;
328: }
329:
330: /**
331: * @return Returns the endpointURI.
332: */
333: public String getEndpointURI() {
334: return endpointURI;
335: }
336:
337: /**
338: * Return the port component list of this service desc.
339: * @return the port component list of this service desc.
340: */
341: public List getPortComponents() {
342: return new Vector(namePCDescBindings.values());
343: }
344:
345: /**
346: * Return the port component desc with the given name if it exists, null
347: * else.
348: * @param pcName the name of the portComponent to retrieve.
349: * @return the port component desc with the given name if it exists, null
350: * else.
351: */
352: public PortComponentDesc getPortComponent(String pcName) {
353: return (PortComponentDesc) namePCDescBindings.get(pcName);
354: }
355:
356: /**
357: * @return Returns a String representation of the ServiceDesc.
358: */
359: public String toString() {
360: StringBuffer sb = new StringBuffer();
361:
362: sb.append("\n" + getClass().getName()); //$NON-NLS-1$
363: sb.append("\ngetName()=" + getName()); //$NON-NLS-1$
364: sb.append("\ngetMapping()=" + getMapping()); //$NON-NLS-1$
365: sb.append("\ngetWSDL()=" + getWSDL()); //$NON-NLS-1$
366:
367: for (Iterator i = getPortComponents().iterator(); i.hasNext();) {
368: sb
369: .append("\ngetPortComponents()=" + ((PortComponentDesc) i.next()).toString()); //$NON-NLS-1$
370: }
371:
372: return sb.toString();
373: }
374:
375: /**
376: * @return Returns the getI18n().
377: */
378: protected static I18n getI18n() {
379: return i18n;
380: }
381:
382: /**
383: * @return Returns the publicationDirectory.
384: */
385: public File getPublicationDirectory() {
386: return publicationDirectory;
387: }
388:
389: /**
390: * @return Returns URL where mapping file can be loaded.
391: */
392: public URL getMappingFileURL() {
393: return mappingFileURL;
394: }
395:
396: /**
397: * @return Returns URL where the WSDL can be loaded locally.
398: */
399: public URL getLocalWSDLURL() {
400: return localWSDLURL;
401: }
402:
403: /**
404: * @return Returns the JAX-RPC Mapping filename
405: */
406: public String getMappingFilename() {
407: return mappingFilename;
408: }
409:
410: /**
411: * @return Returns the WSDL filename
412: */
413: public String getWsdlFilename() {
414: return wsdlFilename;
415: }
416: }
|