diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-24 13:08:55 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-24 13:08:55 +0000 |
commit | 61ed6b103cdea7af530950851857643a0b3efd45 (patch) | |
tree | e712ad12fd50e5194fada5be200b6c7345dfff92 /sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime | |
parent | d1f056a0126252f101d108ad2952ac0eebb78cb0 (diff) |
Enforced coding standards and formatting.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@988521 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
8 files changed, 390 insertions, 360 deletions
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())); } } |