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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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("There is no information about "
  55. + method.getSignature()
  56. + ". Did you remember to compile the right widgetset?", e);
  57. }
  58. }
  59. /**
  60. * Gets the method that an invocation targets.
  61. *
  62. * @param invocation
  63. * the method invocation to get the method for
  64. *
  65. * @since 7.4
  66. * @return the method targeted by this invocation
  67. */
  68. public static Method getMethod(MethodInvocation invocation) {
  69. // Implemented here instead of in MethodInovcation since it's in shared
  70. // and can't use our Method class.
  71. Type type = new Type(invocation.getInterfaceName(), null);
  72. Method method = type.getMethod(invocation.getMethodName());
  73. return method;
  74. }
  75. private static String getSignature(MethodInvocation invocation) {
  76. return invocation.getInterfaceName() + "." + invocation.getMethodName();
  77. }
  78. public Type[] getParameterTypes(MethodInvocation invocation) {
  79. Method method = getMethod(invocation);
  80. try {
  81. Type[] parameterTypes = method.getParameterTypes();
  82. return parameterTypes;
  83. } catch (NoDataException e) {
  84. throw new IllegalStateException("There is no information about "
  85. + method.getSignature()
  86. + ". Did you remember to compile the right widgetset?", e);
  87. }
  88. }
  89. public MethodInvocation parseAndApplyInvocation(JsonArray rpcCall,
  90. ApplicationConnection connection) {
  91. ConnectorMap connectorMap = ConnectorMap.get(connection);
  92. String connectorId = rpcCall.getString(0);
  93. String interfaceName = rpcCall.getString(1);
  94. String methodName = rpcCall.getString(2);
  95. JsonArray parametersJson = rpcCall.getArray(3);
  96. ServerConnector connector = connectorMap.getConnector(connectorId);
  97. MethodInvocation invocation = new MethodInvocation(connectorId,
  98. interfaceName, methodName);
  99. if (connector instanceof HasJavaScriptConnectorHelper) {
  100. ((HasJavaScriptConnectorHelper) connector)
  101. .getJavascriptConnectorHelper().invokeJsRpc(invocation,
  102. parametersJson);
  103. } else {
  104. if (connector == null) {
  105. throw new IllegalStateException("Target connector ("
  106. + connector + ") not found for RCC to "
  107. + getSignature(invocation));
  108. }
  109. parseMethodParameters(invocation, parametersJson, connection);
  110. getLogger().info("Server to client RPC call: " + invocation);
  111. applyInvocation(invocation, connector);
  112. }
  113. return invocation;
  114. }
  115. private void parseMethodParameters(MethodInvocation methodInvocation,
  116. JsonArray parametersJson, ApplicationConnection connection) {
  117. Type[] parameterTypes = getParameterTypes(methodInvocation);
  118. Object[] parameters = new Object[parametersJson.length()];
  119. for (int j = 0; j < parametersJson.length(); ++j) {
  120. parameters[j] = JsonDecoder.decodeValue(parameterTypes[j],
  121. parametersJson.get(j), null, connection);
  122. }
  123. methodInvocation.setParameters(parameters);
  124. }
  125. private static Logger getLogger() {
  126. return Logger.getLogger(RpcManager.class.getName());
  127. }
  128. }