Search in sources :

Example 1 with ComponentDescriptor

use of org.xwiki.component.descriptor.ComponentDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiComponentInvocationHandler method injectComponentDependencies.

/**
 * Retrieves the wiki component dependencies from the component manager and puts them in the method context under
 * the configured key.
 *
 * @param methodContext The context where the dependencies must be injected
 */
private void injectComponentDependencies(Map<String, Object> methodContext) {
    for (Map.Entry<String, ComponentDescriptor> dependency : this.wikiComponent.getDependencies().entrySet()) {
        ComponentDescriptor cd = dependency.getValue();
        Class<?> roleTypeClass = ReflectionUtils.getTypeClass(cd.getRoleType());
        Object componentDependency = null;
        try {
            if (roleTypeClass.isAssignableFrom(List.class)) {
                // If the ParameterizedType is a List, the raw Type is the List Class and the first Type argument
                // is the actual component (which can be a ParameterizedType itself).
                // Example: java.util.List<org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>>
                // raw Type: java.util.List
                // Type arguments [0]: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
                componentDependency = componentManager.getInstanceList(((ParameterizedType) cd.getRoleType()).getActualTypeArguments()[0]);
            } else if (roleTypeClass.isAssignableFrom(Map.class)) {
                // If the ParameterizedType is a Map, the raw Type is the Map, the first argument can only be a
                // String in our implementation and the second argument is the actual component.
                // Example: java.util.Map<java.lang.String,
                // org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>>
                // raw Type: java.util.Map
                // Type arguments [0]: java.lang.String
                // [1]: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
                componentDependency = componentManager.getInstanceMap(((ParameterizedType) cd.getRoleType()).getActualTypeArguments()[1]);
            } else {
                // Not a List or a Map, note that the role Type can be a ParameterizedType itself
                // Example: org.xwiki.model.reference.EntityReferenceSerializer<java.lang.String>
                componentDependency = componentManager.getInstance(cd.getRoleType(), cd.getRoleHint());
            }
        } catch (ComponentLookupException e) {
            this.logger.warn(String.format("No component found for role [%s] with hint [%s], declared as dependency for wiki component [%s]", cd.getRoleType().toString(), cd.getRoleHint(), this.wikiComponent.getDocumentReference()));
        }
        methodContext.put(dependency.getKey(), componentDependency);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ComponentDescriptor(org.xwiki.component.descriptor.ComponentDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ComponentDescriptor

use of org.xwiki.component.descriptor.ComponentDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiComponentBridge method getDependencies.

@Override
public Map<String, ComponentDescriptor> getDependencies(DocumentReference reference) throws WikiComponentException {
    Map<String, ComponentDescriptor> dependencies = new HashMap<String, ComponentDescriptor>();
    XWikiDocument componentDocument = this.getDocument(reference);
    if (componentDocument.getObjectNumbers(DEPENDENCY_CLASS) > 0) {
        for (BaseObject dependency : componentDocument.getObjects(DEPENDENCY_CLASS)) {
            try {
                DefaultComponentDescriptor cd = new DefaultComponentDescriptor();
                cd.setRoleType(ReflectionUtils.unserializeType(dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), Thread.currentThread().getContextClassLoader()));
                cd.setRoleHint(dependency.getStringValue(COMPONENT_ROLE_HINT_FIELD));
                dependencies.put(dependency.getStringValue(DEPENDENCY_BINDING_NAME_FIELD), cd);
            } catch (Exception e) {
                this.logger.warn("Interface [{}] not found, declared as dependency for wiki component [{}]", dependency.getStringValue(COMPONENT_ROLE_TYPE_FIELD), componentDocument.getDocumentReference());
            }
        }
    }
    return dependencies;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) HashMap(java.util.HashMap) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) ComponentDescriptor(org.xwiki.component.descriptor.ComponentDescriptor) XWikiException(com.xpn.xwiki.XWikiException) WikiComponentException(org.xwiki.component.wiki.WikiComponentException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with ComponentDescriptor

use of org.xwiki.component.descriptor.ComponentDescriptor in project xwiki-platform by xwiki.

the class XWikiMigrationManagerTest method registerComponent.

private void registerComponent(Class<?> klass) throws Exception {
    ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
    List<ComponentDescriptor> descriptors = loader.getComponentsDescriptors(klass);
    for (ComponentDescriptor<?> descriptor : descriptors) {
        getComponentManager().registerComponent(descriptor);
    }
}
Also used : ComponentDescriptor(org.xwiki.component.descriptor.ComponentDescriptor) ComponentAnnotationLoader(org.xwiki.component.annotation.ComponentAnnotationLoader)

Example 4 with ComponentDescriptor

use of org.xwiki.component.descriptor.ComponentDescriptor 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

ComponentDescriptor (org.xwiki.component.descriptor.ComponentDescriptor)4 HashMap (java.util.HashMap)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1 ComponentAnnotationLoader (org.xwiki.component.annotation.ComponentAnnotationLoader)1 ComponentRepositoryException (org.xwiki.component.manager.ComponentRepositoryException)1 Initializable (org.xwiki.component.phase.Initializable)1 InitializationException (org.xwiki.component.phase.InitializationException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1