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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2000-2018 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 java.util.logging.Logger;
  19. import com.vaadin.client.ApplicationConnection;
  20. import com.vaadin.client.ConnectorMap;
  21. import com.vaadin.client.ServerConnector;
  22. import com.vaadin.client.metadata.Method;
  23. import com.vaadin.client.metadata.NoDataException;
  24. import com.vaadin.client.metadata.Type;
  25. import com.vaadin.shared.communication.ClientRpc;
  26. import com.vaadin.shared.communication.MethodInvocation;
  27. import elemental.json.JsonArray;
  28. /**
  29. * Client side RPC manager that can invoke methods based on RPC calls received
  30. * from the server.
  31. *
  32. * A GWT generator is used to create an implementation of this class at
  33. * run-time.
  34. *
  35. * @since 7.0
  36. */
  37. public class RpcManager {
  38. /**
  39. * Perform server to client RPC invocation.
  40. *
  41. * @param invocation
  42. * method to invoke
  43. */
  44. public void applyInvocation(MethodInvocation invocation,
  45. ServerConnector connector) {
  46. Method method = getMethod(invocation);
  47. Collection<ClientRpc> implementations = connector
  48. .getRpcImplementations(invocation.getInterfaceName());
  49. try {
  50. for (ClientRpc clientRpc : implementations) {
  51. method.invoke(clientRpc, invocation.getParameters());
  52. }
  53. } catch (NoDataException e) {
  54. throw new IllegalStateException(
  55. "There is no information about " + method.getSignature()
  56. + ". Did you remember to compile the right widgetset?",
  57. e);
  58. }
  59. }
  60. /**
  61. * Gets the method that an invocation targets.
  62. *
  63. * @param invocation
  64. * the method invocation to get the method for
  65. *
  66. * @since 7.4
  67. * @return the method targeted by this invocation
  68. */
  69. public static Method getMethod(MethodInvocation invocation) {
  70. // Implemented here instead of in MethodInovcation since it's in shared
  71. // and can't use our Method class.
  72. Type type = new Type(invocation.getInterfaceName(), null);
  73. Method method = type.getMethod(invocation.getMethodName());
  74. return method;
  75. }
  76. private static String getSignature(MethodInvocation invocation) {
  77. return invocation.getInterfaceName() + "." + invocation.getMethodName();
  78. }
  79. public Type[] getParameterTypes(MethodInvocation invocation) {
  80. Method method = getMethod(invocation);
  81. try {
  82. Type[] parameterTypes = method.getParameterTypes();
  83. return parameterTypes;
  84. } catch (NoDataException e) {
  85. throw new IllegalStateException(
  86. "There is no information about " + method.getSignature()
  87. + ". Did you remember to compile the right widgetset?",
  88. e);
  89. }
  90. }
  91. public MethodInvocation parseAndApplyInvocation(JsonArray rpcCall,
  92. ApplicationConnection connection) {
  93. ConnectorMap connectorMap = ConnectorMap.get(connection);
  94. String connectorId = rpcCall.getString(0);
  95. String interfaceName = rpcCall.getString(1);
  96. String methodName = rpcCall.getString(2);
  97. JsonArray parametersJson = rpcCall.getArray(3);
  98. ServerConnector connector = connectorMap.getConnector(connectorId);
  99. MethodInvocation invocation = new MethodInvocation(connectorId,
  100. interfaceName, methodName);
  101. if (connector instanceof HasJavaScriptConnectorHelper) {
  102. ((HasJavaScriptConnectorHelper) connector)
  103. .getJavascriptConnectorHelper()
  104. .invokeJsRpc(invocation, parametersJson);
  105. } else {
  106. if (connector == null) {
  107. throw new IllegalStateException("Target connector (" + connector
  108. + ") not found for RCC to " + getSignature(invocation));
  109. }
  110. parseMethodParameters(invocation, parametersJson, connection);
  111. getLogger().info("Server to client RPC call: " + invocation);
  112. applyInvocation(invocation, connector);
  113. }
  114. return invocation;
  115. }
  116. private void parseMethodParameters(MethodInvocation methodInvocation,
  117. JsonArray parametersJson, ApplicationConnection connection) {
  118. Type[] parameterTypes = getParameterTypes(methodInvocation);
  119. Object[] parameters = new Object[parametersJson.length()];
  120. for (int j = 0; j < parametersJson.length(); ++j) {
  121. parameters[j] = JsonDecoder.decodeValue(parameterTypes[j],
  122. parametersJson.get(j), null, connection);
  123. }
  124. methodInvocation.setParameters(parameters);
  125. }
  126. private static Logger getLogger() {
  127. return Logger.getLogger(RpcManager.class.getName());
  128. }
  129. }