KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > cxf > builder > CXFBuilder


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

17 package org.apache.geronimo.cxf.builder;
18
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.JAXBElement;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Unmarshaller;
37 import javax.xml.ws.handler.Handler;
38
39 import org.apache.cxf.jaxws.javaee.PortComponentType;
40 import org.apache.cxf.jaxws.javaee.WebserviceDescriptionType;
41 import org.apache.cxf.jaxws.javaee.WebservicesType;
42
43 import org.apache.geronimo.common.DeploymentException;
44 import org.apache.geronimo.gbean.GBeanData;
45 import org.apache.geronimo.gbean.GBeanInfo;
46 import org.apache.geronimo.gbean.GBeanInfoBuilder;
47 import org.apache.geronimo.gbean.AbstractName;
48 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
49 import org.apache.geronimo.j2ee.deployment.WebModule;
50 import org.apache.geronimo.j2ee.deployment.Module;
51 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
52 import org.apache.geronimo.cxf.PortInfo;
53 import org.apache.geronimo.cxf.CXFWebServiceContainerFactoryGBean;
54 import org.apache.geronimo.kernel.repository.Environment;
55 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
56 import org.apache.geronimo.deployment.DeploymentContext;
57 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
58 import org.apache.geronimo.deployment.util.DeploymentUtil;
59
60
61 public class CXFBuilder implements WebServiceBuilder {
62
63     private static final Logger JavaDoc LOG = Logger.getLogger(CXFBuilder.class.getName());
64
65     private final Environment defaultEnvironment;
66     private static final String JavaDoc KEY = CXFBuilder.class.getName();
67     private JAXBContext ctx;
68
69
70     public CXFBuilder(Environment defaultEnvironment) {
71         this.defaultEnvironment = defaultEnvironment;
72     }
73
74     public void findWebServices(JarFile JavaDoc moduleFile, boolean isEJB, Map JavaDoc servletLocations, Environment environment, Map JavaDoc sharedContext) throws DeploymentException {
75         final String JavaDoc path = isEJB ? "META-INF/webservices.xml" : "WEB-INF/webservices.xml";
76         try {
77             URL JavaDoc wsDDUrl = DeploymentUtil.createJarURL(moduleFile, path);
78             Map JavaDoc portMap = parseWebServiceDescriptor(wsDDUrl, moduleFile, isEJB, servletLocations);
79             if (portMap != null) {
80                 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
81                 sharedContext.put(KEY, portMap);
82             }
83         } catch (MalformedURLException JavaDoc e) {
84             // The webservices.xml file doesn't exist.
85
}
86     }
87     
88     private Map JavaDoc<String JavaDoc, PortInfo> parseWebServiceDescriptor(URL JavaDoc wsDDUrl, JarFile JavaDoc moduleFile, boolean isEJB, Map JavaDoc correctedPortLocations) throws DeploymentException {
89
90         LOG.fine("parsing descriptor " + wsDDUrl);
91
92         Map JavaDoc<String JavaDoc, PortInfo> map = new HashMap JavaDoc<String JavaDoc, PortInfo>();
93
94         try {
95             InputStream JavaDoc in = wsDDUrl.openStream();
96             if (in == null) {
97                 throw new DeploymentException("unable to read descriptor " + wsDDUrl);
98             }
99
100             Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
101             Object JavaDoc obj = unmarshaller.unmarshal(in);
102
103             WebservicesType wst = null;
104             if (obj instanceof JAXBElement) {
105                 wst = (WebservicesType) ((JAXBElement) obj).getValue();
106             }
107
108             for (WebserviceDescriptionType desc : wst.getWebserviceDescription()) {
109                 final String JavaDoc wsdlFile = desc.getWsdlFile().getValue();
110                 final String JavaDoc serviceName = desc.getWebserviceDescriptionName().getValue();
111
112                 for (PortComponentType port : desc.getPortComponent()) {
113                     String JavaDoc servlet = port.getServiceImplBean().getServletLink().getValue();
114                     String JavaDoc sei = port.getServiceEndpointInterface().getValue();
115                     String JavaDoc portName = port.getPortComponentName().getValue();
116
117                     PortInfo portInfo = new PortInfo();
118
119                     portInfo.setServiceName(serviceName);
120                     portInfo.setServletLink(servlet);
121                     portInfo.setServiceEndpointInterfaceName(sei);
122                     portInfo.setPortName(portName);
123                     portInfo.setWsdlFile(wsdlFile);
124                     portInfo.setHandlers(port.getHandler());
125
126                     map.put(servlet, portInfo);
127                 }
128             }
129
130             return map;
131         } catch (FileNotFoundException JavaDoc e) {
132             return Collections.EMPTY_MAP;
133         } catch (IOException JavaDoc ex) {
134             ex.printStackTrace();
135             throw new DeploymentException("unable to read " + wsDDUrl, ex);
136         } catch (JAXBException ex) {
137             throw new DeploymentException("unable to parse webservices.xml", ex);
138         }
139     }
140
141
142     public boolean configurePOJO(GBeanData targetGBean, String JavaDoc servletName, Module module, String JavaDoc seiClassName, DeploymentContext context) throws DeploymentException {
143
144         // assert pi instanceof PortInfo : "received incorrect portInfo object";
145

146         Map JavaDoc sharedContext = ((WebModule) module).getSharedContext();
147         Map JavaDoc portInfoMap = (Map JavaDoc) sharedContext.get(KEY);
148         PortInfo portInfo = (PortInfo) portInfoMap.get(servletName);
149         if (portInfo == null) {
150             //not ours
151
return false;
152         }
153
154
155         LOG.info("configuring POJO webservice: " + servletName + " sei: " + seiClassName);
156
157         // verify that the class is loadable
158
ClassLoader JavaDoc classLoader = context.getClassLoader();
159         loadSEI(seiClassName, classLoader);
160
161         /*List<Handler> handlers =*/ buildHandlerChain(portInfo);
162         AbstractName containerFactoryName = context.getNaming().createChildName(targetGBean.getAbstractName(), "cxfWebServiceContainerFactory", NameFactory.GERONIMO_SERVICE);
163         GBeanData containerFactoryData = new GBeanData(containerFactoryName, CXFWebServiceContainerFactoryGBean.GBEAN_INFO);
164         containerFactoryData.setAttribute("portInfo", portInfo);
165         containerFactoryData.setAttribute("endpointClassName", seiClassName);
166         try {
167             context.addGBean(containerFactoryData);
168         } catch (GBeanAlreadyExistsException e) {
169             throw new DeploymentException("Could not add web service container factory gbean", e);
170         }
171
172         targetGBean.setReferencePattern("WebServiceContainerFactory", containerFactoryName);
173         targetGBean.setAttribute("pojoClassName", seiClassName);
174         return true;
175     }
176
177
178     public boolean configureEJB(GBeanData targetGBean, String JavaDoc ejbName, JarFile JavaDoc moduleFile, Map JavaDoc sharedContext, ClassLoader JavaDoc classLoader) throws DeploymentException {
179         throw new DeploymentException("configureEJB NYI");
180     }
181
182     private JAXBContext getJAXBContext() throws JAXBException {
183         if (ctx == null) {
184             ctx = JAXBContext.newInstance("com.sun.java.xml.ns.j2ee", getClass().getClassLoader());
185         }
186         return ctx;
187     }
188
189     Class JavaDoc<?> loadSEI(String JavaDoc className, ClassLoader JavaDoc loader) throws DeploymentException {
190         try {
191             return loader.loadClass(className);
192         } catch (ClassNotFoundException JavaDoc ex) {
193             throw new DeploymentException("unable to load Service Endpoint Interface: " + className, ex);
194         }
195     }
196
197     private List JavaDoc<Handler> buildHandlerChain(PortInfo portInfo) {
198         return new ArrayList JavaDoc<Handler>();
199     }
200
201     public static final GBeanInfo GBEAN_INFO;
202
203     static {
204         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(CXFBuilder.class, NameFactory.MODULE_BUILDER);
205         infoBuilder.addInterface(WebServiceBuilder.class);
206         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
207
208         infoBuilder.setConstructor(new String JavaDoc[]{"defaultEnvironment"});
209
210         GBEAN_INFO = infoBuilder.getBeanInfo();
211     }
212
213     public static GBeanInfo getGBeanInfo() {
214         return GBEAN_INFO;
215     }
216
217 }
218
Popular Tags