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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations