]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add dependency filters through the service init event (#9368)
authorLeif Åstrand <legioth@gmail.com>
Fri, 19 May 2017 12:16:00 +0000 (15:16 +0300)
committerAleksi Hietanen <aleksi@vaadin.com>
Fri, 19 May 2017 12:16:00 +0000 (15:16 +0300)
We shouldn't create new service loader interfaces since each new such
interface has implications for custom integrations. Instead, the
VaadinServiceInitListener mechanism should be enhanced to allow such
listeners to introduce their own dependency filter implementations
similarly to how VaadinServiceInitListener can already be used to
contribute new request handlers

server/src/main/java/com/vaadin/server/DependencyFilter.java
server/src/main/java/com/vaadin/server/ServiceInitEvent.java
server/src/main/java/com/vaadin/server/VaadinService.java

index 3f826470633b4949468e4ac17cd7d9d8a6e12d87..eeccb5528c8f41ac9064b15d9bc5368e28c48340 100644 (file)
@@ -27,6 +27,8 @@ import com.vaadin.ui.Dependency;
  * Filter for dependencies loaded using {@link StyleSheet @StyleSheet},
  * {@link JavaScript @JavaScript} and {@link HtmlImport @HtmlImport}.
  *
+ * @see ServiceInitEvent#addDependencyFilter(DependencyFilter)
+ *
  * @since 8.1
  */
 @FunctionalInterface
index 0eb5b18cffa65024153107ab668d936819e78d1f..482c23dd2386340e7dc2b3d0e81b059cee6a566b 100644 (file)
@@ -34,6 +34,7 @@ import java.util.Objects;
 public class ServiceInitEvent extends EventObject {
 
     private List<RequestHandler> addedRequestHandlers = new ArrayList<>();
+    private List<DependencyFilter> addedDependencyFilters = new ArrayList<>();
 
     /**
      * Creates a new service init event for a given {@link VaadinService} and
@@ -71,6 +72,33 @@ public class ServiceInitEvent extends EventObject {
         return Collections.unmodifiableList(addedRequestHandlers);
     }
 
+    /**
+     * Adds a new dependency filter that will be used by this service.
+     *
+     * @param dependencyFilter
+     *            the dependency filter to add, not <code>null</code>
+     *
+     * @since 8.1
+     */
+    public void addDependencyFilter(DependencyFilter dependencyFilter) {
+        Objects.requireNonNull(dependencyFilter,
+                "Dependency filter cannot be null");
+
+        addedDependencyFilters.add(dependencyFilter);
+    }
+
+    /**
+     * Gets an unmodifiable list of all dependency filters that have been added
+     * for the service.
+     *
+     * @return the current list of added dependency filters.
+     *
+     * @since 8.1
+     */
+    public List<DependencyFilter> getAddedDependencyFilters() {
+        return Collections.unmodifiableList(addedDependencyFilters);
+    }
+
     @Override
     public VaadinService getSource() {
         return (VaadinService) super.getSource();
index e05b935929350363b6bb7fae34bc999ad01df4c4..649826a5d6048f2d5dee9aab5d4e8590d5a2ed30 100644 (file)
@@ -207,8 +207,8 @@ public abstract class VaadinService implements Serializable {
 
         requestHandlers = Collections.unmodifiableCollection(handlers);
 
-        dependencyFilters = Collections
-                .unmodifiableCollection(createDependencyFilters());
+        dependencyFilters = Collections.unmodifiableCollection(
+                initDependencyFilters(event.getAddedDependencyFilters()));
         initialized = true;
     }
 
@@ -1444,41 +1444,43 @@ public abstract class VaadinService implements Serializable {
     }
 
     /**
-     * Constructs the list of resource dependency filters to use for the
+     * Updates the list of resource dependency filters to use for the
      * application.
      * <p>
      * The filters can freely update the dependencies in any way they see fit
      * (bundle, rewrite, merge).
      * <p>
-     * By default all filters found using the service loader are added to the
-     * list.
+     * The framework collects filters from the {@link SessionInitEvent} where
+     * session init listeners can add them. This method is called with the
+     * combined list to optionally modify it, and the result is then stored
+     * by the caller as the final list to use.
      * <p>
-     * The filters are called in the order the service loader returns the
-     * filters, which is undefined. If you need a specific order, you can
+     * The filters are called in the order the session init listeners are
+     * called, which is undefined. If you need a specific order, you can
      * override this method and alter the order.
      *
      * @since 8.1
+     * @param sessionInitFilters
+     *            a list of dependency filters collected from the session init
+     *            event
      * @return the list of dependency filters to use for filtering resources,
      *         not null
      * @throws ServiceException
      *             if something went wrong while determining the filters
      *
      */
-    protected List<DependencyFilter> createDependencyFilters()
-            throws ServiceException {
-        ArrayList<DependencyFilter> filters = new ArrayList<>();
-        ServiceLoader<DependencyFilter> loader = ServiceLoader
-                .load(DependencyFilter.class, getClassLoader());
-        loader.iterator().forEachRemaining(filters::add);
+    protected List<DependencyFilter> initDependencyFilters(
+            List<DependencyFilter> sessionInitFilters) throws ServiceException {
+        assert sessionInitFilters != null;
 
-        return filters;
+        return sessionInitFilters;
     }
 
     /**
      * Gets the filters which all resource dependencies are passed through
      * before being sent to the client for loading.
      *
-     * @see #createDependencyFilters()
+     * @see #initDependencyFilters()
      *
      * @since 8.1
      * @return the dependency filters to pass resources dependencies through