diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-05-19 15:16:00 +0300 |
---|---|---|
committer | Aleksi Hietanen <aleksi@vaadin.com> | 2017-05-19 15:16:00 +0300 |
commit | f427fef8da7f8268ae5b0172c3a067bea141dad6 (patch) | |
tree | 86a795f8304d09973e4fb4635fbaa04e1dafa7af | |
parent | 1d755ee77b1ad60ef809dcc48fb09608dd2625c3 (diff) | |
download | vaadin-framework-f427fef8da7f8268ae5b0172c3a067bea141dad6.tar.gz vaadin-framework-f427fef8da7f8268ae5b0172c3a067bea141dad6.zip |
Add dependency filters through the service init event (#9368)
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
3 files changed, 47 insertions, 15 deletions
diff --git a/server/src/main/java/com/vaadin/server/DependencyFilter.java b/server/src/main/java/com/vaadin/server/DependencyFilter.java index 3f82647063..eeccb5528c 100644 --- a/server/src/main/java/com/vaadin/server/DependencyFilter.java +++ b/server/src/main/java/com/vaadin/server/DependencyFilter.java @@ -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 diff --git a/server/src/main/java/com/vaadin/server/ServiceInitEvent.java b/server/src/main/java/com/vaadin/server/ServiceInitEvent.java index 0eb5b18cff..482c23dd23 100644 --- a/server/src/main/java/com/vaadin/server/ServiceInitEvent.java +++ b/server/src/main/java/com/vaadin/server/ServiceInitEvent.java @@ -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(); diff --git a/server/src/main/java/com/vaadin/server/VaadinService.java b/server/src/main/java/com/vaadin/server/VaadinService.java index e05b935929..649826a5d6 100644 --- a/server/src/main/java/com/vaadin/server/VaadinService.java +++ b/server/src/main/java/com/vaadin/server/VaadinService.java @@ -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 |