KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.providers;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.xml.namespace.QName JavaDoc;
22
23 import org.apache.axis.AxisFault;
24 import org.apache.axis.Constants;
25 import org.apache.axis.MessageContext;
26 import org.apache.axis.AxisEngine;
27 import org.apache.axis.components.logger.LogFactory;
28 import org.apache.axis.description.ServiceDesc;
29 import org.apache.axis.description.JavaServiceDesc;
30 import org.apache.axis.encoding.TypeMapping;
31 import org.apache.axis.handlers.BasicHandler;
32 import org.apache.axis.handlers.soap.SOAPService;
33 import org.apache.axis.utils.Messages;
34 import org.apache.axis.wsdl.fromJava.Emitter;
35 import org.apache.commons.logging.Log;
36 import org.w3c.dom.Document JavaDoc;
37
38 /**
39  * This class has one way of keeping track of the
40  * operations declared for a particular service
41  * provider. I'm not exactly married to this though.
42  */

43 public abstract class BasicProvider extends BasicHandler {
44     
45     public static final String JavaDoc OPTION_WSDL_PORTTYPE = "wsdlPortType";
46     public static final String JavaDoc OPTION_WSDL_SERVICEELEMENT = "wsdlServiceElement";
47     public static final String JavaDoc OPTION_WSDL_SERVICEPORT = "wsdlServicePort";
48     public static final String JavaDoc OPTION_WSDL_TARGETNAMESPACE = "wsdlTargetNamespace";
49     public static final String JavaDoc OPTION_WSDL_INPUTSCHEMA = "wsdlInputSchema";
50     public static final String JavaDoc OPTION_WSDL_SOAPACTION_MODE = "wsdlSoapActionMode";
51     public static final String JavaDoc OPTION_EXTRACLASSES = "extraClasses";
52
53     protected static Log log =
54             LogFactory.getLog(BasicProvider.class.getName());
55
56     // The enterprise category is for stuff that an enterprise product might
57
// want to track, but in a simple environment (like the AXIS build) would
58
// be nothing more than a nuisance.
59
protected static Log entLog =
60             LogFactory.getLog(Constants.ENTERPRISE_LOG_CATEGORY);
61     
62     /**
63      * This method returns a ServiceDesc that contains the correct
64      * implimentation class.
65      */

66     public abstract void initServiceDesc(SOAPService service,
67                                          MessageContext msgContext)
68             throws AxisFault;
69     
70     public void addOperation(String JavaDoc name, QName JavaDoc qname) {
71         Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
72         if (operations == null) {
73             operations = new Hashtable JavaDoc();
74             setOption("Operations", operations);
75         }
76         operations.put(qname, name);
77     }
78     
79     public String JavaDoc getOperationName(QName JavaDoc qname) {
80         Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
81         if (operations == null) return null;
82         return (String JavaDoc)operations.get(qname);
83     }
84     
85     public QName JavaDoc[] getOperationQNames() {
86         Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
87         if (operations == null) return null;
88         Object JavaDoc[] keys = operations.keySet().toArray();
89         QName JavaDoc[] qnames = new QName JavaDoc[keys.length];
90         System.arraycopy(keys,0,qnames,0,keys.length);
91         return qnames;
92     }
93     
94     public String JavaDoc[] getOperationNames() {
95         Hashtable JavaDoc operations = (Hashtable JavaDoc)getOption("Operations");
96         if (operations == null) return null;
97         Object JavaDoc[] values = operations.values().toArray();
98         String JavaDoc[] names = new String JavaDoc[values.length];
99         System.arraycopy(values,0,names,0,values.length);
100         return names;
101     }
102     
103     /**
104      * Generate the WSDL for this service.
105      *
106      * Put in the "WSDL" property of the message context
107      * as a org.w3c.dom.Document
108      */

109     public void generateWSDL(MessageContext msgContext) throws AxisFault {
110         if (log.isDebugEnabled())
111             log.debug("Enter: BasicProvider::generateWSDL (" + this +")");
112
113         /* Find the service we're invoking so we can grab it's options */
114         /***************************************************************/
115         SOAPService service = msgContext.getService();
116         
117         ServiceDesc serviceDesc = service.getInitializedServiceDesc(msgContext);
118
119         // Calculate the appropriate namespaces for the WSDL we're going
120
// to put out.
121
//
122
// If we've been explicitly told which namespaces to use, respect
123
// that. If not:
124
//
125
// The "interface namespace" should be either:
126
// 1) The namespace of the ServiceDesc
127
// 2) The transport URL (if there's no ServiceDesc ns)
128

129         try {
130             // Location URL is whatever is explicitly set in the MC
131
String JavaDoc locationUrl = msgContext.getStrProp(MessageContext.WSDLGEN_SERV_LOC_URL);
132
133             if (locationUrl == null) {
134                 // If nothing, try what's explicitly set in the ServiceDesc
135
locationUrl = serviceDesc.getEndpointURL();
136             }
137
138             if (locationUrl == null) {
139                 // If nothing, use the actual transport URL
140
locationUrl = msgContext.getStrProp(MessageContext.TRANS_URL);
141             }
142
143             // Interface namespace is whatever is explicitly set
144
String JavaDoc interfaceNamespace = msgContext.getStrProp(MessageContext.WSDLGEN_INTFNAMESPACE);
145
146             if (interfaceNamespace == null) {
147                 // If nothing, use the default namespace of the ServiceDesc
148
interfaceNamespace = serviceDesc.getDefaultNamespace();
149             }
150
151             if (interfaceNamespace == null) {
152                 // If nothing still, use the location URL determined above
153
interfaceNamespace = locationUrl;
154             }
155
156             //Do we want to do this?
157
//
158
// if (locationUrl == null) {
159
// locationUrl = url;
160
// } else {
161
// try {
162
// URL urlURL = new URL(url);
163
// URL locationURL = new URL(locationUrl);
164
// URL urlTemp = new URL(urlURL.getProtocol(),
165
// locationURL.getHost(),
166
// locationURL.getPort(),
167
// urlURL.getFile());
168
// interfaceNamespace += urlURL.getFile();
169
// locationUrl = urlTemp.toString();
170
// } catch (Exception e) {
171
// locationUrl = url;
172
// interfaceNamespace = url;
173
// }
174
// }
175

176             Emitter emitter = new Emitter();
177
178             // This seems like a good idea, but in fact isn't because the
179
// emitter will figure out a reasonable name (<classname>Service)
180
// for the WSDL service element name. We provide the 'alias'
181
// setting to explicitly set this name. See bug 13262 for more info.
182
//emitter.setServiceElementName(serviceDesc.getName());
183

184             // service alias may be provided if exact naming is required,
185
// otherwise Axis will name it according to the implementing class name
186
String JavaDoc alias = (String JavaDoc) service.getOption("alias");
187             if (alias != null)
188                 emitter.setServiceElementName(alias);
189
190             // Set style/use
191
emitter.setStyle(serviceDesc.getStyle());
192             emitter.setUse(serviceDesc.getUse());
193
194             if (serviceDesc instanceof JavaServiceDesc) {
195                 emitter.setClsSmart(((JavaServiceDesc)serviceDesc).getImplClass(),
196                                     locationUrl);
197             }
198
199             // If a wsdl target namespace was provided, use the targetNamespace.
200
// Otherwise use the interfaceNamespace constructed above.
201
String JavaDoc targetNamespace = (String JavaDoc) service.getOption(OPTION_WSDL_TARGETNAMESPACE);
202             if (targetNamespace == null || targetNamespace.length() == 0) {
203                 targetNamespace = interfaceNamespace;
204             }
205             emitter.setIntfNamespace(targetNamespace);
206
207             emitter.setLocationUrl(locationUrl);
208             emitter.setServiceDesc(serviceDesc);
209             emitter.setTypeMappingRegistry(msgContext.getTypeMappingRegistry());
210
211             String JavaDoc wsdlPortType = (String JavaDoc) service.getOption(OPTION_WSDL_PORTTYPE);
212             String JavaDoc wsdlServiceElement = (String JavaDoc) service.getOption(OPTION_WSDL_SERVICEELEMENT);
213             String JavaDoc wsdlServicePort = (String JavaDoc) service.getOption(OPTION_WSDL_SERVICEPORT);
214             String JavaDoc wsdlInputSchema = (String JavaDoc) service.getOption(OPTION_WSDL_INPUTSCHEMA);
215             String JavaDoc wsdlSoapActinMode = (String JavaDoc) service.getOption(OPTION_WSDL_SOAPACTION_MODE);
216             String JavaDoc extraClasses = (String JavaDoc) service.getOption(OPTION_EXTRACLASSES);
217
218             if (wsdlPortType != null && wsdlPortType.length() > 0) {
219                 emitter.setPortTypeName(wsdlPortType);
220             }
221             if (wsdlServiceElement != null && wsdlServiceElement.length() > 0) {
222                 emitter.setServiceElementName(wsdlServiceElement);
223             }
224             if (wsdlServicePort != null && wsdlServicePort.length() > 0) {
225                 emitter.setServicePortName(wsdlServicePort);
226             }
227             if (wsdlInputSchema != null && wsdlInputSchema.length() > 0) {
228                 emitter.setInputSchema(wsdlInputSchema);
229             }
230             if (wsdlSoapActinMode != null && wsdlSoapActinMode.length() > 0) {
231                 emitter.setSoapAction(wsdlSoapActinMode);
232             }
233
234             if (extraClasses != null && extraClasses.length() > 0) {
235                 emitter.setExtraClasses(extraClasses);
236             }
237
238             if (msgContext.isPropertyTrue(AxisEngine.PROP_EMIT_ALL_TYPES)) {
239                 emitter.setEmitAllTypes(true);
240             }
241
242             Document JavaDoc doc = emitter.emit(Emitter.MODE_ALL);
243
244             msgContext.setProperty("WSDL", doc);
245         } catch (NoClassDefFoundError JavaDoc e) {
246             entLog.info(Messages.getMessage("toAxisFault00"), e);
247             throw new AxisFault(e.toString(), e);
248         } catch (Exception JavaDoc e) {
249             entLog.info(Messages.getMessage("toAxisFault00"), e);
250             throw AxisFault.makeFault(e);
251         }
252
253         if (log.isDebugEnabled())
254             log.debug("Exit: BasicProvider::generateWSDL (" + this +")");
255     }
256 }
257
Popular Tags