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