From 62d55709374e7c299f720aa9066f8b3dcb315ac0 Mon Sep 17 00:00:00 2001 From: antelder Date: Mon, 18 Jul 2011 10:21:06 +0000 Subject: Create a branch for beta3 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1147804 13f79535-47bb-0310-9956-ffa450edef68 --- .../comet/runtime/CometBindingProviderFactory.java | 69 +++++++++ .../comet/runtime/CometCallbackInvoker.java | 69 +++++++++ .../runtime/CometReferenceBindingProvider.java | 67 ++++++++ .../comet/runtime/CometServiceBindingProvider.java | 88 +++++++++++ .../sca/binding/comet/runtime/JSONUtil.java | 145 ++++++++++++++++++ .../sca/binding/comet/runtime/ServletFactory.java | 139 +++++++++++++++++ .../comet/runtime/callback/CometCallback.java | 39 +++++ .../sca/binding/comet/runtime/callback/Status.java | 27 ++++ .../comet/runtime/handler/CometBindingHandler.java | 168 +++++++++++++++++++++ .../runtime/javascript/JavascriptGenerator.java | 91 +++++++++++ .../runtime/javascript/JavascriptResource.java | 62 ++++++++ .../runtime/manager/CometEndpointManager.java | 51 +++++++ .../runtime/manager/CometOperationManager.java | 51 +++++++ .../comet/runtime/manager/CometSessionManager.java | 51 +++++++ 14 files changed, 1117 insertions(+) create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/JSONUtil.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java create mode 100644 sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java (limited to 'sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet') diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java new file mode 100644 index 0000000000..f6c393dcb4 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java @@ -0,0 +1,69 @@ +/* + * 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.binding.comet.CometBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostHelper; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +/** + * Factory for binding providers. + */ +public class CometBindingProviderFactory implements BindingProviderFactory { + + /** + * Underlying servlet host. + */ + private final ServletHost servletHost; + + public CometBindingProviderFactory(final ExtensionPointRegistry extensionPoints) { + this.servletHost = ServletHostHelper.getServletHost(extensionPoints); + } + + @Override + public Class getModelType() { + return CometBinding.class; + } + + /** + * Creates a provider for a reference that has comet binding specified in + * the scdl. + */ + @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. + */ + @Override + public ServiceBindingProvider createServiceBindingProvider(final RuntimeEndpoint endpoint) { + return new CometServiceBindingProvider(endpoint, this.servletHost); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java new file mode 100644 index 0000000000..b973c90728 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometCallbackInvoker.java @@ -0,0 +1,69 @@ +/* + * 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; + +/** + * Receives callback invocations and sends messages back to the browser. + */ +public class CometCallbackInvoker implements Invoker { + + 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 { + String callbackMethod = msg.getTo().getURI(); + Object[] body = msg.getBody(); + broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + JSONUtil.encodeResponse(body[0]) + "'))"); + response.setBody(Status.OK); + } + return response; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java new file mode 100644 index 0000000000..9777b0c82c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java @@ -0,0 +1,67 @@ +/* + * 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.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; + +/** + * 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 { + + /** + * Endpoint for which the binding provider is created. + */ + private final EndpointReference endpoint; + + public CometReferenceBindingProvider(final EndpointReference endpoint) { + this.endpoint = endpoint; + } + + @Override + public Invoker createInvoker(final Operation operation) { + return new CometCallbackInvoker(operation, endpoint); + } + + @Override + public void start() { + } + + @Override + public void stop() { + } + + @Override + public InterfaceContract getBindingInterfaceContract() { + return endpoint.getReference().getInterfaceContract(); + } + + @Override + public boolean supportsOneWayInvocation() { + return false; + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java new file mode 100644 index 0000000000..58f422c01b --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -0,0 +1,88 @@ +/* + * 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.ComponentService; +import org.apache.tuscany.sca.binding.comet.runtime.javascript.JavascriptGenerator; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometEndpointManager; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometOperationManager; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometSessionManager; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * Provider for services having comet binding specified in the scdl. + */ +public class CometServiceBindingProvider implements ServiceBindingProvider { + + private RuntimeEndpoint endpoint; + private ServletHost servletHost; + + public CometServiceBindingProvider(final RuntimeEndpoint endpoint, final ServletHost servletHost) { + this.endpoint = endpoint; + this.servletHost = servletHost; + } + + /** + * 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); + } + } + + /** + * 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 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/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/JSONUtil.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/JSONUtil.java new file mode 100644 index 0000000000..79ec156131 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/JSONUtil.java @@ -0,0 +1,145 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Operation; + +import com.google.gson.Gson; + +/** + * Helper class to facilitate JSON convertions. + */ +public class JSONUtil { + + private static Gson gson = new Gson(); + + /** + * 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 + */ + public static Object[] decodeJsonParamsForOperation(String jsonData, Operation operation) { + Object[] args = new Object[operation.getInputType().getLogical().size()]; + final String[] json = 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 strings + */ + private static String[] parseArray(String jsonArray) { + List objects = new ArrayList(); + int bracketNum = 0; + int parNum = 0; + int quoteNum = 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 '\"': + quoteNum++; + break; + case ',': + if ((bracketNum == 0) && (parNum == 1) && quoteNum % 2 == 0) { + objects.add(jsonArray.substring(startPos, i)); + startPos = i + 1; + } + } + } + objects.add(jsonArray.substring(startPos, jsonArray.length() - 1)); + return objects.toArray(new String[] {}); + } + + private JSONUtil() { + } + + /** + * Converts a Java object to JSON format. + * + * @param response + * the response to convert + * @return the object in JSON format + */ + public static String encodeResponse(Object response) { + return gson.toJson(response); + } + + /** + * Convert request parameters as JSON array. + * + * @param params + * request parameters + * @return request parameters as JSON array + */ + public static String encodeRequestParams(Object[] params) { + StringBuilder builder = new StringBuilder(); + for (int index = 0; index < params.length; index++) { + Object param = params[index]; + builder.append(index == 0 ? "[" : ","); + builder.append(gson.toJson(param)); + } + builder.append("]"); + return builder.toString(); + } + + /** + * Decode JSON to a given Java type. + * + * @param responseJSON + * the json to convert + * @param returnType + * the return type to convert to + * @return the converted object + */ + public static Object decodeResponse(String responseJSON, Class returnType) { + return gson.fromJson(responseJSON, returnType); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java new file mode 100644 index 0000000000..f40d1f62ce --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java @@ -0,0 +1,139 @@ +/* + * 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.host.http.ServletHost; +import org.atmosphere.cpr.AtmosphereServlet; + +/** + * 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 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"; + + /** + * Init-param key for Atmosphere filters. + */ + private static final String FILTERS_KEY = "org.atmosphere.cpr.broadcastFilterClasses"; + + /** + * Defined filters. + */ + private static final String FILTERS = "org.atmosphere.client.JavascriptClientFilter"; + + /** + * 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(PACKAGE_KEY, HANDLER_PACKAGE); +// ServletFactory.cometServlet.addInitParameter(FILTERS_KEY, FILTERS); + String uri = servletHost.addServletMapping(PATH, cometServlet); + return uri; + } + return null; + } + + private static void registerJavascriptServlet(ServletHost servletHost) { + if (ServletFactory.javascriptServlet == null) { + ServletFactory.javascriptServlet = new AtmosphereServlet(); + ServletFactory.javascriptServlet.addInitParameter(PACKAGE_KEY, JS_PACKAGE); + servletHost.addServletMapping(JS_PATH, javascriptServlet); + } + } + + /** + * Removes servlets from the servlet host. + * + * @param servletHost + * the underlying servlet host. + */ + public static void unregisterServlet(final ServletHost servletHost) { + servletHost.removeServletMapping(PATH); + servletHost.removeServletMapping(JS_PATH); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java new file mode 100644 index 0000000000..1deefb2afa --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/CometCallback.java @@ -0,0 +1,39 @@ +/* + * 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; + +import org.oasisopen.sca.annotation.Remotable; + +/** + * The comet callback interface. + */ +@Remotable +public interface CometCallback { + + /** + * 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/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java new file mode 100644 index 0000000000..6b479a144a --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/callback/Status.java @@ -0,0 +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 +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java new file mode 100644 index 0000000000..0c9902211c --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/handler/CometBindingHandler.java @@ -0,0 +1,168 @@ +/* + * 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.handler; + +import java.lang.reflect.InvocationTargetException; + +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; + +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.binding.comet.runtime.JSONUtil; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometEndpointManager; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometOperationManager; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometSessionManager; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointImpl; +import org.apache.tuscany.sca.core.assembly.impl.RuntimeEndpointReferenceImpl; +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.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.atmosphere.cpr.Broadcaster; +import org.atmosphere.cpr.BroadcasterLifeCyclePolicy; +import org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY; +import org.atmosphere.cpr.BroadcasterLifeCyclePolicyListener; +import org.atmosphere.jersey.JerseyBroadcaster; +import org.atmosphere.jersey.SuspendResponse; + +/** + * Handles requests for comet services and for creating a persistent connection. + */ +@Path("/") +public class CometBindingHandler { + + /** + * 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) { + Broadcaster broadcaster = CometSessionManager.get(sessionId); + if (broadcaster == null) { + broadcaster = new JerseyBroadcaster(sessionId); + BroadcasterLifeCyclePolicy policy = new BroadcasterLifeCyclePolicy.Builder().policy( + ATMOSPHERE_RESOURCE_POLICY.EMPTY_DESTROY).build(); + broadcaster.setBroadcasterLifeCyclePolicy(policy); + broadcaster.addBroadcasterLifeCyclePolicyListener(new CometBroadcasterLifeCyclePolicyListener(sessionId)); + CometSessionManager.add(sessionId, broadcaster); + } + return new SuspendResponse.SuspendResponseBuilder().broadcaster(broadcaster).outputComments(true) + .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 { + String url = "/" + service + "/" + method; + RuntimeEndpoint wire = CometEndpointManager.get(url); + Operation operation = CometOperationManager.get(url); + + final Object[] args = JSONUtil.decodeJsonParamsForOperation(jsonData, operation); + Message msg = createMessageWithMockedCometReference(args, sessionId, callbackMethod); + boolean isVoidReturnType = operation.getOutputType().getLogical().isEmpty(); + if (!isVoidReturnType) { + Object response = wire.invoke(operation, args); + Broadcaster broadcaster = CometSessionManager.get(sessionId); + if (broadcaster != null) { + broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + JSONUtil.encodeResponse(response) + + "'))"); + } + } else { + wire.invoke(operation, msg); + } + } + + /** + * 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 + * 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(); + msg.getHeaders().put(Constants.MESSAGE_ID, sessionId); + msg.setBody(args); + EndpointReference re = new RuntimeEndpointReferenceImpl(); + RuntimeEndpointImpl callbackEndpoint = new RuntimeEndpointImpl(); + callbackEndpoint.setURI(callbackMethod); + re.setCallbackEndpoint(callbackEndpoint); + msg.setFrom(re); + return msg; + } + + public class CometBroadcasterLifeCyclePolicyListener implements BroadcasterLifeCyclePolicyListener { + + private String sessionId; + + public CometBroadcasterLifeCyclePolicyListener(String sessionId) { + this.sessionId = sessionId; + } + + @Override + public void onDestroy() { + } + + @Override + public void onEmpty() { + CometSessionManager.remove(sessionId); + } + + @Override + public void onIdle() { + } + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java new file mode 100644 index 0000000000..ecf470da2d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptGenerator.java @@ -0,0 +1,91 @@ +/* + * 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.javascript; + +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.interfacedef.Operation; + +/** + * Generates javascript proxies for the comet services. + */ +public class JavascriptGenerator { + + /** + * 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 object performing comet specific tasks. + */ + private static final String TUSCANY_COMET = "SCA.TuscanyComet"; + + private static StringBuffer javascript = new StringBuffer(); + + private JavascriptGenerator() { + } + + public static StringBuffer getJavascript() { + return JavascriptGenerator.javascript; + } + + /** + * Generates the javascript 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/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java new file mode 100644 index 0000000000..06f7bfb546 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/javascript/JavascriptResource.java @@ -0,0 +1,62 @@ +/* + * 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.javascript; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.SequenceInputStream; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +/** + * Handles calls for the retrieving the Javascript toolkit. + */ +@Path("/") +@Produces("text/javascript") +public class JavascriptResource { + + /** + * Dependencies for the Tuscany Comet Javascript API. + */ + private static final String[] DEPENDENCIES = { "/jquery.atmosphere.js", "/jquery.json-2.2.min.js", + "/jquery.guid.js", "/cometComponentContext.js" }; + + /** + * Method called when the Javascript toolkit is requested. + * + * @return InputStream containing the Javascript code. + */ + @GET + @Path("/org.apache.tuscany.sca.CometComponentContext.js") + public InputStream getJavascript() { + InputStream stream = null; + for (String dependency : JavascriptResource.DEPENDENCIES) { + if (stream == null) { + stream = this.getClass().getResourceAsStream(dependency); + } else { + stream = new SequenceInputStream(stream, this.getClass().getResourceAsStream(dependency)); + } + } + final String generatedJs = JavascriptGenerator.getJavascript().toString() + "\n}"; + return new SequenceInputStream(stream, new ByteArrayInputStream(generatedJs.getBytes())); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java new file mode 100644 index 0000000000..3d9707ca8e --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometEndpointManager.java @@ -0,0 +1,51 @@ +/* + * 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; +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 CometEndpointManager() { + } + + public static void add(String url, RuntimeEndpoint endpoint) { + endpoints.put(url, endpoint); + } + + public static RuntimeEndpoint get(String url) { + return endpoints.get(url); + } + + public static void remove(String url) { + endpoints.remove(url); + } + + public static void clear() { + endpoints.clear(); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java new file mode 100644 index 0000000000..ea6657e786 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometOperationManager.java @@ -0,0 +1,51 @@ +/* + * 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; +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 CometOperationManager() { + } + + public static void add(String url, Operation operation) { + operations.put(url, operation); + } + + public static Operation get(String url) { + return operations.get(url); + } + + public static void remove(String url) { + operations.remove(url); + } + + public static void clear() { + operations.clear(); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java new file mode 100644 index 0000000000..59fe8d8f2d --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta3/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/manager/CometSessionManager.java @@ -0,0 +1,51 @@ +/* + * 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; +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(); + + private CometSessionManager() { + } + + public static void add(String sessionId, Broadcaster broadcaster) { + broadcasters.put(sessionId, broadcaster); + } + + public static Broadcaster get(String sessionId) { + return broadcasters.get(sessionId); + } + + public static void remove(String sessionId) { + broadcasters.remove(sessionId); + } + + public static void clear() { + broadcasters.clear(); + } +} -- cgit v1.2.3