Search in sources :

Example 1 with InstantiateException

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);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiRestComponent(org.xwiki.rest.XWikiRestComponent) InstantiateException(org.restlet.ext.jaxrs.InstantiateException) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) List(java.util.List) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 InstantiateException (org.restlet.ext.jaxrs.InstantiateException)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 XWikiRestComponent (org.xwiki.rest.XWikiRestComponent)1