aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/communication/RpcManager.java
blob: 1d3447687d5a3ed1c1bba60d2a1272f8ff557f19 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal.gwt.client.communication;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.ServerConnector;

/**
 * Client side RPC manager that can invoke methods based on RPC calls received
 * from the server.
 * 
 * A GWT generator is used to create an implementation of this class at
 * run-time.
 * 
 * @since 7.0
 */
public class RpcManager {

    private final Map<String, RpcMethod> methodMap = new HashMap<String, RpcMethod>();

    public RpcManager() {
        GeneratedRpcMethodProvider provider = GWT
                .create(GeneratedRpcMethodProvider.class);
        Collection<RpcMethod> methods = provider.getGeneratedRpcMethods();
        for (RpcMethod rpcMethod : methods) {
            methodMap.put(
                    rpcMethod.getInterfaceName() + "."
                            + rpcMethod.getMethodName(), rpcMethod);
        }
    }

    /**
     * Perform server to client RPC invocation.
     * 
     * @param invocation
     *            method to invoke
     * @param connectorMap
     *            mapper used to find Connector for the method call and any
     *            connectors referenced in parameters
     */
    public void applyInvocation(MethodInvocation invocation,
            ConnectorMap connectorMap) {
        ServerConnector connector = connectorMap.getConnector(invocation
                .getConnectorId());
        String signature = getSignature(invocation);
        if (connector == null) {
            throw new IllegalStateException("Target connector ("
                    + invocation.getConnectorId() + ") not found for RCC to "
                    + signature);
        }

        RpcMethod rpcMethod = getRpcMethod(signature);
        Collection<ClientRpc> implementations = connector
                .getRpcImplementations(invocation.getInterfaceName());
        for (ClientRpc clientRpc : implementations) {
            rpcMethod.applyInvocation(clientRpc, invocation.getParameters());
        }
    }

    private RpcMethod getRpcMethod(String signature) {
        RpcMethod rpcMethod = methodMap.get(signature);
        if (rpcMethod == null) {
            throw new IllegalStateException("There is no information about "
                    + signature
                    + ". Did you remember to compile the right widgetset?");
        }
        return rpcMethod;
    }

    private static String getSignature(MethodInvocation invocation) {
        return invocation.getInterfaceName() + "." + invocation.getMethodName();
    }

    public Type[] getParameterTypes(MethodInvocation invocation) {
        return getRpcMethod(getSignature(invocation)).getParameterTypes();
    }

}