001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.server.cxf;
018:
019: import org.apache.cxf.Bus;
020: import org.apache.cxf.endpoint.Server;
021: import org.apache.cxf.endpoint.ServerImpl;
022: import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
023: import org.apache.cxf.jaxws.handler.PortInfoImpl;
024: import org.apache.cxf.jaxws.support.JaxWsEndpointImpl;
025: import org.apache.cxf.jaxws.support.JaxWsImplementorInfo;
026: import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
027: import org.apache.cxf.service.Service;
028: import org.apache.openejb.core.webservices.HandlerResolverImpl;
029: import org.apache.openejb.core.webservices.PortData;
030: import org.w3c.dom.Element;
031:
032: import javax.xml.transform.Source;
033: import javax.xml.ws.Binding;
034: import javax.xml.ws.Endpoint;
035: import javax.xml.ws.EndpointReference;
036: import javax.xml.ws.handler.Handler;
037: import javax.xml.ws.http.HTTPBinding;
038: import javax.xml.ws.soap.SOAPBinding;
039: import javax.naming.Context;
040: import java.net.MalformedURLException;
041: import java.net.URL;
042: import java.util.List;
043: import java.util.Map;
044: import java.util.concurrent.Executor;
045:
046: public abstract class CxfEndpoint extends Endpoint {
047: protected Bus bus;
048:
049: protected PortData port;
050:
051: protected Context context;
052:
053: protected Object implementor;
054:
055: protected Server server;
056:
057: protected Service service;
058:
059: protected JaxWsImplementorInfo implInfo;
060:
061: protected JaxWsServiceFactoryBean serviceFactory;
062:
063: protected HandlerResolverImpl handlerResolver;
064:
065: public CxfEndpoint(Bus bus, PortData port, Context context,
066: Object implementor) {
067: this .bus = bus;
068: this .port = port;
069: this .implementor = implementor;
070: this .context = context;
071: }
072:
073: protected URL getWsdlURL(URL configurationBaseUrl, String wsdlFile) {
074: URL wsdlURL = null;
075: if (wsdlFile != null && wsdlFile.trim().length() > 0) {
076: wsdlFile = wsdlFile.trim();
077: try {
078: wsdlURL = new URL(wsdlFile);
079: } catch (MalformedURLException e) {
080: // Not a URL, try as a resource
081: wsdlURL = getImplementorClass().getResource(
082: "/" + wsdlFile);
083:
084: if (wsdlURL == null && configurationBaseUrl != null) {
085: // Cannot get it as a resource, try with
086: // configurationBaseUrl
087: try {
088: wsdlURL = new URL(configurationBaseUrl,
089: wsdlFile);
090: } catch (MalformedURLException ee) {
091: // ignore
092: }
093: }
094: }
095: }
096: return wsdlURL;
097: }
098:
099: protected Class getImplementorClass() {
100: return this .implementor.getClass();
101: }
102:
103: protected org.apache.cxf.endpoint.Endpoint getEndpoint() {
104: return getServer().getEndpoint();
105: }
106:
107: public boolean isSOAP11() {
108: return SOAPBinding.SOAP11HTTP_BINDING.equals(implInfo
109: .getBindingType())
110: || SOAPBinding.SOAP11HTTP_MTOM_BINDING.equals(implInfo
111: .getBindingType());
112: }
113:
114: public ServerImpl getServer() {
115: return (ServerImpl) server;
116: }
117:
118: public Binding getBinding() {
119: return ((JaxWsEndpointImpl) getEndpoint()).getJaxwsBinding();
120: }
121:
122: public void setExecutor(Executor executor) {
123: service.setExecutor(executor);
124: }
125:
126: public Executor getExecutor() {
127: return service.getExecutor();
128: }
129:
130: public Object getImplementor() {
131: return implementor;
132: }
133:
134: public List<Source> getMetadata() {
135: return null;
136: }
137:
138: public Map<String, Object> getProperties() {
139: return null;
140: }
141:
142: public boolean isPublished() {
143: return server != null;
144: }
145:
146: public void publish(Object arg0) {
147: }
148:
149: public void publish(String address) {
150: doPublish(address);
151: }
152:
153: public void setMetadata(List<Source> arg0) {
154: }
155:
156: public void setProperties(Map<String, Object> arg0) {
157: }
158:
159: @SuppressWarnings({"UnusedDeclaration"})
160: public EndpointReference getEndpointReference(
161: Element... referenceParameters) {
162: throw new UnsupportedOperationException(
163: "JaxWS 2.1 APIs are not supported");
164: }
165:
166: @SuppressWarnings({"UnusedDeclaration"})
167: public <T extends EndpointReference> T getEndpointReference(
168: Class<T> clazz, Element... referenceParameters) {
169: throw new UnsupportedOperationException(
170: "JaxWS 2.1 APIs are not supported");
171: }
172:
173: private static class NoInitJaxWsServerFactoryBean extends
174: JaxWsServerFactoryBean {
175: public NoInitJaxWsServerFactoryBean() {
176: // disable CXF resource injection
177: doInit = false;
178: }
179: }
180:
181: protected void doPublish(String address) {
182: JaxWsServerFactoryBean svrFactory = new NoInitJaxWsServerFactoryBean();
183: svrFactory.setBus(bus);
184: svrFactory.setAddress(address);
185: svrFactory.setServiceFactory(serviceFactory);
186: svrFactory.setStart(false);
187: svrFactory.setServiceBean(implementor);
188:
189: if (HTTPBinding.HTTP_BINDING.equals(implInfo.getBindingType())) {
190: svrFactory
191: .setTransportId("http://cxf.apache.org/bindings/xformat");
192: }
193:
194: server = svrFactory.create();
195:
196: init();
197:
198: // todo do we need to call this?
199: getEndpoint();
200:
201: if (getBinding() instanceof SOAPBinding) {
202: ((SOAPBinding) getBinding()).setMTOMEnabled(port
203: .isMtomEnabled());
204: }
205:
206: server.start();
207: }
208:
209: protected void init() {
210: }
211:
212: /**
213: * Set appropriate handlers for the port/service/bindings.
214: */
215: protected void initHandlers() throws Exception {
216: PortInfoImpl portInfo = new PortInfoImpl(implInfo
217: .getBindingType(), serviceFactory.getEndpointName(),
218: service.getName());
219:
220: handlerResolver = new HandlerResolverImpl(port
221: .getHandlerChains(), port.getInjections(), context);
222: List<Handler> chain = handlerResolver.getHandlerChain(portInfo);
223:
224: getBinding().setHandlerChain(chain);
225: }
226:
227: protected void destroyHandlers() {
228: if (this .handlerResolver != null) {
229: handlerResolver.destroyHandlers();
230: handlerResolver = null;
231: }
232: }
233:
234: public void stop() {
235: // shutdown server
236: if (this.server != null) {
237: this.server.stop();
238: }
239: }
240: }
|