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.

AbstractConnector.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  11. import com.vaadin.terminal.gwt.client.ServerConnector;
  12. import com.vaadin.terminal.gwt.client.communication.ClientRpc;
  13. /**
  14. * An abstract implementation of Connector.
  15. *
  16. * @author Vaadin Ltd
  17. * @version @VERSION@
  18. * @since 7.0.0
  19. *
  20. */
  21. public abstract class AbstractConnector implements ServerConnector {
  22. private ApplicationConnection connection;
  23. private String id;
  24. private Map<String, Collection<ClientRpc>> rpcImplementations;
  25. /*
  26. * (non-Javadoc)
  27. *
  28. * @see com.vaadin.terminal.gwt.client.VPaintable#getConnection()
  29. */
  30. public final ApplicationConnection getConnection() {
  31. return connection;
  32. }
  33. /*
  34. * (non-Javadoc)
  35. *
  36. * @see com.vaadin.terminal.gwt.client.Connector#getId()
  37. */
  38. public String getId() {
  39. return id;
  40. }
  41. /**
  42. * Called once by the framework to initialize the connector.
  43. * <p>
  44. * Note that the shared state is not yet available when this method is
  45. * called.
  46. * <p>
  47. * Connector classes should override {@link #init()} instead of this method.
  48. */
  49. public final void doInit(String connectorId,
  50. ApplicationConnection connection) {
  51. this.connection = connection;
  52. id = connectorId;
  53. init();
  54. }
  55. /**
  56. * Called when the connector has been initialized. Override this method to
  57. * perform initialization of the connector.
  58. */
  59. // FIXME: It might make sense to make this abstract to force users to
  60. // use init instead of constructor, where connection and id has not yet been
  61. // set.
  62. protected void init() {
  63. }
  64. /**
  65. * Registers an implementation for a server to client RPC interface.
  66. *
  67. * Multiple registrations can be made for a single interface, in which case
  68. * all of them receive corresponding RPC calls.
  69. *
  70. * @param rpcInterface
  71. * RPC interface
  72. * @param implementation
  73. * implementation that should receive RPC calls
  74. * @param <T>
  75. * The type of the RPC interface that is being registered
  76. */
  77. protected <T extends ClientRpc> void registerRpc(Class<T> rpcInterface,
  78. T implementation) {
  79. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  80. if (null == rpcImplementations) {
  81. rpcImplementations = new HashMap<String, Collection<ClientRpc>>();
  82. }
  83. if (null == rpcImplementations.get(rpcInterfaceId)) {
  84. rpcImplementations.put(rpcInterfaceId, new ArrayList<ClientRpc>());
  85. }
  86. rpcImplementations.get(rpcInterfaceId).add(implementation);
  87. }
  88. /**
  89. * Unregisters an implementation for a server to client RPC interface.
  90. *
  91. * @param rpcInterface
  92. * RPC interface
  93. * @param implementation
  94. * implementation to unregister
  95. */
  96. protected <T extends ClientRpc> void unregisterRpc(Class<T> rpcInterface,
  97. T implementation) {
  98. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  99. if (null != rpcImplementations
  100. && null != rpcImplementations.get(rpcInterfaceId)) {
  101. rpcImplementations.get(rpcInterfaceId).remove(implementation);
  102. }
  103. }
  104. public <T extends ClientRpc> Collection<T> getRpcImplementations(
  105. String rpcInterfaceId) {
  106. if (null == rpcImplementations) {
  107. return Collections.emptyList();
  108. }
  109. return (Collection<T>) rpcImplementations.get(rpcInterfaceId);
  110. }
  111. }