Search in sources :

Example 76 with Singleton

use of javax.inject.Singleton in project verify-hub by alphagov.

the class PolicyModule method jsonClient.

@Provides
@Singleton
public JsonClient jsonClient(JsonResponseProcessor jsonResponseProcessor, Environment environment, PolicyConfiguration configuration) {
    Client client = new ClientProvider(environment, configuration.getJerseyClientConfiguration(), configuration.getEnableRetryTimeOutConnections(), "policyClient").get();
    ErrorHandlingClient errorHandlingClient = new ErrorHandlingClient(client);
    return new JsonClient(errorHandlingClient, jsonResponseProcessor);
}
Also used : JsonClient(uk.gov.ida.jerseyclient.JsonClient) DefaultClientProvider(uk.gov.ida.jerseyclient.DefaultClientProvider) ClientProvider(uk.gov.ida.restclient.ClientProvider) Client(javax.ws.rs.client.Client) JsonClient(uk.gov.ida.jerseyclient.JsonClient) ErrorHandlingClient(uk.gov.ida.jerseyclient.ErrorHandlingClient) RedisClient(io.lettuce.core.RedisClient) ErrorHandlingClient(uk.gov.ida.jerseyclient.ErrorHandlingClient) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 77 with Singleton

use of javax.inject.Singleton in project verify-hub by alphagov.

the class SamlProxyModule method getVerifyMetadataResolver.

@Provides
@Singleton
@Named("VerifyMetadataResolver")
public MetadataResolver getVerifyMetadataResolver(Environment environment, SamlProxyConfiguration configuration) {
    final MetadataResolver metadataResolver = new DropwizardMetadataResolverFactory().createMetadataResolver(environment, configuration.getMetadataConfiguration());
    registerMetadataRefreshTask(environment, metadataResolver, configuration.getMetadataConfiguration(), "metadata");
    return metadataResolver;
}
Also used : DropwizardMetadataResolverFactory(uk.gov.ida.saml.metadata.factories.DropwizardMetadataResolverFactory) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver) AbstractReloadingMetadataResolver(org.opensaml.saml.metadata.resolver.impl.AbstractReloadingMetadataResolver) Named(javax.inject.Named) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 78 with Singleton

use of javax.inject.Singleton in project kernel by exoplatform.

the class MX4JComponentAdapterMT method getInitTasks.

/**
 * {@inheritDoc}
 */
