use of org.exoplatform.container.CyclicDependencyException in project kernel by exoplatform.
the class MX4JComponentAdapterMT method create.
private T create(Callable<T> mainCreateTask) {
ComponentTaskContext ctx = container.getComponentTaskContext();
try {
loadTasks();
loadDependencies(ctx);
return mainCreateTask.call();
} catch (CyclicDependencyException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Cannot create component " + getComponentImplementation(), e);
} finally {
if (ctx == null) {
container.setComponentTaskContext(null);
}
}
}
use of org.exoplatform.container.CyclicDependencyException 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