use of org.glassfish.jersey.internal.BootstrapBag in project jersey by eclipse-ee4j.
the class ApplicationHandler method initialize.
/**
* Assumes the configuration field is initialized with a valid ResourceConfig.
*/
private ServerRuntime initialize(InjectionManager injectionManager, List<BootstrapConfigurator> bootstrapConfigurators, ServerBootstrapBag bootstrapBag) {
this.application = bootstrapBag.getApplication();
this.runtimeConfig = bootstrapBag.getRuntimeConfig();
// Register the binders which are dependent on "Application.properties()"
injectionManager.register(new MessagingBinders.MessageBodyProviders(application.getProperties(), RuntimeType.SERVER));
// Lock original ResourceConfig.
if (application instanceof ResourceConfig) {
((ResourceConfig) application).lock();
}
CompositeApplicationEventListener compositeListener = null;
// mark begin of validation phase
Errors.mark();
try {
// AutoDiscoverable.
if (!CommonProperties.getValue(runtimeConfig.getProperties(), RuntimeType.SERVER, CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, Boolean.FALSE, Boolean.class)) {
runtimeConfig.configureAutoDiscoverableProviders(injectionManager, bootstrapBag.getAutoDiscoverables());
} else {
runtimeConfig.configureForcedAutoDiscoverableProviders(injectionManager);
}
// Configure binders and features.
runtimeConfig.configureMetaProviders(injectionManager, bootstrapBag.getManagedObjectsFinalizer());
ResourceBagConfigurator resourceBagConfigurator = new ResourceBagConfigurator();
resourceBagConfigurator.init(injectionManager, bootstrapBag);
runtimeConfig.lock();
ExternalRequestScopeConfigurator externalRequestScopeConfigurator = new ExternalRequestScopeConfigurator();
externalRequestScopeConfigurator.init(injectionManager, bootstrapBag);
ModelProcessorConfigurator modelProcessorConfigurator = new ModelProcessorConfigurator();
modelProcessorConfigurator.init(injectionManager, bootstrapBag);
ResourceModelConfigurator resourceModelConfigurator = new ResourceModelConfigurator();
resourceModelConfigurator.init(injectionManager, bootstrapBag);
ServerExecutorProvidersConfigurator executorProvidersConfigurator = new ServerExecutorProvidersConfigurator();
executorProvidersConfigurator.init(injectionManager, bootstrapBag);
injectionManager.completeRegistration();
bootstrapConfigurators.forEach(configurator -> configurator.postInit(injectionManager, bootstrapBag));
resourceModelConfigurator.postInit(injectionManager, bootstrapBag);
Iterable<ApplicationEventListener> appEventListeners = Providers.getAllProviders(injectionManager, ApplicationEventListener.class, new RankedComparator<>());
if (appEventListeners.iterator().hasNext()) {
ResourceBag resourceBag = bootstrapBag.getResourceBag();
compositeListener = new CompositeApplicationEventListener(appEventListeners);
compositeListener.onEvent(new ApplicationEventImpl(ApplicationEvent.Type.INITIALIZATION_START, this.runtimeConfig, runtimeConfig.getComponentBag().getRegistrations(), resourceBag.classes, resourceBag.instances, null));
}
if (!disableValidation()) {
ComponentModelValidator validator = new ComponentModelValidator(bootstrapBag.getValueParamProviders(), bootstrapBag.getMessageBodyWorkers());
validator.validate(bootstrapBag.getResourceModel());
}
if (Errors.fatalIssuesFound() && !ignoreValidationError()) {
throw new ModelValidationException(LocalizationMessages.RESOURCE_MODEL_VALIDATION_FAILED_AT_INIT(), ModelErrors.getErrorsAsResourceModelIssues(true));
}
} finally {
if (ignoreValidationError()) {
Errors.logErrors(true);
// reset errors to the state before validation phase
Errors.reset();
} else {
Errors.unmark();
}
}
this.msgBodyWorkers = bootstrapBag.getMessageBodyWorkers();
// assembly request processing chain
ProcessingProviders processingProviders = bootstrapBag.getProcessingProviders();
final ContainerFilteringStage preMatchRequestFilteringStage = new ContainerFilteringStage(processingProviders.getPreMatchFilters(), processingProviders.getGlobalResponseFilters());
final ChainableStage<RequestProcessingContext> routingStage = Routing.forModel(bootstrapBag.getResourceModel().getRuntimeResourceModel()).resourceContext(bootstrapBag.getResourceContext()).configuration(runtimeConfig).entityProviders(msgBodyWorkers).valueSupplierProviders(bootstrapBag.getValueParamProviders()).modelProcessors(Providers.getAllRankedSortedProviders(injectionManager, ModelProcessor.class)).createService(serviceType -> Injections.getOrCreate(injectionManager, serviceType)).processingProviders(processingProviders).resourceMethodInvokerBuilder(bootstrapBag.getResourceMethodInvokerBuilder()).buildStage();
/*
* Root linear request acceptor. This is the main entry point for the whole request processing.
*/
final ContainerFilteringStage resourceFilteringStage = new ContainerFilteringStage(processingProviders.getGlobalRequestFilters(), null);
final ReferencesInitializer referencesInitializer = new ReferencesInitializer(injectionManager, () -> injectionManager.getInstance(RequestProcessingContextReference.class));
final Stage<RequestProcessingContext> rootStage = Stages.chain(referencesInitializer).to(preMatchRequestFilteringStage).to(routingStage).to(resourceFilteringStage).build(Routing.matchedEndpointExtractor());
ServerRuntime serverRuntime = ServerRuntime.createServerRuntime(injectionManager, bootstrapBag, rootStage, compositeListener, processingProviders);
// Inject instances.
ComponentBag componentBag = runtimeConfig.getComponentBag();
ResourceBag resourceBag = bootstrapBag.getResourceBag();
for (final Object instance : componentBag.getInstances(ComponentBag.excludeMetaProviders(injectionManager))) {
injectionManager.inject(instance);
}
for (final Object instance : resourceBag.instances) {
injectionManager.inject(instance);
}
logApplicationInitConfiguration(injectionManager, resourceBag, processingProviders);
if (compositeListener != null) {
ApplicationEvent initFinishedEvent = new ApplicationEventImpl(ApplicationEvent.Type.INITIALIZATION_APP_FINISHED, runtimeConfig, componentBag.getRegistrations(), resourceBag.classes, resourceBag.instances, bootstrapBag.getResourceModel());
compositeListener.onEvent(initFinishedEvent);
MonitoringContainerListener containerListener = injectionManager.getInstance(MonitoringContainerListener.class);
containerListener.init(compositeListener, initFinishedEvent);
}
return serverRuntime;
}
use of org.glassfish.jersey.internal.BootstrapBag in project jersey by eclipse-ee4j.
the class JerseyResourceContextConfigurator method init.
@Override
public void init(InjectionManager injectionManager, BootstrapBag bootstrapBag) {
ServerBootstrapBag serverBag = (ServerBootstrapBag) bootstrapBag;
Consumer<Binding> registerBinding = injectionManager::register;
Function<Class<?>, ?> getOrCreateInstance = clazz -> Injections.getOrCreate(injectionManager, clazz);
Consumer<Object> injectInstance = injectionManager::inject;
// Initialize and register Resource Context
JerseyResourceContext resourceContext = new JerseyResourceContext(getOrCreateInstance, injectInstance, registerBinding);
InstanceBinding<JerseyResourceContext> resourceContextBinding = Bindings.service(resourceContext).to(ResourceContext.class).to(ExtendedResourceContext.class);
injectionManager.register(resourceContextBinding);
serverBag.setResourceContext(resourceContext);
}
use of org.glassfish.jersey.internal.BootstrapBag in project jersey by eclipse-ee4j.
the class ContextResolverFactoryTest method setUp.
@Before
public void setUp() {
InjectionManager injectionManager = Injections.createInjectionManager();
ProviderBinder providerBinder = new ProviderBinder(injectionManager);
providerBinder.bindClasses(Collections.singleton(CustomIntegerResolverC.class));
injectionManager.register(new Binder());
BootstrapBag bootstrapBag = new BootstrapBag();
ContextResolverFactory.ContextResolversConfigurator configurator = new ContextResolverFactory.ContextResolversConfigurator();
configurator.init(injectionManager, bootstrapBag);
injectionManager.completeRegistration();
configurator.postInit(injectionManager, bootstrapBag);
crf = injectionManager.getInstance(ContextResolverFactory.class);
}
use of org.glassfish.jersey.internal.BootstrapBag in project jersey by eclipse-ee4j.
the class ResourceModelConfigurator method bindProvidersAndResources.
private void bindProvidersAndResources(InjectionManager injectionManager, ServerBootstrapBag bootstrapBag, ComponentBag componentBag, Collection<Class<?>> resourceClasses, Collection<Object> resourceInstances, ResourceConfig runtimeConfig) {
Collection<ComponentProvider> componentProviders = bootstrapBag.getComponentProviders().get();
JerseyResourceContext resourceContext = bootstrapBag.getResourceContext();
Set<Class<?>> registeredClasses = runtimeConfig.getRegisteredClasses();
/*
* Check the {@code component} whether it is correctly configured for client or server {@link RuntimeType runtime}.
*/
java.util.function.Predicate<Class<?>> correctlyConfigured = componentClass -> Providers.checkProviderRuntime(componentClass, componentBag.getModel(componentClass), RuntimeType.SERVER, !registeredClasses.contains(componentClass), resourceClasses.contains(componentClass));
/*
* Check the {@code resource class} whether it is correctly configured for client or server {@link RuntimeType runtime}.
*/
BiPredicate<Class<?>, ContractProvider> correctlyConfiguredResource = (resourceClass, model) -> Providers.checkProviderRuntime(resourceClass, model, RuntimeType.SERVER, !registeredClasses.contains(resourceClass), true);
Set<Class<?>> componentClasses = componentBag.getClasses(ComponentBag.excludeMetaProviders(injectionManager)).stream().filter(correctlyConfigured).collect(Collectors.toSet());
// Merge programmatic resource classes with component classes.
Set<Class<?>> classes = Collections.newSetFromMap(new IdentityHashMap<>());
classes.addAll(componentClasses);
classes.addAll(resourceClasses);
// Bind classes.
for (final Class<?> componentClass : classes) {
ContractProvider model = componentBag.getModel(componentClass);
if (bindWithComponentProvider(componentClass, model, componentProviders)) {
continue;
}
if (resourceClasses.contains(componentClass)) {
if (!Resource.isAcceptable(componentClass)) {
LOGGER.warning(LocalizationMessages.NON_INSTANTIABLE_COMPONENT(componentClass));
continue;
}
if (model != null && !correctlyConfiguredResource.test(componentClass, model)) {
model = null;
}
resourceContext.unsafeBindResource(componentClass, model);
} else {
ProviderBinder.bindProvider(componentClass, model, injectionManager);
}
}
// Merge programmatic resource instances with other component instances.
Set<Object> instances = componentBag.getInstances(ComponentBag.excludeMetaProviders(injectionManager)).stream().filter(instance -> correctlyConfigured.test(instance.getClass())).collect(Collectors.toSet());
instances.addAll(resourceInstances);
// Bind instances.
for (Object component : instances) {
ContractProvider model = componentBag.getModel(component.getClass());
if (resourceInstances.contains(component)) {
if (model != null && !correctlyConfiguredResource.test(component.getClass(), model)) {
model = null;
}
resourceContext.unsafeBindResource(component, model);
} else {
ProviderBinder.bindProvider(component, model, injectionManager);
}
}
}
use of org.glassfish.jersey.internal.BootstrapBag in project jersey by eclipse-ee4j.
the class JaxrsProvidersTest method testProviders.
@Test
public void testProviders() throws Exception {
InjectionManager injectionManager = Injections.createInjectionManager();
injectionManager.register(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER));
injectionManager.register(new Binder());
BootstrapBag bootstrapBag = new BootstrapBag();
List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(new RequestScope.RequestScopeConfigurator(), new TestConfigConfigurator(), new ContextResolverFactory.ContextResolversConfigurator(), new MessageBodyFactory.MessageBodyWorkersConfigurator(), new ExceptionMapperFactory.ExceptionMappersConfigurator(), new JaxrsProviders.ProvidersConfigurator());
injectionManager.register(new TestBinder());
TestBinder.initProviders(injectionManager);
bootstrapConfigurators.forEach(configurator -> configurator.init(injectionManager, bootstrapBag));
injectionManager.completeRegistration();
bootstrapConfigurators.forEach(configurator -> configurator.postInit(injectionManager, bootstrapBag));
RequestScope scope = bootstrapBag.getRequestScope();
scope.runInScope((Callable<Object>) () -> {
Providers instance = injectionManager.getInstance(Providers.class);
assertNotNull(instance);
assertSame(JaxrsProviders.class, instance.getClass());
assertNotNull(instance.getExceptionMapper(Throwable.class));
assertNotNull(instance.getMessageBodyReader(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
assertNotNull(instance.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
assertNotNull(instance.getContextResolver(String.class, MediaType.TEXT_PLAIN_TYPE));
return null;
});
}
Aggregations