protected Collection<ComponentTask<Void>> getInitTasks() {
    Component component = null;
    String componentKey;
    boolean debug = false;
    // Get the component
    Object key = getComponentKey();
    if (key instanceof String)
        componentKey = (String) key;
    else
        componentKey = ((Class<?>) key).getName();
    try {
        ConfigurationManager manager = (ConfigurationManager) exocontainer.getComponentInstanceOfType(ConfigurationManager.class);
        component = manager == null ? null : manager.getComponent(componentKey);
        if (component != null) {
            debug = component.getShowDeployInfo();
        }
        List<ComponentTask<Void>> tasks = new ArrayList<ComponentTask<Void>>();
        Set<Dependency> dependencies = new HashSet<Dependency>();
        final Class<T> implementationClass = getComponentImplementation();
        boolean isSingleton = this.isSingleton;
        boolean isInitialized = this.isInitialized;
        if (debug)
            LOG.debug("==> create  component : " + implementationClass.getName());
        boolean hasInjectableConstructor = !isSingleton || ContainerUtil.hasInjectableConstructor(implementationClass);
        boolean hasOnlyEmptyPublicConstructor = !isSingleton || ContainerUtil.hasOnlyEmptyPublicConstructor(implementationClass);
        if (hasInjectableConstructor || hasOnlyEmptyPublicConstructor) {
            // There is at least one constructor JSR 330 compliant or we already know
            // that it is not a singleton such that the new behavior is expected
            List<Dependency> lDependencies = new ArrayList<Dependency>();
            boolean isInjectPresent = container.initializeComponent(implementationClass, lDependencies, tasks, this);
            dependencies.addAll(lDependencies);
            isSingleton = manageScope(isSingleton, isInitialized, hasInjectableConstructor, isInjectPresent);
        } else if (!isInitialized) {
            // The adapter has not been initialized yet
            // The old behavior is expected as there is no constructor JSR 330 compliant
            isSingleton = this.isSingleton = true;
            scope.set(Singleton.class);
        }
        if (component != null && component.getComponentPlugins() != null) {
            addComponentPlugin(tasks, dependencies, debug, component.getComponentPlugins());
        }
        ExternalComponentPlugins ecplugins = manager == null ? null : manager.getConfiguration().getExternalComponentPlugins(componentKey);
        if (ecplugins != null) {
            addComponentPlugin(tasks, dependencies, debug, ecplugins.getComponentPlugins());
        }
        initDependencies.compareAndSet(null, new CopyOnWriteArraySet<Dependency>(dependencies));
        tasks.add(new ComponentTask<Void>("initialize component " + getComponentImplementation().getName(), container, this, ComponentTaskType.INIT) {

            public Void execute(CreationalContextComponentAdapter<?> cCtx) throws Exception {
                // check if component implement the ComponentLifecycle
                if (cCtx.get() instanceof ComponentLifecycle && exocontainer instanceof ExoContainer) {
                    ComponentLifecycle lc = (ComponentLifecycle) cCtx.get();
                    lc.initComponent((ExoContainer) exocontainer);
                }
                return null;
            }
        });
        if (!isInitialized) {
            this.isInitialized = true;
        }
        return tasks;
    } catch (Exception e) {
        String msg = "Cannot initialize component " + getComponentImplementation();
        if (component != null) {
            msg = "Cannot initialize component key=" + component.getKey() + " type=" + component.getType() + " found at " + component.getDocumentURL();
        }
        throw new RuntimeException(msg, e);
    }
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) ArrayList(java.util.ArrayList) ConcurrentContainerMT(org.exoplatform.container.ConcurrentContainerMT) ExternalComponentPlugins(org.exoplatform.container.xml.ExternalComponentPlugins) ComponentTask(org.exoplatform.container.ComponentTask) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) HashSet(java.util.HashSet) Dependency(org.exoplatform.container.Dependency) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Singleton(javax.inject.Singleton) ComponentLifecycle(org.exoplatform.container.component.ComponentLifecycle)

Example 79 with Singleton

use of javax.inject.Singleton in project jersey by jersey.

the class MonitoringFeature method configure.

@Override
public boolean configure(FeatureContext context) {
    final Boolean monitoringEnabledProperty = ServerProperties.getValue(context.getConfiguration().getProperties(), ServerProperties.MONITORING_ENABLED, null, Boolean.class);
    final Boolean statisticsEnabledProperty = ServerProperties.getValue(context.getConfiguration().getProperties(), ServerProperties.MONITORING_STATISTICS_ENABLED, null, Boolean.class);
    final Boolean mbeansEnabledProperty = ServerProperties.getValue(context.getConfiguration().getProperties(), ServerProperties.MONITORING_STATISTICS_MBEANS_ENABLED, null, Boolean.class);
    if (monitoringEnabledProperty != null) {
        monitoringEnabled = monitoringEnabledProperty;
        // monitoring statistics are enabled by default if monitoring is enabled
        statisticsEnabled = monitoringEnabled;
    }
    if (statisticsEnabledProperty != null) {
        monitoringEnabled = monitoringEnabled || statisticsEnabledProperty;
        statisticsEnabled = statisticsEnabledProperty;
    }
    if (mbeansEnabledProperty != null) {
        monitoringEnabled = monitoringEnabled || mbeansEnabledProperty;
        statisticsEnabled = statisticsEnabled || mbeansEnabledProperty;
        mBeansEnabled = mbeansEnabledProperty;
    }
    if (statisticsEnabledProperty != null && !statisticsEnabledProperty) {
        if (mbeansEnabledProperty != null && mBeansEnabled) {
            LOGGER.log(Level.WARNING, LocalizationMessages.WARNING_MONITORING_FEATURE_ENABLED(ServerProperties.MONITORING_STATISTICS_ENABLED));
        } else {
            LOGGER.log(Level.WARNING, LocalizationMessages.WARNING_MONITORING_FEATURE_DISABLED(ServerProperties.MONITORING_STATISTICS_ENABLED));
        }
    }
    if (monitoringEnabled) {
        context.register(ApplicationInfoListener.class);
        context.register(new AbstractBinder() {

            @Override
            protected void configure() {
                bindFactory(ReferencingFactory.<ApplicationInfo>referenceFactory()).to(new GenericType<Ref<ApplicationInfo>>() {
                }).in(Singleton.class);
                bindFactory(ApplicationInfoInjectionFactory.class).to(ApplicationInfo.class);
            }
        });
    }
    if (statisticsEnabled) {
        context.register(MonitoringEventListener.class);
        context.register(new AbstractBinder() {

            @Override
            protected void configure() {
                bindFactory(ReferencingFactory.<MonitoringStatistics>referenceFactory()).to(new GenericType<Ref<MonitoringStatistics>>() {
                }).in(Singleton.class);
                bindFactory(StatisticsInjectionFactory.class).to(MonitoringStatistics.class);
                bind(StatisticsListener.class).to(MonitoringStatisticsListener.class).in(Singleton.class);
            }
        });
    }
    if (mBeansEnabled) {
        // instance registration is needed here as MBeanExposer needs to be a singleton so that
        // one instance handles listening to events of MonitoringStatisticsListener and ContainerLifecycleListener
        context.register(new MBeanExposer());
    }
    return monitoringEnabled;
}
Also used : Ref(org.glassfish.jersey.internal.util.collection.Ref) Singleton(javax.inject.Singleton) MonitoringStatistics(org.glassfish.jersey.server.monitoring.MonitoringStatistics) MonitoringStatisticsListener(org.glassfish.jersey.server.monitoring.MonitoringStatisticsListener) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ApplicationInfo(org.glassfish.jersey.server.monitoring.ApplicationInfo) MBeanExposer(org.glassfish.jersey.server.internal.monitoring.jmx.MBeanExposer)

