Search in sources :

Example 1 with CreationalContextComponentAdapter

use of org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter 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;
            }
        }
    };
}
Also used : ComponentPlugin(org.exoplatform.container.component.ComponentPlugin) ComponentTask(org.exoplatform.container.ComponentTask) Method(java.lang.reflect.Method) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CreationalContextComponentAdapter(org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)

Example 2 with CreationalContextComponentAdapter

use of org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter 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;
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) PrivilegedActionException(java.security.PrivilegedActionException) DefinitionException(org.exoplatform.container.context.DefinitionException) Startable(org.picocontainer.Startable) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) CreationalContextComponentAdapter(org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)

Example 3 with CreationalContextComponentAdapter

use of org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter 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

CreationalContextComponentAdapter (org.exoplatform.container.ConcurrentContainer.CreationalContextComponentAdapter)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ComponentTask (org.exoplatform.container.ComponentTask)2 CyclicDependencyException (org.exoplatform.container.CyclicDependencyException)2 Startable (org.picocontainer.Startable)2 Method (java.lang.reflect.Method)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ComponentTaskContext (org.exoplatform.container.ComponentTaskContext)1 ConcurrentContainerMT (org.exoplatform.container.ConcurrentContainerMT)1 ComponentPlugin (org.exoplatform.container.component.ComponentPlugin)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1 Component (org.exoplatform.container.xml.Component)1 InitParams (org.exoplatform.container.xml.InitParams)1