summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/contrib/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/ServletFactory.java
blob: 6a89bf8a06df37497fd93bbbe1a2b781427c3174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
    }

}