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.

ServerRpcMethodInvocation.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2000-2016 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.server;
  17. import java.lang.reflect.Method;
  18. import java.util.Map;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. import com.vaadin.shared.communication.MethodInvocation;
  21. import com.vaadin.shared.communication.ServerRpc;
  22. public class ServerRpcMethodInvocation extends MethodInvocation {
  23. private static final Map<String, Method> INVOCATION_METHOD_CACHE = new ConcurrentHashMap<>(
  24. 128, 0.75f, 1);
  25. private final Method method;
  26. private final Class<? extends ServerRpc> interfaceClass;
  27. public ServerRpcMethodInvocation(String connectorId,
  28. Class<? extends ServerRpc> interfaceClass, String methodName,
  29. int parameterCount) {
  30. super(connectorId, interfaceClass.getName(), methodName);
  31. assert ServerRpc.class.isAssignableFrom(interfaceClass);
  32. this.interfaceClass = interfaceClass;
  33. method = findInvocationMethod(interfaceClass, methodName,
  34. parameterCount);
  35. }
  36. public Class<? extends ServerRpc> getInterfaceClass() {
  37. return interfaceClass;
  38. }
  39. public Method getMethod() {
  40. return method;
  41. }
  42. /**
  43. * Tries to find the method from the cache or alternatively by invoking
  44. * {@link #doFindInvocationMethod(Class, String, int)} and updating the
  45. * cache.
  46. *
  47. * @param targetType
  48. * @param methodName
  49. * @param parameterCount
  50. * @return
  51. */
  52. private Method findInvocationMethod(Class<?> targetType, String methodName,
  53. int parameterCount) {
  54. // TODO currently only using method name and number of parameters as the
  55. // signature
  56. String signature = targetType.getName() + "." + methodName + "("
  57. + parameterCount;
  58. Method invocationMethod = INVOCATION_METHOD_CACHE.get(signature);
  59. if (invocationMethod == null) {
  60. invocationMethod = doFindInvocationMethod(targetType, methodName,
  61. parameterCount);
  62. if (invocationMethod != null) {
  63. INVOCATION_METHOD_CACHE.put(signature, invocationMethod);
  64. }
  65. }
  66. if (invocationMethod == null) {
  67. throw new IllegalStateException("Can't find method " + methodName
  68. + " with " + parameterCount + " parameters in "
  69. + targetType.getName());
  70. }
  71. return invocationMethod;
  72. }
  73. /**
  74. * Tries to find the method from the class by looping through available
  75. * methods.
  76. *
  77. * @param targetType
  78. * @param methodName
  79. * @param parameterCount
  80. * @return
  81. */
  82. private Method doFindInvocationMethod(Class<?> targetType,
  83. String methodName, int parameterCount) {
  84. Method[] methods = targetType.getMethods();
  85. for (Method method : methods) {
  86. Class<?>[] parameterTypes = method.getParameterTypes();
  87. if (method.getName().equals(methodName)
  88. && parameterTypes.length == parameterCount) {
  89. return method;
  90. }
  91. }
  92. return null;
  93. }
  94. }