001: package org.objectweb.celtix.bus.jaxws.servlet;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.lang.ref.WeakReference;
006: import java.net.MalformedURLException;
007: import java.net.URL;
008: import java.util.ArrayList;
009: import java.util.HashMap;
010: import java.util.Hashtable;
011: import java.util.List;
012: import java.util.Map;
013:
014: import javax.servlet.ServletConfig;
015: import javax.servlet.ServletException;
016: import javax.servlet.http.HttpServlet;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import javax.xml.namespace.QName;
020: import javax.xml.parsers.DocumentBuilderFactory;
021: import javax.xml.parsers.ParserConfigurationException;
022:
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026:
027: import org.xml.sax.SAXException;
028:
029: import org.objectweb.celtix.Bus;
030: import org.objectweb.celtix.BusException;
031: import org.objectweb.celtix.bus.jaxws.EndpointImpl;
032: import org.objectweb.celtix.transports.TransportFactory;
033: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
034: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
035:
036: public class CeltixServlet extends HttpServlet {
037: static final String HTTP_REQUEST = CeltixServlet.class.getName()
038: + ".REQUEST";
039: static final String HTTP_RESPONSE = CeltixServlet.class.getName()
040: + ".RESPONSE";
041:
042: static final Map<String, WeakReference<Bus>> BUS_MAP = new Hashtable<String, WeakReference<Bus>>();
043:
044: protected Bus bus;
045: protected Map<String, ServletServerTransport> servantMap = new HashMap<String, ServletServerTransport>();
046:
047: public void init(ServletConfig servletConfig)
048: throws ServletException {
049: super .init(servletConfig);
050:
051: List<String> list = new ArrayList<String>();
052: String busid = servletConfig.getInitParameter("bus.id");
053: if (null != busid) {
054: list.add("-BUSid");
055: list.add(busid);
056: WeakReference<Bus> ref = BUS_MAP.get(busid);
057: if (null != ref) {
058: bus = ref.get();
059: }
060: }
061: try {
062: if (null == bus) {
063: bus = Bus.init(list.toArray(new String[list.size()]));
064:
065: TransportFactory factory = createServletTransportFactory();
066: factory.init(bus);
067: registerTransport(factory,
068: "http://schemas.xmlsoap.org/wsdl/soap/");
069: registerTransport(factory,
070: "http://schemas.xmlsoap.org/wsdl/soap/http");
071: registerTransport(factory,
072: "http://schemas.xmlsoap.org/wsdl/http/");
073: registerTransport(factory,
074: "http://celtix.objectweb.org/bindings/xmlformat");
075: registerTransport(factory,
076: "http://celtix.objectweb.org/transports/http/configuration");
077: }
078: } catch (BusException ex) {
079: // TODO Auto-generated catch block
080: ex.printStackTrace();
081: }
082: if (null != busid) {
083: BUS_MAP.put(busid, new WeakReference<Bus>(bus));
084: }
085:
086: InputStream ins = servletConfig.getServletContext()
087: .getResourceAsStream("/WEB-INF/celtix-servlet.xml");
088: if (ins != null) {
089: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
090: .newInstance();
091: builderFactory.setNamespaceAware(true);
092: builderFactory.setValidating(false);
093:
094: try {
095: Document doc = builderFactory.newDocumentBuilder()
096: .parse(ins);
097: Node nd = doc.getDocumentElement().getFirstChild();
098: while (nd != null) {
099: if ("endpoint".equals(nd.getLocalName())) {
100: loadEndpoint(servletConfig, nd);
101: }
102: nd = nd.getNextSibling();
103: }
104: } catch (SAXException ex) {
105: // TODO Auto-generated catch block
106: ex.printStackTrace();
107: } catch (IOException ex) {
108: // TODO Auto-generated catch block
109: ex.printStackTrace();
110: } catch (ParserConfigurationException ex) {
111: // TODO Auto-generated catch block
112: ex.printStackTrace();
113: }
114: }
115: }
116:
117: /**
118: * @return
119: */
120: protected TransportFactory createServletTransportFactory() {
121: return new ServletTransportFactory(this );
122: }
123:
124: private void registerTransport(TransportFactory factory,
125: String namespace) throws BusException {
126: this .bus.getTransportFactoryManager().registerTransportFactory(
127: namespace, factory);
128: }
129:
130: public void loadEndpoint(String implName, String serviceName,
131: String wsdlName, String portName, String urlPat) {
132:
133: try {
134: URL url = null;
135: if (wsdlName != null) {
136: try {
137: url = getServletConfig().getServletContext()
138: .getResource(wsdlName);
139: } catch (MalformedURLException ex) {
140: try {
141: url = new URL(wsdlName);
142: } catch (MalformedURLException ex2) {
143: try {
144: url = getServletConfig()
145: .getServletContext().getResource(
146: "/" + wsdlName);
147: } catch (MalformedURLException ex3) {
148: url = null;
149: }
150: }
151: }
152: }
153: Class cls = Class.forName(implName, false, Thread
154: .currentThread().getContextClassLoader());
155: Object impl = cls.newInstance();
156: EndpointReferenceType ref;
157: if (url != null) {
158: ref = EndpointReferenceUtils.getEndpointReference(url,
159: QName.valueOf(serviceName), portName);
160: } else {
161: ref = EndpointReferenceUtils.getEndpointReference(bus
162: .getWSDLManager(), impl);
163: }
164: EndpointImpl ep = new EndpointImpl(bus, impl, null, ref);
165:
166: //doesn't really matter what URL is used here
167: ep.publish("http://localhost"
168: + (urlPat.charAt(0) == '/' ? "" : "/") + urlPat);
169: } catch (ClassNotFoundException ex) {
170: // TODO Auto-generated catch block
171: ex.printStackTrace();
172: } catch (InstantiationException ex) {
173: // TODO Auto-generated catch block
174: ex.printStackTrace();
175: } catch (IllegalAccessException ex) {
176: // TODO Auto-generated catch block
177: ex.printStackTrace();
178: }
179: }
180:
181: public void loadEndpoint(ServletConfig servletConfig, Node node) {
182: Element el = (Element) node;
183: String implName = el.getAttribute("implementation");
184: String serviceName = el.getAttribute("service");
185: String wsdlName = el.getAttribute("wsdl");
186: String portName = el.getAttribute("port");
187: String urlPat = el.getAttribute("url-pattern");
188: /*
189: String intfName = el.getAttribute("interface");
190: String name = el.getAttribute("name");
191: */
192:
193: loadEndpoint(implName, serviceName, wsdlName, portName, urlPat);
194: }
195:
196: public void destroy() {
197: try {
198: String s = bus.getBusID();
199: BUS_MAP.remove(s);
200:
201: bus.shutdown(true);
202: } catch (BusException ex) {
203: ex.printStackTrace();
204: //ignore
205: }
206: }
207:
208: void addServant(URL url, ServletServerTransport servant) {
209: servantMap.put(url.getPath(), servant);
210: }
211:
212: void removeServant(URL url, ServletServerTransport servant) {
213: servantMap.remove(url.getPath());
214: }
215:
216: protected void doPost(HttpServletRequest request,
217: HttpServletResponse response) throws ServletException {
218: ServletServerTransport tp = servantMap.get(request
219: .getPathInfo());
220: if (tp == null) {
221: throw new ServletException("Unknown servlet mapping "
222: + request.getPathInfo());
223: }
224: try {
225: tp.doPost(request, response);
226: } catch (IOException ex) {
227: throw new ServletException(ex.getMessage());
228: }
229: }
230:
231: protected void doGet(HttpServletRequest request,
232: HttpServletResponse response) throws ServletException {
233: ServletServerTransport tp = servantMap.get(request
234: .getPathInfo());
235: if (tp == null) {
236: throw new ServletException("Unknown servlet mapping "
237: + request.getPathInfo());
238: }
239: try {
240: tp.doGet(request, response);
241: } catch (IOException ex) {
242: throw new ServletException(ex.getMessage());
243: }
244: }
245:
246: }
|