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);
}
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;
}
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);
}
}
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;
}
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());
}
Aggregations