本文整理汇总了Java中org.glassfish.hk2.api.ServiceLocator类的典型用法代码示例。如果您正苦于以下问题:Java ServiceLocator类的具体用法?Java ServiceLocator怎么用?Java ServiceLocator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServiceLocator类属于org.glassfish.hk2.api包,在下文中一共展示了ServiceLocator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: JerseyApplication
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Inject
public JerseyApplication(ServiceLocator serviceLocator) {
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(AppLoader.injector);
String disableMoxy = PropertiesHelper.getPropertyNameForRuntime(
CommonProperties.MOXY_JSON_FEATURE_DISABLE,
getConfiguration().getRuntimeType());
property(disableMoxy, true);
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
// add the default Jackson exception mappers
register(JsonParseExceptionMapper.class);
register(JsonMappingExceptionMapper.class);
register(JacksonJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
packages(JerseyApplication.class.getPackage().getName());
for (JerseyConfigurator configurator: AppLoader.getExtensions(JerseyConfigurator.class)) {
configurator.configure(this);
}
}
示例2: setUp
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
@Override
protected void configure() {
bind(SecurityService.class).to(SecurityService.class);
bind(accountDao).to(AccountDao.class);
bind(passwordDao).to(AccountPasswordDao.class);
bind(userProvider).to(UserProvider.class);
bind(event).to(new TypeLiteral<Event<SignedInEvent>>() {
});
}
});
locator.inject(this);
}
示例3: configureGuice
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
private Injector configureGuice(T configuration, Environment environment) throws Exception {
// setup our core modules...
appModules.add(new MetricRegistryModule(environment.metrics()));
appModules.add(new ServletModule());
// ...and add the app's modules
appModules.addAll(addModules(configuration, environment));
final Injector injector = Guice.createInjector(ImmutableList.copyOf(this.appModules));
// Taken from https://github.com/Squarespace/jersey2-guice/wiki#complex-example. HK2 is no fun.
JerseyGuiceUtils.install((name, parent) -> {
if (!name.startsWith("__HK2_Generated_")) {
return null;
}
return injector.createChildInjector(Lists.newArrayList(new JerseyGuiceModule(name)))
.getInstance(ServiceLocator.class);
});
injector.injectMembers(this);
registerWithInjector(environment, injector);
return injector;
}
示例4: TestChain
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
public TestChain(TestServiceRunnerBase serviceRunner) {
this.actions = ImmutableList.of();
this.serviceLocator = new ServiceLocatorHolder();
Feature lifecycleListener = new Feature() {
@Inject
public void setServiceLocator(ServiceLocator s) {
serviceLocator.set(s);
}
@Override
public boolean configure(FeatureContext context) {
return false;
}
};
this.serviceRunner = serviceRunner
.serviceConfig(serviceRunner.getServiceConfig()
.registerInstance(lifecycleListener)
);
}
示例5: backstopperOnlyExceptionMapperFactory_removes_all_exception_mappers_except_Jersey2ApiExceptionHandler
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Test
public void backstopperOnlyExceptionMapperFactory_removes_all_exception_mappers_except_Jersey2ApiExceptionHandler()
throws NoSuchFieldException, IllegalAccessException {
// given
AbstractBinder lotsOfExceptionMappersBinder = new AbstractBinder() {
@Override
protected void configure() {
bind(JsonMappingExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class);
bind(JsonParseExceptionMapper.class).to(ExceptionMapper.class).in(Singleton.class);
bind(generateJerseyApiExceptionHandler(projectApiErrors, utils)).to(ExceptionMapper.class);
}
};
ServiceLocator locator = ServiceLocatorUtilities.bind(lotsOfExceptionMappersBinder);
// when
BackstopperOnlyExceptionMapperFactory overrideExceptionMapper = new BackstopperOnlyExceptionMapperFactory(locator);
// then
Set<Object> emTypesLeft = overrideExceptionMapper.getFieldObj(
ExceptionMapperFactory.class, overrideExceptionMapper, "exceptionMapperTypes"
);
assertThat(emTypesLeft).hasSize(1);
ServiceHandle serviceHandle = overrideExceptionMapper.getFieldObj(emTypesLeft.iterator().next(), "mapper");
assertThat(serviceHandle.getService()).isInstanceOf(Jersey2ApiExceptionHandler.class);
}
示例6: bindLocalServices
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
/**
* Binds local services into a locator. These have to be bound both into our service locator and the Jersey service locator because
* they do not cross bridge or parent/child boundaries.
*
* @param locator
* {@code ServiceLocator} into which local services should be installed
*
* @return Controller to activate {@link Immediate @Immediate} services
*/
private static ImmediateController bindLocalServices(ServiceLocator locator) {
// These have to be local because they rely on the InstantiationService, which can only get the Injectee for local injections
addClasses(locator,
true,
CounterFactory.class,
HistogramFactory.class,
MeterFactory.class,
TimerFactory.class,
AnnotationInterceptionService.class
);
if (locator.getServiceHandle(InheritableThreadContext.class) == null) {
ServiceLocatorUtilities.enableInheritableThreadScope(locator);
}
ExtrasUtilities.enableDefaultInterceptorServiceImplementation(locator);
ExtrasUtilities.enableTopicDistribution(locator);
if (locator.getServiceHandle(ImmediateController.class) == null) {
return ServiceLocatorUtilities.enableImmediateScopeSuspended(locator);
}
return locator.getService(ImmediateController.class);
}
示例7: constructorWithServiceLocatorReturnsGivenServiceLocatorAndSetsConfigContextAndResourceConfig
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Test
public void constructorWithServiceLocatorReturnsGivenServiceLocatorAndSetsConfigContextAndResourceConfig() {
ServiceLocator serviceLocator = getMockServiceLocator();
JerseyApplication<JerseyConfigSection> application =
new JerseyApplicationBase<JerseyConfigSection>(serviceLocator) { };
assertThat(application.serviceLocator()).isEqualTo(serviceLocator);
assertThat(application.configContext()).isEqualTo(configContext);
assertThat(application.resourceConfig()).isEqualTo(resourceConfig);
ApplicationResolver applicationResolverFromServiceLocator = application.serviceLocator()
.getService(ApplicationResolver.class);
assertThat(applicationResolverFromServiceLocator.application()).isEqualTo(application);
}
示例8: configure
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Override
public boolean configure(FeatureContext context) {
InjectionManager injectionManager = InjectionManagerProvider.getInjectionManager(context);
ServiceLocator locator;
if (injectionManager instanceof ImmediateHk2InjectionManager) {
locator = ((ImmediateHk2InjectionManager) injectionManager).getServiceLocator();
} else if (injectionManager instanceof DelayedHk2InjectionManager) {
locator = ((DelayedHk2InjectionManager) injectionManager).getServiceLocator();
} else {
throw new IllegalStateException("expected an hk2 injection manager");
}
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
// register all your modules, here
Injector injector = Guice.createInjector(new GreetingModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
示例9: VelocityViewProcessor
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Inject
public VelocityViewProcessor(final javax.ws.rs.core.Configuration config, final ServiceLocator serviceLocator,
@Optional final ServletContext servletContext) {
super(config, servletContext, "velocity", "vm");
this.factory = getTemplateObjectFactory(serviceLocator, VelocityConfigurationFactory.class,
new Value<VelocityConfigurationFactory>() {
@Override
public VelocityConfigurationFactory get() {
Configuration configuration = getTemplateObjectFactory(serviceLocator, Configuration.class,
Values.<Configuration>empty());
if (configuration == null) {
return new VelocityDefaultConfigurationFactory(servletContext);
} else {
return new VelocitySuppliedConfigurationFactory(configuration);
}
}
});
Velocity.init();
}
示例10: FreemarkerViewProcessor
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
/**
* Create an instance of this processor with injected {@link javax.ws.rs.core.Configuration config} and
* (optional) {@link javax.servlet.ServletContext servlet context}.
*
* @param config config to configure this processor from.
* @param serviceLocator service locator to initialize template object factory if needed.
* @param servletContext (optional) servlet context to obtain template resources from.
*/
@Inject
public FreemarkerViewProcessor(final javax.ws.rs.core.Configuration config, final ServiceLocator serviceLocator,
@Optional final ServletContext servletContext) {
super(config, servletContext, "freemarker", getSupportedExtensions(config, servletContext));
this.factory = getTemplateObjectFactory(serviceLocator, FreemarkerConfigurationFactory.class,
new Value<FreemarkerConfigurationFactory>() {
@Override
public FreemarkerConfigurationFactory get() {
Configuration configuration = getTemplateObjectFactory(serviceLocator, Configuration.class,
Values.<Configuration>empty());
if (configuration == null) {
return new FreemarkerDefaultConfigurationFactory(servletContext);
} else {
return new FreemarkerSuppliedConfigurationFactory(configuration);
}
}
});
}
示例11: setupWithModule
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
protected static void setupWithModule(Module baseModule) {
Log.debug("Setting up Jersey");
application = new TestApplication();
Log.trace("+- application=[{}]", application);
Log.debug("Bootstrapping Jersey2-Guice bridge");
ServiceLocator locator = BootstrapUtils.newServiceLocator();
Log.trace("+- locator=[{}]", locator);
final List<Module> modules = Arrays.asList(new ServletModule(), baseModule);
final Injector injector = BootstrapUtils.newInjector(locator, modules);
Log.trace("+- injector=[{}]", injector);
BootstrapUtils.install(locator);
Log.trace("+- done: locator installed");
}
示例12: Application
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Inject
public Application(ServiceLocator serviceLocator) {
//Since Jersey doesn't support Guice, setup a bridge so that Guice will work
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(Main.injector);
//Gets Jackson all squared away with Jersey
register(Jackson1Feature.class);
register(DefaultObjectMapperProvider.class);
//Register apis and other Jersey things here
register(TestApi.class);
register(TestExceptionMapper.class);
}
示例13: MyApplication
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Inject
public MyApplication(ServiceLocator serviceLocator) {
packages(Resource.class.getPackage().getName()); // todo: try class
// todo: test!
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Service.class);
}
});
// todo: auth intercept (will not work with Guice - make dual mapping or use jersey interceptors or try jersey2-guice)
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
}
示例14: testLog4J
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Test
public void testLog4J()
{
AbstractBinder binder = new AbstractBinder()
{
@Override
protected void configure()
{
install(loginject(Logger.class, LogManager::getLogger, currentClass()).as(Binder.class));
addActiveDescriptor(TestClass.class);
}
};
ServiceLocator serviceLocator = ServiceLocatorUtilities.bind(testName.getMethodName(), binder);
TestClass service = serviceLocator.getService(TestClass.class);
assertEquals(TestClass.class.getName(), service.logger.getName());
}
示例15: testInferredLog4J
import org.glassfish.hk2.api.ServiceLocator; //导入依赖的package包/类
@Test
public void testInferredLog4J()
{
AbstractBinder binder = new AbstractBinder()
{
@Override
protected void configure()
{
install(loginject(LogManager::getLogger, currentClass()).as(Binder.class));
addActiveDescriptor(TestClass.class);
}
};
ServiceLocator serviceLocator = ServiceLocatorUtilities.bind(testName.getMethodName(), binder);
TestClass service = serviceLocator.getService(TestClass.class);
assertEquals(TestClass.class.getName(), service.logger.getName());
}