Search in sources :

Example 1 with AbstractBinder

use of org.glassfish.hk2.utilities.binding.AbstractBinder 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 2 with AbstractBinder

use of org.glassfish.hk2.utilities.binding.AbstractBinder 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 3 with AbstractBinder

use of org.glassfish.hk2.utilities.binding.AbstractBinder 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 4 with AbstractBinder

use of org.glassfish.hk2.utilities.binding.AbstractBinder 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)

Example 5 with AbstractBinder

use of org.glassfish.hk2.utilities.binding.AbstractBinder 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)

Aggregations

AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)14 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)6 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)3 Test (org.junit.Test)3 Singleton (javax.inject.Singleton)2 ServletContext (javax.servlet.ServletContext)2 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)2 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)2 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)2 GraphHopper (com.graphhopper.GraphHopper)1 GraphHopperConfig (com.graphhopper.GraphHopperConfig)1 RealtimeFeed (com.graphhopper.gtfs.RealtimeFeed)1 GraphHopperHealthCheck (com.graphhopper.http.health.GraphHopperHealthCheck)1 JTSTriangulator (com.graphhopper.isochrone.algorithm.JTSTriangulator)1 Triangulator (com.graphhopper.isochrone.algorithm.Triangulator)1 ProfileResolver (com.graphhopper.routing.ProfileResolver)1 EncodingManager (com.graphhopper.routing.util.EncodingManager)1 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)1 LocationIndex (com.graphhopper.storage.index.LocationIndex)1 TranslationMap (com.graphhopper.util.TranslationMap)1