001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice;
021:
022: import java.io.IOException;
023: import java.net.URL;
024:
025: import javax.servlet.ServletConfig;
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServlet;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.log4j.Logger;
032:
033: import de.schlund.pfixcore.webservice.config.Configuration;
034: import de.schlund.pfixcore.webservice.config.ConfigurationReader;
035: import de.schlund.pfixcore.webservice.jaxws.JAXWSProcessor;
036: import de.schlund.pfixcore.webservice.jsonqx.JSONQXProcessor;
037: import de.schlund.pfixcore.webservice.jsonws.JSONWSProcessor;
038: import de.schlund.pfixcore.webservice.jsonws.JSONWSStubGenerator;
039: import de.schlund.pfixxml.resources.FileResource;
040: import de.schlund.pfixxml.resources.ResourceUtil;
041:
042: /**
043: * Webservice HTTP endpoint handling service requests (along with admin/tool stuff).
044: *
045: * @author mleidig@schlund.de
046: */
047: public class WebServiceServlet extends HttpServlet {
048:
049: private static final long serialVersionUID = -5686011510105975584L;
050:
051: private Logger LOG = Logger.getLogger(getClass().getName());
052:
053: private static Object initLock = new Object();
054: private ServiceRuntime runtime;
055:
056: private AdminWebapp adminWebapp;
057:
058: public void init(ServletConfig config) throws ServletException {
059: super .init(config);
060: synchronized (initLock) {
061: runtime = (ServiceRuntime) getServletContext()
062: .getAttribute(ServiceRuntime.class.getName());
063: if (runtime == null) {
064: String servletProp = config
065: .getInitParameter(Constants.PROP_SERVLET_FILE);
066: if (servletProp != null) {
067: FileResource wsConfFile = ResourceUtil
068: .getFileResourceFromDocroot(servletProp);
069: try {
070: Configuration srvConf = ConfigurationReader
071: .read(wsConfFile);
072: if (srvConf.getGlobalServiceConfig()
073: .getContextName() == null
074: && config
075: .getInitParameter(Constants.PROP_CONTEXT_NAME) != null) {
076: srvConf
077: .getGlobalServiceConfig()
078: .setContextName(
079: config
080: .getInitParameter(Constants.PROP_CONTEXT_NAME));
081: }
082: runtime = new ServiceRuntime();
083: runtime.setConfiguration(srvConf);
084: runtime
085: .setApplicationServiceRegistry(new ServiceRegistry(
086: runtime.getConfiguration(),
087: ServiceRegistry.RegistryType.APPLICATION));
088: runtime
089: .addServiceProcessor(
090: Constants.PROTOCOL_TYPE_SOAP,
091: new JAXWSProcessor(
092: getServletContext()));
093: URL metaURL = srvConf.getGlobalServiceConfig()
094: .getDefaultBeanMetaDataURL();
095: runtime.addServiceProcessor(
096: Constants.PROTOCOL_TYPE_JSONWS,
097: new JSONWSProcessor(metaURL));
098: runtime.addServiceProcessor(
099: Constants.PROTOCOL_TYPE_JSONQX,
100: new JSONQXProcessor(metaURL));
101: runtime.addServiceStubGenerator(
102: Constants.PROTOCOL_TYPE_JSONWS,
103: new JSONWSStubGenerator());
104: getServletContext()
105: .setAttribute(
106: ServiceRuntime.class.getName(),
107: runtime);
108: adminWebapp = new AdminWebapp(runtime);
109: } catch (Exception x) {
110: LOG
111: .error(
112: "Error while initializing ServiceRuntime",
113: x);
114: throw new ServletException(
115: "Error while initializing ServiceRuntime",
116: x);
117: }
118: } else
119: LOG.error("No webservice configuration found!!!");
120: }
121: }
122: }
123:
124: public void doPost(HttpServletRequest req, HttpServletResponse res)
125: throws ServletException, IOException {
126: try {
127: runtime.process(req, res);
128: } catch (Throwable t) {
129: LOG.error("Error while processing webservice request", t);
130: if (!res.isCommitted())
131: throw new ServletException(
132: "Error while processing webservice request.", t);
133: }
134: }
135:
136: public void doGet(HttpServletRequest req, HttpServletResponse res)
137: throws ServletException, IOException {
138: adminWebapp.doGet(req, res);
139: }
140:
141: }
|