use of org.exoplatform.container.ComponentTask in project kernel by exoplatform.
the class MX4JComponentAdapterMT method getCreateTask.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
protected ComponentTask<T> getCreateTask() {
Component component = null;
String componentKey;
InitParams params = null;
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) {
params = component.getInitParams();
debug = component.getShowDeployInfo();
}
if (debug)
LOG.debug("==> get constructor of the component : " + getComponentImplementation());
List<Dependency> lDependencies = new ArrayList<Dependency>();
Constructor<?> constructor = container.getConstructor(getComponentImplementation(), lDependencies);
setCreateDependencies(lDependencies);
if (debug)
LOG.debug("==> create component : " + getComponentImplementation());
return (ComponentTask<T>) container.createComponentTask(constructor, params, lDependencies, this);
} catch (Exception e) {
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, e);
}
}
use of org.exoplatform.container.ComponentTask in project kernel by exoplatform.
the class MX4JComponentAdapterMT method createPlugin.
private ComponentTask<Void> createPlugin(final MX4JComponentAdapterMT<T> caller, final ConcurrentContainerMT exocontainer, final Class<?> pluginClass, final boolean debug, final org.exoplatform.container.xml.ComponentPlugin plugin, final Constructor<T> constructor, InitParams params, List<Dependency> lDependencies) throws Exception {
final Object[] args = exocontainer.getArguments(constructor, params, lDependencies);
return new ComponentTask<Void>("create/add plugin " + plugin.getName() + " for component " + getComponentImplementation().getName(), exocontainer, caller, ComponentTaskType.INIT) {
public Void execute(final CreationalContextComponentAdapter<?> cCtx) throws Exception {
try {
getContainer().loadArguments(args);
ComponentPlugin cplugin = (ComponentPlugin) constructor.newInstance(args);
cplugin.setName(plugin.getName());
cplugin.setDescription(plugin.getDescription());
Class<?> clazz = getComponentImplementation();
final Method m = getSetMethod(clazz, plugin.getSetMethod(), pluginClass);
if (m == null) {
LOG.error("Cannot find the method '" + plugin.getSetMethod() + "' that has only one parameter of type '" + pluginClass.getName() + "' in the class '" + clazz.getName() + "'.");
return null;
}
final Object[] params = { cplugin };
SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Void>() {
public Void run() throws Exception {
m.invoke(cCtx.get(), params);
return null;
}
});
if (debug)
LOG.debug("==> add component plugin: " + cplugin);
cplugin.setName(plugin.getName());
cplugin.setDescription(plugin.getDescription());
return null;
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
throw e;
}
}
};
}
use of org.exoplatform.container.ComponentTask 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.ComponentTask in project kernel by exoplatform.
the class MX4JComponentAdapterMT method create.
/**
* {@inheritDoc}
*/
public T create(CreationalContext<T> creationalContext) {
CreationalContextComponentAdapter<T> ctx = (CreationalContextComponentAdapter<T>) creationalContext;
// 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();
ComponentTaskContext taskCtx = container.getComponentTaskContext();
boolean isRoot = taskCtx.isRoot();
if (!isRoot) {
container.setComponentTaskContext(taskCtx = taskCtx.setLastTaskType(ComponentTaskType.CREATE));
}
try {
ComponentTask<T> task = createTask.get();
T result = task.call(ctx);
if (instance_ != null) {
// to component plugins
return instance_;
} else if (ctx.get() != null)
return ctx.get();
ctx.push(result);
} catch (CyclicDependencyException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Cannot create component " + getComponentImplementation(), e);
}
if (isRoot) {
container.setComponentTaskContext(taskCtx = taskCtx.resetDependencies(getComponentKey(), ComponentTaskType.INIT));
} else {
container.setComponentTaskContext(taskCtx = taskCtx.setLastTaskType(ComponentTaskType.INIT));
}
Collection<ComponentTask<Void>> tasks = initTasks.get();
ComponentTask<Void> task = null;
try {
if (tasks != null && !tasks.isEmpty()) {
container.loadDependencies(getComponentKey(), taskCtx, getInitDependencies(), ComponentTaskType.INIT);
for (Iterator<ComponentTask<Void>> it = tasks.iterator(); it.hasNext(); ) {
task = it.next();
task.call(ctx);
task = null;
}
}
if (instance_ != null) {
return instance_;
} else if (instance_ == null && isSingleton) {
// In case of cyclic dependency the component could be already initialized
// so we need to recheck the state
instance_ = ctx.get();
}
} catch (CyclicDependencyException e) {
throw e;
} catch (Exception e) {
if (task != null) {
throw new RuntimeException("Cannot " + task.getName() + " for the component " + getComponentImplementation(), e);
}
throw new RuntimeException("Cannot initialize component " + getComponentImplementation(), e);
}
if (ctx.get() instanceof Startable && exocontainer.canBeStopped()) {
try {
// Start the component if the container is already started
((Startable) ctx.get()).start();
} catch (Exception e) {
throw new RuntimeException("Cannot auto-start component " + getComponentImplementation(), e);
}
}
return ctx.get();
}
Aggregations