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.

ConnectorBundleLoader.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.metadata;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.google.gwt.core.client.JsArrayString;
  22. import com.google.gwt.core.shared.GWT;
  23. import com.google.gwt.dom.client.Style;
  24. import com.google.gwt.dom.client.Style.Display;
  25. import com.google.gwt.dom.client.Style.Position;
  26. import com.google.gwt.dom.client.Style.TextAlign;
  27. import com.google.gwt.dom.client.Style.Unit;
  28. import com.google.gwt.dom.client.Style.Visibility;
  29. import com.google.gwt.dom.client.Style.WhiteSpace;
  30. import com.google.gwt.event.dom.client.ClickEvent;
  31. import com.google.gwt.event.dom.client.ClickHandler;
  32. import com.google.gwt.event.dom.client.TouchStartEvent;
  33. import com.google.gwt.event.dom.client.TouchStartHandler;
  34. import com.google.gwt.user.client.ui.HTML;
  35. import com.google.gwt.user.client.ui.RootPanel;
  36. import com.vaadin.client.FastStringMap;
  37. import com.vaadin.client.metadata.AsyncBundleLoader.State;
  38. public abstract class ConnectorBundleLoader {
  39. public static class CValUiInfo {
  40. public final String widgetset;
  41. public final String product;
  42. public final String version;
  43. public final String type;
  44. public CValUiInfo(String product, String version, String widgetset,
  45. String type) {
  46. this.product = product;
  47. this.version = version;
  48. this.widgetset = widgetset;
  49. this.type = type;
  50. }
  51. }
  52. public static final String EAGER_BUNDLE_NAME = "__eager";
  53. public static final String DEFERRED_BUNDLE_NAME = "__deferred";
  54. private static ConnectorBundleLoader impl;
  55. private FastStringMap<AsyncBundleLoader> asyncBlockLoaders = FastStringMap
  56. .create();
  57. private FastStringMap<String> identifierToBundle = FastStringMap.create();
  58. private final TypeDataStore datStore = new TypeDataStore();
  59. public ConnectorBundleLoader() {
  60. init();
  61. }
  62. public TypeDataStore getTypeDataStore() {
  63. return datStore;
  64. }
  65. public static ConnectorBundleLoader get() {
  66. if (impl == null) {
  67. impl = GWT.create(ConnectorBundleLoader.class);
  68. }
  69. return impl;
  70. }
  71. public void loadBundle(String packageName, BundleLoadCallback callback) {
  72. AsyncBundleLoader loader = asyncBlockLoaders.get(packageName);
  73. switch (loader.getState()) {
  74. case NOT_STARTED:
  75. loader.load(callback, getTypeDataStore());
  76. break;
  77. case LOADING:
  78. loader.addCallback(callback);
  79. break;
  80. case LOADED:
  81. if (callback != null) {
  82. callback.loaded();
  83. }
  84. break;
  85. case ERROR:
  86. if (callback != null) {
  87. callback.failed(loader.getError());
  88. }
  89. }
  90. }
  91. public boolean isBundleLoaded(String bundleName) {
  92. AsyncBundleLoader loader = asyncBlockLoaders.get(bundleName);
  93. if (loader == null) {
  94. throw new IllegalArgumentException(
  95. "Bundle " + bundleName + " not recognized");
  96. }
  97. return loader.getState() == State.LOADED;
  98. }
  99. public void setLoaded(String packageName) {
  100. List<BundleLoadCallback> callbacks = asyncBlockLoaders.get(packageName)
  101. .setLoaded();
  102. for (BundleLoadCallback callback : callbacks) {
  103. if (callback != null) {
  104. callback.loaded();
  105. }
  106. }
  107. }
  108. public void setLoadFailure(String bundleName, Throwable reason) {
  109. reason = new RuntimeException("Failed to load bundle " + bundleName
  110. + ": " + reason.getMessage(), reason);
  111. List<BundleLoadCallback> callbacks = asyncBlockLoaders.get(bundleName)
  112. .setError(reason);
  113. for (BundleLoadCallback callback : callbacks) {
  114. callback.failed(reason);
  115. }
  116. }
  117. public String getBundleForIdentifier(String identifier) {
  118. return identifierToBundle.get(identifier);
  119. }
  120. protected void addAsyncBlockLoader(AsyncBundleLoader loader) {
  121. String name = loader.getName();
  122. asyncBlockLoaders.put(name, loader);
  123. String[] indentifiers = loader.getIndentifiers();
  124. for (String identifier : indentifiers) {
  125. identifierToBundle.put(identifier, name);
  126. }
  127. }
  128. public abstract void init();
  129. protected List<CValUiInfo> cvals = new ArrayList<>();
  130. public void cval(String typeName) {
  131. if (!cvals.isEmpty()) {
  132. for (CValUiInfo c : cvals) {
  133. String ns = c.widgetset.replaceFirst("\\.[^\\.]+$", "");
  134. if (typeName.startsWith(ns)) {
  135. notice(c.product + " " + c.version);
  136. cvals.remove(c);
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. private HTML notice;
  143. // Not using Vaadin notifications (#14597)
  144. private void notice(String productName) {
  145. if (notice == null) {
  146. notice = new HTML();
  147. notice.addClickHandler(new ClickHandler() {
  148. @Override
  149. public void onClick(ClickEvent event) {
  150. notice.removeFromParent();
  151. }
  152. });
  153. notice.addTouchStartHandler(new TouchStartHandler() {
  154. @Override
  155. public void onTouchStart(TouchStartEvent event) {
  156. notice.removeFromParent();
  157. }
  158. });
  159. }
  160. String msg = notice.getText().trim();
  161. msg += msg.isEmpty() ? "Using Evaluation License of: " : ", ";
  162. notice.setText(msg + productName);
  163. RootPanel.get().add(notice);
  164. notice.getElement().setClassName("");
  165. Style s = notice.getElement().getStyle();
  166. s.setPosition(Position.FIXED);
  167. s.setTextAlign(TextAlign.CENTER);
  168. s.setRight(0, Unit.PX);
  169. s.setLeft(0, Unit.PX);
  170. s.setBottom(0, Unit.PX);
  171. s.setProperty("padding", "0.5em 1em");
  172. s.setProperty("font-family", "sans-serif");
  173. s.setFontSize(12, Unit.PX);
  174. s.setLineHeight(1.1, Unit.EM);
  175. s.setColor("white");
  176. s.setBackgroundColor("black");
  177. s.setOpacity(0.7);
  178. s.setZIndex(2147483646);
  179. s.setProperty("top", "auto");
  180. s.setProperty("width", "auto");
  181. s.setDisplay(Display.BLOCK);
  182. s.setWhiteSpace(WhiteSpace.NORMAL);
  183. s.setVisibility(Visibility.VISIBLE);
  184. s.setMargin(0, Unit.PX);
  185. }
  186. /**
  187. * Starts loading the deferred bundle if it hasn't already been started.
  188. */
  189. public void ensureDeferredBundleLoaded() {
  190. if (!isBundleLoaded(DEFERRED_BUNDLE_NAME)) {
  191. loadBundle(DEFERRED_BUNDLE_NAME, new BundleLoadCallback() {
  192. @Override
  193. public void loaded() {
  194. // Nothing to do
  195. }
  196. @Override
  197. public void failed(Throwable reason) {
  198. getLogger().log(Level.SEVERE,
  199. "Error loading deferred bundle", reason);
  200. }
  201. });
  202. }
  203. }
  204. private static Logger getLogger() {
  205. return Logger.getLogger(ConnectorBundleLoader.class.getName());
  206. }
  207. /**
  208. * Gets a list of all currently loaded bundle names.
  209. * <p>
  210. * This method is intended for testing the loading mechanism.
  211. *
  212. * @return a list of bundles, not <code>null</code>
  213. */
  214. public List<String> getLoadedBundles() {
  215. ArrayList<String> bundles = new ArrayList<>();
  216. JsArrayString keys = asyncBlockLoaders.getKeys();
  217. for (int i = 0; i < keys.length(); i++) {
  218. String bundleName = keys.get(i);
  219. if (isBundleLoaded(bundleName)) {
  220. bundles.add(bundleName);
  221. }
  222. }
  223. return bundles;
  224. }
  225. }