Search in sources :

Example 1 with Binder

use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.

the class InjectionManagerTest method testRegisterBinder.

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

        @Override
        protected void configure() {
            bindAsContract(EnglishGreeting.class);
        }
    };
    InjectionManager injectionManager = new HK2InjectionManager();
    injectionManager.initialize();
    injectionManager.register(binder);
    assertNotNull(injectionManager.getInstance(EnglishGreeting.class));
}
Also used : AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 2 with Binder

use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.

the class Main method main.

public static void main(String[] args) {
    ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
    ServiceLocator locator = factory.create("myLocator");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(HelloServiceImpl.class).to(GreetingService.class);
        }
    };
    locator.inject(binder);
    binder.bind(dc);
    dc.commit();
    GreetingWrapper wrapper = new GreetingWrapper(locator);
    System.out.println("result: " + wrapper.getInjectedGreeting());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 3 with Binder

use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.

the class MyServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
    ServiceLocator locator = factory.create("myLocator");
    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(HelloServiceImpl.class).to(GreetingService.class);
        }
    };
    binder.bind(dc);
    dc.commit();
    GreetingWrapper wrapper = new GreetingWrapper(locator);
    response.getWriter().write("Greeting: " + wrapper.getInjectedGreeting());
    locator.shutdown();
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ServiceLocatorFactory(org.glassfish.hk2.api.ServiceLocatorFactory)

Example 4 with Binder

use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.

the class ActiveBindingBindingTest method testReq.

@Test
@Ignore("At the time of ignoring this test, ResourceConfig does not support HK2 Binder registering.")
public void testReq() throws Exception {
    // bootstrap the test application
    ResourceConfig myConfig = new ResourceConfig();
    myConfig.register(ReqResource.class);
    myConfig.register(SingletonResource.class);
    final MyRequestDataDescriptor activeDescriptor = new MyRequestDataDescriptor();
    myConfig.register(new AbstractBinder() {

        @Override
        protected void configure() {
            addActiveDescriptor(activeDescriptor);
        }
    });
    initiateWebApplication(myConfig);
    activeDescriptor.injectionManager = app().getInjectionManager();
    // end bootstrap
    String response;
    response = getResponseEntity("/req");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/req/param");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, containsString(PROXY_TAG));
    response = getResponseEntity("/req");
    assertThat(response, containsString(REQUEST_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, containsString(PROXY_TAG));
    response = getResponseEntity("/singleton/param");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
    response = getResponseEntity("/singleton/param");
    assertThat(response, containsString(SINGLETON_TAG));
    assertThat(response, not(containsString(PROXY_TAG)));
}
Also used : AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Binder

use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.

the class CdiComponentProvider method bindHk2ClassAnalyzer.

private void bindHk2ClassAnalyzer() {
    ClassAnalyzer defaultClassAnalyzer = injectionManager.getInstance(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME);
    int skippedElements = methodsToSkip.size() + fieldsToSkip.size();
    ClassAnalyzer customizedClassAnalyzer = skippedElements > 0 ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip) : defaultClassAnalyzer;
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(customizedClassAnalyzer).analyzeWith(ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME).to(ClassAnalyzer.class).named(CDI_CLASS_ANALYZER);
        }
    };
    injectionManager.register(binder);
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) ClassAnalyzer(org.glassfish.hk2.api.ClassAnalyzer) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint)

Aggregations

AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)5 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)4 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)3 Test (org.junit.Test)3 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 ClassAnalyzer (org.glassfish.hk2.api.ClassAnalyzer)1 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)1 AliasBinding (org.glassfish.jersey.internal.inject.AliasBinding)1 Binder (org.glassfish.jersey.internal.inject.Binder)1 Binding (org.glassfish.jersey.internal.inject.Binding)1 ClassBinding (org.glassfish.jersey.internal.inject.ClassBinding)1 InjectionResolverBinding (org.glassfish.jersey.internal.inject.InjectionResolverBinding)1 InstanceBinding (org.glassfish.jersey.internal.inject.InstanceBinding)1 SupplierClassBinding (org.glassfish.jersey.internal.inject.SupplierClassBinding)1 SupplierInstanceBinding (org.glassfish.jersey.internal.inject.SupplierInstanceBinding)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1