use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.
the class MX4JComponentAdapterMT method getInitTasks.
/**
* {@inheritDoc}
*/
protected Collection<ComponentTask<Void>> getInitTasks() {
Component component = null;
String componentKey;
boolean debug = false;
// Get the component
Object key = getComponentKey();
if (key instanceof String)
componentKey = (String) key;
else
componentKey = ((Class<?>) key).getName();
try {
ConfigurationManager manager = (ConfigurationManager) exocontainer.getComponentInstanceOfType(ConfigurationManager.class);
component = manager == null ? null : manager.getComponent(componentKey);
if (component != null) {
debug = component.getShowDeployInfo();
}
List<ComponentTask<Void>> tasks = new ArrayList<ComponentTask<Void>>();
Set<Dependency> dependencies = new HashSet<Dependency>();
final Class<T> implementationClass = getComponentImplementation();
boolean isSingleton = this.isSingleton;
boolean isInitialized = this.isInitialized;
if (debug)
LOG.debug("==> create component : " + implementationClass.getName());
boolean hasInjectableConstructor = !isSingleton || ContainerUtil.hasInjectableConstructor(implementationClass);
boolean hasOnlyEmptyPublicConstructor = !isSingleton || ContainerUtil.hasOnlyEmptyPublicConstructor(implementationClass);
if (hasInjectableConstructor || hasOnlyEmptyPublicConstructor) {
// There is at least one constructor JSR 330 compliant or we already know
// that it is not a singleton such that the new behavior is expected
List<Dependency> lDependencies = new ArrayList<Dependency>();
boolean isInjectPresent = container.initializeComponent(implementationClass, lDependencies, tasks, this);
dependencies.addAll(lDependencies);
isSingleton = manageScope(isSingleton, isInitialized, hasInjectableConstructor, isInjectPresent);
} else if (!isInitialized) {
// The adapter has not been initialized yet
// The old behavior is expected as there is no constructor JSR 330 compliant
isSingleton = this.isSingleton = true;
scope.set(Singleton.class);
}
if (component != null && component.getComponentPlugins() != null) {
addComponentPlugin(tasks, dependencies, debug, component.getComponentPlugins());
}
ExternalComponentPlugins ecplugins = manager == null ? null : manager.getConfiguration().getExternalComponentPlugins(componentKey);
if (ecplugins != null) {
addComponentPlugin(tasks, dependencies, debug, ecplugins.getComponentPlugins());
}
initDependencies.compareAndSet(null, new CopyOnWriteArraySet<Dependency>(dependencies));
tasks.add(new ComponentTask<Void>("initialize component " + getComponentImplementation().getName(), container, this, ComponentTaskType.INIT) {
public Void execute(CreationalContextComponentAdapter<?> cCtx) throws Exception {
// check if component implement the ComponentLifecycle
if (cCtx.get() instanceof ComponentLifecycle && exocontainer instanceof ExoContainer) {
ComponentLifecycle lc = (ComponentLifecycle) cCtx.get();
lc.initComponent((ExoContainer) exocontainer);
}
return null;
}
});
if (!isInitialized) {
this.isInitialized = true;
}
return tasks;
} catch (Exception e) {
String msg = "Cannot initialize component " + getComponentImplementation();
if (component != null) {
msg = "Cannot initialize component key=" + component.getKey() + " type=" + component.getType() + " found at " + component.getDocumentURL();
}
throw new RuntimeException(msg, e);
}
}
use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.
the class ExoContainer method initContainerInternal.
protected void initContainerInternal() {
ConfigurationManager manager = getComponentInstanceOfType(ConfigurationManager.class);
ContainerUtil.addContainerLifecyclePlugin(this, manager);
ContainerUtil.addComponentLifecyclePlugin(this, manager);
ContainerUtil.addComponents(this, manager);
for (ContainerLifecyclePlugin plugin : containerLifecyclePlugin_) {
try {
plugin.initContainer(this);
} catch (Exception e) {
LOG.warn("An error occurs with the ContainerLifecyclePlugin '" + getPluginName(plugin) + "'", e);
}
}
Collection<ExternalComponentPlugins> unusedPlugins = getExternalComponentPluginsUnused();
if (unusedPlugins != null) {
for (ExternalComponentPlugins plugins : unusedPlugins) {
LOG.warn("Some external plugins has for target '{}' which is unknown, please configure it or " + "define the annotation 'DefinitionByType' to enable the auto-registration on the target", plugins.getTargetComponent());
}
}
}
use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.
the class MX4JComponentAdapter method create.
/**
* {@inheritDoc}
*/
public T create(CreationalContext<T> creationalContext) {
//
T instance;
Component component = null;
ConfigurationManager manager;
String componentKey;
InitParams params = null;
boolean debug = false;
CreationalContextComponentAdapter<T> ctx = (CreationalContextComponentAdapter<T>) creationalContext;
try {
// Avoid to create duplicate instances if it is called at the same time by several threads
if (instance_ != null)
return instance_;
else if (ctx.get() != null)
return ctx.get();
// Get the component
Object key = getComponentKey();
if (key instanceof String)
componentKey = (String) key;
else
componentKey = ((Class<?>) key).getName();
manager = exocontainer.getComponentInstanceOfType(ConfigurationManager.class);
component = manager == null ? null : manager.getComponent(componentKey);
if (component != null) {
params = component.getInitParams();
debug = component.getShowDeployInfo();
}
instance = createInstance(ctx, component, manager, componentKey, params, debug);
if (instance instanceof Startable && exocontainer.canBeStopped()) {
// Start the component if the container is already started
((Startable) instance).start();
}
} catch (Exception ex) {
String msg = "Cannot instantiate component " + getComponentImplementation();
if (component != null) {
msg = "Cannot instantiate component key=" + component.getKey() + " type=" + component.getType() + " found at " + component.getDocumentURL();
}
throw new RuntimeException(msg, ex);
}
return instance;
}
use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.
the class GuiceContainer method start.
/**
* {@inheritDoc}
*/
@Override
public void start() {
ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
// We check if the component has been defined in the configuration of the current container
// The goal is to enable the GuicegContainer only if it is needed
Component component = cm.getComponent(ModuleProvider.class);
if (component == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No ModuleProvider has been defined, thus the GuiceContainer will be disabled." + " To enable the Guice Integration please define a ModuleProvider");
}
} else {
ModuleProvider provider = super.getComponentInstanceOfType(ModuleProvider.class, false);
injector = Guice.createInjector(provider.getModule(), new AbstractModule() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
Binder binder = binder();
for (ComponentAdapter<?> adapter : adapters) {
Object key = adapter.getComponentKey();
Class<?> type;
Annotation annotation = null;
Class<? extends Annotation> annotationType = null;
if (key instanceof Class<?> && !((Class<?>) key).isAnnotation()) {
type = (Class<?>) key;
} else {
if (key instanceof String) {
annotation = Names.named((String) key);
} else if (key instanceof Class<?>) {
annotationType = (Class<? extends Annotation>) key;
}
type = adapter.getComponentImplementation();
}
if (annotation == null && annotationType == null) {
binder.bind(type).toProvider(new ComponentAdapterProvider(type, adapter));
} else {
// As we don't know the type, we will bind it for each super classes and interfaces too
ComponentAdapterProvider provider = new ComponentAdapterProvider(type, adapter);
bindAll(binder, type, provider, annotation, annotationType);
}
}
}
});
LOG.info("A GuiceContainer has been enabled using the ModuleProvider " + provider.getClass());
}
super.start();
}
use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.
the class SpringContainer method start.
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() {
ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
// We check if the component has been defined in the configuration of the current container
// The goal is to enable the SpringContainer only if it is needed
Component component = cm.getComponent(ApplicationContextProvider.class);
if (component == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("No ApplicationContextProvider has been defined, thus the SpringContainer will be disabled." + " To enable the Spring Integration please define an ApplicationContextProvider");
}
} else {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
for (ComponentAdapter<?> adapter : adapters) {
Object key = adapter.getComponentKey();
String name = keyToBeanName(key);
String factoryName = name + "#factory";
RootBeanDefinition def = new RootBeanDefinition(adapter.getComponentImplementation(), AbstractBeanDefinition.AUTOWIRE_NO, false);
def.setScope(BeanDefinition.SCOPE_PROTOTYPE);
def.setFactoryBeanName(factoryName);
def.setFactoryMethodName("getInstance");
def.setLazyInit(true);
def.setTargetType(adapter.getComponentImplementation());
if (key instanceof String) {
def.addQualifier(new AutowireCandidateQualifier(Named.class, key));
} else if (key instanceof Class<?> && ((Class<?>) key).isAnnotation()) {
def.addQualifier(new AutowireCandidateQualifier((Class<?>) key));
} else {
def.setPrimary(true);
}
bf.registerBeanDefinition(name, def);
bf.registerSingleton(factoryName, new ComponentAdapterFactoryBean(adapter));
}
GenericApplicationContext parentContext = new GenericApplicationContext(bf);
parentContext.refresh();
ApplicationContextProvider provider = super.getComponentInstanceOfType(ApplicationContextProvider.class, false);
ctx = provider.getApplicationContext(parentContext);
LOG.info("A SpringContainer has been enabled using the ApplicationContextProvider " + provider.getClass());
}
super.start();
}
Aggregations