Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ServerRpcManager.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Field;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.vaadin.shared.Connector;
  25. /**
  26. * Server side RPC manager that handles RPC calls coming from the client.
  27. *
  28. * Each {@link RpcTarget} (typically a {@link ClientConnector}) should have its
  29. * own instance of {@link ServerRpcManager} if it wants to receive RPC calls
  30. * from the client.
  31. *
  32. * @since 7.0
  33. */
  34. public class ServerRpcManager<T> implements RpcManager {
  35. private final T implementation;
  36. private final Class<T> rpcInterface;
  37. private static final Map<Class<?>, Class<?>> boxedTypes = new HashMap<Class<?>, Class<?>>();
  38. static {
  39. try {
  40. Class<?>[] boxClasses = new Class<?>[] { Boolean.class, Byte.class,
  41. Short.class, Character.class, Integer.class, Long.class,
  42. Float.class, Double.class };
  43. for (Class<?> boxClass : boxClasses) {
  44. Field typeField = boxClass.getField("TYPE");
  45. Class<?> primitiveType = (Class<?>) typeField.get(boxClass);
  46. boxedTypes.put(primitiveType, boxClass);
  47. }
  48. } catch (Exception e) {
  49. throw new RuntimeException(e);
  50. }
  51. }
  52. /**
  53. * Create a RPC manager for an RPC target.
  54. *
  55. * @param target
  56. * RPC call target (normally a {@link Connector})
  57. * @param implementation
  58. * RPC interface implementation for the target
  59. * @param rpcInterface
  60. * RPC interface type
  61. */
  62. public ServerRpcManager(T implementation, Class<T> rpcInterface) {
  63. this.implementation = implementation;
  64. this.rpcInterface = rpcInterface;
  65. }
  66. /**
  67. * Invoke a method in a server side RPC target class. This method is to be
  68. * used by the RPC framework and unit testing tools only.
  69. *
  70. * @param target
  71. * non-null target of the RPC call
  72. * @param invocation
  73. * method invocation to perform
  74. * @throws RpcInvocationException
  75. */
  76. public static void applyInvocation(RpcTarget target,
  77. ServerRpcMethodInvocation invocation) throws RpcInvocationException {
  78. RpcManager manager = target.getRpcManager(invocation
  79. .getInterfaceClass());
  80. if (manager != null) {
  81. manager.applyInvocation(invocation);
  82. } else {
  83. getLogger()
  84. .log(Level.WARNING,
  85. "RPC call received for RpcTarget "
  86. + target.getClass().getName()
  87. + " ("
  88. + invocation.getConnectorId()
  89. + ") but the target has not registered any RPC interfaces");
  90. }
  91. }
  92. /**
  93. * Returns the RPC interface implementation for the RPC target.
  94. *
  95. * @return RPC interface implementation
  96. */
  97. protected T getImplementation() {
  98. return implementation;
  99. }
  100. /**
  101. * Returns the RPC interface type managed by this RPC manager instance.
  102. *
  103. * @return RPC interface type
  104. */
  105. protected Class<T> getRpcInterface() {
  106. return rpcInterface;
  107. }
  108. /**
  109. * Invoke a method in a server side RPC target class. This method is to be
  110. * used by the RPC framework and unit testing tools only.
  111. *
  112. * @param invocation
  113. * method invocation to perform
  114. */
  115. @Override
  116. public void applyInvocation(ServerRpcMethodInvocation invocation)
  117. throws RpcInvocationException {
  118. Method method = invocation.getMethod();
  119. Class<?>[] parameterTypes = method.getParameterTypes();
  120. Object[] args = new Object[parameterTypes.length];
  121. Object[] arguments = invocation.getParameters();
  122. for (int i = 0; i < args.length; i++) {
  123. // no conversion needed for basic cases
  124. // Class<?> type = parameterTypes[i];
  125. // if (type.isPrimitive()) {
  126. // type = boxedTypes.get(type);
  127. // }
  128. args[i] = arguments[i];
  129. }
  130. try {
  131. method.invoke(implementation, args);
  132. } catch (Exception e) {
  133. throw new RpcInvocationException("Unable to invoke method "
  134. + invocation.getMethodName() + " in "
  135. + invocation.getInterfaceName(), e);
  136. }
  137. }
  138. private static Logger getLogger() {
  139. return Logger.getLogger(ServerRpcManager.class.getName());
  140. }
  141. }