ServerProperties.RESOURCE_VALIDATION_DISABLE,
Boolean.FALSE,
Boolean.class);
final ResourceBag resourceBag;
final ProcessingProviders processingProviders;
final List<ComponentProvider> componentProviders;
final ComponentBag componentBag;
ResourceModel resourceModel;
Errors.mark(); // mark begin of validation phase
try {
// AutoDiscoverable.
if (!PropertiesHelper.getValue(runtimeConfig.getProperties(), RuntimeType.SERVER,
CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, Boolean.FALSE, Boolean.class)) {
runtimeConfig.configureAutoDiscoverableProviders(locator);
}
// Configure binders and features.
runtimeConfig.configureMetaProviders(locator);
final ResourceBag.Builder resourceBagBuilder = new ResourceBag.Builder();
// Adding programmatic resource models
for (Resource programmaticResource : runtimeConfig.getResources()) {
resourceBagBuilder.registerProgrammaticResource(programmaticResource);
}
// Introspecting classes & instances
for (Class<?> c : runtimeConfig.getClasses()) {
try {
Resource resource = Resource.from(c, disableValidation);
if (resource != null) {
resourceBagBuilder.registerResource(c, resource);
}
} catch (IllegalArgumentException ex) {
LOGGER.warning(ex.getMessage());
}
}
for (Object o : runtimeConfig.getSingletons()) {
try {
Resource resource = Resource.from(o.getClass(), disableValidation);
if (resource != null) {
resourceBagBuilder.registerResource(o, resource);
}
} catch (IllegalArgumentException ex) {
LOGGER.warning(ex.getMessage());
}
}
resourceBag = resourceBagBuilder.build();
runtimeConfig.lock();
// Registering Injection Bindings
componentProviders = new LinkedList<ComponentProvider>();
// Registering Injection Bindings
for (RankedProvider<ComponentProvider> rankedProvider : getRankedComponentProviders()) {
final ComponentProvider provider = rankedProvider.getProvider();
provider.initialize(locator);
componentProviders.add(provider);
}
componentBag = runtimeConfig.getComponentBag();
bindProvidersAndResources(componentProviders, componentBag, resourceBag.classes, resourceBag.instances);
for (ComponentProvider componentProvider : componentProviders) {
componentProvider.done();
}
processingProviders = getProcessingProviders(componentBag);
// initialize processing provider reference
final GenericType<Ref<ProcessingProviders>> refGenericType = new GenericType<Ref<ProcessingProviders>>() {
};
final Ref<ProcessingProviders> refProcessingProvider = locator.getService(refGenericType.getType());
refProcessingProvider.set(processingProviders);
resourceModel = new ResourceModel.Builder(resourceBag.getRootResources(), false).build();
resourceModel = processResourceModel(resourceModel);
if (!disableValidation) {
final ComponentModelValidator validator = new ComponentModelValidator(locator);
validator.validate(resourceModel);
}
if (Errors.fatalIssuesFound() && !ignoreValidationErrors) {
throw new ModelValidationException(LocalizationMessages.RESOURCE_MODEL_VALIDATION_FAILED_AT_INIT(),
ModelErrors.getErrorsAsResourceModelIssues(true));
}
} finally {
if (ignoreValidationErrors) {
Errors.logErrors(true);
Errors.reset(); // reset errors to the state before validation phase
} else {
Errors.unmark();
}
}
bindEnhancingResourceClasses(resourceModel, resourceBag, componentProviders);
final RuntimeModelBuilder runtimeModelBuilder = locator.getService(RuntimeModelBuilder.class);
runtimeModelBuilder.setProcessingProviders(processingProviders);
// assembly request processing chain
/**
* Root hierarchical request matching acceptor.
* Invoked in a single linear stage as part of the main linear accepting chain.
*/
final Router resourceRoutingRoot = runtimeModelBuilder.buildModel(resourceModel.getRuntimeResourceModel(), false);
final ContainerFilteringStage preMatchRequestFilteringStage =
locator.createAndInitialize(ContainerFilteringStage.Builder.class).build(processingProviders.getPreMatchFilters(),
processingProviders.getGlobalResponseFilters());
final RoutingStage routingStage =
locator.createAndInitialize(RoutingStage.Builder.class).build(resourceRoutingRoot);
final ContainerFilteringStage resourceFilteringStage =
locator.createAndInitialize(ContainerFilteringStage.Builder.class)
.build(processingProviders.getGlobalRequestFilters(), null);
final RoutedInflectorExtractorStage routedInflectorExtractorStage =
locator.createAndInitialize(RoutedInflectorExtractorStage.class);
/**
* Root linear request acceptor. This is the main entry point for the whole request processing.
*/
final Stage<ContainerRequest> rootStage = Stages
.chain(locator.createAndInitialize(ReferencesInitializer.class))
.to(locator.createAndInitialize(ContainerMessageBodyWorkersInitializer.class))
.to(preMatchRequestFilteringStage)
.to(routingStage)
.to(resourceFilteringStage)
.build(routedInflectorExtractorStage);
// Inject instances.
for (Object instance : componentBag.getInstances(ComponentBag.EXCLUDE_META_PROVIDERS)) {
locator.inject(instance);
}
for (Object instance : resourceBag.instances) {
locator.inject(instance);
}
// initiate resource model into JerseyResourceContext
JerseyResourceContext jerseyResourceContext = locator.getService(JerseyResourceContext.class);
jerseyResourceContext.setResourceModel(resourceModel);
this.runtime = locator.createAndInitialize(ServerRuntime.Builder.class).build(rootStage);
// inject self
locator.inject(this);
if (LOGGER.isLoggable(Level.CONFIG)) {
final StringBuilder sb = new StringBuilder(LocalizationMessages.LOGGING_APPLICATION_INITIALIZED()).append('\n');
final List<Resource> rootResourceClasses = resourceBag.getRootResources();
if (!rootResourceClasses.isEmpty()) {
sb.append(LocalizationMessages.LOGGING_ROOT_RESOURCE_CLASSES()).append(":");
for (Resource r : rootResourceClasses) {
for (Class clazz : r.getHandlerClasses()) {
sb.append('\n').append(" ").append(clazz.getName());
}
}
}
sb.append('\n');
Set<MessageBodyReader> messageBodyReaders;
Set<MessageBodyWriter> messageBodyWriters;
if (LOGGER.isLoggable(Level.FINE)) {
messageBodyReaders = Sets.newHashSet(Providers.getAllProviders(locator, MessageBodyReader.class));
messageBodyWriters = Sets.newHashSet(Providers.getAllProviders(locator, MessageBodyWriter.class));
} else {
messageBodyReaders = Providers.getCustomProviders(locator, MessageBodyReader.class);
messageBodyWriters = Providers.getCustomProviders(locator, MessageBodyWriter.class);
}
printProviders(LocalizationMessages.LOGGING_PRE_MATCH_FILTERS(), processingProviders.getPreMatchFilters(), sb);
printProviders(LocalizationMessages.LOGGING_GLOBAL_REQUEST_FILTERS(), processingProviders.getGlobalRequestFilters(), sb);
printProviders(LocalizationMessages.LOGGING_GLOBAL_RESPOSE_FILTERS(), processingProviders.getGlobalResponseFilters(), sb);
printProviders(LocalizationMessages.LOGGING_GLOBAL_READER_INTERCEPTORS(), processingProviders.getGlobalReaderInterceptors(), sb);
printProviders(LocalizationMessages.LOGGING_GLOBAL_WRITER_INTERCEPTORS(), processingProviders.getGlobalWriterInterceptors(), sb);
printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_REQUEST_FILTERS(), processingProviders.getNameBoundRequestFilters(), sb);
printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_RESPOSE_FILTERS(), processingProviders.getNameBoundResponseFilters(), sb);
printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_READER_INTERCEPTORS(), processingProviders.getNameBoundReaderInterceptors(), sb);
printNameBoundProviders(LocalizationMessages.LOGGING_NAME_BOUND_WRITER_INTERCEPTORS(), processingProviders.getNameBoundWriterInterceptors(), sb);
printProviders(LocalizationMessages.LOGGING_DYNAMIC_FEATURES(), processingProviders.getDynamicFeatures(), sb);
printProviders(LocalizationMessages.LOGGING_MESSAGE_BODY_READERS(), Collections2.transform(messageBodyReaders, new WorkersToStringTransform<MessageBodyReader>()), sb);
printProviders(LocalizationMessages.LOGGING_MESSAGE_BODY_WRITERS(), Collections2.transform(messageBodyWriters, new WorkersToStringTransform<MessageBodyWriter>()), sb);
LOGGER.log(Level.CONFIG, sb.toString());
}
}