Example 80 with Singleton

use of javax.inject.Singleton in project jersey by jersey.

the class DisposableSupplierTest method testDisposeComposedObjectWithPerLookupFields.

/**
     * PerLookup fields are not disposed therefore they should never be used as a DisposedSupplier because the field stay in
     * {@link SupplierFactoryBridge} forever.
     */
@Test
public void testDisposeComposedObjectWithPerLookupFields() {
    BindingTestHelper.bind(injectionManager, binder -> {
        binder.bindFactory(DisposableSupplierImpl.class, Singleton.class).to(String.class);
        binder.bindAsContract(ComposedObject.class).in(RequestScoped.class);
        binder.bind(new RequestScope()).to(RequestScope.class);
    });
    RequestScope request = injectionManager.getInstance(RequestScope.class);
    AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
    request.runInScope(() -> {
        // Save Singleton Supplier for later check that the instance was disposed.
        Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
        atomicSupplier.set(supplier);
        // All instances should be the same because they are request scoped.
        ComposedObject instance = injectionManager.getInstance(ComposedObject.class);
        assertEquals("1", instance.first);
        assertEquals("2", instance.second);
        assertEquals("3", instance.third);
    });
    Supplier<String> cleanedSupplier = atomicSupplier.get();
    // Next should be 4
    assertEquals("4", cleanedSupplier.get());
}
Also used : Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Aggregations

Singleton (javax.inject.Singleton)168 Provides (dagger.Provides)98 Provides (com.google.inject.Provides)57 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)21 Named (javax.inject.Named)17 OkHttpClient (okhttp3.OkHttpClient)17 Cache (okhttp3.Cache)10 List (java.util.List)9 ConfigBinder.configBinder (com.facebook.airlift.configuration.ConfigBinder.configBinder)8 Binder (com.google.inject.Binder)8 Module (com.google.inject.Module)8 CachingStripeMetadataSource (com.facebook.presto.orc.CachingStripeMetadataSource)7 DwrfAwareStripeMetadataSourceFactory (com.facebook.presto.orc.DwrfAwareStripeMetadataSourceFactory)7 OrcDataSourceId (com.facebook.presto.orc.OrcDataSourceId)7 StorageStripeMetadataSource (com.facebook.presto.orc.StorageStripeMetadataSource)7 StripeMetadataSource (com.facebook.presto.orc.StripeMetadataSource)7 StripeMetadataSourceFactory (com.facebook.presto.orc.StripeMetadataSourceFactory)7 CachingOrcFileTailSource (com.facebook.presto.orc.cache.CachingOrcFileTailSource)7 OrcCacheConfig (com.facebook.presto.orc.cache.OrcCacheConfig)7 OrcFileTailSource (com.facebook.presto.orc.cache.OrcFileTailSource)7