Search in sources :

Example 1 with ServiceLocatorUtilities.createAndPopulateServiceLocator

use of org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator in project jersey by eclipse-ee4j.

the class ServiceLocatorSetup method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addOneConstant(locator, new HappyService());
    event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HappyService(org.glassfish.jersey.tests.integration.jersey2704.services.HappyService)

Example 2 with ServiceLocatorUtilities.createAndPopulateServiceLocator

use of org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator in project sandbox by backpaper0.

the class HK2Sample method main.

public static void main(String[] args) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    ServiceLocatorUtilities.bind(locator, new AbstractBinder() {

        @Override
        protected void configure() {
            bind(HelloImpl.class).to(Hello.class).in(Singleton.class).proxy(true);
        }
    });
    HK2Sample sample = new HK2Sample();
    locator.inject(sample);
    System.out.println(sample.hello.getClass());
    System.out.println(sample.hello.say());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder)

Example 3 with ServiceLocatorUtilities.createAndPopulateServiceLocator

use of org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator in project sandbox by backpaper0.

the class InterceptorSample method main.

public static void main(String[] args) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    ServiceLocatorUtilities.bind(locator, new AbstractBinder() {

        @Override
        protected void configure() {
            // must be in the singleton scope
            bind(InterceptionServiceImpl.class).to(InterceptionService.class).in(Singleton.class);
            bind(Hello.class).to(Hello.class);
        }
    });
    Hello service = locator.getService(Hello.class);
    System.out.println(service.say());
    System.out.println(service.getClass());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Singleton(javax.inject.Singleton) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder)

Example 4 with ServiceLocatorUtilities.createAndPopulateServiceLocator

use of org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator in project jersey by jersey.

the class ServiceLocatorSetup method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addOneConstant(locator, new HappyService());
    event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HappyService(org.glassfish.jersey.tests.integration.jersey2704.services.HappyService)

Example 5 with ServiceLocatorUtilities.createAndPopulateServiceLocator

use of org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator in project glassfish-hk2 by eclipse-ee4j.

the class HK2TestListenerAdapter method injectTestInstance.

private void injectTestInstance(ITestResult testResult) throws InstantiationException, IllegalAccessException {
    ServiceLocator locator = null;
    Object testInstance = testResult.getMethod().getInstance();
    if (testInstance != null) {
        HK2 hk2 = testInstance.getClass().getAnnotation(HK2.class);
        if (hk2 != null) {
            String locatorName = hk2.value();
            if ("hk2-testng-locator".equals(locatorName)) {
                locatorName = locatorName + "." + testInstance.getClass().getSimpleName();
            }
            ServiceLocator existingLocator = serviceLocators.get(locatorName);
            if (!testClasses.containsKey(testInstance.getClass())) {
                Class<? extends Binder>[] hk2BinderClasses = hk2.binders();
                if (hk2.populate()) {
                    if (existingLocator == null) {
                        locator = ServiceLocatorUtilities.createAndPopulateServiceLocator(locatorName);
                        initializeServiceLocator(locator, hk2);
                        serviceLocators.put(locator.getName(), locator);
                    } else {
                        locator = existingLocator;
                    }
                }
                if (hk2BinderClasses.length > 0) {
                    Binder[] binders = new Binder[hk2BinderClasses.length];
                    int index = 0;
                    for (Class<? extends Binder> binderClass : hk2BinderClasses) {
                        Binder binder = binderClasses.get(binderClass);
                        if (binder == null) {
                            binder = binderClass.newInstance();
                            binderClasses.put(binderClass, binder);
                        }
                        binders[index++] = binder;
                    }
                    if (locator == null) {
                        if (existingLocator == null) {
                            locator = ServiceLocatorUtilities.bind(locatorName, binders);
                            initializeServiceLocator(locator, hk2);
                            serviceLocators.put(locator.getName(), locator);
                        } else {
                            locator = existingLocator;
                            ServiceLocatorUtilities.bind(locator, binders);
                        }
                    } else {
                        ServiceLocatorUtilities.bind(locator, binders);
                    }
                }
                if (locator != null) {
                    locator.inject(testInstance);
                }
                testClasses.put(testInstance.getClass(), testInstance);
            }
        }
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Binder(org.glassfish.hk2.utilities.Binder)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)12 Test (org.junit.Test)6 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)2 HappyService (org.glassfish.jersey.tests.integration.jersey2704.services.HappyService)2 Connection (java.sql.Connection)1 Singleton (javax.inject.Singleton)1 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)1 Binder (org.glassfish.hk2.utilities.Binder)1 AbstractService (org.jvnet.hk2.metadata.tests.faux.stub.AbstractService)1 ConnectionStub (org.jvnet.hk2.metadata.tests.faux.stub.ConnectionStub)1 FailingLargeInterfaceStub (org.jvnet.hk2.metadata.tests.faux.stub.FailingLargeInterfaceStub)1 NamedBean (org.jvnet.hk2.metadata.tests.faux.stub.NamedBean)1 LargeInterface (org.jvnet.hk2.metadata.tests.stub.LargeInterface)1