Search in sources :

Example 1 with XWikiRestComponent

use of org.xwiki.rest.XWikiRestComponent 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)

Example 2 with XWikiRestComponent

use of org.xwiki.rest.XWikiRestComponent in project xwiki-platform by xwiki.

the class XWikiSetupCleanupFilter method getReleasableComponents.

/**
 * @param componentManager the component manager
 * @return the list of JAX-RS resources that are implemented as components with per-lookup policy and that have been
 *         instantiated during this request
 */
private List<XWikiRestComponent> getReleasableComponents(ComponentManager componentManager) {
    try {
        ExecutionContext executionContext = componentManager.<Execution>getInstance(Execution.class).getContext();
        @SuppressWarnings("unchecked") List<XWikiRestComponent> releasableComponents = (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
        return releasableComponents != null ? releasableComponents : Collections.<XWikiRestComponent>emptyList();
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Failed to retrieve the list of releasable components.", e);
        return Collections.emptyList();
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) XWikiRestComponent(org.xwiki.rest.XWikiRestComponent) List(java.util.List) ComponentLifecycleException(org.xwiki.component.manager.ComponentLifecycleException)

Aggregations

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