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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.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> invocationMethodCache = new ConcurrentHashMap<String, Method>(
  24. 128, 0.75f, 1);
  25. private final Method method;
  26. private Class<? extends ServerRpc> interfaceClass;
  27. public ServerRpcMethodInvocation(String connectorId, String interfaceName,
  28. String methodName, int parameterCount) {
  29. super(connectorId, interfaceName, methodName);
  30. interfaceClass = findClass();
  31. method = findInvocationMethod(interfaceClass, methodName,
  32. parameterCount);
  33. }
  34. private Class<? extends ServerRpc> findClass() {
  35. try {
  36. Class<?> rpcInterface = Class.forName(getInterfaceName());
  37. if (!ServerRpc.class.isAssignableFrom(rpcInterface)) {
  38. throw new IllegalArgumentException("The interface "
  39. + getInterfaceName() + "is not a server RPC interface.");
  40. }
  41. return (Class<? extends ServerRpc>) rpcInterface;
  42. } catch (ClassNotFoundException e) {
  43. throw new IllegalArgumentException("The server RPC interface "
  44. + getInterfaceName() + " could not be found", e);
  45. } finally {
  46. }
  47. }
  48. public Class<? extends ServerRpc> getInterfaceClass() {
  49. return interfaceClass;
  50. }
  51. public Method getMethod() {
  52. return method;
  53. }
  54. /**
  55. * Tries to find the method from the cache or alternatively by invoking
  56. * {@link #doFindInvocationMethod(Class, String, int)} and updating the
  57. * cache.
  58. *
  59. * @param targetType
  60. * @param methodName
  61. * @param parameterCount
  62. * @return
  63. */
  64. private Method findInvocationMethod(Class<?> targetType, String methodName,
  65. int parameterCount) {
  66. // TODO currently only using method name and number of parameters as the
  67. // signature
  68. String signature = targetType.getName() + "." + methodName + "("
  69. + parameterCount;
  70. Method invocationMethod = invocationMethodCache.get(signature);
  71. if (invocationMethod == null) {
  72. invocationMethod = doFindInvocationMethod(targetType, methodName,
  73. parameterCount);
  74. if (invocationMethod != null) {
  75. invocationMethodCache.put(signature, invocationMethod);
  76. }
  77. }
  78. if (invocationMethod == null) {
  79. throw new IllegalStateException("Can't find method " + methodName
  80. + " with " + parameterCount + " parameters in "
  81. + targetType.getName());
  82. }
  83. return invocationMethod;
  84. }
  85. /**
  86. * Tries to find the method from the class by looping through available
  87. * methods.
  88. *
  89. * @param targetType
  90. * @param methodName
  91. * @param parameterCount
  92. * @return
  93. */
  94. private Method doFindInvocationMethod(Class<?> targetType,
  95. String methodName, int parameterCount) {
  96. Method[] methods = targetType.getMethods();
  97. for (Method method : methods) {
  98. Class<?>[] parameterTypes = method.getParameterTypes();
  99. if (method.getName().equals(methodName)
  100. && parameterTypes.length == parameterCount) {
  101. return method;
  102. }
  103. }
  104. return null;
  105. }
  106. }