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.

RpcProxy.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 com.vaadin.client.ServerConnector;
  18. import com.vaadin.client.metadata.InvokationHandler;
  19. import com.vaadin.client.metadata.Method;
  20. import com.vaadin.client.metadata.NoDataException;
  21. import com.vaadin.client.metadata.TypeData;
  22. import com.vaadin.shared.communication.MethodInvocation;
  23. import com.vaadin.shared.communication.ServerRpc;
  24. /**
  25. * Class for creating proxy instances for Client to Server RPC.
  26. *
  27. * @since 7.0
  28. */
  29. public class RpcProxy {
  30. public static <T extends ServerRpc> T create(Class<T> rpcInterface,
  31. ServerConnector connector) {
  32. try {
  33. return (T) TypeData.getType(rpcInterface).createProxy(
  34. new RpcInvokationHandler(rpcInterface, connector));
  35. } catch (NoDataException e) {
  36. throw new IllegalStateException(
  37. "There is no information about " + rpcInterface
  38. + ". Did you forget to compile the widgetset?");
  39. }
  40. }
  41. private static final class RpcInvokationHandler
  42. implements InvokationHandler {
  43. private final Class<?> rpcInterface;
  44. private final ServerConnector connector;
  45. private RpcInvokationHandler(Class<?> rpcInterface,
  46. ServerConnector connector) {
  47. this.rpcInterface = rpcInterface;
  48. this.connector = connector;
  49. }
  50. @Override
  51. public Object invoke(Object target, Method method, Object[] params) {
  52. MethodInvocation invocation = new MethodInvocation(
  53. connector.getConnectorId(), rpcInterface.getName(),
  54. method.getName(), params);
  55. ServerRpcQueue serverRpcQueue = ServerRpcQueue
  56. .get(connector.getConnection());
  57. serverRpcQueue.add(invocation, method.isLastOnly());
  58. if (!method.isDelayed()) {
  59. serverRpcQueue.flush();
  60. }
  61. // No RPC iface should have a return value
  62. return null;
  63. }
  64. }
  65. }