Search in sources :

Example 1 with InstantiatorFactory

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

the class VaadinServiceTest method loadInstantiators_twoFactoriesInLookup_throws.

@Test(expected = ServiceException.class)
public void loadInstantiators_twoFactoriesInLookup_throws() throws ServiceException {
    VaadinService service = createService();
    Lookup lookup = Mockito.mock(Lookup.class);
    service.getContext().setAttribute(Lookup.class, lookup);
    InstantiatorFactory factory1 = createInstantiatorFactory(lookup);
    InstantiatorFactory factory2 = createInstantiatorFactory(lookup);
    Mockito.when(lookup.lookupAll(InstantiatorFactory.class)).thenReturn(Arrays.asList(factory1, factory2));
    service.loadInstantiators();
}
Also used : InstantiatorFactory(com.vaadin.flow.di.InstantiatorFactory) Lookup(com.vaadin.flow.di.Lookup) Test(org.junit.Test)

Example 2 with InstantiatorFactory

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

the class VaadinServiceTest method createInstantiatorFactory.

private InstantiatorFactory createInstantiatorFactory(Lookup lookup) {
    InstantiatorFactory factory = Mockito.mock(InstantiatorFactory.class);
    Instantiator instantiator = Mockito.mock(Instantiator.class);
    Mockito.when((factory.createInstantitor(Mockito.any()))).thenReturn(instantiator);
    Mockito.when(instantiator.init(Mockito.any())).thenReturn(true);
    return factory;
}
Also used : InstantiatorFactory(com.vaadin.flow.di.InstantiatorFactory) Instantiator(com.vaadin.flow.di.Instantiator)

Example 3 with InstantiatorFactory

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

the class VaadinServiceTest method loadInstantiators_instantiatorIsLoadedUsingFactoryFromLookup.

@Test
public void loadInstantiators_instantiatorIsLoadedUsingFactoryFromLookup() throws ServiceException {
    VaadinService service = createService();
    Lookup lookup = Mockito.mock(Lookup.class);
    service.getContext().setAttribute(Lookup.class, lookup);
    InstantiatorFactory factory = createInstantiatorFactory(lookup);
    Mockito.when(lookup.lookupAll(InstantiatorFactory.class)).thenReturn(Collections.singletonList(factory));
    Optional<Instantiator> loadedInstantiator = service.loadInstantiators();
    Instantiator instantiator = factory.createInstantitor(null);
    Assert.assertSame(instantiator, loadedInstantiator.get());
}
Also used : InstantiatorFactory(com.vaadin.flow.di.InstantiatorFactory) Lookup(com.vaadin.flow.di.Lookup) Instantiator(com.vaadin.flow.di.Instantiator) Test(org.junit.Test)

Example 4 with InstantiatorFactory

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

the class VaadinService method loadInstantiators.

/**
 * Loads and initializes instantiators.
 * <p>
 * A custom Vaadin service implementation can override this method to pick
 * an instantiator in some other way instead of the default implementation
 * that uses {@link ServiceLoader}.
 * <p>
 * There may be only one applicable instantiator. Otherwise
 * {@link ServiceException} will be thrown.
 *
 * @return an optional instantator, or an empty optional if no instantiator
 *         found
 * @throws ServiceException
 *             if there are multiple applicable instantiators
 * @see #createInstantiator()
 * @see Instantiator
 */
