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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 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("There is no information about "
  37. + rpcInterface
  38. + ". Did you forget to compile the widgetset?");
  39. }
  40. }
  41. private static final class RpcInvokationHandler implements
  42. 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. connector.getConnection().addMethodInvocationToQueue(invocation,
  56. method.isDelayed(), method.isLastOnly());
  57. // No RPC iface should have a return value
  58. return null;
  59. }
  60. }
  61. }