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

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