KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > providers > BasicProvider


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 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 "Axis" 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. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.providers;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.MessageContext;
60 import org.jboss.axis.description.ServiceDesc;
61 import org.jboss.axis.encoding.TypeMapping;
62 import org.jboss.axis.handlers.BasicHandler;
63 import org.jboss.axis.handlers.soap.SOAPService;
64 import org.jboss.axis.utils.Messages;
65 import org.jboss.axis.wsdl.fromJava.Emitter;
66 import org.jboss.logging.Logger;
67 import org.w3c.dom.Document JavaDoc;
68
69 import javax.xml.namespace.QName JavaDoc;
70 import java.util.Hashtable JavaDoc;
71
72 /**
73  * This class has one way of keeping track of the
74  * operations declared for a particular service
75  * provider. I'm not exactly married to this though.
76  */

77 public abstract class BasicProvider extends BasicHandler
78 {
79
80    public static final String JavaDoc OPTION_WSDL_PORTTYPE = "wsdlPortType";
81    public static final String JavaDoc OPTION_WSDL_SERVICEELEMENT = "wsdlServiceElement";
82    public static final String JavaDoc OPTION_WSDL_SERVICEPORT = "wsdlServicePort";
83    public static final String JavaDoc OPTION_WSDL_TARGETNAMESPACE = "wsdlTargetNamespace";
84    public static final String JavaDoc OPTION_WSDL_INPUTSCHEMA = "wsdlInputSchema";
85
86    private static Logger log = Logger.getLogger(BasicProvider.class.getName());
87
88    /**
89     * This method returns a ServiceDesc that contains the correct
90     * implimentation class.
91     */

92    public abstract void initServiceDesc(SOAPService service,
93                                         MessageContext msgContext)
94            throws AxisFault;
95
96    public void addOperation(String JavaDoc name, QName JavaDoc qname)
97    {
98       Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
99       if (operations == null)
100       {
101          operations = new Hashtable JavaDoc();
102          setOption("Operations", operations);
103       }
104       operations.put(qname, name);
105    }
106
107    public String JavaDoc getOperationName(QName JavaDoc qname)
108    {
109       Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
110       if (operations == null) return null;
111       return (String JavaDoc)operations.get(qname);
112    }
113
114    public QName JavaDoc[] getOperationQNames()
115    {
116       Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
117       if (operations == null) return null;
118       Object JavaDoc[] keys = operations.keySet().toArray();
119       QName JavaDoc[] qnames = new QName JavaDoc[keys.length];
120       System.arraycopy(keys, 0, qnames, 0, keys.length);
121       return qnames;
122    }
123
124    public String JavaDoc[] getOperationNames()
125    {
126       Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
127       if (operations == null) return null;
128       Object JavaDoc[] values = operations.values().toArray();
129       String JavaDoc[] names = new String JavaDoc[values.length];
130       System.arraycopy(values, 0, names, 0, values.length);
131       return names;
132    }
133
134    /**
135     * Generate the WSDL for this service.
136     * <p/>
137     * Put in the "WSDL" property of the message context
138     * as a org.w3c.dom.Document
139     */

140    public void generateWSDL(MessageContext msgContext) throws AxisFault
141    {
142       if (log.isDebugEnabled())
143          log.debug("Enter: BSFProvider::generateWSDL (" + this + ")");
144
145       /* Find the service we're invoking so we can grab it's options */
146       /***************************************************************/
147       SOAPService service = msgContext.getService();
148       ServiceDesc serviceDesc = service.getInitializedServiceDesc(msgContext);
149
150       // Calculate the appropriate namespaces for the WSDL we're going
151
// to put out.
152
//
153
// If we've been explicitly told which namespaces to use, respect
154
// that. If not:
155
//
156
// The "interface namespace" should be either:
157
// 1) The namespace of the ServiceDesc
158
// 2) The transport URL (if there's no ServiceDesc ns)
159

160       try
161       {
162          // Location URL is whatever is explicitly set in the MC
163
String JavaDoc locationUrl =
164                  msgContext.getStrProp(MessageContext.WSDLGEN_SERV_LOC_URL);
165
166          if (locationUrl == null)
167          {
168             // If nothing, try what's explicitly set in the ServiceDesc
169
locationUrl = serviceDesc.getEndpointURL();
170          }
171
172          if (locationUrl == null)
173          {
174             // If nothing, use the actual transport URL
175
locationUrl = msgContext.getStrProp(MessageContext.TRANS_URL);
176          }
177
178          // Interface namespace is whatever is explicitly set
179
String JavaDoc interfaceNamespace =
180                  msgContext.getStrProp(MessageContext.WSDLGEN_INTFNAMESPACE);
181
182          if (interfaceNamespace == null)
183          {
184             // If nothing, use the default namespace of the ServiceDesc
185
interfaceNamespace = serviceDesc.getDefaultNamespace();
186          }
187
188          if (interfaceNamespace == null)
189          {
190             // If nothing still, use the location URL determined above
191
interfaceNamespace = locationUrl;
192          }
193
194 // Do we want to do this?
195
//
196
// if (locationUrl == null) {
197
// locationUrl = url;
198
// } else {
199
// try {
200
// URL urlURL = new URL(url);
201
// URL locationURL = new URL(locationUrl);
202
// URL urlTemp = new URL(urlURL.getProtocol(),
203
// locationURL.getHost(),
204
// locationURL.getPort(),
205
// urlURL.getFile());
206
// interfaceNamespace += urlURL.getFile();
207
// locationUrl = urlTemp.toString();
208
// } catch (Exception e) {
209
// locationUrl = url;
210
// interfaceNamespace = url;
211
// }
212
// }
213

214          Emitter emitter = new Emitter();
215
216          // Set the name for the target service.
217
emitter.setServiceElementName(serviceDesc.getName());
218
219          // service alias may be provided if exact naming is required,
220
// otherwise Axis will name it according to the implementing class name
221
String JavaDoc alias = (String JavaDoc)service.getOption("alias");
222          if (alias != null) emitter.setServiceElementName(alias);
223
224          // Set style/use
225
emitter.setStyle(serviceDesc.getStyle());
226          emitter.setUse(serviceDesc.getUse());
227
228          emitter.setClsSmart(serviceDesc.getImplClass(), locationUrl);
229
230          // If a wsdl target namespace was provided, use the targetNamespace.
231
// Otherwise use the interfaceNamespace constructed above.
232
String JavaDoc targetNamespace = (String JavaDoc)service.getOption(OPTION_WSDL_TARGETNAMESPACE);
233          if (targetNamespace == null ||
234                  targetNamespace.length() == 0)
235          {
236             targetNamespace = interfaceNamespace;
237          }
238          emitter.setIntfNamespace(targetNamespace);
239
240          emitter.setLocationUrl(locationUrl);
241          emitter.setServiceDesc(serviceDesc);
242          emitter.setTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry()
243                  .getTypeMapping(serviceDesc.getUse().getEncoding()));
244          emitter.setDefaultTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry().
245                  getDefaultTypeMapping());
246
247          String JavaDoc wsdlPortType = (String JavaDoc)service.getOption(OPTION_WSDL_PORTTYPE);
248          String JavaDoc wsdlServiceElement = (String JavaDoc)service.getOption(OPTION_WSDL_SERVICEELEMENT);
249          String JavaDoc wsdlServicePort = (String JavaDoc)service.getOption(OPTION_WSDL_SERVICEPORT);
250
251          if (wsdlPortType != null && wsdlPortType.length() > 0)
252          {
253             emitter.setPortTypeName(wsdlPortType);
254          }
255          if (wsdlServiceElement != null && wsdlServiceElement.length() > 0)
256          {
257             emitter.setServiceElementName(wsdlServiceElement);
258          }
259          if (wsdlServicePort != null && wsdlServicePort.length() > 0)
260          {
261             emitter.setServicePortName(wsdlServicePort);
262          }
263
264          String JavaDoc wsdlInputSchema = (String JavaDoc)
265                  service.getOption(OPTION_WSDL_INPUTSCHEMA);
266          if (null != wsdlInputSchema && wsdlInputSchema.length() > 0)
267          {
268             emitter.setInputSchema(wsdlInputSchema);
269          }
270
271          Document JavaDoc doc = emitter.emit(Emitter.MODE_ALL);
272
273          msgContext.setProperty("WSDL", doc);
274       }
275       catch (NoClassDefFoundError JavaDoc e)
276       {
277          log.info(Messages.getMessage("toAxisFault00"), e);
278          throw new AxisFault(e.toString(), e);
279       }
280       catch (Exception JavaDoc e)
281       {
282          log.info(Messages.getMessage("toAxisFault00"), e);
283          throw AxisFault.makeFault(e);
284       }
285
286       if (log.isDebugEnabled())
287          log.debug("Exit: JavaProvider::generateWSDL (" + this + ")");
288    }
289 }
290
Popular Tags