Search in sources :

Example 6 with Component

use of org.exoplatform.container.xml.Component in project kernel by exoplatform.

the class WeldContainer 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 WeldContainer only if it is needed
    Component component = cm.getComponent(WeldContainerHelper.class);
    if (component == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No WeldContainerHelper has been defined, thus the WeldContainer will be disabled." + " To enable the Weld Integration please define an WeldContainerHelper");
        }
    } else {
        Weld weld = new Weld();
        weld.addExtension(new WeldExtension());
        WeldContainerHelper helper = super.getComponentInstanceOfType(WeldContainerHelper.class, false);
        List<Extension> extensions = helper.getExtensions();
        if (extensions != null) {
            for (Extension e : extensions) {
                weld.addExtension(e);
            }
        }
        this.helper = helper;
        this.container = weld.initialize();
        // This is an ugly hack to make sure that the BeanManagerProxy is initialized with the right Container
        // This is needed especially when we intend to initialize several weld containers within the same instance
        container.getBeanManager().getBeans(org.jboss.weld.environment.se.WeldContainer.class);
        this.weldContainer = org.jboss.weld.Container.instance();
        this.weld = weld;
        LOG.info("A WeldContainer has been enabled using the WeldContainerHelper " + helper.getClass());
    }
    super.start();
}
Also used : Extension(javax.enterprise.inject.spi.Extension) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager) Weld(org.jboss.weld.environment.se.Weld)

Example 7 with Component

use of org.exoplatform.container.xml.Component in project kernel by exoplatform.

