Class ServiceSupplier<T>

  • Type Parameters:
    T - the supplying service type
    All Implemented Interfaces:
    AutoCloseable, Supplier<T>

    public class ServiceSupplier<T>
    extends Object
    implements AutoCloseable, Supplier<T>
    Supplier of an instance of the single known service for the given type. Can be used to get and use service multiple times. Clients are responsible to close supplier when no longer needed. Try-with-resources usage is possible.

    Typical usage example:

     public class MyClass
     {
         private ServiceSupplier<IPublicService> publicService =
             ServiceAccess.supplier(IPublicService.class, MyClassPlugin.getPlugin());
    
         public void serviceUasageMethod(EObject modelObject)
         {
             ...
    
             // Use service, when needed
             getPublicService().method1(modelObject);
    
             ...
    
             getPublicService().method2(modelObject);
    
             ...
         }
    
         public void dispose()
         {
             // Close access, when no longer needed
             publicService.close();
         }
    
         private IPublicService getPublicService()
         {
             return publicService.get();
         }
     }
     
    • Constructor Detail

      • ServiceSupplier

        public ServiceSupplier​(Class<T> serviceType,
                               org.osgi.framework.BundleContext context)
        Creates service supplier with the given class.
        Parameters:
        serviceType - the type of service to get, cannot be null
        context - the caller bundle context to get service, cannot be null
      • ServiceSupplier

        public ServiceSupplier​(Class<T> serviceType,
                               org.osgi.framework.BundleContext context,
                               Map<String,​?> properties)
        Creates service supplier with the given class and OSGi service properties.
        Parameters:
        serviceType - the type of service to get, cannot be null
        context - the caller bundle context to get service, cannot be null
        properties - the service properties map, cannot be null
    • Method Detail

      • get

        public T get()
        Returns a supplied service.

        If service is unavailable, supplier will wait for service for a certain time to prevent initialization issues (invoking thread will be blocked) and then ServiceUnavailableException will be thrown.

        Specified by:
        get in interface Supplier<T>
        Throws:
        ServiceUnavailableException - if there is no such service available or get timeout reached
      • close

        public void close()
        Closes supplier service access.
        Specified by:
        close in interface AutoCloseable
      • finalize

        @Deprecated(forRemoval=true)
        protected void finalize()
                         throws Throwable
        Deprecated, for removal: This API element is subject to removal in a future version.
        The finalize method has been deprecated and will be removed.
        Ensures that the close() method of this supplier is called when there are no more references to it.
        Overrides:
        finalize in class Object
        Throws:
        Throwable