Search in sources :

Example 1 with ScheduledExecutorServiceProvider

use of org.glassfish.jersey.spi.ScheduledExecutorServiceProvider in project jersey by jersey.

the class ExecutorProviders method createInjectionBindings.

/**
     * Create qualified {@link java.util.concurrent.ExecutorService} and {@link java.util.concurrent.ScheduledExecutorService}
     * injection bindings based on the registered providers implementing the
     * {@link org.glassfish.jersey.spi.ExecutorServiceProvider} and/or
     * {@link org.glassfish.jersey.spi.ScheduledExecutorServiceProvider} SPI.
     * <p>
     * This method supports creation of qualified injection bindings based on custom
     * {@link javax.inject.Qualifier qualifier annotations} attached to the registered provider implementation classes
     * as well as named injection bindings based on the {@link javax.inject.Named} qualifier annotation attached to the
     * registered provider implementation classes.
     * </p>
     *
     * @param injectionManager application's injection manager.
     */
public static void createInjectionBindings(InjectionManager injectionManager) {
    /*
         * Add ExecutorService into DI framework.
         */
    Map<Class<? extends Annotation>, List<ExecutorServiceProvider>> executorProviderMap = getQualifierToProviderMap(injectionManager, ExecutorServiceProvider.class);
    for (Map.Entry<Class<? extends Annotation>, List<ExecutorServiceProvider>> qualifierToProviders : executorProviderMap.entrySet()) {
        Class<? extends Annotation> qualifierAnnotationClass = qualifierToProviders.getKey();
        Iterator<ExecutorServiceProvider> bucketProviderIterator = qualifierToProviders.getValue().iterator();
        ExecutorServiceProvider executorProvider = bucketProviderIterator.next();
        logExecutorServiceProvider(qualifierAnnotationClass, bucketProviderIterator, executorProvider);
        SupplierInstanceBinding<ExecutorService> descriptor = Bindings.supplier(new ExecutorServiceSupplier(executorProvider)).in(Singleton.class).to(ExecutorService.class);
        Annotation qualifier = executorProvider.getClass().getAnnotation(qualifierAnnotationClass);
        if (qualifier instanceof Named) {
            descriptor.named(((Named) qualifier).value());
        } else {
            descriptor.qualifiedBy(qualifier);
        }
        injectionManager.register(descriptor);
    }
    /*
         * Add ScheduledExecutorService into DI framework.
         */
    Map<Class<? extends Annotation>, List<ScheduledExecutorServiceProvider>> schedulerProviderMap = getQualifierToProviderMap(injectionManager, ScheduledExecutorServiceProvider.class);
    for (Map.Entry<Class<? extends Annotation>, List<ScheduledExecutorServiceProvider>> qualifierToProviders : schedulerProviderMap.entrySet()) {
        Class<? extends Annotation> qualifierAnnotationClass = qualifierToProviders.getKey();
        Iterator<ScheduledExecutorServiceProvider> bucketProviderIterator = qualifierToProviders.getValue().iterator();
        ScheduledExecutorServiceProvider executorProvider = bucketProviderIterator.next();
        logScheduledExecutorProvider(qualifierAnnotationClass, bucketProviderIterator, executorProvider);
        SupplierInstanceBinding<ScheduledExecutorService> descriptor = Bindings.supplier(new ScheduledExecutorServiceSupplier(executorProvider)).in(Singleton.class).to(ScheduledExecutorService.class);
        if (!executorProviderMap.containsKey(qualifierAnnotationClass)) {
            // it is safe to register binding for ExecutorService too...
            descriptor.to(ExecutorService.class);
        }
        Annotation qualifier = executorProvider.getClass().getAnnotation(qualifierAnnotationClass);
        if (qualifier instanceof Named) {
            descriptor.named(((Named) qualifier).value());
        } else {
            descriptor.qualifiedBy(qualifier);
        }
        injectionManager.register(descriptor);
    }
}
Also used : Named(javax.inject.Named) ScheduledExecutorServiceProvider(org.glassfish.jersey.spi.ScheduledExecutorServiceProvider) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Annotation(java.lang.annotation.Annotation) ExecutorServiceProvider(org.glassfish.jersey.spi.ExecutorServiceProvider) ScheduledExecutorServiceProvider(org.glassfish.jersey.spi.ScheduledExecutorServiceProvider) Singleton(javax.inject.Singleton) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Named (javax.inject.Named)1 Singleton (javax.inject.Singleton)1 ExecutorServiceProvider (org.glassfish.jersey.spi.ExecutorServiceProvider)1 ScheduledExecutorServiceProvider (org.glassfish.jersey.spi.ScheduledExecutorServiceProvider)1