the class MX4JComponentAdapterMT method getCreateTask.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
protected ComponentTask<T> getCreateTask() {
    Component component = null;
    String componentKey;
    InitParams params = null;
    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) {
            params = component.getInitParams();
            debug = component.getShowDeployInfo();
        }
        if (debug)
            LOG.debug("==> get constructor of the component : " + getComponentImplementation());
        List<Dependency> lDependencies = new ArrayList<Dependency>();
        Constructor<?> constructor = container.getConstructor(getComponentImplementation(), lDependencies);
        setCreateDependencies(lDependencies);
        if (debug)
            LOG.debug("==> create component : " + getComponentImplementation());
        return (ComponentTask<T>) container.createComponentTask(constructor, params, lDependencies, this);
    } catch (Exception e) {
        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, e);
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) ArrayList(java.util.ArrayList) Dependency(org.exoplatform.container.Dependency) CyclicDependencyException(org.exoplatform.container.CyclicDependencyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ComponentTask(org.exoplatform.container.ComponentTask) Component(org.exoplatform.container.xml.Component) ConfigurationManager(org.exoplatform.container.configuration.ConfigurationManager)

Example 8 with Component

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

use of org.exoplatform.container.xml.Component in project kernel by exoplatform.

the class TestConfigurationXML method testTrimValue.

@SuppressWarnings("unchecked")
public void testTrimValue() throws Exception {
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test-trim-value.xml");
    assertNotNull(is);
    try {
        IBindingFactory bfact = BindingDirectory.getFactory(XMLObject.class);
        IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
        Configuration conf = (Configuration) uctx.unmarshalDocument(is, null);
        assertNotNull(conf);
        Iterator it = conf.getContainerLifecyclePluginIterator();
        assertNotNull(it);
        assertTrue(it.hasNext());
        ContainerLifecyclePlugin conlp = (ContainerLifecyclePlugin) it.next();
        assertEquals("container-lifecycle-plugin-type", conlp.getType());
        assertNotNull(conlp.getInitParams());
        assertEquals("container-lifecycle-plugin-value-param-value", (conlp.getInitParams().getValueParam("container-lifecycle-plugin-value-param-name")).getValue());
        it = conf.getComponentLifecyclePluginIterator();
        assertNotNull(it);
        assertTrue(it.hasNext());
        ComponentLifecyclePlugin comlp = (ComponentLifecyclePlugin) it.next();
        assertEquals("component-lifecycle-plugin", comlp.getType());
        ManageableComponents mc = comlp.getManageableComponents();
        assertNotNull(mc);
        assertEquals("manageable-components-component-type", mc.getComponentsType().get(0));
        ValuesParam valuesParam = comlp.getInitParams().getValuesParam("component-lifecycle-plugin-values-param-name");
        assertNotNull(valuesParam);
        assertNotNull(valuesParam.getValues());
        assertTrue(valuesParam.getValues().contains("component-lifecycle-plugin-values-param-value1"));
        assertTrue(valuesParam.getValues().contains("component-lifecycle-plugin-values-param-value2"));
        Component c = conf.getComponent("component-key1");
        assertNotNull(c);
        assertEquals("component-type1", c.getType());
        PropertiesParam propertiesParam = c.getInitParams().getPropertiesParam("component-key1-properties-param-name");
        assertNotNull(propertiesParam);
        assertEquals("component-key1-properties-param-prop-value", propertiesParam.getProperty("component-key1-properties-param-prop-name"));
        c = conf.getComponent("component-type2");
        assertNotNull(c);
        ObjectParameter objectParameter = c.getInitParams().getObjectParam("component-key2-object-param-name");
        assertNotNull(objectParameter);
        MyObject o = (MyObject) objectParameter.getObject();
        assertNotNull(o);
        assertEquals("string-value", o.field1);
        assertEquals(1, o.field2);
        assertEquals(1l, o.field3);
        assertEquals(1d, o.field4);
        assertEquals(true, o.field5);
        assertNotNull(o.field6);
        assertEquals("entry-value", o.field6.get("entry-name"));
        assertNotNull(o.field7);
        assertTrue(o.field7.contains("string-value"));
        assertNotNull(o.field8);
        assertEquals(1, o.field8[0]);
        List list = c.getComponentPlugins();
        assertNotNull(list);
        assertFalse(list.isEmpty());
        ComponentPlugin cp = (ComponentPlugin) list.get(0);
        assertEquals("component-plugins-name", cp.getName());
        assertEquals("set-method-name", cp.getSetMethod());
        assertEquals("component-plugins-type", cp.getType());
        assertEquals(1, cp.getPriority());
        it = conf.getExternalComponentPluginsIterator();
        assertNotNull(it);
        assertTrue(it.hasNext());
        ExternalComponentPlugins ecps = (ExternalComponentPlugins) it.next();
        assertEquals("target-component-name", ecps.getTargetComponent());
        list = ecps.getComponentPlugins();
        assertNotNull(list);
        assertFalse(list.isEmpty());
        cp = (ComponentPlugin) list.get(0);
        assertEquals("component-plugins-name", cp.getName());
        assertEquals("set-method-name", cp.getSetMethod());
        assertEquals("component-plugins-type", cp.getType());
        assertEquals(1, cp.getPriority());
        list = conf.getImports();
        assertNotNull(list);
        assertFalse(list.isEmpty());
        assertEquals("import-value", list.get(0));
        list = conf.getRemoveConfiguration();
        assertNotNull(list);
        assertFalse(list.isEmpty());
        assertEquals("remove-configuration-value", list.get(0));
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        // ignore me
        }
    }
}
Also used : ComponentPlugin(org.exoplatform.container.xml.ComponentPlugin) Configuration(org.exoplatform.container.xml.Configuration) IBindingFactory(org.jibx.runtime.IBindingFactory) InputStream(java.io.InputStream) PropertiesParam(org.exoplatform.container.xml.PropertiesParam) ContainerLifecyclePlugin(org.exoplatform.container.xml.ContainerLifecyclePlugin) IUnmarshallingContext(org.jibx.runtime.IUnmarshallingContext) ComponentLifecyclePlugin(org.exoplatform.container.xml.ComponentLifecyclePlugin) ValuesParam(org.exoplatform.container.xml.ValuesParam) ExternalComponentPlugins(org.exoplatform.container.xml.ExternalComponentPlugins) ObjectParameter(org.exoplatform.container.xml.ObjectParameter) Iterator(java.util.Iterator) List(java.util.List) ManageableComponents(org.exoplatform.container.xml.ManageableComponents) Component(org.exoplatform.container.xml.Component)

Example 10 with Component

use of org.exoplatform.container.xml.Component 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)

Aggregations

Component (org.exoplatform.container.xml.Component)16 Configuration (org.exoplatform.container.xml.Configuration)9 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)6 InitParams (org.exoplatform.container.xml.InitParams)5 ObjectParameter (org.exoplatform.container.xml.ObjectParameter)4 ValueParam (org.exoplatform.container.xml.ValueParam)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PrivilegedActionException (java.security.PrivilegedActionException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)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 DefinitionException (org.exoplatform.container.context.DefinitionException)2 ComponentAdapter (org.exoplatform.container.spi.ComponentAdapter)2 ExternalComponentPlugins (org.exoplatform.container.xml.ExternalComponentPlugins)2 XMLField (org.exoplatform.xml.object.XMLField)2 XMLObject (org.exoplatform.xml.object.XMLObject)2 IBindingFactory (org.jibx.runtime.IBindingFactory)2