use of org.xwiki.component.manager.ComponentLookupException 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);
}
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class DefaultWikiComponentInvocationHandler method injectComponentDependencies.
/**
* Retrieves the wiki component dependencies from the component manager and puts them in the method context under
* the configured key.
*
* @param methodContext The context where the dependencies must be injected
*/
private void injectComponentDependencies(Map<String, Object> methodContext) {
for (Map.Entry<String, ComponentDescriptor> dependency : this.wikiComponent.getDependencies().entrySet()) {
ComponentDescriptor cd = dependency.getValue();
Class<?> roleTypeClass = ReflectionUtils.getTypeClass(cd.getRoleType());
Object componentDependency = null;
try {
if (roleTypeClass.isAssignableFrom(List.class)) {
// If the ParameterizedType is a List, the raw Type is the List Class and the first Type argument
// is the actual component (which can be a ParameterizedType itself).
// Example: java.util.List<org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>>
// raw Type: java.util.List
// Type arguments [0]: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
componentDependency = componentManager.getInstanceList(((ParameterizedType) cd.getRoleType()).getActualTypeArguments()[0]);
} else if (roleTypeClass.isAssignableFrom(Map.class)) {
// If the ParameterizedType is a Map, the raw Type is the Map, the first argument can only be a
// String in our implementation and the second argument is the actual component.
// Example: java.util.Map<java.lang.String,
// org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>>
// raw Type: java.util.Map
// Type arguments [0]: java.lang.String
// [1]: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
componentDependency = componentManager.getInstanceMap(((ParameterizedType) cd.getRoleType()).getActualTypeArguments()[1]);
} else {
// Not a List or a Map, note that the role Type can be a ParameterizedType itself
// Example: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
componentDependency = componentManager.getInstance(cd.getRoleType(), cd.getRoleHint());
}
} catch (ComponentLookupException e) {
this.logger.warn(String.format("No component found for role [%s] with hint [%s], declared as dependency for wiki component [%s]", cd.getRoleType().toString(), cd.getRoleHint(), this.wikiComponent.getDocumentReference()));
}
methodContext.put(dependency.getKey(), componentDependency);
}
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class ContextComponentManagerTest method testRegisterComponentInWikiComponentManager.
@Test
public void testRegisterComponentInWikiComponentManager() throws Exception {
final States state = getMockery().states("test");
getMockery().checking(new Expectations() {
{
allowing(mockWikiDescriptorManager).getCurrentWikiId();
when(state.isNot("otherwiki"));
will(returnValue("wiki1"));
allowing(mockCurrentSpaceReferenceProvider).get();
when(state.isNot("otherwiki"));
will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
allowing(mockCurrentDocumentReferenceProvider).get();
when(state.isNot("otherwiki"));
will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
allowing(mockDocumentAccessBridge).getCurrentUserReference();
when(state.isNot("otherwiki"));
will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
}
});
// Register in the current wiki.
ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
cd.setRoleType(Role.class);
cd.setImplementation(RoleImpl.class);
wikiCM.registerComponent(cd);
// Verify we can lookup the component from the context CM.
ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
// Now verify that we cannot look it up anymore if there's another wiki in the context
state.become("otherwiki");
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.
}
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class ContextComponentManagerTest method testRegisterComponentInDocumentComponentManager.
@Test
public void testRegisterComponentInDocumentComponentManager() throws Exception {
final States state = getMockery().states("test");
getMockery().checking(new Expectations() {
{
allowing(mockWikiDescriptorManager).getCurrentWikiId();
when(state.isNot("otherdocument"));
will(returnValue("wiki1"));
allowing(mockCurrentSpaceReferenceProvider).get();
when(state.isNot("otherdocument"));
will(returnValue(new SpaceReference("space1", new WikiReference("wiki"))));
allowing(mockCurrentDocumentReferenceProvider).get();
when(state.isNot("otherdocument"));
will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
allowing(mockDocumentAccessBridge).getCurrentUserReference();
when(state.isNot("otherdocument"));
will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
}
});
ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
cd.setRoleType(Role.class);
cd.setImplementation(RoleImpl.class);
// Register component for the current user
documentCM.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("otherdocument");
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.
}
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class ContextComponentManagerTest method testRegisterComponentInUserComponentManager.
@Test
public void testRegisterComponentInUserComponentManager() throws Exception {
final States state = getMockery().states("test");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getCurrentUserReference();
when(state.isNot("otheruser"));
will(returnValue(new DocumentReference("wiki", "XWiki", "user1")));
allowing(mockWikiDescriptorManager).getCurrentWikiId();
will(returnValue("wiki"));
allowing(mockCurrentSpaceReferenceProvider).get();
will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
allowing(mockCurrentDocumentReferenceProvider).get();
will(returnValue(new DocumentReference("wiki", "space", "document")));
}
});
ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
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.
}
// Register component for the current user
ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "user");
DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
cd.setRoleType(Role.class);
cd.setImplementation(RoleImpl.class);
userCM.registerComponent(cd);
// Verify we can lookup the component from the Context CM
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("otheruser");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getCurrentUserReference();
will(returnValue(new DocumentReference("wiki", "XWiki", "user2")));
allowing(mockWikiDescriptorManager).getCurrentWikiId();
will(returnValue("wiki"));
allowing(mockCurrentSpaceReferenceProvider).get();
will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
allowing(mockCurrentDocumentReferenceProvider).get();
will(returnValue(new DocumentReference("wiki", "space", "document")));
}
});
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.
}
}
Aggregations