use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.
the class UntypedRecordableEventDescriptorComponentBuilderTest method testBuildComponent.
@Test
public void testBuildComponent() throws Exception {
BaseObject baseObject = mock(BaseObject.class);
XWikiDocument parentDocument = mock(XWikiDocument.class);
DocumentReference documentReference = mock(DocumentReference.class);
when(baseObject.getOwnerDocument()).thenReturn(parentDocument);
when(parentDocument.getDocumentReference()).thenReturn(documentReference);
// Ensure that the user rights are correctly checked
when(this.authorizationManager.hasAccess(any(), any(), any())).thenReturn(true);
List<WikiComponent> result = this.mocker.getComponentUnderTest().buildComponents(baseObject);
assertEquals(1, result.size());
}
use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.
the class DefaultWikiComponentBuilder method buildComponents.
@Override
public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException {
List<WikiComponent> components = new ArrayList<WikiComponent>();
if (!this.componentBridge.hasProgrammingRights(reference)) {
throw new WikiComponentException("Registering wiki components requires programming rights");
}
DefaultWikiComponent rawComponent = new DefaultWikiComponent(reference, componentBridge.getAuthorReference(reference), componentBridge.getRoleType(reference), componentBridge.getRoleHint(reference), componentBridge.getScope(reference));
rawComponent.setHandledMethods(componentBridge.getHandledMethods(reference));
rawComponent.setImplementedInterfaces(componentBridge.getDeclaredInterfaces(reference));
rawComponent.setDependencies(componentBridge.getDependencies(reference));
rawComponent.setSyntax(componentBridge.getSyntax(reference));
// Create the method invocation handler of the proxy
InvocationHandler handler = new DefaultWikiComponentInvocationHandler(rawComponent, contextComponentManager);
// Prepare a list containing the interfaces the component implements
List<Class<?>> implementedInterfaces = new ArrayList<Class<?>>();
// Add the main role
Class<?> roleTypeClass = ReflectionUtils.getTypeClass(rawComponent.getRoleType());
// Add the component role
implementedInterfaces.add(ReflectionUtils.getTypeClass(roleTypeClass));
// Add the additional interfaces declared through XObjects
implementedInterfaces.addAll(rawComponent.getImplementedInterfaces());
// Add the interfaces from the java class itself (interfaces implemented by DefaultWikiComponent)
implementedInterfaces.addAll(Arrays.asList(rawComponent.getClass().getInterfaces()));
// Create the proxy
Class<?>[] implementedInterfacesArray = implementedInterfaces.toArray(new Class<?>[0]);
WikiComponent component = (WikiComponent) Proxy.newProxyInstance(roleTypeClass.getClassLoader(), implementedInterfacesArray, handler);
components.add(component);
return components;
}
use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.
the class DefaultWikiComponentInvocationHandler method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
// We look for the method in the XObjects.
if (!this.wikiComponent.getHandledMethods().containsKey(method.getName())) {
if (method.getDeclaringClass() == Object.class || method.getDeclaringClass() == WikiComponent.class) {
// return ObjectMethodsProxy.invoke(proxy, method, args);
return method.invoke(wikiComponent, args);
} else {
// See http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/UndeclaredThrowableException.html
throw new WikiComponentRuntimeException(String.format("You need to add an Object of type [%s] in document [%s] to implement method [%s.%s]", WikiComponentConstants.METHOD_CLASS, this.wikiComponent.getDocumentReference(), method.getDeclaringClass().getName(), method.getName()));
}
} else {
WikiComponentMethodExecutor methodExecutor = componentManager.getInstance(WikiComponentMethodExecutor.class);
Map<String, Object> methodContext = new HashMap<String, Object>();
XDOM xdom = this.wikiComponent.getHandledMethods().get(method.getName());
methodContext.put(METHOD_CONTEXT_COMPONENT_KEY, proxy);
this.injectComponentDependencies(methodContext);
return methodExecutor.execute(method, args, wikiComponent.getDocumentReference(), xdom, wikiComponent.getSyntax(), methodContext);
}
}
use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.
the class PanelWikiUIExtensionComponentBuilder method buildComponents.
@Override
public List<WikiComponent> buildComponents(BaseObject baseObject) throws WikiComponentException {
String content = baseObject.getStringValue("content");
Syntax syntax = baseObject.getOwnerDocument().getSyntax();
DocumentReference documentReference = baseObject.getOwnerDocument().getDocumentReference();
DocumentReference authorReference = baseObject.getOwnerDocument().getAuthorReference();
XDOM xdom = this.parser.parse(content, syntax, documentReference);
try {
return Collections.<WikiComponent>singletonList(new PanelWikiUIExtension(baseObject.getReference(), authorReference, xdom, syntax, this.componentManager));
} catch (ComponentLookupException e) {
throw new WikiComponentException(String.format("Failed to initialize Panel UI extension [%s]", baseObject), e);
}
}
use of org.xwiki.component.wiki.WikiComponent in project xwiki-platform by xwiki.
the class DefaultWikiComponentManagerEventListener method registerAllDocumentComponents.
/**
* Register all the wiki components that come from a document in the current wiki.
*/
private void registerAllDocumentComponents() {
try {
// Retrieve all components definitions and register them.
this.wikiComponentProviders = this.componentManager.getInstanceList(WikiComponentBuilder.class);
for (WikiComponentBuilder provider : this.wikiComponentProviders) {
for (DocumentReference reference : provider.getDocumentReferences()) {
try {
List<WikiComponent> components = provider.buildComponents(reference);
this.wikiComponentManagerEventListenerHelper.registerComponentList(components);
} catch (WikiComponentException e) {
this.logger.warn("Failed to build the wiki component located in the document [{}]: {}", reference, ExceptionUtils.getRootCauseMessage(e));
}
}
}
} catch (ComponentLookupException e) {
this.logger.warn(String.format("Unable to get a list of registered WikiComponentBuilder: %s", e));
}
}
Aggregations