Search in sources :

Example 1 with ComponentLifecycle

use of org.exoplatform.container.component.ComponentLifecycle 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);
    }
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) ArrayList(java.util.ArrayList) ConcurrentContainerMT(org.exoplatform.container.ConcurrentContainerMT) ExternalComponentPlugins(org.exoplatform.container.xml.ExternalComponentPlugins) ComponentTask(org.exoplatform.container.ComponentTask) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) HashSet(java.util.HashSet) Dependency(org.exoplatform.container.Dependency) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Singleton(javax.inject.Singleton) ComponentLifecycle(org.exoplatform.container.component.ComponentLifecycle)

Example 2 with ComponentLifecycle

use of org.exoplatform.container.component.ComponentLifecycle in project kernel by exoplatform.

the class MX4JComponentAdapter method createInstance.

private T createInstance(final CreationalContextComponentAdapter<T> ctx, final Component component, final ConfigurationManager manager, final String componentKey, final InitParams params, final boolean debug) throws Exception {
    try {
        return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<T>() {

            public T run() throws Exception {
                T instance;
                final Class<T> implementationClass = getComponentImplementation();
                // Please note that we cannot fully initialize the Object "instance_" before releasing other
                // threads because it could cause StackOverflowError due to recursive calls
                instance = exocontainer.createComponent(implementationClass, params);
                if (instance_ != null) {
                    // to component plugins
                    return instance_;
                } else if (ctx.get() != null)
                    return ctx.get();
                ctx.push(instance);
                boolean isSingleton = MX4JComponentAdapter.this.isSingleton;
                boolean isInitialized = MX4JComponentAdapter.this.isInitialized;
                if (debug)
                    LOG.debug("==> create  component : " + instance);
                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
                    boolean isInjectPresent = container.initializeComponent(instance);
                    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 = MX4JComponentAdapter.this.isSingleton = true;
                    scope.set(Singleton.class);
                }
                if (component != null && component.getComponentPlugins() != null) {
                    addComponentPlugin(debug, instance, component.getComponentPlugins(), exocontainer);
                }
                ExternalComponentPlugins ecplugins = manager == null ? null : manager.getConfiguration().getExternalComponentPlugins(componentKey);
                if (ecplugins != null) {
                    addComponentPlugin(debug, instance, ecplugins.getComponentPlugins(), exocontainer);
                }
                // check if component implement the ComponentLifecycle
                if (instance instanceof ComponentLifecycle) {
                    ComponentLifecycle lc = (ComponentLifecycle) instance;
                    lc.initComponent(exocontainer);
                }
                if (!isInitialized) {
                    if (isSingleton) {
                        instance_ = instance;
                    }
                    MX4JComponentAdapter.this.isInitialized = true;
                }
                return instance;
            }
        });
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof Exception) {
            throw (Exception) cause;
        }
        throw new Exception(cause);
    }
}
Also used : ExternalComponentPlugins(org.exoplatform.container.xml.ExternalComponentPlugins) PrivilegedActionException(java.security.PrivilegedActionException) ComponentLifecycle(org.exoplatform.container.component.ComponentLifecycle) PrivilegedActionException(java.security.PrivilegedActionException) DefinitionException(org.exoplatform.container.context.DefinitionException)

Aggregations

ComponentLifecycle (org.exoplatform.container.component.ComponentLifecycle)2 ExternalComponentPlugins (org.exoplatform.container.xml.ExternalComponentPlugins)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Singleton (javax.inject.Singleton)1 ComponentTask (org.exoplatform.container.ComponentTask)1 ConcurrentContainerMT (org.exoplatform.container.ConcurrentContainerMT)1 CyclicDependencyException (org.exoplatform.container.CyclicDependencyException)1 Dependency (org.exoplatform.container.Dependency)1 ExoContainer (org.exoplatform.container.ExoContainer)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1 Component (org.exoplatform.container.xml.Component)1