Search in sources :

Example 31 with ComponentLookupException

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

the class DefaultResourceReferenceResolver method resolve.

@Override
public ResourceReference resolve(ExtendedURL extendedURL, ResourceType type, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
    ResourceReferenceResolver resolver;
    // Step 1: Look for a URL-scheme-specific Resolver (a general one that is independent of the passed
    // Resource Type). This allows URL-scheme implementation to completely override handling of any
    // Resource Type if they wish.
    DefaultParameterizedType parameterizedType = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
    String hint = this.configuration.getURLFormatId();
    if (this.componentManager.hasComponent(parameterizedType, hint)) {
        try {
            resolver = this.componentManager.getInstance(parameterizedType, hint);
        } catch (ComponentLookupException e) {
            throw new CreateResourceReferenceException(String.format("Failed to create Resource Reference for [%s].", extendedURL.getWrappedURL()), e);
        }
    } else {
        // Step 2: If not found, use the Generic Resolver, which tries to find a Resolver registered for the
        // specific Resource Type.
        resolver = this.genericResourceReferenceResolver;
    }
    return resolver.resolve(extendedURL, type, parameters);
}
Also used : ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 32 with ComponentLookupException

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

the class DefaultStringResourceTypeResolver method resolve.

@Override
public ResourceType resolve(String type, Map<String, Object> parameters) throws CreateResourceTypeException {
    ResourceTypeResolver resolver;
    DefaultParameterizedType parameterizedType = new DefaultParameterizedType(null, ResourceTypeResolver.class, String.class);
    String hint = this.configuration.getURLFormatId();
    if (this.componentManager.hasComponent(parameterizedType, hint)) {
        try {
            resolver = this.componentManager.getInstance(parameterizedType, hint);
        } catch (ComponentLookupException e) {
            throw new CreateResourceTypeException(String.format("Failed to convert Resource Type from String [%s] to [%s]", type, ResourceType.class.getSimpleName()), e);
        }
    } else {
        // No specific String Resource Type Resolver for the Scheme URL, use the generic one!
        resolver = this.genericResourceTypeResolver;
    }
    return resolver.resolve(type, parameters);
}
Also used : ResourceTypeResolver(org.xwiki.resource.ResourceTypeResolver) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ResourceType(org.xwiki.resource.ResourceType) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) CreateResourceTypeException(org.xwiki.resource.CreateResourceTypeException)

Example 33 with ComponentLookupException

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

the class DefaultMandatoryDocumentInitializerManager method getMandatoryDocumentInitializer.

@Override
public MandatoryDocumentInitializer getMandatoryDocumentInitializer(DocumentReference documentReference) {
    ComponentManager componentManager = this.componentManagerProvider.get();
    String fullReference = this.serializer.serialize(documentReference);
    try {
        if (componentManager.hasComponent(MandatoryDocumentInitializer.class, fullReference)) {
            return componentManager.getInstance(MandatoryDocumentInitializer.class, fullReference);
        } else {
            String localReference = this.localSerializer.serialize(documentReference);
            if (componentManager.hasComponent(MandatoryDocumentInitializer.class, localReference)) {
                return componentManager.getInstance(MandatoryDocumentInitializer.class, localReference);
            }
        }
    } catch (ComponentLookupException e) {
        this.logger.error("Failed to initialize component [{}] for document", MandatoryDocumentInitializer.class, documentReference, e);
    }
    return null;
}
Also used : MandatoryDocumentInitializer(com.xpn.xwiki.doc.MandatoryDocumentInitializer) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 34 with ComponentLookupException

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

the class XWikiAttachment method loadAttachmentContent.

public void loadAttachmentContent(XWikiContext xcontext) throws XWikiException {
    if (this.content == null) {
        WikiReference currentWiki = xcontext.getWikiReference();
        try {
            // Make sure we work on the attachment's wiki
            WikiReference attachmentWiki = getReference().getDocumentReference().getWikiReference();
            if (attachmentWiki != null) {
                xcontext.setWikiReference(attachmentWiki);
            }
            try {
                XWikiAttachmentStoreInterface store = getAttachmentContentStore(xcontext);
                store.loadAttachmentContent(this, xcontext, true);
            } catch (ComponentLookupException e) {
                throw new XWikiException("Failed to find store for attachment [" + getReference() + "]", e);
            }
        } finally {
            if (currentWiki != null) {
                xcontext.setWikiReference(currentWiki);
            }
        }
    }
}
Also used : XWikiAttachmentStoreInterface(com.xpn.xwiki.store.XWikiAttachmentStoreInterface) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 35 with ComponentLookupException

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

the class Utils method getComponent.

/**
 * Lookup a XWiki component by role and hint.
 *
 * @param roleType the class (aka role) that the component implements
 * @param roleHint a value to differentiate different component implementations for the same role
 * @return the component's instance
 * @throws RuntimeException if the component cannot be found/initialized, or if the component manager is not
 *             initialized
 * @deprecated starting with 4.1M2 use the Component Script Service instead
 */
@Deprecated
public static <T> T getComponent(Type roleType, String roleHint) {
    T component;
    ComponentManager componentManager = getContextComponentManager();
    if (componentManager != null) {
        try {
            component = componentManager.getInstance(roleType, roleHint);
        } catch (ComponentLookupException e) {
            throw new RuntimeException("Failed to load component for type [" + roleType + "] for hint [" + roleHint + "]", e);
        }
    } else {
        throw new RuntimeException("Component manager has not been initialized before lookup for [" + roleType + "] for hint [" + roleHint + "]");
    }
    return component;
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)104 ComponentManager (org.xwiki.component.manager.ComponentManager)24 Test (org.junit.Test)15 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 InitializationException (org.xwiki.component.phase.InitializationException)8 ArrayList (java.util.ArrayList)7 XWikiException (com.xpn.xwiki.XWikiException)5 HashMap (java.util.HashMap)5 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)5 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)5 FilterException (org.xwiki.filter.FilterException)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Type (java.lang.reflect.Type)4 HashSet (java.util.HashSet)4 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 WikiReference (org.xwiki.model.reference.WikiReference)4