Search in sources :

Example 11 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder 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 12 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder in project jersey by jersey.

the class CommonConfigTest method testFeatureInjections.

@Test
public void testFeatureInjections() throws Exception {
    config.register(InjectIntoFeatureClass.class).register(new InjectIntoFeatureInstance()).register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new InjectMe());
        }
    });
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.configureMetaProviders(injectionManager);
    assertThat("Feature instance not injected", config.getProperty("instance-injected").toString(), is("true"));
    assertThat("Feature class not injected", config.getProperty("class-injected").toString(), is("true"));
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 13 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder in project jersey by jersey.

the class AbstractBinderTest method testFirstLayer.

@Test
public void testFirstLayer() {
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(CacheControlProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(CookieProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(DateProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(EntityTagProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
            bind(LinkProvider.class).to(HeaderDelegateProvider.class).in(Singleton.class);
        }
    };
    List<Binding> bindings = new ArrayList<>(binder.getBindings());
    assertEquals(5, bindings.size());
    // Keep ordering.
    assertEquals(CacheControlProvider.class, ((ClassBinding) bindings.get(0)).getService());
    assertEquals(CookieProvider.class, ((ClassBinding) bindings.get(1)).getService());
    assertEquals(DateProvider.class, ((ClassBinding) bindings.get(2)).getService());
    assertEquals(EntityTagProvider.class, ((ClassBinding) bindings.get(3)).getService());
    assertEquals(LinkProvider.class, ((ClassBinding) bindings.get(4)).getService());
}
Also used : Binding(org.glassfish.jersey.internal.inject.Binding) ClassBinding(org.glassfish.jersey.internal.inject.ClassBinding) HeaderDelegateProvider(org.glassfish.jersey.spi.HeaderDelegateProvider) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder in project jersey by jersey.

the class SpringComponentProvider method initialize.

@Override
public void initialize(InjectionManager injectionManager) {
    this.injectionManager = injectionManager;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine(LocalizationMessages.CTX_LOOKUP_STARTED());
    }
    ServletContext sc = injectionManager.getInstance(ServletContext.class);
    if (sc != null) {
        // servlet container
        ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    } else {
        // non-servlet container
        ctx = createSpringContext();
    }
    if (ctx == null) {
        LOGGER.severe(LocalizationMessages.CTX_LOOKUP_FAILED());
        return;
    }
    LOGGER.config(LocalizationMessages.CTX_LOOKUP_SUCESSFUL());
    // initialize HK2 spring-bridge
    HK2InjectionManager hk2InjectionManager = (HK2InjectionManager) injectionManager;
    SpringBridge.getSpringBridge().initializeSpringBridge(hk2InjectionManager.getServiceLocator());
    SpringIntoHK2Bridge springBridge = injectionManager.getInstance(SpringIntoHK2Bridge.class);
    springBridge.bridgeSpringBeanFactory(ctx);
    // register Spring @Autowired annotation handler with HK2 ServiceLocator
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new AutowiredInjectResolver(ctx)).to(InjectionResolver.class);
            bind(ctx).to(ApplicationContext.class).named("SpringContext");
        }
    };
    injectionManager.register(binder);
    LOGGER.config(LocalizationMessages.SPRING_COMPONENT_PROVIDER_INITIALIZED());
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpringIntoHK2Bridge(org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ServletContext(javax.servlet.ServletContext) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Example 15 with AbstractBinder

use of org.glassfish.jersey.internal.inject.AbstractBinder in project jersey by jersey.

the class MetaInfServicesAutoDiscoverable method configure.

@Override
public void configure(final FeatureContext context) {
    final Map<String, Object> properties = context.getConfiguration().getProperties();
    final RuntimeType runtimeType = context.getConfiguration().getRuntimeType();
    context.register(new AbstractBinder() {

        @Override
        protected void configure() {
            // Message Body providers.
            install(new ServiceFinderBinder<MessageBodyReader>(MessageBodyReader.class, properties, runtimeType));
            install(new ServiceFinderBinder<MessageBodyWriter>(MessageBodyWriter.class, properties, runtimeType));
            // Exception Mappers.
            install(new ServiceFinderBinder<ExceptionMapper>(ExceptionMapper.class, properties, runtimeType));
        }
    });
}
Also used : ServiceFinderBinder(org.glassfish.jersey.internal.ServiceFinderBinder) RuntimeType(javax.ws.rs.RuntimeType) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder)

Aggregations

AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)19 Test (org.junit.Test)7 Singleton (javax.inject.Singleton)5 Binder (org.glassfish.jersey.internal.inject.Binder)4 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 Configuration (javax.ws.rs.core.Configuration)3 ArrayList (java.util.ArrayList)2 Response (javax.ws.rs.core.Response)2 Binding (org.glassfish.jersey.internal.inject.Binding)2 ClassBinding (org.glassfish.jersey.internal.inject.ClassBinding)2 RequestScope (org.glassfish.jersey.process.internal.RequestScope)2 HeaderDelegateProvider (org.glassfish.jersey.spi.HeaderDelegateProvider)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 ServletContext (javax.servlet.ServletContext)1 RuntimeType (javax.ws.rs.RuntimeType)1 ClassAnalyzer (org.glassfish.hk2.api.ClassAnalyzer)1 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)1 ServiceFinderBinder (org.glassfish.jersey.internal.ServiceFinderBinder)1 PerThread (org.glassfish.jersey.internal.inject.PerThread)1