From ce6863c6bc0ac494aef9ea78f23c3005270e09ef Mon Sep 17 00:00:00 2001 From: fmoga Date: Tue, 31 May 2011 10:46:24 +0000 Subject: Code cleanup. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1129609 13f79535-47bb-0310-9956-ffa450edef68 --- .../comet/runtime/CometBindingProviderFactory.java | 5 +- .../comet/runtime/CometCallbackInvoker.java | 80 +++++++++ .../sca/binding/comet/runtime/CometInvoker.java | 67 -------- .../runtime/CometReferenceBindingProvider.java | 15 +- .../comet/runtime/CometServiceBindingProvider.java | 83 +++++----- .../sca/binding/comet/runtime/ServletFactory.java | 183 +++++++++++---------- .../comet/runtime/callback/CometCallback.java | 12 +- .../sca/binding/comet/runtime/callback/Status.java | 24 ++- .../comet/runtime/handler/CometBindingHandler.java | 92 +++++++---- .../runtime/javascript/JavascriptGenerator.java | 44 ++--- .../runtime/javascript/JavascriptResource.java | 6 +- .../runtime/manager/CometEndpointManager.java | 51 ++++-- .../runtime/manager/CometOperationManager.java | 51 ++++-- .../comet/runtime/manager/CometSessionManager.java | 21 +++ 14 files changed, 432 insertions(+), 302 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java delete mode 100644 sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java (limited to 'sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java') diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java index fc5fd9f371..f6c393dcb4 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java @@ -35,13 +35,10 @@ import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; public class CometBindingProviderFactory implements BindingProviderFactory { /** - * Underlying servlet host. Injected by constructor. + * Underlying servlet host. */ private final ServletHost servletHost; - /** - * Constructor. - */ public CometBindingProviderFactory(final ExtensionPointRegistry extensionPoints) { this.servletHost = ServletHostHelper.getServletHost(extensionPoints); } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java new file mode 100644 index 0000000000..c3733e6dfe --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.binding.comet.runtime; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.binding.comet.runtime.callback.Status; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometSessionManager; +import org.apache.tuscany.sca.core.invocation.Constants; +import org.apache.tuscany.sca.core.invocation.impl.MessageImpl; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.atmosphere.cpr.Broadcaster; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * Receives callback invocations and sends messages back to the browser. + */ +public class CometCallbackInvoker implements Invoker { + + /** + * JSON converter + */ + private static Gson gson = new GsonBuilder().serializeNulls().create(); + + protected Operation operation; + protected EndpointReference endpoint; + + public CometCallbackInvoker(final Operation operation, final EndpointReference endpoint) { + this.operation = operation; + this.endpoint = endpoint; + } + + /** + * Sends message back to the browser client and reports connection status to + * the runtime. + * + * @param msg + * message to send to the browser + * @return the connection status + */ + @Override + public Message invoke(final Message msg) { + String sessionId = (String) msg.getHeaders().get(Constants.RELATES_TO); + Broadcaster broadcaster = CometSessionManager.get(sessionId); + Message response = new MessageImpl(); + if (broadcaster == null) { + response.setBody(Status.CLIENT_DISCONNECTED); + } else if (broadcaster.getAtmosphereResources().isEmpty()) { + CometSessionManager.remove(sessionId); + response.setBody(Status.CLIENT_DISCONNECTED); + } else { + String callbackMethod = msg.getTo().getURI(); + Object[] body = msg.getBody(); + broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + gson.toJson(body[0]) + "'))"); + response.setBody(Status.OK); + } + return response; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java deleted file mode 100644 index 1835c062c3..0000000000 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.sca.binding.comet.runtime; - -import org.apache.tuscany.sca.assembly.EndpointReference; -import org.apache.tuscany.sca.binding.comet.runtime.callback.Status; -import org.apache.tuscany.sca.binding.comet.runtime.manager.CometSessionManager; -import org.apache.tuscany.sca.core.invocation.Constants; -import org.apache.tuscany.sca.core.invocation.impl.MessageImpl; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.atmosphere.cpr.Broadcaster; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public class CometInvoker implements Invoker { - - private static Gson gson = new GsonBuilder().serializeNulls().create(); - - protected Operation operation; - protected EndpointReference endpoint; - - public CometInvoker(final Operation operation, final EndpointReference endpoint) { - this.operation = operation; - this.endpoint = endpoint; - } - - @Override - public Message invoke(final Message msg) { - String sessionId = (String) msg.getHeaders().get(Constants.RELATES_TO); - Broadcaster broadcaster = CometSessionManager.get(sessionId); - Message response = new MessageImpl(); - if (broadcaster == null) { - response.setBody(Status.CLIENT_DISCONNECTED); - } else if (broadcaster.getAtmosphereResources().isEmpty()) { - CometSessionManager.remove(sessionId); - response.setBody(Status.CLIENT_DISCONNECTED); - } else { - String callbackMethod = msg.getTo().getURI(); - Object[] body = msg.getBody(); - broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + gson.toJson(body[0]) + "'))"); - response.setBody(Status.OK); - } - return response; - - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java index 4002f29a0d..9777b0c82c 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java @@ -26,8 +26,9 @@ import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.provider.ReferenceBindingProvider; /** - * Provider for references that have comet binding specified in the scdl. Not - * used as comet binding references would occur in client browser's Javascript. + * Provider for references and callbacks that have comet binding specified in + * the scdl. Used by callbacks to create invokers. Not used for comet + * references as they are javascript proxies not Java objects. */ public class CometReferenceBindingProvider implements ReferenceBindingProvider { @@ -42,19 +43,13 @@ public class CometReferenceBindingProvider implements ReferenceBindingProvider { @Override public Invoker createInvoker(final Operation operation) { - return new CometInvoker(operation, this.endpoint); + return new CometCallbackInvoker(operation, endpoint); } - /** - * No behavior. - */ @Override public void start() { } - /** - * No behavior. - */ @Override public void stop() { } @@ -66,7 +61,7 @@ public class CometReferenceBindingProvider implements ReferenceBindingProvider { @Override public boolean supportsOneWayInvocation() { - return true; + return false; } } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java index 6c1d002575..58f422c01b 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -36,50 +36,53 @@ import org.apache.tuscany.sca.runtime.RuntimeEndpoint; */ public class CometServiceBindingProvider implements ServiceBindingProvider { - private final RuntimeEndpoint endpoint; - private final ServletHost servletHost; + private RuntimeEndpoint endpoint; + private ServletHost servletHost; - public CometServiceBindingProvider(final RuntimeEndpoint endpoint, final ServletHost servletHost) { - this.endpoint = endpoint; - this.servletHost = servletHost; - } + public CometServiceBindingProvider(final RuntimeEndpoint endpoint, final ServletHost servletHost) { + this.endpoint = endpoint; + this.servletHost = servletHost; + } - @Override - public void start() { - String deployedURI = ServletFactory.registerServlet(this.servletHost); - endpoint.setDeployedURI(deployedURI); - final ComponentService service = this.endpoint.getService(); - final Interface serviceInterface = service.getInterfaceContract().getInterface(); - JavascriptGenerator.generateServiceProxy(service); - for (final Operation operation : serviceInterface.getOperations()) { - final String url = "/" + endpoint.getService().getName() + "/" + operation.getName(); - CometEndpointManager.add(url, endpoint); - CometOperationManager.add(url, operation); - JavascriptGenerator.generateMethodProxy(service, operation); - } - } + /** + * Init the comet binding server-side infrastructure. + */ + @Override + public void start() { + String deployedURI = ServletFactory.registerServlet(this.servletHost); + endpoint.setDeployedURI(deployedURI); + final ComponentService service = this.endpoint.getService(); + final Interface serviceInterface = service.getInterfaceContract().getInterface(); + JavascriptGenerator.generateServiceProxy(service); + for (final Operation operation : serviceInterface.getOperations()) { + final String url = "/" + endpoint.getService().getName() + "/" + operation.getName(); + CometEndpointManager.add(url, endpoint); + CometOperationManager.add(url, operation); + JavascriptGenerator.generateMethodProxy(service, operation); + } + } - /** - * This method is used to stop the provider. - */ - @Override - public void stop() { - ServletFactory.unregisterServlet(this.servletHost); - CometEndpointManager.clear(); - CometOperationManager.clear(); - CometSessionManager.clear(); - } + /** + * Stop the comet binding server-side infrastructure. + */ + @Override + public void stop() { + ServletFactory.unregisterServlet(this.servletHost); + CometEndpointManager.clear(); + CometOperationManager.clear(); + CometSessionManager.clear(); + } - @Override - public InterfaceContract getBindingInterfaceContract() { - return endpoint.getService().getInterfaceContract(); - } + @Override + public InterfaceContract getBindingInterfaceContract() { + return endpoint.getService().getInterfaceContract(); + } - @Override - public boolean supportsOneWayInvocation() { - // set to false so the runtime will add a nonBlocking interceptor to - // handle @OneWay calls - return false; - } + @Override + public boolean supportsOneWayInvocation() { + // set to false so the runtime will add a nonBlocking interceptor to + // handle @OneWay calls + return false; + } } 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 3d32c7ad4e..a334eaf006 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 @@ -23,93 +23,106 @@ import org.apache.tuscany.sca.host.http.ServletHost; import org.atmosphere.cpr.AtmosphereServlet; /** - * This class is used to create two servlets: one exposing all the comet - * services, the other one exposing the javascript toolkit. Exposing all comet - * services through a single servlet is needed as the browsers are undergone by - * the two http connection limit so all comet services should send their - * responses via the same http connection to the same client. Dispatching to the - * corresponding endpoint and operation is done internally using Jersey RESTful - * Web Services integration with the AtmosphereServlet. The Javascript toolkit - * servlet is unique as it is not tied to any of the services - it offers a - * global API. + * This class adds two servlets to the runtime: one exposing all the comet + * services, the other one exposing the javascript toolkit. + * + * Exposing all comet services through a single servlet is needed as browsers + * are undergone by the two http connection limit per domain so all comet + * services should send their responses via the same http connection to a single + * client. + * + * Dispatching to the corresponding endpoint and operation is done internally + * using Jersey RESTful Web Services integration with the AtmosphereServlet. + * + * The Javascript toolkit is not tied to any of the services so it is exposed by + * a separate servlet. */ 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"; - - /** - * 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 = "/tuscany-comet-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() { - } - - public static synchronized String registerServlet(final ServletHost servletHost) { - String uri = registerCometServlet(servletHost); - registerJavascriptServlet(servletHost); - return uri; - } - - private static String registerCometServlet(ServletHost servletHost) { - if (ServletFactory.cometServlet == null) { - ServletFactory.cometServlet = new AtmosphereServlet(); - ServletFactory.cometServlet.addInitParameter(ServletFactory.PACKAGE_KEY, ServletFactory.PACKAGE_VALUE); - String uri = servletHost.addServletMapping(ServletFactory.PATH, ServletFactory.cometServlet); - return uri; - } - return null; - } - - 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); - } - } - - public static 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 HANDLER_PACKAGE = "org.apache.tuscany.sca.binding.comet.runtime.handler"; + + /** + * Package of the class handling Javascript toolkit retrieval. + */ + private static final String JS_PACKAGE = "org.apache.tuscany.sca.binding.comet.runtime.javascript"; + + /** + * 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 = "/tuscany-comet-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; + + /** + * Prevent instantiation of singleton class. + */ + private ServletFactory() { + } + + /** + * Adds servlets to the underlying servlet host. No need for thread safety + * as calls to this method are sequentially done for each comet endpoint + * found by the runtime. + * + * @param servletHost + * underlying servlet host + * @return uri where servlet has been mapped + */ + public static String registerServlet(final ServletHost servletHost) { + String uri = registerCometServlet(servletHost); + registerJavascriptServlet(servletHost); + return uri; + } + + private static String registerCometServlet(ServletHost servletHost) { + if (ServletFactory.cometServlet == null) { + ServletFactory.cometServlet = new AtmosphereServlet(); + ServletFactory.cometServlet.addInitParameter(ServletFactory.PACKAGE_KEY, ServletFactory.HANDLER_PACKAGE); + String uri = servletHost.addServletMapping(ServletFactory.PATH, ServletFactory.cometServlet); + return uri; + } + return null; + } + + private static void registerJavascriptServlet(ServletHost servletHost) { + if (ServletFactory.javascriptServlet == null) { + ServletFactory.javascriptServlet = new AtmosphereServlet(); + ServletFactory.javascriptServlet.addInitParameter(ServletFactory.PACKAGE_KEY, ServletFactory.JS_PACKAGE); + servletHost.addServletMapping(ServletFactory.JS_PATH, ServletFactory.javascriptServlet); + } + } + + /** + * Removes servlets from the servlet host. + * + * @param servletHost + * the underlying servlet host. + */ + public static void unregisterServlet(final ServletHost servletHost) { + servletHost.removeServletMapping(ServletFactory.PATH); + servletHost.removeServletMapping(ServletFactory.JS_PATH); + } } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java index cf457e9d7b..1deefb2afa 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java @@ -21,9 +21,19 @@ package org.apache.tuscany.sca.binding.comet.runtime.callback; import org.oasisopen.sca.annotation.Remotable; +/** + * The comet callback interface. + */ @Remotable public interface CometCallback { - Status sendMessage(Object message); + /** + * Send message back to the browser client. + * + * @param message + * message to send + * @return status of the underlying connection with the browser + */ + Status sendMessage(Object message); } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java index 6c29d0fa2e..6b479a144a 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java @@ -1,5 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.tuscany.sca.binding.comet.runtime.callback; +/** + * Status of the connection with the browser client. + */ public enum Status { - OK, CLIENT_DISCONNECTED + OK, CLIENT_DISCONNECTED } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java index c2d2181814..9035c2b599 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java @@ -49,15 +49,27 @@ import org.atmosphere.jersey.SuspendResponse; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +/** + * Handles requests for comet services and for creating a persistent connection. + */ @Path("/") public class CometBindingHandler { + /** + * JSON converter + */ private static Gson gson = new GsonBuilder().serializeNulls().create(); + /** + * Suspends the current HTTP connection. + * + * @param sessionId + * session id to identify client + * @return a response that is not committed, just flushed + */ @GET @Path("/connect") public SuspendResponse connect(@QueryParam("sessionId") String sessionId) { - System.out.println("-- connect -- Session Id: " + sessionId); Broadcaster broadcaster = CometSessionManager.get(sessionId); if (broadcaster == null) { broadcaster = new JerseyBroadcaster(sessionId); @@ -67,12 +79,27 @@ public class CometBindingHandler { .build(); } + /** + * Handles requests for service operations. + * + * @param service + * the service to invoke + * @param method + * the method to invoke + * @param sessionId + * the client session id + * @param callbackMethod + * the callbackMethod to invoke once a response is available + * @param jsonData + * method arguments sent by the client in JSON format + * @throws InvocationTargetException + * if a problem occurs while invoking the service implementation + */ @POST @Path("/{service}/{method}") public void handleRequest(@PathParam("service") String service, @PathParam("method") String method, @FormParam("sessionId") String sessionId, @FormParam("callbackMethod") String callbackMethod, @FormParam("params") String jsonData) throws InvocationTargetException { - System.out.println("-- handleRequest -- Session Id: " + sessionId); String url = "/" + service + "/" + method; RuntimeEndpoint wire = CometEndpointManager.get(url); Operation operation = CometOperationManager.get(url); @@ -93,32 +120,18 @@ public class CometBindingHandler { } /** - * Convert request parameters from JSON to operation parameter types. - * - * @param jsonData - * @param operation - * @return - */ - private Object[] decodeJsonDataForOperation(String jsonData, Operation operation) { - Object[] args = new Object[operation.getInputType().getLogical().size()]; - final String[] json = this.parseArray(jsonData); - int index = 0; - // convert each argument to the corresponding class - for (final DataType dataType : operation.getInputType().getLogical()) { - args[index] = gson.fromJson(json[index], dataType.getPhysical()); - index++; - } - return args; - } - - /** - * Creates the message to be sent with a mocked EndpointReference in the - * 'from' field as the request comes from a browser (there is no actual - * comet reference running in a controlled environment). + * Creates a message with a mocked EndpointReference in the 'from' field to + * simulate a comet reference (because requests are coming from browsers). + * This is needed by the callback mechanism to have a source for the + * request. * * @param args + * arguments for the method invocation + * @param sessionId + * the session id of the client * @param callbackMethod - * @return + * method to call once a response is available + * @return an invocation message */ private Message createMessageWithMockedCometReference(Object[] args, String sessionId, String callbackMethod) { Message msg = new MessageImpl(); @@ -133,17 +146,37 @@ public class CometBindingHandler { } /** - * Parse the JSON array containing the arguments for the method call in + * Convert request parameters from JSON to operation parameter types. + * + * @param jsonData + * parameters in JSON array format + * @param operation + * the operation to invoke + * @return an array of objects + */ + private Object[] decodeJsonDataForOperation(String jsonData, Operation operation) { + Object[] args = new Object[operation.getInputType().getLogical().size()]; + final String[] json = this.parseArray(jsonData); + int index = 0; + for (final DataType dataType : operation.getInputType().getLogical()) { + args[index] = gson.fromJson(json[index], dataType.getPhysical()); + index++; + } + return args; + } + + /** + * Split the JSON array containing the arguments for the method call in * order to avoid converting JSON to Object[]. Converting each object * separately to it's corresponding type avoids type mismatch problems at * service invocation. * * @param jsonArray * the JSON array - * @return an array of JSON formatted objects + * @return an array of JSON formatted strings */ - private String[] parseArray(final String jsonArray) { - final List objects = new ArrayList(); + private String[] parseArray(String jsonArray) { + List objects = new ArrayList(); int bracketNum = 0; int parNum = 0; int startPos = 1; @@ -168,7 +201,6 @@ public class CometBindingHandler { } } } - // add last object objects.add(jsonArray.substring(startPos, jsonArray.length() - 1)); return objects.toArray(new String[] {}); } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java index b1205596ea..ecf470da2d 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java @@ -23,7 +23,7 @@ import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.interfacedef.Operation; /** - * This class generates proxies for the comet services. + * Generates javascript proxies for the comet services. */ public class JavascriptGenerator { @@ -42,50 +42,38 @@ public class JavascriptGenerator { */ private static final String TUSCANY_COMET = "SCA.TuscanyComet"; - /** - * Generated Javascript. - */ private static StringBuffer javascript = new StringBuffer(); - /** - * Default constructor for utility class. - */ private JavascriptGenerator() { } - /** - * Getter for the generated Javascript. - * - * @return the generated Javascript - */ public static StringBuffer getJavascript() { return JavascriptGenerator.javascript; } /** - * Generates the proxy for a service. + * Generates the javascript proxy for a service. * - * @param service the service for which generation is performed + * @param service + * the service for which generation is performed */ public static void generateServiceProxy(final ComponentService service) { - JavascriptGenerator.javascript.append(JavascriptGenerator.COMPONENT_CONTEXT + "." - + service.getName() - + " = new Object();\n"); + JavascriptGenerator.javascript.append(JavascriptGenerator.COMPONENT_CONTEXT + "." + service.getName() + + " = new Object();\n"); } /** * Generates the method inside the service proxy for the specified * operation. * - * @param service the service containing the operation - * @param operation the operation + * @param service + * the service containing the operation + * @param operation + * the operation */ public static void generateMethodProxy(final ComponentService service, final Operation operation) { - JavascriptGenerator.javascript.append(JavascriptGenerator.COMPONENT_CONTEXT + "." - + service.getName() - + "." - + operation.getName() - + " = function("); + JavascriptGenerator.javascript.append(JavascriptGenerator.COMPONENT_CONTEXT + "." + service.getName() + "." + + operation.getName() + " = function("); for (int i = 0; i < operation.getInputType().getLogical().size(); i++) { JavascriptGenerator.javascript.append("p" + i + ", "); } @@ -95,12 +83,8 @@ public class JavascriptGenerator { for (int i = 0; i < operation.getInputType().getLogical().size(); i++) { JavascriptGenerator.javascript.append(" params.push(p" + i + ");\n"); } - JavascriptGenerator.javascript.append(" " + JavascriptGenerator.TUSCANY_COMET - + ".callAsync('" - + service.getName() - + "/" - + operation.getName() - + "', $.toJSON(params), callbackMethod);\n"); + JavascriptGenerator.javascript.append(" " + JavascriptGenerator.TUSCANY_COMET + ".callAsync('" + + service.getName() + "/" + operation.getName() + "', $.toJSON(params), callbackMethod);\n"); JavascriptGenerator.javascript.append("}\n"); } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java index 6131cec7cd..06f7bfb546 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java @@ -28,7 +28,7 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; /** - * Class serving the calls performed to retrieve the Javascript toolkit. + * Handles calls for the retrieving the Javascript toolkit. */ @Path("/") @Produces("text/javascript") @@ -49,15 +49,13 @@ public class JavascriptResource { @Path("/org.apache.tuscany.sca.CometComponentContext.js") public InputStream getJavascript() { InputStream stream = null; - // add dependencies in the specified order - for (final String dependency : JavascriptResource.DEPENDENCIES) { + for (String dependency : JavascriptResource.DEPENDENCIES) { if (stream == null) { stream = this.getClass().getResourceAsStream(dependency); } else { stream = new SequenceInputStream(stream, this.getClass().getResourceAsStream(dependency)); } } - // add generated proxies final String generatedJs = JavascriptGenerator.getJavascript().toString() + "\n}"; return new SequenceInputStream(stream, new ByteArrayInputStream(generatedJs.getBytes())); } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java index 9fe0353f03..3d9707ca8e 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.apache.tuscany.sca.binding.comet.runtime.manager; import java.util.concurrent.ConcurrentHashMap; @@ -5,26 +23,29 @@ import java.util.concurrent.ConcurrentMap; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +/** + * Manager for Tuscany comet endpoints. This is a thread-safe singleton class. + */ public class CometEndpointManager { - private static final ConcurrentMap endpoints = new ConcurrentHashMap(); + private static final ConcurrentMap endpoints = new ConcurrentHashMap(); - private CometEndpointManager() { - } + private CometEndpointManager() { + } - public static void add(String url, RuntimeEndpoint endpoint) { - endpoints.put(url, endpoint); - } + public static void add(String url, RuntimeEndpoint endpoint) { + endpoints.put(url, endpoint); + } - public static RuntimeEndpoint get(String url) { - return endpoints.get(url); - } + public static RuntimeEndpoint get(String url) { + return endpoints.get(url); + } - public static void remove(String url) { - endpoints.remove(url); - } + public static void remove(String url) { + endpoints.remove(url); + } - public static void clear() { - endpoints.clear(); - } + public static void clear() { + endpoints.clear(); + } } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java index 3179603b96..ea6657e786 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.apache.tuscany.sca.binding.comet.runtime.manager; import java.util.concurrent.ConcurrentHashMap; @@ -5,26 +23,29 @@ import java.util.concurrent.ConcurrentMap; import org.apache.tuscany.sca.interfacedef.Operation; +/** + * Manager for Tuscany comet operations. This is a thread-safe singleton class. + */ public class CometOperationManager { - private static final ConcurrentMap operations = new ConcurrentHashMap(); + private static final ConcurrentMap operations = new ConcurrentHashMap(); - private CometOperationManager() { - } + private CometOperationManager() { + } - public static void add(String url, Operation operation) { - operations.put(url, operation); - } + public static void add(String url, Operation operation) { + operations.put(url, operation); + } - public static Operation get(String url) { - return operations.get(url); - } + public static Operation get(String url) { + return operations.get(url); + } - public static void remove(String url) { - operations.remove(url); - } + public static void remove(String url) { + operations.remove(url); + } - public static void clear() { - operations.clear(); - } + public static void clear() { + operations.clear(); + } } diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java index a34a3615d5..59fe8d8f2d 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.apache.tuscany.sca.binding.comet.runtime.manager; import java.util.concurrent.ConcurrentHashMap; @@ -5,6 +23,9 @@ import java.util.concurrent.ConcurrentMap; import org.atmosphere.cpr.Broadcaster; +/** + * Manager for Tuscany comet sessions. This is a thread-safe singleton class. + */ public class CometSessionManager { private static final ConcurrentMap broadcasters = new ConcurrentHashMap(); -- cgit v1.2.3