Added ServletFactory in order to keep a singleton servlet for all comet services in a scdl so that the two connection limit issue with the browsers is avoided.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@985414 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7c39643f21
commit
eff7bb9dd4
3 changed files with 70 additions and 23 deletions
|
|
@ -1,13 +1,18 @@
|
|||
package org.apache.tuscany.sca.binding.comet.runtime;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
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.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Request;
|
||||
|
||||
import org.apache.tuscany.sca.interfacedef.Operation;
|
||||
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
|
||||
|
|
@ -25,29 +30,36 @@ import com.sun.jersey.spi.container.servlet.PerSession;
|
|||
public class CometBindingHandler {
|
||||
|
||||
private Broadcaster broadcaster;
|
||||
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";
|
||||
private Map<String, RuntimeEndpoint> endpoints;
|
||||
private Map<String, Operation> operations;
|
||||
|
||||
@Context
|
||||
private ServletContext sc;
|
||||
|
||||
|
||||
@GET
|
||||
public SuspendResponse<String> connect() {
|
||||
System.out.println("Entering connect...");
|
||||
broadcaster = new DefaultBroadcaster();
|
||||
return new SuspendResponse.SuspendResponseBuilder<String>()
|
||||
.broadcaster(broadcaster)
|
||||
.outputComments(true)
|
||||
// .addListener(new EventsLogger())
|
||||
.build();
|
||||
endpoints = (Map<String, RuntimeEndpoint>)sc.getAttribute(ServletFactory.ENDPOINTS_KEY);
|
||||
operations = (Map<String, Operation>)sc.getAttribute(ServletFactory.OPERATIONS_KEY);
|
||||
return new SuspendResponse.SuspendResponseBuilder<String>().broadcaster(broadcaster).outputComments(true)
|
||||
.addListener(new EventsLogger()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/{component}/{service}/{method}")
|
||||
@Broadcast
|
||||
public Broadcastable callAndRespond() throws InvocationTargetException {
|
||||
RuntimeEndpoint wire = (RuntimeEndpoint)sc.getAttribute(ENDPOINT_KEY);
|
||||
Operation operation = (Operation)sc.getAttribute(OPERATION_KEY);
|
||||
public Broadcastable callAndRespond(@PathParam("component") String component,
|
||||
@PathParam("service") String service,
|
||||
@PathParam("method") String method,
|
||||
@FormParam("callback") String callbackMethod) throws InvocationTargetException {
|
||||
String url = "/" + component + "/" + service + "/" + method;
|
||||
System.out.println("Entered callAndRespond with url: " + url);
|
||||
System.out.println("Callback method: " + callbackMethod);
|
||||
RuntimeEndpoint wire = endpoints.get(url);
|
||||
Operation operation = operations.get(url);
|
||||
Object response = wire.invoke(operation, new Object[] {});
|
||||
return new Broadcastable(response.toString(), "", broadcaster);
|
||||
// TODO: replace with JSON
|
||||
return new Broadcastable(callbackMethod + "#" + response.toString(), "", broadcaster);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,6 @@ 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;
|
||||
|
||||
|
|
@ -44,13 +41,8 @@ public class CometServiceBindingProvider implements ServiceBindingProvider {
|
|||
public void start() {
|
||||
ComponentService service = endpoint.getService();
|
||||
Interface serviceInterface = service.getInterfaceContract().getInterface();
|
||||
for (Operation op : serviceInterface.getOperations()) {
|
||||
String path = endpoint.getBinding().getURI() + "/" + op.getName() + "/*";
|
||||
AtmosphereServlet servlet = new AtmosphereServlet();
|
||||
servlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE);
|
||||
servletHost.addServletMapping(path, servlet);
|
||||
servlet.getServletContext().setAttribute(CometBindingHandler.ENDPOINT_KEY, endpoint);
|
||||
servlet.getServletContext().setAttribute(CometBindingHandler.OPERATION_KEY, op);
|
||||
for (Operation operation : serviceInterface.getOperations()) {
|
||||
ServletFactory.registerServlet(servletHost, endpoint, operation);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
package org.apache.tuscany.sca.binding.comet.runtime;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.tuscany.sca.host.http.ServletHost;
|
||||
import org.apache.tuscany.sca.interfacedef.Operation;
|
||||
import org.apache.tuscany.sca.runtime.RuntimeEndpoint;
|
||||
import org.atmosphere.cpr.AtmosphereServlet;
|
||||
|
||||
public class ServletFactory {
|
||||
|
||||
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";
|
||||
public static final String ENDPOINTS_KEY = "org.apache.tuscany.sca.binding.comet.endpoints";
|
||||
public static final String OPERATIONS_KEY = "org.apache.tuscany.sca.binding.comet.operations";
|
||||
public static final String PATH = "/tuscany-comet/*";
|
||||
|
||||
private static AtmosphereServlet servlet = null;
|
||||
|
||||
public static synchronized void registerServlet(ServletHost servletHost,
|
||||
RuntimeEndpoint endpoint,
|
||||
Operation operation) {
|
||||
if (servlet == null) {
|
||||
servlet = new AtmosphereServlet();
|
||||
servlet.addInitParameter(PACKAGE_KEY, PACKAGE_VALUE);
|
||||
servletHost.addServletMapping(PATH, servlet);
|
||||
Map<String, RuntimeEndpoint> endpoints = new HashMap<String, RuntimeEndpoint>();
|
||||
servlet.getServletContext().setAttribute(ENDPOINTS_KEY, endpoints);
|
||||
Map<String, Operation> operations = new HashMap<String, Operation>();
|
||||
servlet.getServletContext().setAttribute(OPERATIONS_KEY, operations);
|
||||
}
|
||||
String url = endpoint.getBinding().getURI() + "/" + operation.getName();
|
||||
System.out.println("Adding endpoint and operation for url: " + url);
|
||||
Map<String, RuntimeEndpoint> endpoints =
|
||||
(Map<String, RuntimeEndpoint>)servlet.getServletContext().getAttribute(ENDPOINTS_KEY);
|
||||
endpoints.put(url, endpoint);
|
||||
Map<String, Operation> operations =
|
||||
(Map<String, Operation>)servlet.getServletContext().getAttribute(OPERATIONS_KEY);
|
||||
operations.put(url, operation);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue