Search in sources :

Example 16 with WikiComponentException

use of org.xwiki.component.wiki.WikiComponentException in project xwiki-platform by xwiki.

the class DefaultWikiComponentManagerEventListener method registerDocumentComponents.

/**
 * Registers the components linked to the given document. For that, we get a list of providers that can build a
 * component using this document, then the first available provider is selected in order to register a component.
 *
 * @param documentReference the document used to create the component
 */
private void registerDocumentComponents(DocumentReference documentReference) {
    // Unregister all wiki components registered under the given entity. We do this as otherwise we would need to
    // handle the specific cases of elements added, elements updated and elements deleted, etc.
    // Instead we unregister all wiki components and re-register them all.
    this.wikiComponentManagerEventListenerHelper.unregisterComponents(documentReference);
    // Re-register all wiki components in the passed document
    for (WikiComponentBuilder provider : this.wikiComponentProviders) {
        // Check whether the given document has a wiki component defined in it.
        if (provider.getDocumentReferences().contains(documentReference)) {
            try {
                List<WikiComponent> components = provider.buildComponents(documentReference);
                this.wikiComponentManagerEventListenerHelper.registerComponentList(components);
            } catch (WikiComponentException e) {
                this.logger.warn("Failed to create wiki component(s) for document [{}]: {}", documentReference, ExceptionUtils.getRootCauseMessage(e));
            }
            break;
        }
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponentBuilder(org.xwiki.component.wiki.WikiComponentBuilder) WikiComponent(org.xwiki.component.wiki.WikiComponent)

Example 17 with WikiComponentException

use of org.xwiki.component.wiki.WikiComponentException in project xwiki-platform by xwiki.

the class DefaultWikiComponentManager method unregisterWikiComponent.

private void unregisterWikiComponent(Iterator<WikiComponent> iterator) throws WikiComponentException {
    WikiComponent wikiComponent = iterator.next();
    // Save current context information
    DocumentReference currentUserReference = this.wikiComponentManagerContext.getCurrentUserReference();
    EntityReference currentEntityReference = this.wikiComponentManagerContext.getCurrentEntityReference();
    try {
        // Set the proper information so the component manager use the proper keys to find components to
        // unregister
        this.wikiComponentManagerContext.setCurrentUserReference(wikiComponent.getAuthorReference());
        this.wikiComponentManagerContext.setCurrentEntityReference(wikiComponent.getEntityReference());
        // Remove from the Component Manager
        getComponentManager(wikiComponent.getScope()).unregisterComponent(wikiComponent.getRoleType(), wikiComponent.getRoleHint());
        // Remove from the wiki component cache
        iterator.remove();
    } catch (ComponentLookupException e) {
        throw new WikiComponentException(String.format("Failed to find a component manager for scope [%s]", wikiComponent.getScope()), e);
    } finally {
        this.wikiComponentManagerContext.setCurrentUserReference(currentUserReference);
        this.wikiComponentManagerContext.setCurrentEntityReference(currentEntityReference);
    }
}
Also used : WikiComponentException(org.xwiki.component.wiki.WikiComponentException) WikiComponent(org.xwiki.component.wiki.WikiComponent) EntityReference(org.xwiki.model.reference.EntityReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 18 with WikiComponentException

use of org.xwiki.component.wiki.WikiComponentException in project xwiki-platform by xwiki.

the class DefaultWikiComponentManager method registerWikiComponent.

@Override
public void registerWikiComponent(WikiComponent component) throws WikiComponentException {
    // Save current context information
    DocumentReference currentUserReference = this.wikiComponentManagerContext.getCurrentUserReference();
    EntityReference currentEntityReference = this.wikiComponentManagerContext.getCurrentEntityReference();
    try {
        // Get the component role interface
        Type roleType = component.getRoleType();
        Class<?> roleTypeClass = ReflectionUtils.getTypeClass(roleType);
        ComponentDescriptor componentDescriptor = createComponentDescriptor(roleType, component.getRoleHint());
        // Set the proper information so the component manager use the proper keys to find components to register
        this.wikiComponentManagerContext.setCurrentUserReference(component.getAuthorReference());
        this.wikiComponentManagerContext.setCurrentEntityReference(component.getEntityReference());
        // Since we are responsible to create the component instance, we also are responsible of its initialization
        if (this.isInitializable(component.getClass().getInterfaces())) {
            try {
                ((Initializable) component).initialize();
            } catch (InitializationException e) {
                this.logger.error("Failed to initialize wiki component", e);
            }
        }
        // Register the wiki component against the Component Manager
        getComponentManager(component.getScope()).registerComponent(componentDescriptor, roleTypeClass.cast(component));
        // And add it the wiki component cache so that we can remove it later on. We need to do this since we need
        // to be able to unregister a wiki component associated with a wiki page
        cacheWikiComponent(component);
    } catch (ComponentLookupException e) {
        throw new WikiComponentException(String.format("Failed to find a component manager for scope [%s] wiki " + "component registration failed", component.getScope()), e);
    } catch (ComponentRepositoryException e) {
        throw new WikiComponentException("Failed to register wiki component against component repository", e);
    } finally {
        this.wikiComponentManagerContext.setCurrentUserReference(currentUserReference);
        this.wikiComponentManagerContext.setCurrentEntityReference(currentEntityReference);
    }
}
Also used : Type(java.lang.reflect.Type) WikiComponentException(org.xwiki.component.wiki.WikiComponentException) Initializable(org.xwiki.component.phase.Initializable) ComponentDescriptor(org.xwiki.component.descriptor.ComponentDescriptor) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) EntityReference(org.xwiki.model.reference.EntityReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ComponentRepositoryException(org.xwiki.component.manager.ComponentRepositoryException) InitializationException(org.xwiki.component.phase.InitializationException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

WikiComponentException (org.xwiki.component.wiki.WikiComponentException)18 BaseObject (com.xpn.xwiki.objects.BaseObject)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 WikiComponent (org.xwiki.component.wiki.WikiComponent)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 XWikiException (com.xpn.xwiki.XWikiException)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ComponentDescriptor (org.xwiki.component.descriptor.ComponentDescriptor)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 WikiComponentBuilder (org.xwiki.component.wiki.WikiComponentBuilder)2 EntityReference (org.xwiki.model.reference.EntityReference)2 NotificationException (org.xwiki.notifications.NotificationException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 HashMap (java.util.HashMap)1 ComponentRepositoryException (org.xwiki.component.manager.ComponentRepositoryException)1 Initializable (org.xwiki.component.phase.Initializable)1