if (resourceConfig == null) {
throw new IllegalArgumentException("ResourceConfig instance MUST NOT be null");
}
if (initiated) {
throw new ContainerException(ImplMessages.WEB_APP_ALREADY_INITIATED());
}
this.initiated = true;
// Validate the resource config
resourceConfig.validate();
// Check the resource configuration
this.resourceConfig = resourceConfig;
this.provider = _provider;
// Set up the component provider factory to be
// used with non-resource class components
this.cpFactory = (_provider == null)
? new ProviderFactory(injectableFactory)
: new IoCProviderFactory(injectableFactory, _provider);
// Set up the resource component provider factory
this.rcpFactory = (_provider == null)
? new ResourceFactory(this.resourceConfig, this.injectableFactory)
: new IoCResourceFactory(this.resourceConfig, this.injectableFactory, _provider);
this.resourceContext = new ResourceContext() {
public <T> T getResource(Class<T> c) {
final ResourceClass rc = getResourceClass(c);
if (rc == null) {
LOGGER.severe("No resource class found for class " + c.getName());
throw new ContainerException("No resource class found for class " + c.getName());
}
final Object instance = rc.rcProvider.getInstance(context);
return instance != null ? c.cast(instance) : null;
}
};
ProviderServices providerServices = new ProviderServices(
this.injectableFactory,
this.cpFactory,
resourceConfig.getProviderClasses(),
resourceConfig.getProviderSingletons());
// Add injectable provider for @Inject
injectableFactory.add(
new InjectableProvider<Inject, Type>() {
public ComponentScope getScope() {
return ComponentScope.Undefined;
}
@SuppressWarnings("unchecked")
public Injectable<Object> getInjectable(ComponentContext ic, Inject a, final Type c) {
if (!(c instanceof Class))
return null;
if (provider == null)
return null;
final IoCComponentProvider p = provider.getComponentProvider(ic, (Class)c);
return new Injectable<Object>() {
public Object getValue() {
try {
return p.getInstance();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not get instance from IoC component provider for type " +
c, e);
throw new ContainerException("Could not get instance from IoC component provider for type " +
c, e);
}
}
};
}