001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.axis2;
017:
018: import org.apache.axis2.Constants;
019: import org.apache.axis2.description.AxisOperation;
020: import org.apache.axis2.description.AxisService;
021: import org.apache.axis2.description.Parameter;
022: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
023: import org.apache.axis2.engine.MessageReceiver;
024: import org.apache.axis2.jaxws.description.DescriptionFactory;
025: import org.apache.axis2.jaxws.description.EndpointDescription;
026: import org.apache.axis2.jaxws.description.ServiceDescription;
027: import org.apache.axis2.jaxws.description.builder.BindingTypeAnnot;
028: import org.apache.axis2.jaxws.description.builder.DescriptionBuilderComposite;
029: import org.apache.axis2.jaxws.description.builder.MethodDescriptionComposite;
030: import org.apache.axis2.jaxws.description.builder.WebServiceAnnot;
031: import org.apache.axis2.jaxws.description.builder.WebServiceProviderAnnot;
032: import org.apache.axis2.jaxws.description.builder.WsdlComposite;
033: import org.apache.axis2.jaxws.description.builder.WsdlGenerator;
034: import org.apache.axis2.jaxws.description.builder.converter.JavaClassToDBCConverter;
035: import org.apache.axis2.jaxws.server.JAXWSMessageReceiver;
036: import org.apache.axis2.wsdl.WSDLUtil;
037: import org.apache.openejb.core.webservices.JaxWsUtils;
038: import org.apache.openejb.core.webservices.PortData;
039: import org.apache.openejb.server.axis2.util.SimpleWsdlLocator;
040: import org.apache.ws.commons.schema.utils.NamespaceMap;
041:
042: import javax.wsdl.Binding;
043: import javax.wsdl.Definition;
044: import javax.wsdl.Port;
045: import javax.wsdl.Service;
046: import javax.wsdl.WSDLException;
047: import javax.wsdl.extensions.http.HTTPBinding;
048: import javax.wsdl.extensions.soap.SOAPBinding;
049: import javax.wsdl.extensions.soap12.SOAP12Binding;
050: import javax.wsdl.factory.WSDLFactory;
051: import javax.wsdl.xml.WSDLReader;
052: import javax.xml.namespace.QName;
053: import javax.xml.ws.WebServiceException;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.net.URL;
057: import java.util.HashMap;
058: import java.util.Iterator;
059: import java.util.List;
060:
061: public class AxisServiceGenerator {
062: private MessageReceiver messageReceiver;
063:
064: public AxisServiceGenerator() {
065: this .messageReceiver = new JAXWSMessageReceiver();
066: }
067:
068: public void setMessageReceiver(MessageReceiver messageReceiver) {
069: this .messageReceiver = messageReceiver;
070: }
071:
072: public AxisService getServiceFromClass(Class endpointClass)
073: throws Exception {
074: ServiceDescription serviceDescription = DescriptionFactory
075: .createServiceDescription(endpointClass);
076: EndpointDescription[] edArray = serviceDescription
077: .getEndpointDescriptions();
078: AxisService service = edArray[0].getAxisService();
079:
080: if (service.getNameSpacesMap() == null) {
081: NamespaceMap map = new NamespaceMap();
082: map.put(Java2WSDLConstants.AXIS2_NAMESPACE_PREFIX,
083: Java2WSDLConstants.AXIS2_XSD);
084: map.put(Java2WSDLConstants.DEFAULT_SCHEMA_NAMESPACE_PREFIX,
085: Java2WSDLConstants.URI_2001_SCHEMA_XSD);
086: service.setNameSpacesMap(map);
087: }
088:
089: String endpointClassName = endpointClass.getName();
090: ClassLoader classLoader = endpointClass.getClassLoader();
091:
092: service.addParameter(new Parameter(Constants.SERVICE_CLASS,
093: endpointClassName));
094: service.setClassLoader(classLoader);
095:
096: for (Iterator<AxisOperation> opIterator = service
097: .getOperations(); opIterator.hasNext();) {
098: AxisOperation operation = opIterator.next();
099: operation.setMessageReceiver(this .messageReceiver);
100: }
101:
102: Parameter serviceDescriptionParam = new Parameter(
103: EndpointDescription.AXIS_SERVICE_PARAMETER, edArray[0]);
104: service.addParameter(serviceDescriptionParam);
105:
106: return service;
107: }
108:
109: public AxisService getServiceFromWSDL(PortData portData,
110: Class endpointClass) throws Exception {
111: if (portData.getWsdlUrl() == null) {
112: throw new Exception("WSDL file is required.");
113: }
114:
115: String endpointClassName = endpointClass.getName();
116: ClassLoader classLoader = endpointClass.getClassLoader();
117:
118: QName serviceQName = portData.getWsdlService();
119: if (serviceQName == null) {
120: serviceQName = JaxWsUtils.getServiceQName(endpointClass);
121: }
122:
123: QName portQName = portData.getWsdlPort();
124: if (portQName == null) {
125: portQName = JaxWsUtils.getPortQName(endpointClass);
126: }
127:
128: Definition wsdlDefinition = readWSDL(portData.getWsdlUrl());
129:
130: Service wsdlService = wsdlDefinition.getService(serviceQName);
131: if (wsdlService == null) {
132: throw new Exception("Service '" + serviceQName
133: + "' not found in WSDL");
134: }
135:
136: Port port = wsdlService.getPort(portQName.getLocalPart());
137: if (port == null) {
138: throw new Exception("Port '" + portQName.getLocalPart()
139: + "' not found in WSDL");
140: }
141:
142: Binding binding = port.getBinding();
143: List extElements = binding.getExtensibilityElements();
144: Iterator extElementsIterator = extElements.iterator();
145: String bindingS = javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING; //this is the default.
146: while (extElementsIterator.hasNext()) {
147: Object o = extElementsIterator.next();
148: if (o instanceof SOAPBinding) {
149: SOAPBinding sp = (SOAPBinding) o;
150: if (sp.getElementType().getNamespaceURI().equals(
151: "http://schemas.xmlsoap.org/wsdl/soap/")) {
152: //todo: how to we tell if it is MTOM or not.
153: bindingS = javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING;
154: }
155: } else if (o instanceof SOAP12Binding) {
156: SOAP12Binding sp = (SOAP12Binding) o;
157: if (sp.getElementType().getNamespaceURI().equals(
158: "http://schemas.xmlsoap.org/wsdl/soap12/")) {
159: //todo: how to we tell if it is MTOM or not.
160: bindingS = javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING;
161: }
162: } else if (o instanceof HTTPBinding) {
163: HTTPBinding sp = (HTTPBinding) o;
164: if (sp.getElementType().getNamespaceURI().equals(
165: "http://www.w3.org/2004/08/wsdl/http")) {
166: bindingS = javax.xml.ws.http.HTTPBinding.HTTP_BINDING;
167: }
168: }
169: }
170:
171: Class endPointClass = classLoader.loadClass(endpointClassName);
172: JavaClassToDBCConverter converter = new JavaClassToDBCConverter(
173: endPointClass);
174: HashMap<String, DescriptionBuilderComposite> dbcMap = converter
175: .produceDBC();
176:
177: DescriptionBuilderComposite dbc = dbcMap.get(endpointClassName);
178: dbc.setClassLoader(classLoader);
179: dbc.setWsdlDefinition(wsdlDefinition);
180: dbc.setClassName(endpointClassName);
181: dbc
182: .setCustomWsdlGenerator(new WSDLGeneratorImpl(
183: wsdlDefinition));
184:
185: if (dbc.getWebServiceAnnot() != null) { //information specified in .wsdl should overwrite annotation.
186: WebServiceAnnot serviceAnnot = dbc.getWebServiceAnnot();
187: serviceAnnot.setPortName(portQName.getLocalPart());
188: serviceAnnot.setServiceName(serviceQName.getLocalPart());
189: serviceAnnot.setTargetNamespace(serviceQName
190: .getNamespaceURI());
191: if (dbc.getBindingTypeAnnot() != null && bindingS != null
192: && !bindingS.equals("")) {
193: BindingTypeAnnot bindingAnnot = dbc
194: .getBindingTypeAnnot();
195: bindingAnnot.setValue(bindingS);
196: }
197: } else if (dbc.getWebServiceProviderAnnot() != null) {
198: WebServiceProviderAnnot serviceProviderAnnot = dbc
199: .getWebServiceProviderAnnot();
200: serviceProviderAnnot.setPortName(portQName.getLocalPart());
201: serviceProviderAnnot.setServiceName(serviceQName
202: .getLocalPart());
203: serviceProviderAnnot.setTargetNamespace(serviceQName
204: .getNamespaceURI());
205: if (dbc.getBindingTypeAnnot() != null && bindingS != null
206: && !bindingS.equals("")) {
207: BindingTypeAnnot bindingAnnot = dbc
208: .getBindingTypeAnnot();
209: bindingAnnot.setValue(bindingS);
210: }
211: }
212:
213: AxisService service = getService(dbcMap);
214:
215: service.setName(serviceQName.getLocalPart());
216: service.setEndpointName(portQName.getLocalPart());
217:
218: for (Iterator<AxisOperation> opIterator = service
219: .getOperations(); opIterator.hasNext();) {
220: AxisOperation operation = opIterator.next();
221: operation.setMessageReceiver(this .messageReceiver);
222: String MEP = operation.getMessageExchangePattern();
223: if (!WSDLUtil.isOutputPresentForMEP(MEP)) {
224: List<MethodDescriptionComposite> mdcList = dbc
225: .getMethodDescriptionComposite(operation
226: .getName().toString());
227: for (Iterator<MethodDescriptionComposite> mIterator = mdcList
228: .iterator(); mIterator.hasNext();) {
229: MethodDescriptionComposite mdc = mIterator.next();
230: //TODO: JAXWS spec says need to check Holder param exist before taking a method as OneWay
231: mdc.setOneWayAnnot(true);
232: }
233: }
234: }
235:
236: return service;
237: }
238:
239: private AxisService getService(
240: HashMap<String, DescriptionBuilderComposite> dbcMap) {
241: return getEndpointDescription(dbcMap).getAxisService();
242: }
243:
244: private EndpointDescription getEndpointDescription(
245: HashMap<String, DescriptionBuilderComposite> dbcMap) {
246: List<ServiceDescription> serviceDescList = DescriptionFactory
247: .createServiceDescriptionFromDBCMap(dbcMap);
248: if (serviceDescList == null || serviceDescList.isEmpty()) {
249: throw new RuntimeException("No service");
250: }
251: ServiceDescription serviceDescription = serviceDescList.get(0);
252: EndpointDescription[] edArray = serviceDescription
253: .getEndpointDescriptions();
254: if (edArray == null || edArray.length == 0) {
255: throw new RuntimeException("No endpoint");
256: }
257: return edArray[0];
258: }
259:
260: private static class WSDLGeneratorImpl implements WsdlGenerator {
261: private Definition def;
262:
263: public WSDLGeneratorImpl(Definition def) {
264: this .def = def;
265: }
266:
267: public WsdlComposite generateWsdl(String implClass,
268: String bindingType) throws WebServiceException {
269: // Need WSDL generation code
270: WsdlComposite composite = new WsdlComposite();
271: composite.setWsdlFileName(implClass);
272: HashMap<String, Definition> testMap = new HashMap<String, Definition>();
273: testMap.put(composite.getWsdlFileName(), def);
274: composite.setWsdlDefinition(testMap);
275: return composite;
276: }
277: }
278:
279: protected Definition readWSDL(URL wsdlUrl) throws IOException,
280: WSDLException {
281: Definition wsdlDefinition = null;
282: InputStream wsdlStream = null;
283: try {
284: wsdlStream = wsdlUrl.openStream();
285: WSDLFactory factory = WSDLFactory.newInstance();
286: WSDLReader reader = factory.newWSDLReader();
287: reader.setFeature("javax.wsdl.importDocuments", true);
288: reader.setFeature("javax.wsdl.verbose", false);
289: SimpleWsdlLocator wsdlLocator = new SimpleWsdlLocator(
290: wsdlUrl.toString());
291: wsdlDefinition = reader.readWSDL(wsdlLocator);
292: } finally {
293: if (wsdlStream != null) {
294: wsdlStream.close();
295: }
296: }
297: return wsdlDefinition;
298: }
299:
300: public static EndpointDescription getEndpointDescription(
301: AxisService service) {
302: Parameter param = service
303: .getParameter(EndpointDescription.AXIS_SERVICE_PARAMETER);
304: return (param == null) ? null : (EndpointDescription) param
305: .getValue();
306: }
307:
308: public static boolean isSOAP11(AxisService service) {
309: EndpointDescription desc = AxisServiceGenerator
310: .getEndpointDescription(service);
311: return javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING
312: .equals(desc.getBindingType())
313: || javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING
314: .equals(desc.getBindingType());
315: }
316:
317: }
|