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.

DependencyLoader.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2018 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;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.core.client.JsArray;
  19. import com.google.gwt.user.client.Command;
  20. import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
  21. import com.vaadin.client.ResourceLoader.ResourceLoadListener;
  22. /**
  23. * Handles loading of dependencies (style sheets and scripts) in the
  24. * application.
  25. *
  26. * Use {@link ApplicationConfiguration#runWhenDependenciesLoaded(Command)} to
  27. * execute a command after all dependencies have finished loading.
  28. *
  29. * @author Vaadin Ltd
  30. * @since 8.0
  31. */
  32. public class DependencyLoader {
  33. private static final String DEPENDENCIES = "dependencies";
  34. private ApplicationConnection connection = null;
  35. private ResourceLoader loader = ResourceLoader.get();
  36. private ResourceLoadListener dependencyLoadingTracker = new ResourceLoadListener() {
  37. @Override
  38. public void onLoad(ResourceLoadEvent event) {
  39. ApplicationConfiguration.endDependencyLoading();
  40. }
  41. @Override
  42. public void onError(ResourceLoadEvent event) {
  43. String error = event.getResourceUrl() + " could not be loaded.";
  44. if (event.getResourceUrl().endsWith("css")) {
  45. error += " or the load detection failed because the stylesheet is empty.";
  46. }
  47. getLogger().severe(error);
  48. // The show must go on
  49. onLoad(event);
  50. }
  51. };
  52. /**
  53. * Sets the ApplicationConnection this instance is connected to.
  54. *
  55. * Only used internally.
  56. *
  57. * @param connection
  58. * The ApplicationConnection for this instance
  59. */
  60. public void setConnection(ApplicationConnection connection) {
  61. if (this.connection != null) {
  62. throw new IllegalStateException(
  63. "Application connection has already been set");
  64. }
  65. if (connection == null) {
  66. throw new IllegalArgumentException(
  67. "ApplicationConnection can not be null");
  68. }
  69. this.connection = connection;
  70. }
  71. /**
  72. * Loads the any dependencies present in the given json snippet.
  73. *
  74. * Handles all dependencies found with the key "{@literal dependencies}".
  75. *
  76. * Ensures that
  77. * <ul>
  78. * <li>JavaScript dependencies are loaded in the given order.
  79. * <li>HTML imports are loaded after all JavaScripts are loaded and
  80. * executed.
  81. * <li>Style sheets are loaded and evaluated in some undefined order
  82. * </ul>
  83. *
  84. * @param json
  85. * the JSON containing the dependencies to load
  86. */
  87. public void loadDependencies(ValueMap json) {
  88. if (!json.containsKey(DEPENDENCIES)) {
  89. return;
  90. }
  91. JsArray<ValueMap> deps = json.getJSValueMapArray(DEPENDENCIES);
  92. for (int i = 0; i < deps.length(); i++) {
  93. ValueMap dep = deps.get(i);
  94. String type = dep.getAsString("type");
  95. String url = connection.translateVaadinUri(dep.getAsString("url"));
  96. ApplicationConfiguration.startDependencyLoading();
  97. if (type.equals("STYLESHEET")) {
  98. loader.loadStylesheet(url, dependencyLoadingTracker);
  99. } else if (type.equals("JAVASCRIPT")) {
  100. loader.loadScript(url, dependencyLoadingTracker);
  101. } else if (type.equals("HTMLIMPORT")) {
  102. loader.loadHtmlImport(url, dependencyLoadingTracker);
  103. } else {
  104. ApplicationConfiguration.endDependencyLoading();
  105. throw new IllegalArgumentException("Unknown type: " + type);
  106. }
  107. }
  108. }
  109. private static Logger getLogger() {
  110. return Logger.getLogger(DependencyLoader.class.getName());
  111. }
  112. }