KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > metadata > serviceref > ServiceRefMetaData


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.webservice.metadata.serviceref;
23
24 // $Id: ServiceRefMetaData.java 58121 2006-11-04 19:57:58Z thomas.diesler@jboss.com $
25

26 import java.io.Serializable JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedHashMap JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.xml.namespace.QName JavaDoc;
36
37 import org.jboss.deployment.DeploymentException;
38 import org.jboss.logging.Logger;
39 import org.jboss.metadata.MetaData;
40 import org.jboss.metadata.Ref;
41 import org.jboss.xb.QNameBuilder;
42 import org.w3c.dom.DocumentType JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44
45 /** The metdata data from service-ref element in web.xml, ejb-jar.xml, and
46  * application-client.xml.
47  *
48  * @author Thomas.Diesler@jboss.org
49  * @version $Revision: 58121 $
50  */

51 public class ServiceRefMetaData extends Ref
52    implements Serializable JavaDoc
53 {
54    private static final long serialVersionUID = -3723577995017108437L;
55
56    // provide logging
57
private static Logger log = Logger.getLogger(ServiceRefMetaData.class);
58    
59    // The required <service-ref-name> element
60
private String JavaDoc serviceRefName;
61    // The required <service-interface> element
62
private String JavaDoc serviceInterface;
63    // service-res-type
64
private String JavaDoc serviceResType;
65    // The optional <wsdl-file> element
66
private String JavaDoc wsdlFile;
67    // The optional <jaxrpc-mapping-file> element
68
private String JavaDoc jaxrpcMappingFile;
69    // The optional <service-qname> element
70
private QName JavaDoc serviceQName;
71    // The LinkedHashMap<String, PortComponentRefMetaData> for <port-component-ref> elements
72
private LinkedHashMap JavaDoc portComponentRefs = new LinkedHashMap JavaDoc();
73    // The optional <handler> elements
74
private ArrayList JavaDoc handlers = new ArrayList JavaDoc();
75
76    // The optional JBossWS config-name
77
private String JavaDoc configName;
78    // The optional JBossWS config-file
79
private String JavaDoc configFile;
80    /** The URL of the actual WSDL to use, <wsdl-override> */
81    private URL JavaDoc wsdlOverride;
82    /** Arbitrary proxy properties given by <call-property> */
83    private Properties JavaDoc callProperties;
84    private String JavaDoc mappedName;
85
86    /** The ClassLoader to load additional resources */
87    private transient URLClassLoader JavaDoc resourceCL;
88
89    /** Default constructor, used when unmarshalling on the client side
90     */

91    public ServiceRefMetaData()
92    {
93    }
94
95    /** Constructor with a given service ref name
96     */

97    public ServiceRefMetaData(String JavaDoc serviceRefName)
98    {
99       this.serviceRefName = serviceRefName;
100    }
101
102    /** Constructor with a given resource classloader, used on the server side
103     */

104    public ServiceRefMetaData(URLClassLoader JavaDoc resourceCl)
105    {
106       setResourceCL(resourceCl);
107    }
108
109    /** Set the resource classloader that can load the wsdl file
110     * On the client side this is set expicitly after unmarshalling.
111     */

112    public void setResourceCL(URLClassLoader JavaDoc resourceCL)
113    {
114       if (resourceCL == null)
115          throw new IllegalArgumentException JavaDoc("ResourceClassLoader cannot be null");
116
117       this.resourceCL = resourceCL;
118    }
119
120    public URLClassLoader JavaDoc getResourceCL()
121    {
122       if (resourceCL == null)
123          resourceCL = new URLClassLoader JavaDoc(new URL JavaDoc[] {}, Thread.currentThread().getContextClassLoader());
124
125       return resourceCL;
126    }
127
128    public String JavaDoc getJaxrpcMappingFile()
129    {
130       return jaxrpcMappingFile;
131    }
132    public void setJaxrpcMappingFile(String JavaDoc file)
133    {
134       this.jaxrpcMappingFile = jaxrpcMappingFile;
135    }
136
137    public URL JavaDoc getJavaWsdlMappingURL()
138    {
139       URL JavaDoc mappingURL = null;
140       if (jaxrpcMappingFile != null)
141       {
142          mappingURL = getResourceCL().findResource(jaxrpcMappingFile);
143          if (mappingURL == null)
144             throw new IllegalStateException JavaDoc("Cannot find resource: " + jaxrpcMappingFile);
145       }
146       return mappingURL;
147    }
148
149    public PortComponentRefMetaData[] getPortComponentRefs()
150    {
151       PortComponentRefMetaData[] array = new PortComponentRefMetaData[portComponentRefs.size()];
152       portComponentRefs.values().toArray(array);
153       return array;
154    }
155
156    public HandlerMetaData[] getHandlers()
157    {
158       HandlerMetaData[] array = new HandlerMetaData[handlers.size()];
159       handlers.toArray(array);
160       return array;
161    }
162
163    public String JavaDoc getServiceInterface()
164    {
165       return serviceInterface;
166    }
167    public void setServiceInterface(String JavaDoc serviceInterface)
168    {
169       this.serviceInterface = serviceInterface;
170    }
171
172    public QName JavaDoc getServiceQName()
173    {
174       return serviceQName;
175    }
176    public void setServiceQName(QName JavaDoc serviceQName)
177    {
178       this.serviceQName = serviceQName;
179    }
180
181    public String JavaDoc getServiceRefName()
182    {
183       return serviceRefName;
184    }
185    public void setServiceRefName(String JavaDoc serviceRefName)
186    {
187       this.serviceRefName = serviceRefName;
188    }
189
190    public String JavaDoc getServiceResType()
191    {
192       return serviceResType;
193    }
194    public void setServiceResType(String JavaDoc serviceResType)
195    {
196       this.serviceResType = serviceResType;
197    }
198
199    public String JavaDoc getWsdlFile()
200    {
201       return wsdlFile;
202    }
203    public void setWsdlFile(String JavaDoc file)
204    {
205       this.wsdlFile = file;
206    }
207
208    public String JavaDoc getConfigFile()
209    {
210       return configFile;
211    }
212
213    public void setConfigFile(String JavaDoc configFile)
214    {
215       this.configFile = configFile;
216    }
217
218    public String JavaDoc getConfigName()
219    {
220       return configName;
221    }
222
223    public String JavaDoc getMappedName()
224    {
225       return mappedName;
226    }
227
228    public void setMappedName(String JavaDoc mappedName)
229    {
230       this.mappedName = mappedName;
231    }
232
233    public void setConfigName(String JavaDoc configName)
234    {
235       this.configName = configName;
236    }
237    public URL JavaDoc getWsdlOverride()
238    {
239       return wsdlOverride;
240    }
241
242    public URL JavaDoc getWsdlURL()
243    {
244       URL JavaDoc wsdlURL = wsdlOverride;
245       if (wsdlURL == null && wsdlFile != null)
246       {
247          wsdlURL = getResourceCL().findResource(wsdlFile);
248          if (wsdlURL == null)
249             throw new IllegalStateException JavaDoc("Cannot find resource: " + wsdlFile);
250       }
251       return wsdlURL;
252    }
253
254    public Properties JavaDoc getCallProperties()
255    {
256       return callProperties;
257    }
258
259    public static boolean isValidDoctype(Element JavaDoc element)
260    {
261       // Verify J2EE-1.4
262
String JavaDoc nsURI = element.getOwnerDocument().getDocumentElement().getNamespaceURI();
263       boolean isValid = "http://java.sun.com/xml/ns/j2ee".equals(nsURI);
264
265       // Verify JBoss-4.0
266
DocumentType JavaDoc doctype = element.getOwnerDocument().getDoctype();
267       if (isValid == false && doctype != null)
268       {
269          String JavaDoc publicId = doctype.getPublicId();
270          isValid |= "-//JBoss//DTD JBOSS 4.0//EN".equals(publicId);
271          isValid |= "-//JBoss//DTD Web Application 2.4//EN".equals(publicId);
272          isValid |= "-//JBoss//DTD Application Client 4.0//EN".equals(publicId);
273       }
274       
275       if (isValid == false)
276       {
277          String JavaDoc dtstr = (doctype != null ? "[public=" + doctype.getPublicId() + ",system=" + doctype.getSystemId() + "]" : null);
278          log.debug("Skip <service-ref> for: nsURI=" + nsURI + ",doctype=" + dtstr);
279       }
280       return isValid;
281    }
282    
283    public void importStandardXml(Element JavaDoc element) throws DeploymentException
284    {
285       serviceRefName = MetaData.getUniqueChildContent(element, "service-ref-name");
286
287       serviceInterface = MetaData.getUniqueChildContent(element, "service-interface");
288
289       wsdlFile = MetaData.getOptionalChildContent(element, "wsdl-file");
290
291       jaxrpcMappingFile = MetaData.getOptionalChildContent(element, "jaxrpc-mapping-file");
292
293       Element JavaDoc qnameElement = MetaData.getOptionalChild(element, "service-qname");
294       if (qnameElement != null)
295          serviceQName = QNameBuilder.buildQName(qnameElement, MetaData.getElementContent(qnameElement));
296
297       // Parse the port-component-ref elements
298
Iterator JavaDoc iterator = MetaData.getChildrenByTagName(element, "port-component-ref");
299       while (iterator.hasNext())
300       {
301          Element JavaDoc pcrefElement = (Element JavaDoc)iterator.next();
302          PortComponentRefMetaData pcrefMetaData = new PortComponentRefMetaData(this);
303          pcrefMetaData.importStandardXml(pcrefElement);
304          portComponentRefs.put(pcrefMetaData.getServiceEndpointInterface(), pcrefMetaData);
305       }
306
307       // Parse the handler elements
308
iterator = MetaData.getChildrenByTagName(element, "handler");
309       while (iterator.hasNext())
310       {
311          Element JavaDoc handlerElement = (Element JavaDoc)iterator.next();
312          HandlerMetaData handlerMetaData = new HandlerMetaData();
313          handlerMetaData.importStandardXml(handlerElement);
314          handlers.add(handlerMetaData);
315       }
316    }
317
318    /** Parse jboss specific service-ref child elements
319     * @param element
320     * @throws DeploymentException
321     */

322    public void importJBossXml(Element JavaDoc element) throws DeploymentException
323    {
324       configName = MetaData.getOptionalChildContent(element, "config-name");
325
326       configFile = MetaData.getOptionalChildContent(element, "config-file");
327       String JavaDoc wsdlOverrideOption = MetaData.getOptionalChildContent(element, "wsdl-override");
328       try
329       {
330          if (wsdlOverrideOption != null)
331             wsdlOverride = new URL JavaDoc(wsdlOverrideOption);
332       }
333       catch (MalformedURLException JavaDoc e)
334       {
335          throw new DeploymentException("Invalid WSDL override: " + wsdlOverrideOption);
336       }
337
338       // Parse the port-component-ref elements
339
Iterator JavaDoc iterator = MetaData.getChildrenByTagName(element, "port-component-ref");
340       while (iterator.hasNext())
341       {
342          Element JavaDoc pcrefElement = (Element JavaDoc)iterator.next();
343          String JavaDoc name = MetaData.getOptionalChildContent(pcrefElement, "service-endpoint-interface");
344          if (name != null)
345          {
346             PortComponentRefMetaData pcrefMetaData = (PortComponentRefMetaData)portComponentRefs.get(name);
347             if (pcrefMetaData == null)
348             {
349                // Its ok to only have the <port-component-ref> in jboss.xml and not in ejb-jar.xml
350
pcrefMetaData = new PortComponentRefMetaData(this);
351                pcrefMetaData.importStandardXml(pcrefElement);
352                portComponentRefs.put(pcrefMetaData.getServiceEndpointInterface(), pcrefMetaData);
353             }
354
355             pcrefMetaData.importJBossXml(pcrefElement);
356          }
357       }
358
359       // Parse the call-property elements
360
iterator = MetaData.getChildrenByTagName(element, "call-property");
361       while (iterator.hasNext())
362       {
363          Element JavaDoc propElement = (Element JavaDoc)iterator.next();
364          String JavaDoc name = MetaData.getUniqueChildContent(propElement, "prop-name");
365          String JavaDoc value = MetaData.getUniqueChildContent(propElement, "prop-value");
366          if (callProperties == null)
367             callProperties = new Properties JavaDoc();
368          callProperties.setProperty(name, value);
369       }
370    }
371 }
372
Popular Tags