diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java')
-rw-r--r-- | sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java | 230 |
1 files changed, 116 insertions, 114 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java index c1244b3183..ed40ccd4f9 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java @@ -19,9 +19,6 @@ package org.apache.tuscany.sca.binding.comet.runtime; -import java.util.HashMap; -import java.util.Map; - import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; @@ -40,116 +37,121 @@ import org.atmosphere.cpr.AtmosphereServlet; */ public final class ServletFactory { - /** - * Init-param key for the AtmosphereServlet defining where to look for - * Jersey classes. - */ - private static final String PACKAGE_KEY = "com.sun.jersey.config.property.packages"; - - /** - * Package of the class handling dispatching to endpoints. - */ - private static final String PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.handler"; - - /** - * Package of the class handling Javascript toolkit retrieval. - */ - private static final String JS_PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.javascript"; - - /** - * Property in the ServletContext where endpoints are added incrementally as - * the Tuscany runtime calls the CometServiceBindingProvider for each comet - * service. - */ - public static final String ENDPOINTS_KEY = "org.apache.tuscany.sca.binding.comet.endpoints"; - - /** - * Property in the ServletContext where operations are added incrementally - * as the CometServiceBindingProvider is calling the registerServlet method - * for each comet service method. - */ - public static final String OPERATIONS_KEY = "org.apache.tuscany.sca.binding.comet.operations"; - - /** - * Path where services will be exposed. - */ - public static final String PATH = "/tuscany-comet/*"; - - /** - * Path where Javascript toolkit will be exposed. - */ - public static final String JS_PATH = "/org.apache.tuscany.sca.cometComponentContext.js/*"; - - /** - * The servlet that is exposing the comet services. - */ - private static AtmosphereServlet cometServlet = null; - - /** - * The servlet that is exposing the Javascript toolkit. - */ - private static AtmosphereServlet javascriptServlet = null; - - /** - * Private constructor for the singleton class. - */ - private ServletFactory() { - } - - /** - * Method called by CometServiceBindingProvider for each endpoint in order - * to create the two singleton servlets. - * - * @param servletHost the underlying servlet host - */ - public static synchronized void registerServlet(final ServletHost servletHost) { - if (ServletFactory.cometServlet == null) { - ServletFactory.cometServlet = new AtmosphereServlet(); - ServletFactory.cometServlet.addInitParameter(ServletFactory.PACKAGE_KEY, ServletFactory.PACKAGE_VALUE); - servletHost.addServletMapping(ServletFactory.PATH, ServletFactory.cometServlet); - // store operations and corresponding endpoint in the ServletContext - // so that they can be retrieved from inside the web service methods - final Map<String, RuntimeEndpoint> endpoints = new HashMap<String, RuntimeEndpoint>(); - ServletFactory.cometServlet.getServletContext().setAttribute(ServletFactory.ENDPOINTS_KEY, endpoints); - final Map<String, Operation> operations = new HashMap<String, Operation>(); - ServletFactory.cometServlet.getServletContext().setAttribute(ServletFactory.OPERATIONS_KEY, operations); - } - if (ServletFactory.javascriptServlet == null) { - ServletFactory.javascriptServlet = new AtmosphereServlet(); - ServletFactory.javascriptServlet.addInitParameter(ServletFactory.PACKAGE_KEY, - ServletFactory.JS_PACKAGE_VALUE); - servletHost.addServletMapping(ServletFactory.JS_PATH, ServletFactory.javascriptServlet); - } - } - - /** - * Method called by CometServiceBindingProvider for each endpoint operation - * in order to store all the operations the servlet will serve. - * - * @param endpoint the endpoint - * @param operation the operation - */ - public static synchronized void addOperation(final RuntimeEndpoint endpoint, final Operation operation) { - final String url = "/" + endpoint.getService().getName() + "/" + operation.getName(); - final Map<String, RuntimeEndpoint> endpoints = - (Map<String, RuntimeEndpoint>)ServletFactory.cometServlet.getServletContext() - .getAttribute(ServletFactory.ENDPOINTS_KEY); - endpoints.put(url, endpoint); - final Map<String, Operation> operations = - (Map<String, Operation>)ServletFactory.cometServlet.getServletContext() - .getAttribute(ServletFactory.OPERATIONS_KEY); - operations.put(url, operation); - } - - /** - * Method called by CometServiceBindingProvider for each endpoint operation - * in order to remove the two servlets. - * - * @param servletHost the underlying servlet host - */ - public static synchronized void unregisterServlet(final ServletHost servletHost) { - servletHost.removeServletMapping(ServletFactory.PATH); - servletHost.removeServletMapping(ServletFactory.JS_PATH); - } + /** + * Init-param key for the AtmosphereServlet defining where to look for + * Jersey classes. + */ + private static final String PACKAGE_KEY = "com.sun.jersey.config.property.packages"; + + /** + * Package of the class handling dispatching to endpoints. + */ + private static final String PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.handler"; + + /** + * Package of the class handling Javascript toolkit retrieval. + */ + private static final String JS_PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime.javascript"; + + /** + * Key in the ServletContext where the comet component context is stored. + */ + public static final String COMET_COMPONENT_CONTEXT_KEY = "org.apache.tuscany.sca.binding.comet.operations"; + + /** + * Path where services will be exposed. + */ + public static final String PATH = "/tuscany-comet/*"; + + /** + * Path where Javascript toolkit will be exposed. + */ + public static final String JS_PATH = "/org.apache.tuscany.sca.cometComponentContext.js/*"; + + /** + * The servlet that is exposing the comet services. + */ + private static AtmosphereServlet cometServlet = null; + + /** + * The servlet that is exposing the Javascript toolkit. + */ + private static AtmosphereServlet javascriptServlet = null; + + /** + * Private constructor for the singleton class. + */ + private ServletFactory() { + } + + /** + * Method called by CometServiceBindingProvider for each endpoint in order + * to create the two singleton servlets. + * + * @param servletHost + * the underlying servlet host + */ + public static synchronized void registerServlet( + final ServletHost servletHost) { + registerCometServlet(servletHost); + registerJavascriptServlet(servletHost); + } + + private static void registerCometServlet(ServletHost servletHost) { + if (ServletFactory.cometServlet == null) { + ServletFactory.cometServlet = new AtmosphereServlet(); + ServletFactory.cometServlet.addInitParameter( + ServletFactory.PACKAGE_KEY, ServletFactory.PACKAGE_VALUE); + servletHost.addServletMapping(ServletFactory.PATH, + ServletFactory.cometServlet); + final CometComponentContext context = new CometComponentContext(); + ServletFactory.cometServlet.getServletContext().setAttribute( + ServletFactory.COMET_COMPONENT_CONTEXT_KEY, context); + } + } + + private static void registerJavascriptServlet(ServletHost servletHost) { + if (ServletFactory.javascriptServlet == null) { + ServletFactory.javascriptServlet = new AtmosphereServlet(); + ServletFactory.javascriptServlet + .addInitParameter(ServletFactory.PACKAGE_KEY, + ServletFactory.JS_PACKAGE_VALUE); + servletHost.addServletMapping(ServletFactory.JS_PATH, + ServletFactory.javascriptServlet); + } + } + + /** + * Method called by CometServiceBindingProvider for each endpoint operation + * in order to store all the operations the servlet will serve. + * + * @param endpoint + * the endpoint + * @param operation + * the operation + */ + public static void registerOperation(final RuntimeEndpoint endpoint, + final Operation operation) { + final String url = "/" + endpoint.getService().getName() + "/" + + operation.getName(); + CometComponentContext context = (CometComponentContext) cometServlet + .getServletContext().getAttribute(COMET_COMPONENT_CONTEXT_KEY); + context.addEndpoint(url, endpoint); + context.addOperation(url, operation); + } + + /** + * Method called by CometServiceBindingProvider for each endpoint operation + * in order to remove the two servlets. + * + * @param servletHost + * the underlying servlet host + */ + public static void unregisterServlet(final ServletHost servletHost) { + synchronized (servletHost) { + servletHost.removeServletMapping(ServletFactory.PATH); + servletHost.removeServletMapping(ServletFactory.JS_PATH); + } + } } |