diff options
22 files changed, 481 insertions, 424 deletions
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml b/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml index 8fa336cbb9..b7d9e45a8d 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/pom.xml @@ -77,10 +77,20 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.apache.tuscany.sca</groupId> + <artifactId>tuscany-contribution</artifactId> + <version>2.0-SNAPSHOT</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>1.4</version> - <scope>compile</scope> + </dependency> + <dependency> + <groupId>javax</groupId> + <artifactId>javaee-web-api</artifactId> + <version>6.0</version> </dependency> </dependencies> diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java index 6e5cc28abf..fc5fd9f371 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java @@ -37,31 +37,36 @@ public class CometBindingProviderFactory implements BindingProviderFactory<Comet /** * Underlying servlet host. Injected by constructor. */ - private ServletHost servletHost; + private final ServletHost servletHost; /** * Constructor. */ - public CometBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + public CometBindingProviderFactory(final ExtensionPointRegistry extensionPoints) { this.servletHost = ServletHostHelper.getServletHost(extensionPoints); } + @Override public Class<CometBinding> getModelType() { return CometBinding.class; } /** - * Creates a provider for a reference that has comet binding specified in the scdl. + * Creates a provider for a reference that has comet binding specified in + * the scdl. */ - public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpoint) { + @Override + public ReferenceBindingProvider createReferenceBindingProvider(final RuntimeEndpointReference endpoint) { return new CometReferenceBindingProvider(endpoint); } /** - * Creates a provider for a service that has comet binding specified in the scdl. + * Creates a provider for a service that has comet binding specified in the + * scdl. */ - public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { - return new CometServiceBindingProvider(endpoint, servletHost); + @Override + public ServiceBindingProvider createServiceBindingProvider(final RuntimeEndpoint endpoint) { + return new CometServiceBindingProvider(endpoint, this.servletHost); } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java index 9a7679ba3b..9353571cb1 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java @@ -30,10 +30,23 @@ import org.apache.tuscany.sca.invocation.Message; */ public class CometInvoker implements Invoker { + /** + * The invoked operation. + */ protected Operation operation; + + /** + * The endpoint to which the operation belongs. + */ protected EndpointReference endpoint; - public CometInvoker(Operation operation, EndpointReference endpoint) { + /** + * Default constructor. + * + * @param operation the operation + * @param endpoint the endpoint + */ + public CometInvoker(final Operation operation, final EndpointReference endpoint) { this.operation = operation; this.endpoint = endpoint; } @@ -41,7 +54,8 @@ public class CometInvoker implements Invoker { /** * No behavior. */ - public Message invoke(Message msg) { + @Override + public Message invoke(final Message msg) { return null; } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java index 47bdbf9aab..17470e3738 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java @@ -26,37 +26,45 @@ 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 that have comet binding specified in the scdl. Not + * used as comet binding references would occur in client browser's Javascript. */ public class CometReferenceBindingProvider implements ReferenceBindingProvider { - private EndpointReference endpoint; + /** + * Endpoint for which the binding provider is created. + */ + private final EndpointReference endpoint; - public CometReferenceBindingProvider(EndpointReference endpoint) { + public CometReferenceBindingProvider(final EndpointReference endpoint) { this.endpoint = endpoint; } - - public Invoker createInvoker(Operation operation) { - return new CometInvoker(operation, endpoint); + + @Override + public Invoker createInvoker(final Operation operation) { + return new CometInvoker(operation, this.endpoint); } /** * No behavior. */ + @Override public void start() { } /** * No behavior. */ + @Override public void stop() { } + @Override public InterfaceContract getBindingInterfaceContract() { return null; } + @Override public boolean supportsOneWayInvocation() { return true; } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java index 8be122d1b8..9c65acaa54 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -33,63 +33,62 @@ import org.apache.tuscany.sca.runtime.RuntimeEndpoint; */ public class CometServiceBindingProvider implements ServiceBindingProvider { - /** - * Service's endpoint. - */ - private RuntimeEndpoint endpoint; + /** + * Service's endpoint. + */ + private final RuntimeEndpoint endpoint; - /** - * The underlying servlet host. - */ - private ServletHost servletHost; + /** + * The underlying servlet host. + */ + private final ServletHost servletHost; - /** - * Constructor. - * - * @param endpoint - * the given endpoint - * @param servletHost - * the given servlet host - */ - public CometServiceBindingProvider(RuntimeEndpoint endpoint, - ServletHost servletHost) { - this.endpoint = endpoint; - this.servletHost = servletHost; - } + /** + * Constructor. + * + * @param endpoint the given endpoint + * @param servletHost the given servlet host + */ + public CometServiceBindingProvider(final RuntimeEndpoint endpoint, final ServletHost servletHost) { + this.endpoint = endpoint; + this.servletHost = servletHost; + } - /** - * This method is used to start the provider. - */ - public void start() { - ComponentService service = endpoint.getService(); - Interface serviceInterface = service.getInterfaceContract() - .getInterface(); - JavascriptGenerator.generateServiceProxy(service); - for (Operation operation : serviceInterface.getOperations()) { - JavascriptGenerator.generateMethodProxy(service, operation); - ServletFactory.registerServlet(servletHost, endpoint, operation); - } - } + /** + * This method is used to start the provider. + */ + @Override + public void start() { + final ComponentService service = this.endpoint.getService(); + final Interface serviceInterface = service.getInterfaceContract().getInterface(); + JavascriptGenerator.generateServiceProxy(service); + for (final Operation operation : serviceInterface.getOperations()) { + JavascriptGenerator.generateMethodProxy(service, operation); + ServletFactory.registerServlet(this.servletHost, this.endpoint, operation); + } + } - /** - * This method is used to stop the provider. - */ - public void stop() { - ComponentService service = endpoint.getService(); - Interface serviceInterface = service.getInterfaceContract() - .getInterface(); - for (Operation op : serviceInterface.getOperations()) { - String path = service.getName() + "/" + op.getName(); - servletHost.removeServletMapping(path); - } - } + /** + * This method is used to stop the provider. + */ + @Override + public void stop() { + final ComponentService service = this.endpoint.getService(); + final Interface serviceInterface = service.getInterfaceContract().getInterface(); + for (final Operation op : serviceInterface.getOperations()) { + final String path = service.getName() + "/" + op.getName(); + this.servletHost.removeServletMapping(path); + } + } - public InterfaceContract getBindingInterfaceContract() { - return null; - } + @Override + public InterfaceContract getBindingInterfaceContract() { + return null; + } - public boolean supportsOneWayInvocation() { - return true; - } + @Override + public boolean supportsOneWayInvocation() { + return true; + } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java index 1002446b65..588702f880 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java @@ -29,112 +29,113 @@ 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. + * 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. */ -public 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; - - /** - * Method called by CometServiceBindingProvider for each endpoint operation - * in order to create the two servlets and keep track of all the endpoints - * and their operations. - * - * @param servletHost - * the underlying servlet host - * @param endpoint - * the endpoint - * @param operation - * the operation - */ - public static synchronized void registerServlet(ServletHost servletHost, - RuntimeEndpoint endpoint, Operation operation) { - if (cometServlet == null) { - cometServlet = new AtmosphereServlet(); - cometServlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE); - servletHost.addServletMapping(PATH, cometServlet); - // store operations and corresponding endpoint in the ServletContext - // so that they can be retrieved from inside the web service methods - Map<String, RuntimeEndpoint> endpoints = new HashMap<String, RuntimeEndpoint>(); - cometServlet.getServletContext().setAttribute(ENDPOINTS_KEY, - endpoints); - Map<String, Operation> operations = new HashMap<String, Operation>(); - cometServlet.getServletContext().setAttribute(OPERATIONS_KEY, - operations); - } - // add current operation to ServletContext - String url = "/" + endpoint.getService().getName() + "/" - + operation.getName(); - Map<String, RuntimeEndpoint> endpoints = (Map<String, RuntimeEndpoint>) cometServlet - .getServletContext().getAttribute(ENDPOINTS_KEY); - endpoints.put(url, endpoint); - Map<String, Operation> operations = (Map<String, Operation>) cometServlet - .getServletContext().getAttribute(OPERATIONS_KEY); - operations.put(url, operation); - - if (javascriptServlet == null) { - javascriptServlet = new AtmosphereServlet(); - javascriptServlet.addInitParameter(PACKAGE_KEY, JS_PACKAGE_VALUE); - servletHost.addServletMapping(JS_PATH, javascriptServlet); - } - } +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 operation + * in order to create the two servlets and keep track of all the endpoints + * and their operations. + * + * @param servletHost the underlying servlet host + * @param endpoint the endpoint + * @param operation the operation + */ + public static synchronized void registerServlet(final ServletHost servletHost, + final RuntimeEndpoint endpoint, + final Operation operation) { + 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); + } + // add current operation to ServletContext + 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); + + 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); + } + } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java index 4d21f48c57..512b834840 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java @@ -54,127 +54,121 @@ import com.sun.jersey.spi.container.servlet.PerSession; @PerSession public class CometBindingHandler { - /** - * The object used to suspend the response and send async responses back to - * client. - */ - private Broadcaster broadcaster; + /** + * The object used to suspend the response and send async responses back to + * client. + */ + private Broadcaster broadcaster; - /** - * The service endpoints corresponding to each operation. - */ - private Map<String, RuntimeEndpoint> endpoints; + /** + * The service endpoints corresponding to each operation. + */ + private Map<String, RuntimeEndpoint> endpoints; - /** - * The comet operations. - */ - private Map<String, Operation> operations; + /** + * The comet operations. + */ + private Map<String, Operation> operations; - /** - * JSON converter. - */ - private Gson gson; + /** + * JSON converter. + */ + private Gson gson; - @Context - private ServletContext sc; + /** + * The underlying servlet context. + */ + @Context + private ServletContext sc; - /** - * Method called at comet connect time. This suspends the response and keeps - * the connection opened. - * - * @return the suspended response - */ - @GET - public SuspendResponse<String> connect() { - broadcaster = new DefaultBroadcaster(); - endpoints = (Map<String, RuntimeEndpoint>) sc - .getAttribute(ServletFactory.ENDPOINTS_KEY); - operations = (Map<String, Operation>) sc - .getAttribute(ServletFactory.OPERATIONS_KEY); - gson = new Gson(); - return new SuspendResponse.SuspendResponseBuilder<String>() - .broadcaster(broadcaster).outputComments(true).build(); - } + /** + * Method called at comet connect time. This suspends the response and keeps + * the connection opened. + * + * @return the suspended response + */ + @GET + public SuspendResponse<String> connect() { + this.broadcaster = new DefaultBroadcaster(); + this.endpoints = (Map<String, RuntimeEndpoint>)this.sc.getAttribute(ServletFactory.ENDPOINTS_KEY); + this.operations = (Map<String, Operation>)this.sc.getAttribute(ServletFactory.OPERATIONS_KEY); + this.gson = new Gson(); + return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(this.broadcaster).outputComments(true) + .build(); + } - /** - * Method called on service calls. - * - * @param service - * service called - * @param method - * operation called - * @param callbackMethod - * the callback method from Javascript - * @param jsonData - * arguments for the method sent as JSON array - * @return object used by the Broadcaster to send response through the - * persisted connection - * @throws InvocationTargetException - * if problems occur at service invocation - */ - @POST - @Path("/{service}/{method}") - @Broadcast - public Broadcastable callAndRespond(@PathParam("service") String service, - @PathParam("method") String method, - @FormParam("callback") String callbackMethod, - @FormParam("params") String jsonData) - throws InvocationTargetException { - String url = "/" + service + "/" + method; - RuntimeEndpoint wire = endpoints.get(url); - Operation operation = operations.get(url); - Object[] args = new Object[operation.getInputType().getLogical().size()]; - String[] json = parseArray(jsonData); - int index = 0; - // convert each argument to the corresponding class - for (DataType<?> dataType : operation.getInputType().getLogical()) { - args[index] = gson.fromJson(json[index], dataType.getPhysical()); - index++; - } - // invoke the service operation - Object response = wire.invoke(operation, args); - return new Broadcastable(callbackMethod + "($.secureEvalJSON('" - + gson.toJson(response) + "'))", "", broadcaster); - } + /** + * Method called on service calls. + * + * @param service service called + * @param method operation called + * @param callbackMethod the callback method from Javascript + * @param jsonData arguments for the method sent as JSON array + * @return object used by the Broadcaster to send response through the + * persisted connection + * @throws InvocationTargetException if problems occur at service invocation + */ + @POST + @Path("/{service}/{method}") + @Broadcast + public Broadcastable callAndRespond(@PathParam("service") final String service, + @PathParam("method") final String method, + @FormParam("callback") final String callbackMethod, + @FormParam("params") final String jsonData) throws InvocationTargetException { + final String url = "/" + service + "/" + method; + final RuntimeEndpoint wire = this.endpoints.get(url); + final Operation operation = this.operations.get(url); + final 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] = this.gson.fromJson(json[index], dataType.getPhysical()); + index++; + } + // invoke the service operation + final Object response = wire.invoke(operation, args); + return new Broadcastable(callbackMethod + "($.secureEvalJSON('" + this.gson.toJson(response) + "'))", "", + this.broadcaster); + } - /** - * Parse 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 - */ - private String[] parseArray(String jsonArray) { - List<String> objects = new ArrayList<String>(); - int bracketNum = 0; - int parNum = 0; - int startPos = 1; - for (int i = 0; i < jsonArray.length(); i++) { - switch (jsonArray.charAt(i)) { - case '{': - bracketNum++; - break; - case '}': - bracketNum--; - break; - case '[': - parNum++; - break; - case ']': - parNum--; - break; - case ',': - if (bracketNum == 0 && parNum == 1) { - objects.add(jsonArray.substring(startPos, i)); - startPos = i + 1; - } - } - } - // add last object - objects.add(jsonArray.substring(startPos, jsonArray.length() - 1)); - return objects.toArray(new String[] {}); - } + /** + * Parse 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 + */ + private String[] parseArray(final String jsonArray) { + final List<String> objects = new ArrayList<String>(); + int bracketNum = 0; + int parNum = 0; + int startPos = 1; + for (int i = 0; i < jsonArray.length(); i++) { + switch (jsonArray.charAt(i)) { + case '{': + bracketNum++; + break; + case '}': + bracketNum--; + break; + case '[': + parNum++; + break; + case ']': + parNum--; + break; + case ',': + if ((bracketNum == 0) && (parNum == 1)) { + objects.add(jsonArray.substring(startPos, i)); + startPos = i + 1; + } + } + } + // add last object + objects.add(jsonArray.substring(startPos, jsonArray.length() - 1)); + return objects.toArray(new String[] {}); + } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java index 4a4cadfeea..b1205596ea 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java @@ -27,72 +27,81 @@ import org.apache.tuscany.sca.interfacedef.Operation; */ public class JavascriptGenerator { - /** - * Namespace for the Tuscany Comet Javascript toolkit. - */ - public static final String JS_NAMESPACE = "SCA"; + /** + * Namespace for the Tuscany Comet Javascript toolkit. + */ + public static final String JS_NAMESPACE = "SCA"; - /** - * Name for the SCA component context. - */ - private static final String COMPONENT_CONTEXT = "this.CometComponentContext"; + /** + * Name for the SCA component context. + */ + private static final String COMPONENT_CONTEXT = "this.CometComponentContext"; - /** - * Name for the object performing comet specific tasks. - */ - private static final String TUSCANY_COMET = "SCA.TuscanyComet"; + /** + * Name for the object performing comet specific tasks. + */ + private static final String TUSCANY_COMET = "SCA.TuscanyComet"; - /** - * Generated Javascript. - */ - private static StringBuffer javascript = new StringBuffer(); + /** + * Generated Javascript. + */ + private static StringBuffer javascript = new StringBuffer(); - /** - * Getter for the generated Javascript. - * - * @return the generated Javascript - */ - public static StringBuffer getJavascript() { - return javascript; - } + /** + * Default constructor for utility class. + */ + private JavascriptGenerator() { + } - /** - * Generates the proxy for a service. - * - * @param service - * the service for which generation is performed - */ - public static void generateServiceProxy(ComponentService service) { - javascript.append(COMPONENT_CONTEXT + "." + service.getName() - + " = new Object();\n"); - } + /** + * Getter for the generated Javascript. + * + * @return the generated Javascript + */ + public static StringBuffer getJavascript() { + return JavascriptGenerator.javascript; + } - /** - * Generates the method inside the service proxy for the specified - * operation. - * - * @param service - * the service containing the operation - * @param operation - * the operation - */ - public static void generateMethodProxy(ComponentService service, - Operation operation) { - javascript.append(COMPONENT_CONTEXT + "." + service.getName() + "." - + operation.getName() + " = function("); - for (int i = 0; i < operation.getInputType().getLogical().size(); i++) { - javascript.append("p" + i + ", "); - } - javascript.append("callbackMethod) {\n"); - // send method argumets as JSON array - javascript.append(" var params = [];\n"); - for (int i = 0; i < operation.getInputType().getLogical().size(); i++) { - javascript.append(" params.push(p" + i + ");\n"); - } - javascript.append(" " + TUSCANY_COMET + ".callAsync('" - + service.getName() + "/" + operation.getName() - + "', $.toJSON(params), callbackMethod);\n"); - javascript.append("}\n"); - } + /** + * Generates the proxy for a service. + * + * @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"); + } + + /** + * Generates the method inside the service proxy for the specified + * 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("); + for (int i = 0; i < operation.getInputType().getLogical().size(); i++) { + JavascriptGenerator.javascript.append("p" + i + ", "); + } + JavascriptGenerator.javascript.append("callbackMethod) {\n"); + // send method argumets as JSON array + JavascriptGenerator.javascript.append(" var params = [];\n"); + 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("}\n"); + } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java index 675b9838ae..4333324377 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java @@ -28,15 +28,15 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; /** - * Class serving the calls performed to retrieve the Javascript toolkit. + * Class serving the calls performed to retrieve the Javascript toolkit. */ @Path("/") @Produces("text/javascript") public class JavascriptResource { - /** - * Dependencies for the Tuscany Comet Javascript API. - */ + /** + * Dependencies for the Tuscany Comet Javascript API. + */ private static final String[] DEPENDENCIES = {"/jquery-1.4.2.min.js", "/jquery.atmosphere.js", "/jquery.json-2.2.min.js", "/cometComponentContext.js"}; @@ -47,19 +47,19 @@ public class JavascriptResource { */ @GET public InputStream getJavascript() { - // add namespace for embedded libraries + // add namespace for embedded libraries InputStream stream = new ByteArrayInputStream(("var " + JavascriptGenerator.JS_NAMESPACE + " = new function() {\n").getBytes()); // add dependencies in the specified order - for (String dependency : DEPENDENCIES) { + for (final String dependency : JavascriptResource.DEPENDENCIES) { if (stream == null) { - stream = getClass().getResourceAsStream(dependency); + stream = this.getClass().getResourceAsStream(dependency); } else { - stream = new SequenceInputStream(stream, getClass().getResourceAsStream(dependency)); + stream = new SequenceInputStream(stream, this.getClass().getResourceAsStream(dependency)); } } // add generated proxies - String generatedJs = JavascriptGenerator.getJavascript().toString() + "\n}"; + final String generatedJs = JavascriptGenerator.getJavascript().toString() + "\n}"; return new SequenceInputStream(stream, new ByteArrayInputStream(generatedJs.getBytes())); } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTestCase.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTestCase.java index 9729d8ef27..e485fd2ca6 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTestCase.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/CometTestCase.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.test; +import junit.framework.Assert; import junit.framework.TestCase; import org.apache.tuscany.sca.node.Contribution; @@ -31,24 +32,22 @@ import org.apache.tuscany.sca.node.NodeFactory; */ public class CometTestCase extends TestCase { - /** - * Test consisting in starting up a node containing services exposed via the - * comet binding. - */ - public void testComet() { - try { - String location = ContributionLocationHelper - .getContributionLocation("test.composite"); - Node node = NodeFactory.newInstance().createNode("test.composite", - new Contribution("c1", location)); - node.start(); - // System.out.println("Press any key to stop the node."); - // System.in.read(); - node.stop(); - } catch (Exception e) { - e.printStackTrace(); - fail(); - } - } + /** + * Test consisting in starting up a node containing services exposed via the + * comet binding. + */ + public void testComet() { + try { + final String location = ContributionLocationHelper.getContributionLocation("test.composite"); + final Node node = NodeFactory.newInstance().createNode("test.composite", new Contribution("c1", location)); + node.start(); + // System.out.println("Press any key to stop the node."); + // System.in.read(); + node.stop(); + } catch (final Exception e) { + e.printStackTrace(); + Assert.fail(); + } + } } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java index 7dbfb07b74..6d23479f30 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockService.java @@ -22,7 +22,7 @@ import org.oasisopen.sca.annotation.Remotable; @Remotable public interface StockService { - + String getQuotes(); } diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java index 80e7a7e94b..359d7dec0b 100644 --- a/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime/src/test/java/org/apache/tuscany/sca/test/StockServiceImpl.java @@ -28,12 +28,12 @@ import org.oasisopen.sca.annotation.Service; public class StockServiceImpl implements StockService { public static final int MAX_VALUE = 1000; - private Random random = new Random(new Date().getTime()); + private final Random random = new Random(new Date().getTime()); @Override public String getQuotes() { - Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE)); + final Double value = Math.abs(this.random.nextDouble() * this.random.nextInt(StockServiceImpl.MAX_VALUE)); return "ASF" + "#" + Double.valueOf(new DecimalFormat("#.##").format(value)); } - + } diff --git a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBinding.java b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBinding.java index 471224ad73..33db265a49 100644 --- a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBinding.java +++ b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBinding.java @@ -21,13 +21,14 @@ package org.apache.tuscany.sca.binding.comet; import javax.xml.namespace.QName; +import org.apache.tuscany.sca.assembly.Base; import org.apache.tuscany.sca.assembly.Binding; /** - * Comet Binding model + * Comet Binding model. */ public interface CometBinding extends Binding { - QName TYPE = new QName(SCA11_TUSCANY_NS, "binding.comet"); + QName TYPE = new QName(Base.SCA11_TUSCANY_NS, "binding.comet"); } diff --git a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBindingFactory.java b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBindingFactory.java index a6bf7a0609..cf90b13bf6 100644 --- a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBindingFactory.java +++ b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/CometBindingFactory.java @@ -20,13 +20,13 @@ package org.apache.tuscany.sca.binding.comet; /** - * Factory to create CometBinding instances + * Factory to create CometBinding instances. */ public interface CometBindingFactory { /** - * Create CometBinding model + * Create CometBinding model. */ CometBinding createCometBinding(); - + } diff --git a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingFactoryImpl.java b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingFactoryImpl.java index 72034c5a46..067020ff7a 100644 --- a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingFactoryImpl.java +++ b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingFactoryImpl.java @@ -23,7 +23,7 @@ import org.apache.tuscany.sca.binding.comet.CometBinding; import org.apache.tuscany.sca.binding.comet.CometBindingFactory; /** - * Factory implementation to create JSONP Models + * Factory implementation to create Comet models. */ public class CometBindingFactoryImpl implements CometBindingFactory { diff --git a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingImpl.java b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingImpl.java index afc6fcaa90..65ca2a9d47 100644 --- a/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingImpl.java +++ b/sca-java-2.x/contrib/modules/binding-comet/src/main/java/org/apache/tuscany/sca/binding/comet/impl/CometBindingImpl.java @@ -32,56 +32,70 @@ public class CometBindingImpl implements CometBinding { private String name; private String uri; + @Override public String getName() { - return name; + return this.name; } + @Override public String getURI() { - return uri; + return this.uri; } - public void setURI(String uri) { + @Override + public void setURI(final String uri) { this.uri = uri; } - public void setName(String name) { + @Override + public void setName(final String name) { this.name = name; } + @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } + @Override public boolean isUnresolved() { return false; } - public void setUnresolved(boolean unresolved) { + @Override + public void setUnresolved(final boolean unresolved) { } + @Override public QName getType() { - return TYPE; + return CometBinding.TYPE; } - + + @Override public WireFormat getRequestWireFormat() { return null; } - - public void setRequestWireFormat(WireFormat wireFormat) { + + @Override + public void setRequestWireFormat(final WireFormat wireFormat) { } - + + @Override public WireFormat getResponseWireFormat() { return null; } - - public void setResponseWireFormat(WireFormat wireFormat) { + + @Override + public void setResponseWireFormat(final WireFormat wireFormat) { } - + + @Override public OperationSelector getOperationSelector() { return null; } - - public void setOperationSelector(OperationSelector operationSelector) { - } + + @Override + public void setOperationSelector(final OperationSelector operationSelector) { + } } diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/Helper.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/Helper.java index 4a26f3720d..eecc6044ea 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/Helper.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/Helper.java @@ -22,10 +22,13 @@ package org.apache.tuscany.sca.sample.comet; import java.util.Date; import java.util.Random; -public class Helper { +public final class Helper { - public static int randomInt(int max) { + public static int randomInt(final int max) { return (new Random(new Date().getTime()).nextInt(100)); } + private Helper() { + } + } diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java index f31b6a79df..afbdcbc8d7 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationService.java @@ -27,5 +27,5 @@ import org.oasisopen.sca.annotation.Remotable; public interface PrecipitationService { Response getPrecipitation(Location location); - + } diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java index dca144ad84..4278ef975b 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java @@ -29,8 +29,8 @@ import org.oasisopen.sca.annotation.Service; public class PrecipitationServiceImpl implements PrecipitationService { @Override - public Response getPrecipitation(Location location) { - Response response = new Response(); + public Response getPrecipitation(final Location location) { + final Response response = new Response(); response.setDate(new Date()); response.setData(Helper.randomInt(100) + "%"); return response; diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java index 9556c91484..55f935fa9f 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java @@ -29,18 +29,18 @@ import org.oasisopen.sca.annotation.Service; public class TemperatureHumidityServiceImpl implements TemperatureService, HumidityService { @Override - public Response getHumidity(Location location) { - Response response = new Response(); + public Response getHumidity(final Location location) { + final Response response = new Response(); response.setDate(new Date()); response.setData(Helper.randomInt(90) + "%"); return response; } @Override - public Response getTemperature(Location location, int scale) { - Response response = new Response(); + public Response getTemperature(final Location location, final int scale) { + final Response response = new Response(); response.setDate(new Date()); - String data = "" + Helper.randomInt(scale == CELSIUS ? 40 : 150); + final String data = "" + Helper.randomInt(scale == TemperatureService.CELSIUS ? 40 : 150); response.setData(data); return response; } diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Location.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Location.java index da36b3741f..ff2da3a360 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Location.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Location.java @@ -25,18 +25,18 @@ public class Location { private String country; public String getCity() { - return city; + return this.city; } - public void setCity(String city) { + public void setCity(final String city) { this.city = city; } public String getCountry() { - return country; + return this.country; } - public void setCountry(String country) { + public void setCountry(final String country) { this.country = country; } diff --git a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Response.java b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Response.java index 1a1649101f..aaa23f0b8a 100644 --- a/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Response.java +++ b/sca-java-2.x/contrib/samples/comet-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/model/Response.java @@ -27,18 +27,18 @@ public class Response { private String data; public Date getDate() { - return date; + return this.date; } - public void setDate(Date date) { + public void setDate(final Date date) { this.date = date; } public String getData() { - return data; + return this.data; } - public void setData(String data) { + public void setData(final String data) { this.data = data; } |