diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-06 06:30:27 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-06 06:30:27 +0000 |
commit | 70615570d33731ce75b9d3b7a652a87d03c555e3 (patch) | |
tree | cf409631e325b64ff4ac5135f461069fc6645467 /sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding | |
parent | 52aa2ac844a47e2924a8fc14e155b99ce697a58a (diff) |
Added implementation using Jersey RESTful Web Services.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@982883 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding')
7 files changed, 402 insertions, 0 deletions
diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java new file mode 100644 index 0000000000..d31b1332a7 --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingHandler.java @@ -0,0 +1,49 @@ +package org.apache.tuscany.sca.binding.comet.runtime; + +import java.lang.reflect.InvocationTargetException; + +import javax.servlet.ServletContext; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; + +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.atmosphere.annotation.Broadcast; +import org.atmosphere.cpr.Broadcaster; +import org.atmosphere.cpr.DefaultBroadcaster; +import org.atmosphere.jersey.Broadcastable; +import org.atmosphere.jersey.SuspendResponse; + +import com.sun.jersey.spi.container.servlet.PerSession; + +@Produces("text/html;charset=ISO-8859-1") +@PerSession +public class CometBindingHandler { + + private Broadcaster broadcaster = new DefaultBroadcaster(); + public static final String ENDPOINT_KEY = "org.apache.tuscany.sca.binding.comet.endpoint"; + public static final String OPERATION_KEY = "org.apache.tuscany.sca.binding.comet.operation"; + + @Context + private ServletContext sc; + + @GET + public SuspendResponse<String> register() { + System.out.println("Register method: " + sc); + return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(broadcaster).outputComments(true) + .addListener(new EventsLogger()).build(); + } + + @POST + @Broadcast + public Broadcastable publish() throws InvocationTargetException { + System.out.println("Publish method: " + sc); + RuntimeEndpoint wire = (RuntimeEndpoint)sc.getAttribute(ENDPOINT_KEY); + Operation operation = (Operation)sc.getAttribute(OPERATION_KEY); + Object response = wire.invoke(operation, new Object[] {}); + return new Broadcastable(response.toString(), "", broadcaster); + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java new file mode 100644 index 0000000000..747ea4d78e --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometBindingProviderFactory.java @@ -0,0 +1,52 @@ +/* + * 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; + +public class CometBindingProviderFactory implements BindingProviderFactory<CometBinding> { + + private ServletHost servletHost; + + public CometBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + this.servletHost = ServletHostHelper.getServletHost(extensionPoints); + } + + public Class<CometBinding> getModelType() { + return CometBinding.class; + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpoint) { + return new CometReferenceBindingProvider(endpoint); + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + return new CometServiceBindingProvider(endpoint, servletHost); + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java new file mode 100644 index 0000000000..3b3f9beed3 --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java @@ -0,0 +1,41 @@ +/* + * 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.Operation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; + +public class CometInvoker implements Invoker { + + protected Operation operation; + protected EndpointReference endpoint; + + public CometInvoker(Operation operation, EndpointReference endpoint) { + this.operation = operation; + this.endpoint = endpoint; + } + + public Message invoke(Message msg) { + return null; + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java new file mode 100644 index 0000000000..091fc836be --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometReferenceBindingProvider.java @@ -0,0 +1,53 @@ +/* + * 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; + +public class CometReferenceBindingProvider implements ReferenceBindingProvider { + + private EndpointReference endpoint; + + public CometReferenceBindingProvider(EndpointReference endpoint) { + this.endpoint = endpoint; + } + public Invoker createInvoker(Operation operation) { + return new CometInvoker(operation, endpoint); + } + + public void start() { + } + + public void stop() { + } + + public InterfaceContract getBindingInterfaceContract() { + return null; + } + + public boolean supportsOneWayInvocation() { + return true; + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java new file mode 100644 index 0000000000..cd19be9dce --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometServiceBindingProvider.java @@ -0,0 +1,74 @@ +/* + * 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.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; +import org.atmosphere.cpr.AtmosphereServlet; + +public class CometServiceBindingProvider implements ServiceBindingProvider { + + private static final String PACKAGE_KEY = "com.sun.jersey.config.property.packages"; + private static final String PACKAGE_VALUE = "org.apache.tuscany.sca.binding.comet.runtime"; + + private RuntimeEndpoint endpoint; + private ServletHost servletHost; + + public CometServiceBindingProvider(RuntimeEndpoint endpoint, ServletHost servletHost) { + this.endpoint = endpoint; + this.servletHost = servletHost; + } + + public void start() { + ComponentService service = endpoint.getService(); + Interface serviceInterface = service.getInterfaceContract().getInterface(); + for (Operation op : serviceInterface.getOperations()) { + AtmosphereServlet servlet = new AtmosphereServlet(); + servlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE); + String path = endpoint.getBinding().getURI() + "/" + op.getName(); + servletHost.addServletMapping(path, servlet); + servlet.getServletContext().setAttribute(CometBindingHandler.ENDPOINT_KEY, endpoint); + servlet.getServletContext().setAttribute(CometBindingHandler.OPERATION_KEY, op); + } + } + + public void stop() { + ComponentService service = endpoint.getService(); + Interface serviceInterface = service.getInterfaceContract().getInterface(); + for (Operation op : serviceInterface.getOperations()) { + String path = endpoint.getBinding().getURI() + "/" + op.getName(); + servletHost.removeServletMapping(path); + } + } + + public InterfaceContract getBindingInterfaceContract() { + return null; + } + + public boolean supportsOneWayInvocation() { + return true; + } + +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java new file mode 100644 index 0000000000..934f5ef907 --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/EventsLogger.java @@ -0,0 +1,67 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.apache.tuscany.sca.binding.comet.runtime; + +import org.atmosphere.cpr.AtmosphereResourceEvent; +import org.atmosphere.cpr.AtmosphereResourceEventListener; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class EventsLogger implements AtmosphereResourceEventListener { + + public EventsLogger() { + } + + public void onSuspend(final AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onSuspend: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onResume(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onResume: " + event.getResource().getRequest().getRemoteAddr()); + } + + public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onDisconnect: " + event.getResource().getRequest().getRemoteAddr() + + event.getResource().getRequest().getRemotePort()); + } + + public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) { + System.out.println("onBroadcast: " + event.getMessage()); + } +} diff --git a/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/FileResource.java b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/FileResource.java new file mode 100644 index 0000000000..5cb663fc6f --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-comet-runtime-alternate/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/FileResource.java @@ -0,0 +1,66 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.apache.tuscany.sca.binding.comet.runtime; + +import java.io.InputStream; + +import javax.servlet.ServletContext; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.PathSegment; + +@Path("/") +@Produces("text/html") +public class FileResource { + + @Context + private ServletContext sc; + + @Path("/js/{id}") + @GET + public InputStream getJQuery(@PathParam("id") PathSegment ps) { + return sc.getResourceAsStream("/js/" + ps.getPath()); + } + + @GET + public InputStream getIndex() { + return sc.getResourceAsStream("/index.html"); + } +} |