final ComponentBag componentBag,
final Collection<Class<?>> resourceClasses,
final Collection<Object> resourceInstances) {
final JerseyResourceContext resourceContext = locator.getService(JerseyResourceContext.class);
final DynamicConfiguration dc = Injections.getConfiguration(locator);
final Set<Class<?>> registeredClasses = runtimeConfig.getRegisteredClasses();
// Merge programmatic resource classes with component classes.
final Set<Class<?>> classes = Sets.newIdentityHashSet();
classes.addAll(Sets.filter(componentBag.getClasses(ComponentBag.EXCLUDE_META_PROVIDERS),
new Predicate<Class<?>>() {
@Override
public boolean apply(final Class<?> componentClass) {
return Providers.checkProviderRuntime(
componentClass,
componentBag.getModel(componentClass),
RuntimeType.SERVER,
!registeredClasses.contains(componentClass),
resourceClasses.contains(componentClass));
}
}
));
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 && !Providers.checkProviderRuntime(
componentClass,
model,
RuntimeType.SERVER,
!registeredClasses.contains(componentClass),
true)) {
model = null;
}
resourceContext.unsafeBindResource(componentClass, model, dc);
} else {
ProviderBinder.bindProvider(componentClass, model, dc);
}
}
// Merge programmatic resource instances with other component instances.
final Set<Object> instances = Sets.newHashSet();
instances.addAll(Sets.filter(componentBag.getInstances(ComponentBag.EXCLUDE_META_PROVIDERS),
new Predicate<Object>() {
@Override
public boolean apply(final Object component) {
final Class<?> componentClass = component.getClass();
return Providers.checkProviderRuntime(
componentClass,
componentBag.getModel(componentClass),
RuntimeType.SERVER,
!registeredClasses.contains(componentClass),
resourceInstances.contains(component));
}
}
));
instances.addAll(resourceInstances);
// Bind instances.
for (final Object component : instances) {
ContractProvider model = componentBag.getModel(component.getClass());
if (resourceInstances.contains(component)) {
if (model != null && !Providers.checkProviderRuntime(
component.getClass(),
model,
RuntimeType.SERVER,
!registeredClasses.contains(component.getClass()),
true)) {
model = null;
}
resourceContext.unsafeBindResource(component, model, dc);
} else {
ProviderBinder.bindProvider(component, model, dc);
}
}
dc.commit();
}