Search in sources :

Example 6 with ConfigurationManager

use of org.exoplatform.container.configuration.ConfigurationManager 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 7 with ConfigurationManager

use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.

the class ExoContainer method initContainerInternal.

protected void initContainerInternal() {
    ConfigurationManager manager = getComponentInstanceOfType(ConfigurationManager.class);
    ContainerUtil.addContainerLifecyclePlugin(this, manager);
    ContainerUtil.addComponentLifecyclePlugin(this, manager);
    ContainerUtil.addComponents(this, manager);
    for (ContainerLifecyclePlugin plugin : containerLifecyclePlugin_) {
        try {
            plugin.initContainer(this);
        } catch (Exception e) {
            LOG.warn("An error occurs with the ContainerLifecyclePlugin '" + getPluginName(plugin) + "'", e);
        }
    }
    Collection<ExternalComponentPlugins> unusedPlugins = getExternalComponentPluginsUnused();
    if (unusedPlugins != null) {
        for (ExternalComponentPlugins plugins : unusedPlugins) {
            LOG.warn("Some external plugins has for target '{}' which is unknown, please configure it or " + "define the annotation 'DefinitionByType' to enable the auto-registration on the target", plugins.getTargetComponent());
        }
    }
}
Also used : ExternalComponentPlugins(org.exoplatform.container.xml.ExternalComponentPlugins) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) ContainerException(org.exoplatform.container.spi.ContainerException)

Example 8 with ConfigurationManager

use of org.exoplatform.container.configuration.ConfigurationManager 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 9 with ConfigurationManager

use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.

the class GuiceContainer method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() {
    ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
    // We check if the component has been defined in the configuration of the current container
    // The goal is to enable the GuicegContainer only if it is needed
    Component component = cm.getComponent(ModuleProvider.class);
    if (component == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No ModuleProvider has been defined, thus the GuiceContainer will be disabled." + " To enable the Guice Integration please define a ModuleProvider");
        }
    } else {
        ModuleProvider provider = super.getComponentInstanceOfType(ModuleProvider.class, false);
        injector = Guice.createInjector(provider.getModule(), new AbstractModule() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            protected void configure() {
                Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
                Binder binder = binder();
                for (ComponentAdapter<?> adapter : adapters) {
                    Object key = adapter.getComponentKey();
                    Class<?> type;
                    Annotation annotation = null;
                    Class<? extends Annotation> annotationType = null;
                    if (key instanceof Class<?> && !((Class<?>) key).isAnnotation()) {
                        type = (Class<?>) key;
                    } else {
                        if (key instanceof String) {
                            annotation = Names.named((String) key);
                        } else if (key instanceof Class<?>) {
                            annotationType = (Class<? extends Annotation>) key;
                        }
                        type = adapter.getComponentImplementation();
                    }
                    if (annotation == null && annotationType == null) {
                        binder.bind(type).toProvider(new ComponentAdapterProvider(type, adapter));
                    } else {
                        // As we don't know the type, we will bind it for each super classes and interfaces too
                        ComponentAdapterProvider provider = new ComponentAdapterProvider(type, adapter);
                        bindAll(binder, type, provider, annotation, annotationType);
                    }
                }
            }
        });
        LOG.info("A GuiceContainer has been enabled using the ModuleProvider " + provider.getClass());
    }
    super.start();
}
Also used : Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Binder(com.google.inject.Binder) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) AbstractComponentAdapter(org.exoplatform.container.AbstractComponentAdapter) ComponentAdapter(org.exoplatform.container.spi.ComponentAdapter)

Example 10 with ConfigurationManager

use of org.exoplatform.container.configuration.ConfigurationManager in project kernel by exoplatform.

the class SpringContainer method start.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void start() {
    ConfigurationManager cm = super.getComponentInstanceOfType(ConfigurationManager.class, false);
    // We check if the component has been defined in the configuration of the current container
    // The goal is to enable the SpringContainer only if it is needed
    Component component = cm.getComponent(ApplicationContextProvider.class);
    if (component == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No ApplicationContextProvider has been defined, thus the SpringContainer will be disabled." + " To enable the Spring Integration please define an ApplicationContextProvider");
        }
    } else {
        DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
        bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
        Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
        for (ComponentAdapter<?> adapter : adapters) {
            Object key = adapter.getComponentKey();
            String name = keyToBeanName(key);
            String factoryName = name + "#factory";
            RootBeanDefinition def = new RootBeanDefinition(adapter.getComponentImplementation(), AbstractBeanDefinition.AUTOWIRE_NO, false);
            def.setScope(BeanDefinition.SCOPE_PROTOTYPE);
            def.setFactoryBeanName(factoryName);
            def.setFactoryMethodName("getInstance");
            def.setLazyInit(true);
            def.setTargetType(adapter.getComponentImplementation());
            if (key instanceof String) {
                def.addQualifier(new AutowireCandidateQualifier(Named.class, key));
            } else if (key instanceof Class<?> && ((Class<?>) key).isAnnotation()) {
                def.addQualifier(new AutowireCandidateQualifier((Class<?>) key));
            } else {
                def.setPrimary(true);
            }
            bf.registerBeanDefinition(name, def);
            bf.registerSingleton(factoryName, new ComponentAdapterFactoryBean(adapter));
        }
        GenericApplicationContext parentContext = new GenericApplicationContext(bf);
        parentContext.refresh();
        ApplicationContextProvider provider = super.getComponentInstanceOfType(ApplicationContextProvider.class, false);
        ctx = provider.getApplicationContext(parentContext);
        LOG.info("A SpringContainer has been enabled using the ApplicationContextProvider " + provider.getClass());
    }
    super.start();
}
Also used : Named(javax.inject.Named) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) QualifierAnnotationAutowireCandidateResolver(org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) ComponentAdapter(org.exoplatform.container.spi.ComponentAdapter) AbstractComponentAdapter(org.exoplatform.container.AbstractComponentAdapter)

Aggregations

ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)10 Component (org.exoplatform.container.xml.Component)6 InitParams (org.exoplatform.container.xml.InitParams)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PrivilegedActionException (java.security.PrivilegedActionException)2 ArrayList (java.util.ArrayList)2 AbstractComponentAdapter (org.exoplatform.container.AbstractComponentAdapter)2 ComponentTask (org.exoplatform.container.ComponentTask)2 CyclicDependencyException (org.exoplatform.container.CyclicDependencyException)2 Dependency (org.exoplatform.container.Dependency)2 ComponentAdapter (org.exoplatform.container.spi.ComponentAdapter)2 ContainerException (org.exoplatform.container.spi.ContainerException)2 ExternalComponentPlugins (org.exoplatform.container.xml.ExternalComponentPlugins)2 AbstractModule (com.google.inject.AbstractModule)1 Binder (com.google.inject.Binder)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1