Search in sources :

Example 1 with WikiComponent

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());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiComponent(org.xwiki.component.wiki.WikiComponent) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 2 with WikiComponent

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;
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponent(org.xwiki.component.wiki.WikiComponent) ArrayList(java.util.ArrayList) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 3 with WikiComponent

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);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) WikiComponent(org.xwiki.component.wiki.WikiComponent) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 4 with WikiComponent

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);
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) XDOM(org.xwiki.rendering.block.XDOM) WikiComponent(org.xwiki.component.wiki.WikiComponent) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with WikiComponent

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));
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponentBuilder(org.xwiki.component.wiki.WikiComponentBuilder) WikiComponent(org.xwiki.component.wiki.WikiComponent) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

WikiComponent (org.xwiki.component.wiki.WikiComponent)8 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)5 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 Test (org.junit.Test)2 WikiComponentBuilder (org.xwiki.component.wiki.WikiComponentBuilder)2 XDOM (org.xwiki.rendering.block.XDOM)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 StringWriter (java.io.StringWriter)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 VelocityContext (org.apache.velocity.VelocityContext)1 WikiComponentRuntimeException (org.xwiki.component.wiki.WikiComponentRuntimeException)1 ContentParser (org.xwiki.component.wiki.internal.bridge.ContentParser)1 EntityReference (org.xwiki.model.reference.EntityReference)1 Syntax (org.xwiki.rendering.syntax.Syntax)1 VelocityManager (org.xwiki.velocity.VelocityManager)1