protected Optional<Instantiator> loadInstantiators() throws ServiceException {
    Lookup lookup = getContext().getAttribute(Lookup.class);
    List<Instantiator> instantiators = null;
    if (lookup != null) {
        // lookup may be null in tests
        Collection<InstantiatorFactory> factories = lookup.lookupAll(InstantiatorFactory.class);
        instantiators = new ArrayList<>(factories.size());
        for (InstantiatorFactory factory : factories) {
            Instantiator instantiator = factory.createInstantitor(this);
            // let's respect its deprecated method
            if (instantiator != null && instantiator.init(this)) {
                instantiators.add(instantiator);
            }
        }
    }
    if (instantiators == null) {
        instantiators = new ArrayList<>();
    }
    // the code to support previous way of loading instantiators
    StreamSupport.stream(ServiceLoader.load(Instantiator.class, getClassLoader()).spliterator(), false).filter(iterator -> iterator.init(this)).forEach(instantiators::add);
    if (instantiators.size() > 1) {
        throw new ServiceException("Cannot init VaadinService because there are multiple eligible instantiator implementations: " + instantiators);
    }
    return instantiators.stream().findFirst();
}
Also used : InstantiatorFactory(com.vaadin.flow.di.InstantiatorFactory) StreamRequestHandler(com.vaadin.flow.server.communication.StreamRequestHandler) URL(java.net.URL) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) Registration(com.vaadin.flow.shared.Registration) LoggerFactory(org.slf4j.LoggerFactory) Json(elemental.json.Json) UidlRequestHandler(com.vaadin.flow.server.communication.UidlRequestHandler) Router(com.vaadin.flow.router.Router) Future(java.util.concurrent.Future) PushMode(com.vaadin.flow.shared.communication.PushMode) JsonUtil(elemental.json.impl.JsonUtil) Locale(java.util.Locale) Map(java.util.Map) UsageStatistics(com.vaadin.flow.internal.UsageStatistics) WebComponentBootstrapHandler(com.vaadin.flow.server.communication.WebComponentBootstrapHandler) IndexHtmlResponse(com.vaadin.flow.server.communication.IndexHtmlResponse) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) I18NProvider(com.vaadin.flow.i18n.I18NProvider) PrintWriter(java.io.PrintWriter) RouteData(com.vaadin.flow.router.RouteData) Servlet(javax.servlet.Servlet) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SessionRequestHandler(com.vaadin.flow.server.communication.SessionRequestHandler) ServiceLoader(java.util.ServiceLoader) WebComponentProvider(com.vaadin.flow.server.communication.WebComponentProvider) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) IndexHtmlRequestListener(com.vaadin.flow.server.communication.IndexHtmlRequestListener) List(java.util.List) JavaScriptBootstrapHandler(com.vaadin.flow.server.communication.JavaScriptBootstrapHandler) Entry(java.util.Map.Entry) Optional(java.util.Optional) HeartbeatHandler(com.vaadin.flow.server.communication.HeartbeatHandler) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) LocaleUtil(com.vaadin.flow.internal.LocaleUtil) MessageDigest(java.security.MessageDigest) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) StreamSupport(java.util.stream.StreamSupport) PwaHandler(com.vaadin.flow.server.communication.PwaHandler) JsonException(elemental.json.JsonException) OutputStream(java.io.OutputStream) Instantiator(com.vaadin.flow.di.Instantiator) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BufferedWriter(java.io.BufferedWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) JsonConstants(com.vaadin.flow.shared.JsonConstants) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InstantiatorFactory(com.vaadin.flow.di.InstantiatorFactory) TimeUnit(java.util.concurrent.TimeUnit) AtmospherePushConnection(com.vaadin.flow.server.communication.AtmospherePushConnection) Lock(java.util.concurrent.locks.Lock) DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) ServletContext(javax.servlet.ServletContext) JsonObject(elemental.json.JsonObject) RequestType(com.vaadin.flow.server.HandlerHelper.RequestType) Collections(java.util.Collections) InputStream(java.io.InputStream) Lookup(com.vaadin.flow.di.Lookup) Instantiator(com.vaadin.flow.di.Instantiator) DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator)

Aggregations

InstantiatorFactory (com.vaadin.flow.di.InstantiatorFactory)4 Instantiator (com.vaadin.flow.di.Instantiator)3 Lookup (com.vaadin.flow.di.Lookup)3 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 I18NProvider (com.vaadin.flow.i18n.I18NProvider)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 LocaleUtil (com.vaadin.flow.internal.LocaleUtil)1 UsageStatistics (com.vaadin.flow.internal.UsageStatistics)1 RouteData (com.vaadin.flow.router.RouteData)1 Router (com.vaadin.flow.router.Router)1 RequestType (com.vaadin.flow.server.HandlerHelper.RequestType)1 AtmospherePushConnection (com.vaadin.flow.server.communication.AtmospherePushConnection)1 HeartbeatHandler (com.vaadin.flow.server.communication.HeartbeatHandler)1 IndexHtmlRequestListener (com.vaadin.flow.server.communication.IndexHtmlRequestListener)1 IndexHtmlResponse (com.vaadin.flow.server.communication.IndexHtmlResponse)1 JavaScriptBootstrapHandler (com.vaadin.flow.server.communication.JavaScriptBootstrapHandler)1 PwaHandler (com.vaadin.flow.server.communication.PwaHandler)1