001: package org.objectweb.celtix.bus.transports.http;
002:
003: import java.io.IOException;
004: import java.lang.ref.WeakReference;
005: import java.lang.reflect.Field;
006: import java.net.URL;
007: import java.util.Map;
008:
009: import javax.wsdl.WSDLException;
010: import javax.xml.bind.JAXBException;
011:
012: import org.objectweb.celtix.Bus;
013: import org.objectweb.celtix.bindings.ClientBinding;
014: import org.objectweb.celtix.bus.transports.http.protocol.pipe.Handler;
015: import org.objectweb.celtix.bus.transports.http.protocol.pipe.PipeHTTPServerTransport;
016: import org.objectweb.celtix.buslifecycle.BusLifeCycleListener;
017: import org.objectweb.celtix.transports.ClientTransport;
018: import org.objectweb.celtix.transports.ServerTransport;
019: import org.objectweb.celtix.transports.TransportFactory;
020: import org.objectweb.celtix.transports.http.configuration.HTTPClientPolicy;
021: import org.objectweb.celtix.transports.http.configuration.HTTPServerPolicy;
022: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
023: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
024: import org.objectweb.celtix.wsdl.JAXBExtensionHelper;
025: import org.xmlsoap.schemas.wsdl.http.AddressType;
026:
027: public class HTTPTransportFactory implements TransportFactory {
028: protected Bus bus;
029: /* surefire runs in a classloader that won't allow the URL
030: * class to load the Handler. We'll need to manually add
031: * it to the list.
032: */
033: static {
034: addHandler();
035: }
036:
037: @SuppressWarnings("unchecked")
038: static void addHandler() {
039: try {
040: Field field = URL.class.getDeclaredField("handlers");
041: field.setAccessible(true);
042: Map handlers = (Map) field.get(null);
043: handlers.put("pipe", new Handler());
044: } catch (Exception e) {
045: //ignore
046: }
047: }
048:
049: public void init(Bus b) {
050: bus = b;
051: try {
052: JAXBExtensionHelper.addExtensions(bus.getWSDLManager()
053: .getExtenstionRegistry(), javax.wsdl.Port.class,
054: HTTPClientPolicy.class);
055: JAXBExtensionHelper.addExtensions(bus.getWSDLManager()
056: .getExtenstionRegistry(), javax.wsdl.Port.class,
057: HTTPServerPolicy.class);
058: JAXBExtensionHelper.addExtensions(bus.getWSDLManager()
059: .getExtenstionRegistry(), javax.wsdl.Port.class,
060: AddressType.class);
061:
062: } catch (JAXBException e) {
063: //ignore, we can continue without the extension registered
064: }
065: String val = System.getProperty("java.protocol.handler.pkgs");
066: if (val == null) {
067: System
068: .setProperty("java.protocol.handler.pkgs",
069: "org.objectweb.celtix.bus.transports.http.protocol");
070: } else if (val
071: .indexOf("org.objectweb.celtix.bus.transports.http.protocol") == -1) {
072: val += "|org.objectweb.celtix.bus.transports.http.protocol";
073: System.setProperty("java.protocol.handler.pkgs", val);
074: }
075: bus.getLifeCycleManager().registerLifeCycleListener(
076: new ShutdownListener(this ));
077: }
078:
079: public ServerTransport createServerTransport(
080: EndpointReferenceType address) throws WSDLException,
081: IOException {
082: URL url = new URL(EndpointReferenceUtils.getAddress(address));
083: if ("pipe".equals(url.getProtocol())) {
084: return new PipeHTTPServerTransport(bus, address);
085: }
086: return new JettyHTTPServerTransport(bus, address);
087: }
088:
089: public ServerTransport createTransientServerTransport(
090: EndpointReferenceType address) throws WSDLException {
091:
092: // TODO Auto-generated method stub
093: return null;
094: }
095:
096: public ClientTransport createClientTransport(
097: EndpointReferenceType ref, ClientBinding binding)
098: throws WSDLException, IOException {
099: return new HTTPClientTransport(bus, ref, binding, this );
100: }
101:
102: protected Bus getBus() {
103: return bus;
104: }
105:
106: private void shutdown() {
107: }
108:
109: private static class ShutdownListener extends
110: WeakReference<HTTPTransportFactory> implements
111: BusLifeCycleListener {
112:
113: ShutdownListener(HTTPTransportFactory factory) {
114: super (factory);
115: }
116:
117: public void initComplete() {
118: //nothing
119: }
120:
121: public void preShutdown() {
122: if (get() != null) {
123: HTTPTransportFactory factory = get();
124: factory.shutdown();
125: clear();
126: }
127: }
128:
129: public void postShutdown() {
130: //nothing
131: }
132: }
133: }
|