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

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