001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.geronimo.cxf;
019:
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.concurrent.Executor;
025:
026: import javax.xml.transform.Source;
027: import javax.xml.ws.Binding;
028: import javax.xml.ws.Endpoint;
029: import javax.xml.ws.WebServiceException;
030: import javax.xml.ws.handler.Handler;
031: import javax.xml.ws.http.HTTPBinding;
032: import javax.xml.ws.soap.SOAPBinding;
033:
034: import org.apache.cxf.Bus;
035: import org.apache.cxf.endpoint.Server;
036: import org.apache.cxf.endpoint.ServerImpl;
037: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
038: import org.apache.cxf.jaxws.handler.PortInfoImpl;
039: import org.apache.cxf.jaxws.javaee.HandlerChainsType;
040: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
041: import org.apache.cxf.jaxws.support.JaxWsImplementorInfo;
042: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
043: import org.apache.cxf.service.Service;
044: import org.apache.geronimo.jaxws.PortInfo;
045: import org.apache.geronimo.jaxws.annotations.AnnotationException;
046: import org.apache.geronimo.jaxws.annotations.AnnotationProcessor;
047:
048: public abstract class CXFEndpoint extends Endpoint {
049:
050: protected Bus bus;
051:
052: protected Object implementor;
053:
054: protected Server server;
055:
056: protected Service service;
057:
058: protected JaxWsImplementorInfo implInfo;
059:
060: protected JaxWsServiceFactoryBean serviceFactory;
061:
062: protected PortInfo portInfo;
063:
064: protected AnnotationProcessor annotationProcessor;
065:
066: public CXFEndpoint(Bus bus, Object implementor) {
067: this .bus = bus;
068: this .implementor = implementor;
069: this .portInfo = (PortInfo) bus.getExtension(PortInfo.class);
070: }
071:
072: protected URL getWsdlURL(URL configurationBaseUrl, String wsdlFile) {
073: URL wsdlURL = null;
074: if (wsdlFile != null && wsdlFile.trim().length() > 0) {
075: wsdlFile = wsdlFile.trim();
076: try {
077: wsdlURL = new URL(wsdlFile);
078: } catch (MalformedURLException e) {
079: // Not a URL, try as a resource
080: wsdlURL = getImplementorClass().getResource(
081: "/" + wsdlFile);
082:
083: if (wsdlURL == null && configurationBaseUrl != null) {
084: // Cannot get it as a resource, try with
085: // configurationBaseUrl
086: try {
087: wsdlURL = new URL(configurationBaseUrl,
088: wsdlFile);
089: } catch (MalformedURLException ee) {
090: // ignore
091: }
092: }
093: }
094: }
095: return wsdlURL;
096: }
097:
098: protected Class getImplementorClass() {
099: return this .implementor.getClass();
100: }
101:
102: protected org.apache.cxf.endpoint.Endpoint getEndpoint() {
103: return ((ServerImpl) getServer()).getEndpoint();
104: }
105:
106: public boolean isSOAP11() {
107: return SOAPBinding.SOAP11HTTP_BINDING.equals(implInfo
108: .getBindingType())
109: || SOAPBinding.SOAP11HTTP_MTOM_BINDING.equals(implInfo
110: .getBindingType());
111: }
112:
113: public ServerImpl getServer() {
114: return (ServerImpl) server;
115: }
116:
117: public Binding getBinding() {
118: return ((JaxWsEndpointImpl) getEndpoint()).getJaxwsBinding();
119: }
120:
121: public void setExecutor(Executor executor) {
122: service.setExecutor(executor);
123: }
124:
125: public Executor getExecutor() {
126: return service.getExecutor();
127: }
128:
129: @Override
130: public Object getImplementor() {
131: return implementor;
132: }
133:
134: @Override
135: public List<Source> getMetadata() {
136: // TODO Auto-generated method stub
137: return null;
138: }
139:
140: @Override
141: public Map<String, Object> getProperties() {
142: // TODO Auto-generated method stub
143: return null;
144: }
145:
146: @Override
147: public boolean isPublished() {
148: return server != null;
149: }
150:
151: @Override
152: public void publish(Object arg0) {
153: // TODO Auto-generated method stub
154: }
155:
156: @Override
157: public void publish(String address) {
158: doPublish(address);
159: }
160:
161: public void setMetadata(List<Source> arg0) {
162: // TODO Auto-generated method stub
163: }
164:
165: @Override
166: public void setProperties(Map<String, Object> arg0) {
167: // TODO Auto-generated method stub
168: }
169:
170: private static class GeronimoJaxWsServerFactoryBean extends
171: JaxWsServerFactoryBean {
172: public GeronimoJaxWsServerFactoryBean() {
173: // disable CXF resource injection
174: doInit = false;
175: }
176: }
177:
178: protected void doPublish(String address) {
179: JaxWsServerFactoryBean svrFactory = new GeronimoJaxWsServerFactoryBean();
180: svrFactory.setBus(bus);
181: svrFactory.setAddress(address);
182: svrFactory.setServiceFactory(serviceFactory);
183: svrFactory.setStart(false);
184: svrFactory.setServiceBean(implementor);
185:
186: if (HTTPBinding.HTTP_BINDING.equals(implInfo.getBindingType())) {
187: svrFactory
188: .setTransportId("http://cxf.apache.org/bindings/xformat");
189: }
190:
191: server = svrFactory.create();
192:
193: init();
194:
195: org.apache.cxf.endpoint.Endpoint endpoint = getEndpoint();
196:
197: if (getBinding() instanceof SOAPBinding) {
198: ((SOAPBinding) getBinding()).setMTOMEnabled(this .portInfo
199: .isMTOMEnabled());
200: }
201:
202: /**
203: if (endpoint.getEnableSchemaValidation()) {
204: endpoint.ge
205: endpoint.put(Message.SCHEMA_VALIDATION_ENABLED,
206: endpoint.getEnableSchemaValidation());
207: }
208: **/
209: server.start();
210: }
211:
212: protected void init() {
213: }
214:
215: /*
216: * Set appropriate handlers for the port/service/bindings.
217: */
218: protected void initHandlers() throws Exception {
219: HandlerChainsType handlerChains = this .portInfo
220: .getHandlers(HandlerChainsType.class);
221: CXFHandlerResolver handlerResolver = new CXFHandlerResolver(
222: getImplementorClass().getClassLoader(),
223: getImplementorClass(), handlerChains, null);
224:
225: PortInfoImpl portInfo = new PortInfoImpl(implInfo
226: .getBindingType(), serviceFactory.getEndpointName(),
227: service.getName());
228:
229: List<Handler> chain = handlerResolver.getHandlerChain(portInfo);
230:
231: getBinding().setHandlerChain(chain);
232: }
233:
234: protected void injectResources(Object instance)
235: throws AnnotationException {
236: this .annotationProcessor.processAnnotations(instance);
237: this .annotationProcessor.invokePostConstruct(instance);
238: }
239:
240: protected void injectHandlers() {
241: List<Handler> handlers = getBinding().getHandlerChain();
242: try {
243: for (Handler handler : handlers) {
244: injectResources(handler);
245: }
246: } catch (AnnotationException e) {
247: throw new WebServiceException("Handler annotation failed",
248: e);
249: }
250: }
251:
252: protected void destroyHandlers() {
253: if (this .annotationProcessor != null) {
254: // call handlers preDestroy
255: List<Handler> handlers = getBinding().getHandlerChain();
256: for (Handler handler : handlers) {
257: this .annotationProcessor.invokePreDestroy(handler);
258: }
259: }
260: }
261:
262: public void stop() {
263: // shutdown server
264: if (this.server != null) {
265: this.server.stop();
266: }
267: }
268: }
|