Search in sources :

Example 71 with ComponentManager

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

the class JARTranslationBundleFactoryListener method extensionDeleted.

/**
 * @param extension the installed extension
 * @param namespace the namespace where the extension has been installed
 */
private void extensionDeleted(InstalledExtension extension, String namespace) {
    try {
        ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(extension);
        ComponentManager componentManager = this.componentManagerManager.getComponentManager(namespace, false);
        componentManager.unregisterComponent(descriptor);
    } catch (Exception e) {
        this.logger.error("Failed to create TranslationBundle descriptor for extension [{}]", extension, e);
    }
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) ComponentManager(org.xwiki.component.manager.ComponentManager) InitializationException(org.xwiki.component.phase.InitializationException) MalformedURLException(java.net.MalformedURLException)

Example 72 with ComponentManager

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

the class JARTranslationBundleFactoryListener method extensionAdded.

/**
 * @param extension the uninstalled extension
 * @param namespace the namespace from where the extension has been uninstalled
 */
private void extensionAdded(InstalledExtension extension, String namespace) {
    try {
        File jarFile = new File(extension.getFile().getAbsolutePath());
        ComponentManager componentManager = this.componentManagerManager.getComponentManager(namespace, false);
        if (componentManager == null) {
            componentManager = this.rootComponentManager;
        }
        JARFileTranslationBundle bundle = new JARFileTranslationBundle(jarFile, componentManager, this.translationParser);
        ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(jarFile.toURI().toURL());
        componentManager.registerComponent(descriptor, bundle);
    } catch (Exception e) {
        this.logger.error("Failed to register a TranslationBundle component for extension [{}] on namespace [{}]", extension, namespace, e);
    }
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) ComponentManager(org.xwiki.component.manager.ComponentManager) File(java.io.File) InitializationException(org.xwiki.component.phase.InitializationException) MalformedURLException(java.net.MalformedURLException)

Example 73 with ComponentManager

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

the class LocalizationScriptServiceTest method setUp.

@Before
public void setUp() throws Exception {
    componentManager = mock(ComponentManager.class);
    Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(componentManager);
    renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
    when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
    localizationContext = mocker.getInstance(LocalizationContext.class);
    localizationManager = mocker.getInstance(LocalizationManager.class);
    localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
    translation = mock(Translation.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
            printer.print("print result");
            return null;
        }
    }).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
    when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
    when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
    when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
    environment = mocker.getInstance(Environment.class);
}
Also used : Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Provider(javax.inject.Provider) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) LocalizationManager(org.xwiki.localization.LocalizationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Before(org.junit.Before)

Example 74 with ComponentManager

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

the class XWikiRestletServlet method createApplication.

@Override
protected Application createApplication(Context context) {
    Context applicationContext = context.createChildContext();
    /* Retrieve the component manager and make it available in the restlet application context. */
    ComponentManager componentManager = getComponentManager();
    applicationContext.getAttributes().put(Constants.XWIKI_COMPONENT_MANAGER, componentManager);
    JaxRsApplication application;
    try {
        application = componentManager.getInstance(JaxRsApplication.class);
    } catch (ComponentLookupException e) {
        log("Failed to lookup default JAX-RS Application", e);
        return null;
    }
    application.setContext(applicationContext);
    // Make the servlet available
    try {
        JaxRsServletProvider applicationProvider = componentManager.getInstance(JaxRsServletProvider.class);
        applicationProvider.setApplication(this);
    } catch (ComponentLookupException e) {
        log("Failed to lookup JaxRsApplicationProvider. Dyncamically added/removed resources won'tbe supported", e);
    }
    return application;
}
Also used : Context(org.restlet.Context) ComponentManager(org.xwiki.component.manager.ComponentManager) JaxRsApplication(org.restlet.ext.jaxrs.JaxRsApplication) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 75 with ComponentManager

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

the class MainResourceReferenceHandlerManagerTest method handleWithOrder.

@Test
public void handleWithOrder() throws Exception {
    // First Handler component will lower priority
    ResourceReferenceHandler testHandler = mock(ResourceReferenceHandler.class, "handler1");
    when(testHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
    // Second Handler component will higher priority so that it's executed first
    ResourceReferenceHandler beforeTestHandler = mock(ResourceReferenceHandler.class, "handler2");
    when(beforeTestHandler.getSupportedResourceReferences()).thenReturn(Arrays.asList(new ResourceType("test")));
    // We return 1 to mean that the second Handler has a higher priority than the first Handler
    when(beforeTestHandler.compareTo(testHandler)).thenReturn(-1);
    ComponentManager contextComponentManager = this.componentManager.getInstance(ComponentManager.class, "context");
    when(contextComponentManager.<ResourceReferenceHandler>getInstanceList(new DefaultParameterizedType(null, ResourceReferenceHandler.class, ResourceType.class))).thenReturn(Arrays.asList(testHandler, beforeTestHandler));
    ResourceReference reference = mock(ResourceReference.class);
    when(reference.getType()).thenReturn(new ResourceType("test"));
    this.componentManager.getComponentUnderTest().handle(reference);
    // Verify that the second Action is called (since it has a higher priority).
    verify(beforeTestHandler).handle(same(reference), any(ResourceReferenceHandlerChain.class));
}
Also used : ResourceReferenceHandler(org.xwiki.resource.ResourceReferenceHandler) ComponentManager(org.xwiki.component.manager.ComponentManager) ResourceType(org.xwiki.resource.ResourceType) ResourceReference(org.xwiki.resource.ResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ResourceReferenceHandlerChain(org.xwiki.resource.ResourceReferenceHandlerChain) Test(org.junit.Test)

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