Search in sources :

Example 1 with AbstractLookupInitializer

use of com.vaadin.flow.di.AbstractLookupInitializer in project flow by vaadin.

the class LookupServletContainerInitializer method getLookupInitializer.

private AbstractLookupInitializer getLookupInitializer(Map<Class<?>, Collection<Class<?>>> services) throws ServletException {
    Collection<Class<?>> initializers = services.remove(AbstractLookupInitializer.class);
    if (initializers == null) {
        initializers = Collections.emptyList();
    } else {
        initializers.remove(LookupInitializer.class);
    }
    AbstractLookupInitializer initializer;
    if (initializers.isEmpty()) {
        initializer = new LookupInitializer();
    } else if (initializers.size() > 1) {
        throw new ServletException("Several implementation of " + AbstractLookupInitializer.class.getSimpleName() + " are found in the claspath: " + initializers);
    } else {
        initializer = AbstractLookupInitializer.class.cast(ReflectTools.createInstance(initializers.iterator().next()));
    }
    return initializer;
}
Also used : ServletException(javax.servlet.ServletException) AbstractLookupInitializer(com.vaadin.flow.di.AbstractLookupInitializer) AbstractLookupInitializer(com.vaadin.flow.di.AbstractLookupInitializer) LookupInitializer(com.vaadin.flow.di.LookupInitializer)

Example 2 with AbstractLookupInitializer

use of com.vaadin.flow.di.AbstractLookupInitializer in project flow by vaadin.

the class LookupServletContainerInitializer method process.

@Override
public void process(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException {
    if (classSet == null) {
        throw new ServletException(ServletContainerInitializer.class.getSimpleName() + " is called but the " + "provided set of classes is 'null'. " + LookupInitializer.class + " always presents " + "and has to be passed to the 'onStartup' method as an argument " + "in the set of classes if the servlet container supports Servlet 3.0 specification. " + "The propject configuration is broken somehow or you are using Servlet 3.0 incompatible container.");
    }
    if (!classSet.contains(LookupInitializer.class)) {
        // executed at all.
        return;
    }
    VaadinServletContext vaadinContext = new VaadinServletContext(servletContext);
    Map<Class<?>, Collection<Class<?>>> services = new HashMap<>();
    collectSubclasses(AbstractLookupInitializer.class, classSet, services);
    AbstractLookupInitializer initializer = getLookupInitializer(services);
    services.remove(AbstractLookupInitializer.class);
    collectServiceImplementations(classSet, services);
    initializer.initialize(vaadinContext, services, lookup -> {
        vaadinContext.setAttribute(Lookup.class, lookup);
        DeferredServletContextInitializers deferredInitializers;
        synchronized (servletContext) {
            deferredInitializers = vaadinContext.getAttribute(DeferredServletContextInitializers.class);
            vaadinContext.removeAttribute(DeferredServletContextInitializers.class);
        }
        if (deferredInitializers != null) {
            deferredInitializers.runInitializers(servletContext);
        }
    });
}
Also used : ServletException(javax.servlet.ServletException) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) HashMap(java.util.HashMap) Collection(java.util.Collection) AbstractLookupInitializer(com.vaadin.flow.di.AbstractLookupInitializer) AbstractLookupInitializer(com.vaadin.flow.di.AbstractLookupInitializer) LookupInitializer(com.vaadin.flow.di.LookupInitializer)

Aggregations

AbstractLookupInitializer (com.vaadin.flow.di.AbstractLookupInitializer)2 LookupInitializer (com.vaadin.flow.di.LookupInitializer)2 ServletException (javax.servlet.ServletException)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1