Search in sources :

Example 1 with ComponentRepositoryException

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

the class StandardExtendedURLResourceTypeResolver method registerEntityResourceReferenceResolver.

private void registerEntityResourceReferenceResolver(String registrationHint, Class<? extends ResourceReferenceResolver<ExtendedURL>> registrationImplementation, String wikiExtractorHint) throws InitializationException {
    DefaultComponentDescriptor<ResourceReferenceResolver<ExtendedURL>> resolverDescriptor = new DefaultComponentDescriptor<>();
    resolverDescriptor.setImplementation(registrationImplementation);
    resolverDescriptor.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
    String hint = computeHint(registrationHint);
    resolverDescriptor.setRoleHint(hint);
    resolverDescriptor.setRoleType(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
    // Register dependencies
    DefaultComponentDependency<WikiReferenceExtractor> wikiReferenceExtractorDependency = new DefaultComponentDependency<>();
    wikiReferenceExtractorDependency.setRoleType(WikiReferenceExtractor.class);
    wikiReferenceExtractorDependency.setRoleHint(wikiExtractorHint);
    wikiReferenceExtractorDependency.setName("wikiExtractor");
    resolverDescriptor.addComponentDependency(wikiReferenceExtractorDependency);
    DefaultComponentDependency<EntityReferenceResolver<EntityReference>> entityReferenceResolverDependency = new DefaultComponentDependency<>();
    entityReferenceResolverDependency.setRoleType(new DefaultParameterizedType(null, EntityReferenceResolver.class, EntityReference.class));
    entityReferenceResolverDependency.setName("defaultReferenceEntityReferenceResolver");
    resolverDescriptor.addComponentDependency(entityReferenceResolverDependency);
    DefaultComponentDependency<StandardURLConfiguration> standardURLConfigurationDependency = new DefaultComponentDependency<>();
    standardURLConfigurationDependency.setRoleType(StandardURLConfiguration.class);
    standardURLConfigurationDependency.setName("configuration");
    resolverDescriptor.addComponentDependency(standardURLConfigurationDependency);
    DefaultComponentDependency<EntityResourceActionLister> entityResourceActionListerDependency = new DefaultComponentDependency<>();
    entityResourceActionListerDependency.setRoleType(EntityResourceActionLister.class);
    entityResourceActionListerDependency.setName("entityResourceActionLister");
    resolverDescriptor.addComponentDependency(entityResourceActionListerDependency);
    try {
        this.rootComponentManager.registerComponent(resolverDescriptor);
    } catch (ComponentRepositoryException e) {
        throw new InitializationException(String.format("Failed to dynamically register Resource Reference Resolver for hint [%s]", hint), e);
    }
}
Also used : DefaultComponentDependency(org.xwiki.component.descriptor.DefaultComponentDependency) EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) BinEntityResourceReferenceResolver(org.xwiki.url.internal.standard.entity.BinEntityResourceReferenceResolver) ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) WikiEntityResourceReferenceResolver(org.xwiki.url.internal.standard.entity.WikiEntityResourceReferenceResolver) ComponentRepositoryException(org.xwiki.component.manager.ComponentRepositoryException) ExtendedURL(org.xwiki.url.ExtendedURL) InitializationException(org.xwiki.component.phase.InitializationException) EntityResourceActionLister(org.xwiki.resource.internal.entity.EntityResourceActionLister) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) EntityReference(org.xwiki.model.reference.EntityReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 2 with ComponentRepositoryException

use of org.xwiki.component.manager.ComponentRepositoryException 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

DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 ComponentRepositoryException (org.xwiki.component.manager.ComponentRepositoryException)2 InitializationException (org.xwiki.component.phase.InitializationException)2 EntityReference (org.xwiki.model.reference.EntityReference)2 Type (java.lang.reflect.Type)1 ComponentDescriptor (org.xwiki.component.descriptor.ComponentDescriptor)1 DefaultComponentDependency (org.xwiki.component.descriptor.DefaultComponentDependency)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 Initializable (org.xwiki.component.phase.Initializable)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)1 ResourceReferenceResolver (org.xwiki.resource.ResourceReferenceResolver)1 EntityResourceActionLister (org.xwiki.resource.internal.entity.EntityResourceActionLister)1 ExtendedURL (org.xwiki.url.ExtendedURL)1 BinEntityResourceReferenceResolver (org.xwiki.url.internal.standard.entity.BinEntityResourceReferenceResolver)1 WikiEntityResourceReferenceResolver (org.xwiki.url.internal.standard.entity.WikiEntityResourceReferenceResolver)1