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.geronimo.axis2.builder;
017:
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.jar.JarFile;
026:
027: import javax.xml.ws.http.HTTPBinding;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.geronimo.axis2.pojo.POJOWebServiceContainerFactoryGBean;
032: import org.apache.geronimo.common.DeploymentException;
033: import org.apache.geronimo.gbean.GBeanData;
034: import org.apache.geronimo.gbean.GBeanInfo;
035: import org.apache.geronimo.gbean.GBeanInfoBuilder;
036: import org.apache.geronimo.j2ee.deployment.Module;
037: import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
038: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
039: import org.apache.geronimo.jaxws.JAXWSUtils;
040: import org.apache.geronimo.jaxws.PortInfo;
041: import org.apache.geronimo.jaxws.builder.JAXWSServiceBuilder;
042: import org.apache.geronimo.jaxws.builder.WARWebServiceFinder;
043: import org.apache.geronimo.jaxws.builder.WsdlGenerator;
044: import org.apache.geronimo.kernel.repository.Environment;
045: import org.apache.geronimo.xbeans.javaee.PortComponentType;
046: import org.apache.geronimo.xbeans.javaee.ServiceImplBeanType;
047: import org.apache.geronimo.xbeans.javaee.WebserviceDescriptionType;
048: import org.apache.geronimo.xbeans.javaee.WebservicesDocument;
049: import org.apache.geronimo.xbeans.javaee.WebservicesType;
050: import org.apache.xmlbeans.XmlCursor;
051: import org.apache.xmlbeans.XmlObject;
052:
053: /**
054: * @version $Rev$ $Date$
055: */
056: public class Axis2Builder extends JAXWSServiceBuilder {
057:
058: private static final Log log = LogFactory
059: .getLog(Axis2Builder.class);
060:
061: public Axis2Builder(Environment defaultEnviroment) {
062: super (defaultEnviroment);
063: this .webServiceFinder = new WARWebServiceFinder();
064: }
065:
066: public Axis2Builder() {
067: super (null);
068: }
069:
070: protected GBeanInfo getContainerFactoryGBeanInfo() {
071: return POJOWebServiceContainerFactoryGBean.GBEAN_INFO;
072: }
073:
074: protected Map<String, PortInfo> parseWebServiceDescriptor(
075: InputStream in, URL wsDDUrl, JarFile moduleFile,
076: boolean isEJB, Map correctedPortLocations)
077: throws DeploymentException {
078:
079: log.debug("Parsing descriptor " + wsDDUrl);
080:
081: Map<String, PortInfo> map = null;
082: XmlCursor cursor = null;
083:
084: try {
085: XmlObject xobj = XmlObject.Factory.parse(in);
086:
087: cursor = xobj.newCursor();
088: cursor.toStartDoc();
089: cursor.toFirstChild();
090: //the checking is needed as we also send JAX-RPC based webservices.xml here
091: if ("http://java.sun.com/xml/ns/javaee".equals(cursor
092: .getName().getNamespaceURI())) {
093: WebservicesDocument wd = (WebservicesDocument) xobj
094: .changeType(WebservicesDocument.type);
095: WebservicesType wst = wd.getWebservices();
096:
097: for (WebserviceDescriptionType desc : wst
098: .getWebserviceDescriptionArray()) {
099: String wsdlFile = null;
100: if (desc.getWsdlFile() != null) {
101: wsdlFile = getString(desc.getWsdlFile()
102: .getStringValue());
103: }
104:
105: String serviceName = desc
106: .getWebserviceDescriptionName()
107: .getStringValue();
108:
109: for (PortComponentType port : desc
110: .getPortComponentArray()) {
111:
112: PortInfo portInfo = new PortInfo();
113: String serviceLink = null;
114: ServiceImplBeanType beanType = port
115: .getServiceImplBean();
116: if (beanType.getEjbLink() != null) {
117: serviceLink = beanType.getEjbLink()
118: .getStringValue();
119: } else if (beanType.getServletLink()
120: .getStringValue() != null) {
121: serviceLink = beanType.getServletLink()
122: .getStringValue();
123: }
124: portInfo.setServiceLink(serviceLink);
125:
126: if (port.getServiceEndpointInterface() != null) {
127: String sei = port
128: .getServiceEndpointInterface()
129: .getStringValue();
130: portInfo
131: .setServiceEndpointInterfaceName(sei);
132: }
133:
134: String portName = port.getPortComponentName()
135: .getStringValue();
136: portInfo.setPortName(portName);
137:
138: portInfo.setProtocolBinding(port
139: .getProtocolBinding());
140: portInfo.setServiceName(serviceName);
141: portInfo.setWsdlFile(wsdlFile);
142:
143: if (port.getEnableMtom() != null) {
144: portInfo.setEnableMTOM(port.getEnableMtom()
145: .getBooleanValue());
146: }
147:
148: if (port.getHandlerChains() != null) {
149: StringBuffer chains = new StringBuffer(
150: "<handler-chains xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
151: chains.append(port.getHandlerChains()
152: .xmlText());
153: chains.append("</handler-chains>");
154: portInfo
155: .setHandlersAsXML(chains.toString());
156: }
157:
158: if (port.getWsdlPort() != null) {
159: portInfo.setWsdlPort(port.getWsdlPort()
160: .getQNameValue());
161: }
162:
163: if (port.getWsdlService() != null) {
164: portInfo.setWsdlService(port
165: .getWsdlService().getQNameValue());
166: }
167:
168: String location = (String) correctedPortLocations
169: .get(serviceLink);
170: portInfo.setLocation(location);
171:
172: if (map == null) {
173: map = new HashMap<String, PortInfo>();
174: }
175:
176: map.put(serviceLink, portInfo);
177: }
178: }
179: } else {
180: log
181: .debug("Descriptor ignored (not a Java EE 5 descriptor)");
182: }
183:
184: return map;
185: } catch (FileNotFoundException e) {
186: return Collections.emptyMap();
187: } catch (IOException ex) {
188: throw new DeploymentException("Unable to read " + wsDDUrl,
189: ex);
190: } catch (Exception ex) {
191: throw new DeploymentException("Unknown deployment error",
192: ex);
193: } finally {
194: if (cursor != null) {
195: cursor.dispose();
196: }
197: try {
198: in.close();
199: } catch (IOException e) {
200: // ignore
201: }
202: }
203: }
204:
205: private static String getString(String in) {
206: if (in != null) {
207: in = in.trim();
208: if (in.length() == 0) {
209: return null;
210: }
211: }
212: return in;
213: }
214:
215: @Override
216: protected void initialize(GBeanData targetGBean,
217: Class serviceClass, PortInfo portInfo, Module module)
218: throws DeploymentException {
219: if (isWsdlSet(portInfo, serviceClass)) {
220: log.debug("Service " + portInfo.getServiceName()
221: + " has WSDL.");
222: return;
223: }
224:
225: if (isHTTPBinding(portInfo, serviceClass)) {
226: log
227: .debug("Service "
228: + portInfo.getServiceName()
229: + " is HTTPBinding. Only SOAP 1.1 or 1.2 is supported.");
230: return;
231: }
232:
233: log.debug("Service " + portInfo.getServiceName()
234: + " does not have WSDL. Generating WSDL...");
235:
236: WsdlGenerator generator = new WsdlGenerator();
237: generator.setAxis2SAAJ();
238:
239: // set wsdl service
240: if (portInfo.getWsdlService() == null) {
241: generator.setWsdlService(JAXWSUtils
242: .getServiceQName(serviceClass));
243: } else {
244: generator.setWsdlService(portInfo.getWsdlService());
245: }
246:
247: // set wsdl port
248: if (portInfo.getWsdlPort() != null) {
249: generator.setWsdlPort(portInfo.getWsdlPort());
250: }
251:
252: String wsdlFile = generator.generateWsdl(module, serviceClass
253: .getName(), module.getEarContext(), portInfo);
254: portInfo.setWsdlFile(wsdlFile);
255:
256: log.debug("Generated " + wsdlFile + " for service "
257: + portInfo.getServiceName());
258: }
259:
260: private boolean isWsdlSet(PortInfo portInfo, Class serviceClass) {
261: return (portInfo.getWsdlFile() != null && !portInfo
262: .getWsdlFile().trim().equals(""))
263: || JAXWSUtils.containsWsdlLocation(serviceClass,
264: serviceClass.getClassLoader());
265: }
266:
267: private boolean isHTTPBinding(PortInfo portInfo, Class serviceClass) {
268: String bindingURI = "";
269: String bindingURIFromAnnot;
270:
271: if (portInfo.getProtocolBinding() != null) {
272: bindingURI = JAXWSUtils.getBindingURI(portInfo
273: .getProtocolBinding());
274: }
275: bindingURIFromAnnot = JAXWSUtils.getBindingURIFromAnnot(
276: serviceClass, serviceClass.getClassLoader());
277:
278: if (bindingURI != null && !bindingURI.trim().equals("")) {
279: return bindingURI.equals(HTTPBinding.HTTP_BINDING);
280: } else if (bindingURIFromAnnot != null
281: && !bindingURIFromAnnot.trim().equals("")) {
282: return bindingURIFromAnnot.equals(HTTPBinding.HTTP_BINDING);
283: }
284:
285: return false;
286: }
287:
288: public static final GBeanInfo GBEAN_INFO;
289:
290: static {
291: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
292: Axis2Builder.class, NameFactory.MODULE_BUILDER);
293: infoBuilder.addInterface(WebServiceBuilder.class);
294: infoBuilder.addAttribute("defaultEnvironment",
295: Environment.class, true, true);
296: infoBuilder
297: .setConstructor(new String[] { "defaultEnvironment" });
298: GBEAN_INFO = infoBuilder.getBeanInfo();
299: }
300:
301: public static GBeanInfo getGBeanInfo() {
302: return GBEAN_INFO;
303: }
304: }
|