use of org.xwiki.component.phase.Initializable 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