001: package org.objectweb.celtix.geronimo.container;
002:
003: import java.net.MalformedURLException;
004: import java.net.URL;
005: import java.util.logging.Logger;
006:
007: import javax.xml.namespace.QName;
008:
009: import org.apache.geronimo.gbean.GBeanLifecycle;
010: import org.apache.geronimo.webservices.WebServiceContainer;
011: import org.objectweb.celtix.Bus;
012: import org.objectweb.celtix.BusException;
013: import org.objectweb.celtix.bus.jaxws.EndpointImpl;
014: import org.objectweb.celtix.geronimo.builder.PortInfo;
015: import org.objectweb.celtix.transports.TransportFactoryManager;
016: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
017: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
018:
019: public class CeltixWebServiceContainer implements WebServiceContainer,
020: GBeanLifecycle {
021:
022: private static final Logger LOG = Logger
023: .getLogger(CeltixWebServiceContainer.class.getName());
024:
025: private final PortInfo portInfo;
026:
027: private transient GeronimoTransportFactory factory;
028: private transient GeronimoServerTransport serverTransport;
029: private transient EndpointImpl publishedEndpoint;
030: private transient Bus bus;
031: private transient boolean started;
032:
033: public CeltixWebServiceContainer(PortInfo pi) {
034: portInfo = pi;
035: }
036:
037: public void invoke(Request request, Response response)
038: throws Exception {
039:
040: Object target = request
041: .getAttribute(WebServiceContainer.POJO_INSTANCE);
042: assert target != null : "target object not available in request";
043:
044: ClassLoader origLoader = Thread.currentThread()
045: .getContextClassLoader();
046:
047: try {
048: Thread.currentThread().setContextClassLoader(
049: target.getClass().getClassLoader());
050: if (!isEndpointPublished()) {
051: doStart();
052: publishEndpoint(target);
053: }
054: serverTransport.invoke(request, response);
055: } finally {
056: Thread.currentThread().setContextClassLoader(origLoader);
057: }
058: }
059:
060: public void getWsdl(Request request, Response response)
061: throws Exception {
062: // TODO Auto-generated method stub
063: System.out.println(this + " getWsdl called "
064: + request.getParameters());
065: }
066:
067: public void doStart() throws Exception {
068:
069: if (started) {
070: return;
071: }
072:
073: TransportFactoryManager tfm = getBus()
074: .getTransportFactoryManager();
075: getTransportFactory().init(getBus());
076: tfm.registerTransportFactory(
077: "http://schemas.xmlsoap.org/wsdl/soap/", factory);
078: tfm.registerTransportFactory(
079: "http://schemas.xmlsoap.org/wsdl/soap/http", factory);
080: tfm
081: .registerTransportFactory(
082: "http://celtix.objectweb.org/transports/http/configuration",
083: factory);
084: started = true;
085: }
086:
087: public void doStop() throws Exception {
088: }
089:
090: public void doFail() {
091: // TODO Auto-generated method stub
092: }
093:
094: void setServerTransport(GeronimoServerTransport st) {
095: serverTransport = st;
096: }
097:
098: GeronimoServerTransport getServerTransport() {
099: return serverTransport;
100: }
101:
102: private synchronized Bus getBus() throws BusException {
103: if (bus == null) {
104: // TODO: have the option of getting init values here
105: bus = Bus.init();
106: }
107: return bus;
108: }
109:
110: // setter for bus for unit testing purposes
111: void setBus(Bus b) {
112: bus = b;
113: }
114:
115: private boolean isEndpointPublished() {
116: return publishedEndpoint != null;
117: }
118:
119: private synchronized void publishEndpoint(Object target)
120: throws BusException, MalformedURLException {
121:
122: assert target != null : "null target received";
123:
124: URL url = resolveWSDL(portInfo.getWsdlFile(), target.getClass()
125: .getClassLoader());
126: assert url != null;
127:
128: EndpointReferenceType ref = EndpointReferenceUtils
129: .getEndpointReference(url, QName.valueOf(portInfo
130: .getServiceName()), portInfo.getPortName());
131:
132: GeronimoTransportFactory tfactory = getTransportFactory();
133: assert tfactory != null : "failed to get transport factory";
134:
135: try {
136: tfactory.setCurrentContainer(this );
137: EndpointImpl ep = new EndpointImpl(getBus(), target,
138: "http://schemas.xmlsoap.org/wsdl/soap/http", ref);
139: LOG.fine("publishing endpoint " + ep);
140: ep.publish("http://localhost/");
141: publishedEndpoint = ep;
142:
143: assert serverTransport != null : "server transport not initialized";
144: } finally {
145: tfactory.setCurrentContainer(null);
146: }
147: }
148:
149: private URL resolveWSDL(String resource, ClassLoader loader) {
150:
151: try {
152: return new URL(resource);
153: } catch (MalformedURLException ex) {
154: return loader.getResource(resource);
155: }
156: }
157:
158: private synchronized GeronimoTransportFactory getTransportFactory() {
159: if (factory == null) {
160: factory = new GeronimoTransportFactory();
161: }
162: return factory;
163: }
164: }
|