use of org.restlet.ext.jaxrs.InstantiateException in project xwiki-platform by xwiki.
the class ComponentsObjectFactory method getInstance.
@Override
public <T> T getInstance(Class<T> clazz) throws InstantiateException {
try {
ComponentManager componentManager = this.componentManagerProvider.get();
// Use the component manager to lookup the class. This ensure that injections are properly executed.
XWikiRestComponent component = componentManager.getInstance(XWikiRestComponent.class, clazz.getName());
// JAX-RS resources and providers must be declared as components whose hint is the FQN of the class
// implementing it. This is needed because of they are looked up using the FQN as the hint.
ComponentDescriptor<XWikiRestComponent> componentDescriptor = componentManager.getComponentDescriptor(XWikiRestComponent.class, clazz.getName());
// Retrieve the list of releasable components from the execution context. This is used to store component
// instances that need to be released at the end of the request.
ExecutionContext executionContext = this.execution.getContext();
List<XWikiRestComponent> releasableComponentReferences = (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
if (releasableComponentReferences == null) {
releasableComponentReferences = new ArrayList<>();
executionContext.setProperty(Constants.RELEASABLE_COMPONENT_REFERENCES, releasableComponentReferences);
}
// Only add the components that have a per-lookup instantiation strategy.
if (componentDescriptor.getInstantiationStrategy() == ComponentInstantiationStrategy.PER_LOOKUP) {
releasableComponentReferences.add(component);
}
// component hint to the actual fully qualified name of the Java class.
return (T) component;
} catch (ComponentLookupException e) {
throw new InstantiateException(e);
}
}
Aggregations