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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright 2000-2013 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.ui;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;
  23. import com.google.gwt.core.client.JsArrayString;
  24. import com.google.gwt.event.shared.GwtEvent;
  25. import com.google.gwt.event.shared.HandlerManager;
  26. import com.google.web.bindery.event.shared.HandlerRegistration;
  27. import com.vaadin.client.ApplicationConnection;
  28. import com.vaadin.client.FastStringMap;
  29. import com.vaadin.client.JsArrayObject;
  30. import com.vaadin.client.Profiler;
  31. import com.vaadin.client.ServerConnector;
  32. import com.vaadin.client.Util;
  33. import com.vaadin.client.VConsole;
  34. import com.vaadin.client.communication.RpcProxy;
  35. import com.vaadin.client.communication.StateChangeEvent;
  36. import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
  37. import com.vaadin.client.metadata.NoDataException;
  38. import com.vaadin.client.metadata.OnStateChangeMethod;
  39. import com.vaadin.client.metadata.Type;
  40. import com.vaadin.client.metadata.TypeData;
  41. import com.vaadin.client.metadata.TypeDataStore;
  42. import com.vaadin.shared.communication.ClientRpc;
  43. import com.vaadin.shared.communication.ServerRpc;
  44. import com.vaadin.shared.communication.SharedState;
  45. import com.vaadin.shared.communication.URLReference;
  46. /**
  47. * An abstract implementation of Connector.
  48. *
  49. * @author Vaadin Ltd
  50. * @since 7.0.0
  51. *
  52. */
  53. public abstract class AbstractConnector implements ServerConnector,
  54. StateChangeHandler {
  55. private ApplicationConnection connection;
  56. private String id;
  57. private HandlerManager handlerManager;
  58. private FastStringMap<HandlerManager> statePropertyHandlerManagers;
  59. private FastStringMap<Collection<ClientRpc>> rpcImplementations;
  60. private final boolean debugLogging = false;
  61. private SharedState state;
  62. private ServerConnector parent;
  63. /**
  64. * A map from client-to-server RPC interface class to the RPC proxy that
  65. * sends outgoing RPC calls for that interface.
  66. */
  67. private FastStringMap<ServerRpc> rpcProxyMap = FastStringMap.create();
  68. /**
  69. * Temporary storage for last enabled state to be able to see if it has
  70. * changed. Can be removed once we are able to listen specifically for
  71. * enabled changes in the state. Widget.isEnabled() cannot be used as all
  72. * Widgets do not implement HasEnabled
  73. */
  74. private boolean lastEnabledState = true;
  75. private List<ServerConnector> children;
  76. /*
  77. * (non-Javadoc)
  78. *
  79. * @see com.vaadin.client.VPaintable#getConnection()
  80. */
  81. @Override
  82. public final ApplicationConnection getConnection() {
  83. return connection;
  84. }
  85. /*
  86. * (non-Javadoc)
  87. *
  88. * @see com.vaadin.client.Connector#getId()
  89. */
  90. @Override
  91. public String getConnectorId() {
  92. return id;
  93. }
  94. /**
  95. * Called once by the framework to initialize the connector.
  96. * <p>
  97. * Note that the shared state is not yet available when this method is
  98. * called.
  99. * <p>
  100. * Connector classes should override {@link #init()} instead of this method.
  101. */
  102. @Override
  103. public final void doInit(String connectorId,
  104. ApplicationConnection connection) {
  105. Profiler.enter("AbstractConnector.doInit");
  106. this.connection = connection;
  107. id = connectorId;
  108. addStateChangeHandler(this);
  109. if (Profiler.isEnabled()) {
  110. Profiler.enter("AbstractConnector.init " + Util.getSimpleName(this));
  111. }
  112. init();
  113. if (Profiler.isEnabled()) {
  114. Profiler.leave("AbstractConnector.init " + Util.getSimpleName(this));
  115. }
  116. Profiler.leave("AbstractConnector.doInit");
  117. }
  118. /**
  119. * Called when the connector has been initialized. Override this method to
  120. * perform initialization of the connector.
  121. */
  122. // FIXME: It might make sense to make this abstract to force users to
  123. // use init instead of constructor, where connection and id has not yet been
  124. // set.
  125. protected void init() {
  126. }
  127. /**
  128. * Registers an implementation for a server to client RPC interface.
  129. *
  130. * Multiple registrations can be made for a single interface, in which case
  131. * all of them receive corresponding RPC calls.
  132. *
  133. * @param rpcInterface
  134. * RPC interface
  135. * @param implementation
  136. * implementation that should receive RPC calls
  137. * @param <T>
  138. * The type of the RPC interface that is being registered
  139. */
  140. protected <T extends ClientRpc> void registerRpc(Class<T> rpcInterface,
  141. T implementation) {
  142. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  143. if (null == rpcImplementations) {
  144. rpcImplementations = FastStringMap.create();
  145. }
  146. if (null == rpcImplementations.get(rpcInterfaceId)) {
  147. rpcImplementations.put(rpcInterfaceId, new ArrayList<ClientRpc>());
  148. }
  149. rpcImplementations.get(rpcInterfaceId).add(implementation);
  150. }
  151. /**
  152. * Unregisters an implementation for a server to client RPC interface.
  153. *
  154. * @param rpcInterface
  155. * RPC interface
  156. * @param implementation
  157. * implementation to unregister
  158. */
  159. protected <T extends ClientRpc> void unregisterRpc(Class<T> rpcInterface,
  160. T implementation) {
  161. String rpcInterfaceId = rpcInterface.getName().replaceAll("\\$", ".");
  162. if (null != rpcImplementations
  163. && null != rpcImplementations.get(rpcInterfaceId)) {
  164. rpcImplementations.get(rpcInterfaceId).remove(implementation);
  165. }
  166. }
  167. /**
  168. * Returns an RPC proxy object which can be used to invoke the RPC method on
  169. * the server.
  170. *
  171. * @param <T>
  172. * The type of the ServerRpc interface
  173. * @param rpcInterface
  174. * The ServerRpc interface to retrieve a proxy object for
  175. * @return A proxy object which can be used to invoke the RPC method on the
  176. * server.
  177. */
  178. protected <T extends ServerRpc> T getRpcProxy(Class<T> rpcInterface) {
  179. String name = rpcInterface.getName();
  180. if (!rpcProxyMap.containsKey(name)) {
  181. rpcProxyMap.put(name, RpcProxy.create(rpcInterface, this));
  182. }
  183. return (T) rpcProxyMap.get(name);
  184. }
  185. @Override
  186. public <T extends ClientRpc> Collection<T> getRpcImplementations(
  187. String rpcInterfaceId) {
  188. if (null == rpcImplementations) {
  189. return Collections.emptyList();
  190. }
  191. return (Collection<T>) rpcImplementations.get(rpcInterfaceId);
  192. }
  193. @Override
  194. public void fireEvent(GwtEvent<?> event) {
  195. String profilerKey = null;
  196. if (Profiler.isEnabled()) {
  197. profilerKey = "Fire " + Util.getSimpleName(event) + " for "
  198. + Util.getSimpleName(this);
  199. Profiler.enter(profilerKey);
  200. }
  201. if (handlerManager != null) {
  202. handlerManager.fireEvent(event);
  203. }
  204. if (statePropertyHandlerManagers != null
  205. && event instanceof StateChangeEvent) {
  206. Profiler.enter("AbstractConnector.fireEvent statePropertyHandlerManagers");
  207. StateChangeEvent stateChangeEvent = (StateChangeEvent) event;
  208. JsArrayString keys = statePropertyHandlerManagers.getKeys();
  209. for (int i = 0; i < keys.length(); i++) {
  210. String property = keys.get(i);
  211. if (stateChangeEvent.hasPropertyChanged(property)) {
  212. statePropertyHandlerManagers.get(property).fireEvent(event);
  213. }
  214. }
  215. Profiler.leave("AbstractConnector.fireEvent statePropertyHandlerManagers");
  216. }
  217. if (Profiler.isEnabled()) {
  218. Profiler.leave(profilerKey);
  219. }
  220. }
  221. protected HandlerManager ensureHandlerManager() {
  222. if (handlerManager == null) {
  223. handlerManager = new HandlerManager(this);
  224. }
  225. return handlerManager;
  226. }
  227. @Override
  228. public HandlerRegistration addStateChangeHandler(StateChangeHandler handler) {
  229. return ensureHandlerManager()
  230. .addHandler(StateChangeEvent.TYPE, handler);
  231. }
  232. @Override
  233. public void removeStateChangeHandler(StateChangeHandler handler) {
  234. ensureHandlerManager().removeHandler(StateChangeEvent.TYPE, handler);
  235. }
  236. @Override
  237. public HandlerRegistration addStateChangeHandler(String propertyName,
  238. StateChangeHandler handler) {
  239. return ensureHandlerManager(propertyName).addHandler(
  240. StateChangeEvent.TYPE, handler);
  241. }
  242. @Override
  243. public void removeStateChangeHandler(String propertyName,
  244. StateChangeHandler handler) {
  245. ensureHandlerManager(propertyName).removeHandler(StateChangeEvent.TYPE,
  246. handler);
  247. }
  248. private HandlerManager ensureHandlerManager(String propertyName) {
  249. if (statePropertyHandlerManagers == null) {
  250. statePropertyHandlerManagers = FastStringMap.create();
  251. }
  252. HandlerManager manager = statePropertyHandlerManagers.get(propertyName);
  253. if (manager == null) {
  254. manager = new HandlerManager(this);
  255. statePropertyHandlerManagers.put(propertyName, manager);
  256. }
  257. return manager;
  258. }
  259. @Override
  260. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  261. Profiler.enter("AbstractConnector.onStateChanged");
  262. if (debugLogging) {
  263. VConsole.log("State change event for "
  264. + Util.getConnectorString(stateChangeEvent.getConnector())
  265. + " received by " + Util.getConnectorString(this));
  266. }
  267. updateEnabledState(isEnabled());
  268. FastStringMap<JsArrayObject<OnStateChangeMethod>> handlers = TypeDataStore
  269. .getOnStateChangeMethods(getClass());
  270. if (handlers != null) {
  271. Profiler.enter("AbstractConnector.onStateChanged @OnStateChange");
  272. HashSet<OnStateChangeMethod> invokedMethods = new HashSet<OnStateChangeMethod>();
  273. JsArrayString propertyNames = handlers.getKeys();
  274. for (int i = 0; i < propertyNames.length(); i++) {
  275. String propertyName = propertyNames.get(i);
  276. if (stateChangeEvent.hasPropertyChanged(propertyName)) {
  277. JsArrayObject<OnStateChangeMethod> propertyMethods = handlers
  278. .get(propertyName);
  279. for (int j = 0; j < propertyMethods.size(); j++) {
  280. OnStateChangeMethod method = propertyMethods.get(j);
  281. if (invokedMethods.add(method)) {
  282. method.invoke(stateChangeEvent);
  283. }
  284. }
  285. }
  286. }
  287. Profiler.leave("AbstractConnector.onStateChanged @OnStateChange");
  288. }
  289. Profiler.leave("AbstractConnector.onStateChanged");
  290. }
  291. /*
  292. * (non-Javadoc)
  293. *
  294. * @see com.vaadin.client.ServerConnector#onUnregister()
  295. */
  296. @Override
  297. public void onUnregister() {
  298. if (debugLogging) {
  299. VConsole.log("Unregistered connector "
  300. + Util.getConnectorString(this));
  301. }
  302. }
  303. /**
  304. * Returns the shared state object for this connector.
  305. *
  306. * Override this method to define the shared state type for your connector.
  307. *
  308. * @return the current shared state (never null)
  309. */
  310. @Override
  311. public SharedState getState() {
  312. if (state == null) {
  313. Profiler.enter("AbstractConnector.createState()");
  314. state = createState();
  315. Profiler.leave("AbstractConnector.createState()");
  316. }
  317. return state;
  318. }
  319. /**
  320. * Creates a state object with default values for this connector. The
  321. * created state object must be compatible with the return type of
  322. * {@link #getState()}. The default implementation creates a state object
  323. * using GWT.create() using the defined return type of {@link #getState()}.
  324. *
  325. * @return A new state object
  326. */
  327. protected SharedState createState() {
  328. try {
  329. Type stateType = getStateType(this);
  330. Object stateInstance = stateType.createInstance();
  331. return (SharedState) stateInstance;
  332. } catch (NoDataException e) {
  333. throw new IllegalStateException(
  334. "There is no information about the state for "
  335. + Util.getSimpleName(this)
  336. + ". Did you remember to compile the right widgetset?",
  337. e);
  338. }
  339. }
  340. public static Type getStateType(ServerConnector connector) {
  341. try {
  342. return TypeData.getType(connector.getClass()).getMethod("getState")
  343. .getReturnType();
  344. } catch (NoDataException e) {
  345. throw new IllegalStateException(
  346. "There is no information about the state for "
  347. + Util.getSimpleName(connector)
  348. + ". Did you remember to compile the right widgetset?",
  349. e);
  350. }
  351. }
  352. @Override
  353. public ServerConnector getParent() {
  354. return parent;
  355. }
  356. @Override
  357. public void setParent(ServerConnector parent) {
  358. this.parent = parent;
  359. }
  360. @Override
  361. public List<ServerConnector> getChildren() {
  362. if (children == null) {
  363. return Collections.emptyList();
  364. }
  365. return children;
  366. }
  367. @Override
  368. public void setChildren(List<ServerConnector> children) {
  369. this.children = children;
  370. }
  371. @Override
  372. public boolean isEnabled() {
  373. if (!getState().enabled) {
  374. return false;
  375. }
  376. if (getParent() == null) {
  377. return true;
  378. } else {
  379. return getParent().isEnabled();
  380. }
  381. }
  382. @Override
  383. public void updateEnabledState(boolean enabledState) {
  384. if (lastEnabledState == enabledState) {
  385. return;
  386. }
  387. Profiler.enter("AbstractConnector.updateEnabledState");
  388. lastEnabledState = enabledState;
  389. for (ServerConnector c : getChildren()) {
  390. // Update children as they might be affected by the enabled state of
  391. // their parent
  392. c.updateEnabledState(c.isEnabled());
  393. }
  394. Profiler.leave("AbstractConnector.updateEnabledState");
  395. }
  396. /**
  397. * Gets the URL for a resource that has been added by the server-side
  398. * connector using
  399. * {@link com.vaadin.terminal.AbstractClientConnector#setResource(String, com.vaadin.terminal.Resource)}
  400. * with the same key. <code>null</code> is returned if no corresponding
  401. * resource is found.
  402. *
  403. * @param key
  404. * a string identifying the resource.
  405. * @return the resource URL as a string, or <code>null</code> if no
  406. * corresponding resource is found.
  407. */
  408. public String getResourceUrl(String key) {
  409. URLReference urlReference = getState().resources.get(key);
  410. if (urlReference == null) {
  411. return null;
  412. } else {
  413. return urlReference.getURL();
  414. }
  415. }
  416. /*
  417. * (non-Javadoc)
  418. *
  419. * @see com.vaadin.client.ServerConnector#hasEventListener(java.lang.String)
  420. */
  421. @Override
  422. public boolean hasEventListener(String eventIdentifier) {
  423. Set<String> reg = getState().registeredEventListeners;
  424. return (reg != null && reg.contains(eventIdentifier));
  425. }
  426. }