選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RpcManager.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.terminal.gwt.client.communication;
  17. import java.util.Collection;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import com.google.gwt.core.client.GWT;
  21. import com.google.gwt.json.client.JSONArray;
  22. import com.google.gwt.json.client.JSONString;
  23. import com.vaadin.shared.communication.ClientRpc;
  24. import com.vaadin.shared.communication.MethodInvocation;
  25. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  26. import com.vaadin.terminal.gwt.client.ConnectorMap;
  27. import com.vaadin.terminal.gwt.client.ServerConnector;
  28. import com.vaadin.terminal.gwt.client.VConsole;
  29. /**
  30. * Client side RPC manager that can invoke methods based on RPC calls received
  31. * from the server.
  32. *
  33. * A GWT generator is used to create an implementation of this class at
  34. * run-time.
  35. *
  36. * @since 7.0
  37. */
  38. public class RpcManager {
  39. private final Map<String, RpcMethod> methodMap = new HashMap<String, RpcMethod>();
  40. public RpcManager() {
  41. GeneratedRpcMethodProvider provider = GWT
  42. .create(GeneratedRpcMethodProvider.class);
  43. Collection<RpcMethod> methods = provider.getGeneratedRpcMethods();
  44. for (RpcMethod rpcMethod : methods) {
  45. methodMap.put(
  46. rpcMethod.getInterfaceName() + "."
  47. + rpcMethod.getMethodName(), rpcMethod);
  48. }
  49. }
  50. /**
  51. * Perform server to client RPC invocation.
  52. *
  53. * @param invocation
  54. * method to invoke
  55. */
  56. public void applyInvocation(MethodInvocation invocation,
  57. ServerConnector connector) {
  58. String signature = getSignature(invocation);
  59. RpcMethod rpcMethod = getRpcMethod(signature);
  60. Collection<ClientRpc> implementations = connector
  61. .getRpcImplementations(invocation.getInterfaceName());
  62. for (ClientRpc clientRpc : implementations) {
  63. rpcMethod.applyInvocation(clientRpc, invocation.getParameters());
  64. }
  65. }
  66. private RpcMethod getRpcMethod(String signature) {
  67. RpcMethod rpcMethod = methodMap.get(signature);
  68. if (rpcMethod == null) {
  69. throw new IllegalStateException("There is no information about "
  70. + signature
  71. + ". Did you remember to compile the right widgetset?");
  72. }
  73. return rpcMethod;
  74. }
  75. private static String getSignature(MethodInvocation invocation) {
  76. return invocation.getInterfaceName() + "." + invocation.getMethodName();
  77. }
  78. public Type[] getParameterTypes(MethodInvocation invocation) {
  79. return getRpcMethod(getSignature(invocation)).getParameterTypes();
  80. }
  81. public void parseAndApplyInvocation(JSONArray rpcCall,
  82. ApplicationConnection connection) {
  83. ConnectorMap connectorMap = ConnectorMap.get(connection);
  84. String connectorId = ((JSONString) rpcCall.get(0)).stringValue();
  85. String interfaceName = ((JSONString) rpcCall.get(1)).stringValue();
  86. String methodName = ((JSONString) rpcCall.get(2)).stringValue();
  87. JSONArray parametersJson = (JSONArray) rpcCall.get(3);
  88. ServerConnector connector = connectorMap.getConnector(connectorId);
  89. MethodInvocation invocation = new MethodInvocation(connectorId,
  90. interfaceName, methodName);
  91. if (connector instanceof HasJavaScriptConnectorHelper) {
  92. ((HasJavaScriptConnectorHelper) connector)
  93. .getJavascriptConnectorHelper().invokeJsRpc(invocation,
  94. parametersJson);
  95. } else {
  96. if (connector == null) {
  97. throw new IllegalStateException("Target connector ("
  98. + connector + ") not found for RCC to "
  99. + getSignature(invocation));
  100. }
  101. parseMethodParameters(invocation, parametersJson, connection);
  102. VConsole.log("Server to client RPC call: " + invocation);
  103. applyInvocation(invocation, connector);
  104. }
  105. }
  106. private void parseMethodParameters(MethodInvocation methodInvocation,
  107. JSONArray parametersJson, ApplicationConnection connection) {
  108. Type[] parameterTypes = getParameterTypes(methodInvocation);
  109. Object[] parameters = new Object[parametersJson.size()];
  110. for (int j = 0; j < parametersJson.size(); ++j) {
  111. parameters[j] = JsonDecoder.decodeValue(parameterTypes[j],
  112. parametersJson.get(j), null, connection);
  113. }
  114. methodInvocation.setParameters(parameters);
  115. }
  116. }