Search in sources :

Example 91 with ComponentLookupException

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

the class DefaultResourceReferenceSerializer method serialize.

@Override
public ExtendedURL serialize(ResourceReference resource) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    ResourceReferenceSerializer<ResourceReference, ExtendedURL> serializer;
    ParameterizedType type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, ResourceReference.class, ExtendedURL.class);
    try {
        serializer = this.componentManager.getInstance(type, this.urlContextManager.getURLFormatId());
    } catch (ComponentLookupException e) {
        throw new UnsupportedResourceReferenceException(String.format("Invalid URL format id [%s]. Cannot serialize Resource Reference [%s].", this.urlContextManager.getURLFormatId(), resource), e);
    }
    return serializer.serialize(resource);
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ResourceReference(org.xwiki.resource.ResourceReference) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 92 with ComponentLookupException

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

the class AbstractDocumentMojo method createXWikiContext.

protected XWikiContext createXWikiContext() throws MojoExecutionException {
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    ecm.initialize(this.getClass().getClassLoader());
    Utils.setComponentManager(ecm);
    // Context component manager is totally useless here
    ecm.unregisterComponent(ComponentManager.class, "context");
    // We need to initialize the Component Manager so that the components can be looked up
    XWikiContext xcontext = new XWikiContext();
    xcontext.put(ComponentManager.class.getName(), ecm);
    // Initialize the Container fields (request, response, session).
    try {
        ExecutionContextManager ecim = ecm.getInstance(ExecutionContextManager.class);
        ExecutionContext econtext = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code.
        xcontext.declareInExecutionContext(econtext);
        ecim.initialize(econtext);
    } catch (ExecutionContextException | ComponentLookupException e) {
        throw new MojoExecutionException("Failed to initialize Execution Context.", e);
    }
    return xcontext;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 93 with ComponentLookupException

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

the class ContextComponentManagerTest method testRegisterComponentInSpaceComponentManager.

@Test
public void testRegisterComponentInSpaceComponentManager() throws Exception {
    final States state = getMockery().states("test");
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            when(state.isNot("otherspace"));
            will(returnValue("wiki1"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            when(state.isNot("otherspace"));
            will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            when(state.isNot("otherspace"));
            will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otherspace"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "space");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    // Register component for the current user
    userCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getInstance(Role.class));
    // Now verify that we cannot look it up anymore if there's another user in the context
    state.become("otherspace");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki2"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
        }
    });
    try {
        contextCM.getInstance(Role.class);
        Assert.fail("Should have raised an exception");
    } catch (ComponentLookupException expected) {
    // No need to assert the message, we just want to ensure an exception is raised.
    }
}
Also used : States(org.jmock.States) Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 94 with ComponentLookupException

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

the class ComponentScriptServiceTest method getComponentInstanceWithHintWhenComponentDoesntExist.

@Test
public void getComponentInstanceWithHintWhenComponentDoesntExist() throws Exception {
    when(this.dab.hasProgrammingRights()).thenReturn(true);
    when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenThrow(new ComponentLookupException("error"));
    ExecutionContext context = new ExecutionContext();
    when(this.execution.getContext()).thenReturn(context);
    assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint"));
    assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Test(org.junit.Test)

Example 95 with ComponentLookupException

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

the class ComponentScriptServiceTest method getComponentInstanceWhenComponentDoesntExist.

@Test
public void getComponentInstanceWhenComponentDoesntExist() throws Exception {
    when(this.dab.hasProgrammingRights()).thenReturn(true);
    when(this.contextComponentManager.getInstance(SomeRole.class)).thenThrow(new ComponentLookupException("error"));
    ExecutionContext context = new ExecutionContext();
    when(this.execution.getContext()).thenReturn(context);
    assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class));
    assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Test(org.junit.Test)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)104 ComponentManager (org.xwiki.component.manager.ComponentManager)24 Test (org.junit.Test)15 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 InitializationException (org.xwiki.component.phase.InitializationException)8 ArrayList (java.util.ArrayList)7 XWikiException (com.xpn.xwiki.XWikiException)5 HashMap (java.util.HashMap)5 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)5 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)5 FilterException (org.xwiki.filter.FilterException)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Type (java.lang.reflect.Type)4 HashSet (java.util.HashSet)4 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 WikiReference (org.xwiki.model.reference.WikiReference)4