Search in sources :

Example 26 with ComponentManager

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

the class MailStorageScriptServiceTest method setUp.

@Before
public void setUp() throws Exception {
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    when(execution.getContext()).thenReturn(executionContext);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) Before(org.junit.Before)

Example 27 with ComponentManager

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

the class UsersMimeMessageFactoryTest method createMessageWhenNotExistingMimeMessageFactory.

@Test
public void createMessageWhenNotExistingMimeMessageFactory() throws Exception {
    DocumentReference userReference = new DocumentReference("wiki", "space", "page");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hint", "factoryHint");
    parameters.put("source", "factoryHint");
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    try {
        this.mocker.getComponentUnderTest().createMessage(Arrays.asList(userReference), parameters);
        fail("Should have thrown an exception");
    } catch (MessagingException expected) {
        assertEquals("Failed to find a [MimeMessageFactory<MimeMessage>] for hint [factoryHint]", expected.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 28 with ComponentManager

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

the class DefaultMandatoryDocumentInitializerManager method getMandatoryDocumentInitializer.

@Override
public MandatoryDocumentInitializer getMandatoryDocumentInitializer(DocumentReference documentReference) {
    ComponentManager componentManager = this.componentManagerProvider.get();
    String fullReference = this.serializer.serialize(documentReference);
    try {
        if (componentManager.hasComponent(MandatoryDocumentInitializer.class, fullReference)) {
            return componentManager.getInstance(MandatoryDocumentInitializer.class, fullReference);
        } else {
            String localReference = this.localSerializer.serialize(documentReference);
            if (componentManager.hasComponent(MandatoryDocumentInitializer.class, localReference)) {
                return componentManager.getInstance(MandatoryDocumentInitializer.class, localReference);
            }
        }
    } catch (ComponentLookupException e) {
        this.logger.error("Failed to initialize component [{}] for document", MandatoryDocumentInitializer.class, documentReference, e);
    }
    return null;
}
Also used : MandatoryDocumentInitializer(com.xpn.xwiki.doc.MandatoryDocumentInitializer) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 29 with ComponentManager

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

the class Utils method getComponent.

/**
 * Lookup a XWiki component by role and hint.
 *
 * @param roleType the class (aka role) that the component implements
 * @param roleHint a value to differentiate different component implementations for the same role
 * @return the component's instance
 * @throws RuntimeException if the component cannot be found/initialized, or if the component manager is not
 *             initialized
 * @deprecated starting with 4.1M2 use the Component Script Service instead
 */
@Deprecated
public static <T> T getComponent(Type roleType, String roleHint) {
    T component;
    ComponentManager componentManager = getContextComponentManager();
    if (componentManager != null) {
        try {
            component = componentManager.getInstance(roleType, roleHint);
        } catch (ComponentLookupException e) {
            throw new RuntimeException("Failed to load component for type [" + roleType + "] for hint [" + roleHint + "]", e);
        }
    } else {
        throw new RuntimeException("Component manager has not been initialized before lookup for [" + roleType + "] for hint [" + roleHint + "]");
    }
    return component;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 30 with ComponentManager

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

the class Utils method getComponentList.

/**
 * @param <T> the component type
 * @param role the role for which to return implementing components
 * @return all components implementing the passed role
 * @throws RuntimeException if some of the components cannot be found/initialized, or if the component manager is
 *             not initialized
 * @since 2.0M3
 * @deprecated since 4.0M1 use {@link #getComponentManager()} instead
 */
@Deprecated
public static <T> List<T> getComponentList(Class<T> role) {
    List<T> components;
    ComponentManager componentManager = getContextComponentManager();
    if (componentManager != null) {
        try {
            components = componentManager.getInstanceList(role);
        } catch (ComponentLookupException e) {
            throw new RuntimeException("Failed to load components with role [" + role.getName() + "]", e);
        }
    } else {
        throw new RuntimeException("Component manager has not been initialized before lookup for role [" + role.getName() + "]");
    }
    return components;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

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