Interface Lookup

All Known Implementing Classes:
LookupInitializer.LookupImpl

public interface Lookup
Provides a way to discover services used by Flow (SPI).

A lookup instance may be created based on a service, see of(Object, Class...). Several lookup instances may be combined via compose(Lookup, Lookup) method which allows to make a lookup instance based on a number of services. The resulting lookup instance may be used in internal Flow code to transfer data in the unified way which allows to change the available data types during the code evolution without changing the internal API (like arguments in methods and constructors).

There is the "global" application Lookup instance and the VaadinContext. It has one to one mapping and is available even before a DeploymentConfiguration (and VaadinServlet) is created. So this is kind of a singleton for a Web Application. As a consequence it provides and may return only web app singleton services. Dependency injection frameworks can provide an implementation for the application Lookup which manages instances according to the conventions of that framework.

The application Lookup is similar to the Instantiator class but a Lookup instance is available even before a VaadinService instance is created (and as a consequence there is no yet an Instantiator instance).

This is the code which one may use to get the application Lookup instance:

 
     VaadinContext context = ...;
     Lookup lookup = context.getAttribute(Lookup.class);
 
 

This SPI is mostly for internal framework usage since Instantiator provides all required services for the application developer.

Since:
Author:
Vaadin Ltd
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Lookup
    compose(Lookup lookup1, Lookup lookup2)
    Make a composite lookup which contains the services from both lookup1 and lookup2.
    <T> T
    lookup(Class<T> serviceClass)
    Lookup for a service of the given type.
    <T> Collection<T>
    lookupAll(Class<T> serviceClass)
    Lookup for all services by the provided serviceClass.
    static <T> Lookup
    of(T service, Class<? super T>... serviceTypes)
    Creates a lookup which contains (only) the provided service as instance of given serviceTypes.
  • Method Details

    • lookup

      <T> T lookup(Class<T> serviceClass)
      Lookup for a service of the given type.

      The serviceClass is usually an interface (though it doesn't have to be) and the returned value is some implementation of this interface.

      Type Parameters:
      T - a service type
      Parameters:
      serviceClass - a service SPI class
      Returns:
      a service which implements the serviceClass, may be null if no services are registered for this SPI
      See Also:
    • lookupAll

      <T> Collection<T> lookupAll(Class<T> serviceClass)
      Lookup for all services by the provided serviceClass.

      The serviceClass is usually an interface class (though it doesn't have to be) and the returned value is all implementations of this interface.

      Type Parameters:
      T - a service type
      Parameters:
      serviceClass - a service SPI class
      Returns:
      all services which implement the serviceClass, if no services found an empty list is returned (so null is not returned)
    • of

      @SafeVarargs static <T> Lookup of(T service, Class<? super T>... serviceTypes)
      Creates a lookup which contains (only) the provided service as instance of given serviceTypes.

      This method may be used to create a temporary lookup which then can be used to extend an existing lookup via compose(Lookup, Lookup).

      Type Parameters:
      T - the service type
      Parameters:
      service - the service object
      serviceTypes - the supertypes of the service which may be used to access the service
      Returns:
      a lookup initialized with the given service
    • compose

      static Lookup compose(Lookup lookup1, Lookup lookup2)
      Make a composite lookup which contains the services from both lookup1 and lookup2.

      lookup(Class) method will return the service from the first lookup if it's not null and fallbacks to the lookup2 otherwise. So the first lookup takes precedence. The method lookupAll(Class) simply combines all the services from both lookups.

      The resulting lookup is intended to be a "temporary" (short living) lookup to extend an existing lookup with some additional data which is required only in some isolated object.

      Parameters:
      lookup1 - the first lookup to compose
      lookup2 - the second lookup to compose
      Returns:
      the composite lookup