You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RpcManager.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2000-2014 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.client.communication;
  17. import java.util.Collection;
  18. import com.google.gwt.json.client.JSONArray;
  19. import com.google.gwt.json.client.JSONString;
  20. import com.vaadin.client.ApplicationConnection;
  21. import com.vaadin.client.ConnectorMap;
  22. import com.vaadin.client.ServerConnector;
  23. import com.vaadin.client.VConsole;
  24. import com.vaadin.client.metadata.Method;
  25. import com.vaadin.client.metadata.NoDataException;
  26. import com.vaadin.client.metadata.Type;
  27. import com.vaadin.shared.communication.ClientRpc;
  28. import com.vaadin.shared.communication.MethodInvocation;
  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. /**
  40. * Perform server to client RPC invocation.
  41. *
  42. * @param invocation
  43. * method to invoke
  44. */
  45. public void applyInvocation(MethodInvocation invocation,
  46. ServerConnector connector) {
  47. Method method = getMethod(invocation);
  48. Collection<ClientRpc> implementations = connector
  49. .getRpcImplementations(invocation.getInterfaceName());
  50. try {
  51. for (ClientRpc clientRpc : implementations) {
  52. method.invoke(clientRpc, invocation.getParameters());
  53. }
  54. } catch (NoDataException e) {
  55. throw new IllegalStateException("There is no information about "
  56. + method.getSignature()
  57. + ". Did you remember to compile the right widgetset?", e);
  58. }
  59. }
  60. private Method getMethod(MethodInvocation invocation) {
  61. Type type = new Type(invocation.getInterfaceName(), null);
  62. Method method = type.getMethod(invocation.getMethodName());
  63. return method;
  64. }
  65. private static String getSignature(MethodInvocation invocation) {
  66. return invocation.getInterfaceName() + "." + invocation.getMethodName();
  67. }
  68. public Type[] getParameterTypes(MethodInvocation invocation) {
  69. Method method = getMethod(invocation);
  70. try {
  71. Type[] parameterTypes = method.getParameterTypes();
  72. return parameterTypes;
  73. } catch (NoDataException e) {
  74. throw new IllegalStateException("There is no information about "
  75. + method.getSignature()
  76. + ". Did you remember to compile the right widgetset?", e);
  77. }
  78. }
  79. public void parseAndApplyInvocation(JSONArray rpcCall,
  80. ApplicationConnection connection) {
  81. ConnectorMap connectorMap = ConnectorMap.get(connection);
  82. String connectorId = ((JSONString) rpcCall.get(0)).stringValue();
  83. String interfaceName = ((JSONString) rpcCall.get(1)).stringValue();
  84. String methodName = ((JSONString) rpcCall.get(2)).stringValue();
  85. JSONArray parametersJson = (JSONArray) rpcCall.get(3);
  86. ServerConnector connector = connectorMap.getConnector(connectorId);
  87. MethodInvocation invocation = new MethodInvocation(connectorId,
  88. interfaceName, methodName);
  89. if (connector instanceof HasJavaScriptConnectorHelper) {
  90. ((HasJavaScriptConnectorHelper) connector)
  91. .getJavascriptConnectorHelper().invokeJsRpc(invocation,
  92. parametersJson);
  93. } else {
  94. if (connector == null) {
  95. throw new IllegalStateException("Target connector ("
  96. + connector + ") not found for RCC to "
  97. + getSignature(invocation));
  98. }
  99. parseMethodParameters(invocation, parametersJson, connection);
  100. VConsole.log("Server to client RPC call: " + invocation);
  101. applyInvocation(invocation, connector);
  102. }
  103. }
  104. private void parseMethodParameters(MethodInvocation methodInvocation,
  105. JSONArray parametersJson, ApplicationConnection connection) {
  106. Type[] parameterTypes = getParameterTypes(methodInvocation);
  107. Object[] parameters = new Object[parametersJson.size()];
  108. for (int j = 0; j < parametersJson.size(); ++j) {
  109. parameters[j] = JsonDecoder.decodeValue(parameterTypes[j],
  110. parametersJson.get(j), null, connection);
  111. }
  112. methodInvocation.setParameters(parameters);
  113. }
  114. }