Search in sources :

Example 1 with CyclicDependencyException

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);
        }
    }
}
Also used : ComponentTaskContext(org.exoplatform.container.ComponentTaskContext) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException)

Example 2 with CyclicDependencyException

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();
}
Also used : ComponentTaskContext(org.exoplatform.container.ComponentTaskContext) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Startable(org.picocontainer.Startable) ConcurrentContainerMT(org.exoplatform.container.ConcurrentContainerMT) ComponentTask(org.exoplatform.container.ComponentTask) CreationalContextComponentAdapter(org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ComponentTaskContext (org.exoplatform.container.ComponentTaskContext)2 CyclicDependencyException (org.exoplatform.container.CyclicDependencyException)2 ComponentTask (org.exoplatform.container.ComponentTask)1 CreationalContextComponentAdapter (org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)1 ConcurrentContainerMT (org.exoplatform.container.ConcurrentContainerMT)1 Startable (org.picocontainer.Startable)1