Search in sources :

Example 16 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ComponentScriptService method getInstance.

/**
 * Find a component instance that implements that passed type. If the component has a singleton lifecycle then this
 * method always return the same instance.
 *
 * @param <T> the component role type
 * @param roleType the class (aka role) that the component implements
 * @return the component instance or null if not found
 * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility)
 */
public <T> T getInstance(Type roleType) {
    T result = null;
    ComponentManager cm = getContextComponentManager();
    if (cm != null) {
        try {
            result = cm.getInstance(roleType);
        } catch (ComponentLookupException e) {
            setError(e);
        }
    }
    return result;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 17 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ComponentScriptService method getInstance.

/**
 * Find a component instance that implements that passed interface class. If the component has a singleton lifecycle
 * then this method always return the same instance.
 *
 * @param <T> the component role type
 * @param roleType the class (aka role) that the component implements
 * @param roleHint the hint that differentiates a component implementation from another one (each component is
 *            registered with a hint; the "default" hint being the default)
 * @return the component instance or null if not found
 * @since 4.0RC1 and modified in 6.1M2 to be public (had package visibility)
 */
public <T> T getInstance(Type roleType, String roleHint) {
    T result = null;
    ComponentManager cm = getContextComponentManager();
    if (cm != null) {
        try {
            result = cm.getInstance(roleType, roleHint);
        } catch (ComponentLookupException e) {
            setError(e);
        }
    }
    return result;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 18 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ComponentScriptServiceTest method getComponentManagerForNamespaceWhenProgrammingRights.

@Test
public void getComponentManagerForNamespaceWhenProgrammingRights() throws Exception {
    when(this.dab.hasProgrammingRights()).thenReturn(true);
    ComponentManager wikiComponentManager = mock(ComponentManager.class);
    when(this.componentManagerManager.getComponentManager("wiki:chess", false)).thenReturn(wikiComponentManager);
    assertSame(wikiComponentManager, this.mocker.getComponentUnderTest().getComponentManager("wiki:chess"));
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) Test(org.junit.Test)

Example 19 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class AbstractEnityComponentManagerFactory method createComponentManager.

@Override
public ComponentManager createComponentManager(String namespace, ComponentManager parentComponentManager) {
    // Get entity reference
    EntityReference reference = this.resolver.resolve(namespace.substring(getEntityType().getLowerCase().length() + 1), getEntityType());
    // Get parent reference
    EntityReference parentReference = reference.getParent();
    ComponentManager parent;
    if (parentReference != null) {
        // Get parent namespace
        String parentNamespace = parentReference.getType().getLowerCase() + ':' + this.serializer.serialize(parentReference);
        // Get parent component manager
        parent = this.manager.getComponentManager(parentNamespace, true);
    } else {
        parent = parentComponentManager;
    }
    return this.defaultFactory.createComponentManager(namespace, parent);
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager)

Example 20 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class AbstractEntityComponentManager method getComponentManagerInternal.

@Override
protected ComponentManager getComponentManagerInternal() {
    // Get current user reference
    EntityReference entityReference = getCurrentReference();
    if (entityReference == null) {
        return null;
    }
    ExecutionContext econtext = this.execution.getContext();
    // If there is no context don't try to find or register the component manager
    if (econtext == null) {
        return super.getComponentManagerInternal();
    }
    // Try to find the user component manager in the context
    EntityComponentManagerInstance contextComponentManager = (EntityComponentManagerInstance) econtext.getProperty(this.contextKey);
    if (contextComponentManager != null && contextComponentManager.entityReference.equals(entityReference)) {
        return contextComponentManager.componentManager;
    }
    // Fallback on regular user component manager search
    ComponentManager componentManager = super.getComponentManagerInternal();
    // Cache the component manager
    econtext.setProperty(this.contextKey, new EntityComponentManagerInstance(entityReference, componentManager));
    return componentManager;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) AbstractGenericComponentManager(org.xwiki.component.internal.multi.AbstractGenericComponentManager)

Aggregations

ComponentManager (org.xwiki.component.manager.ComponentManager)76 Test (org.junit.Test)34 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)23 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)18 DocumentReference (org.xwiki.model.reference.DocumentReference)17 Provider (javax.inject.Provider)14 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)12 Before (org.junit.Before)11 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)11 XWikiContext (com.xpn.xwiki.XWikiContext)9 Expectations (org.jmock.Expectations)9 WikiReference (org.xwiki.model.reference.WikiReference)9 HashMap (java.util.HashMap)7 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 XWiki (com.xpn.xwiki.XWiki)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 ArrayList (java.util.ArrayList)6 ExecutionContext (org.xwiki.context.ExecutionContext)6 MimeMessage (javax.mail.internet.MimeMessage)5