Search in sources :

Example 1 with ExoContainer

use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.

the class ContextManagerListener method requestInitialized.

/**
 * {@inheritDoc}
 */
public void requestInitialized(ServletRequestEvent event) {
    final ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
    ExoContainer container = null;
    boolean hasBeenSet = false;
    try {
        container = getContainer(event);
        if (container == null)
            return;
        if (!container.equals(oldContainer)) {
            if (container instanceof PortalContainer) {
                PortalContainer.setInstance((PortalContainer) container);
            }
            ExoContainerContext.setCurrentContainer(container);
            hasBeenSet = true;
        }
        onRequestInitialized(container, event);
    } finally {
        if (hasBeenSet) {
            if (container instanceof PortalContainer) {
                // Remove the current Portal Container and the current ExoContainer
                PortalContainer.setInstance(null);
            }
            // Re-set the old container
            ExoContainerContext.setCurrentContainer(oldContainer);
        }
    }
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) PortalContainer(org.exoplatform.container.PortalContainer)

Example 2 with ExoContainer

use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.

the class RequestLifeCycleStack method begin.

void begin(ExoContainer container, boolean local) {
    // Need to make a copy as modifying the list is cached by the container
    List<ComponentRequestLifecycle> components = new ArrayList<ComponentRequestLifecycle>((List<ComponentRequestLifecycle>) container.getComponentInstancesOfType(ComponentRequestLifecycle.class));
    // 
    if (!local) {
        for (ExoContainer current = container.getParent(); current != null; current = current.getParent()) {
            components.addAll((List<ComponentRequestLifecycle>) current.getComponentInstancesOfType(ComponentRequestLifecycle.class));
        }
    }
    // Remove components that have already started their life cycle
    components.removeAll(allComponents);
    // Contribute to the all component set
    allComponents.addAll(components);
    // 
    RequestLifeCycle lifeCycle = new RequestLifeCycle(container, components);
    // 
    addLast(lifeCycle);
    // 
    lifeCycle.doBegin();
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) ArrayList(java.util.ArrayList)

Example 3 with ExoContainer

use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.

the class ThreadContextHandler method store.

/**
 * Stores into memory the current values of all the Thread Local variables
 * of all the registered {@link ThreadContextHolder} of the {@link ExoContainer}
 */
public void store() {
    final List<ThreadContextHolder> components = new ArrayList<ThreadContextHolder>((List<ThreadContextHolder>) container.getComponentInstancesOfType(ThreadContextHolder.class));
    for (ExoContainer current = container.getParent(); current != null; current = current.getParent()) {
        components.addAll((List<ThreadContextHolder>) current.getComponentInstancesOfType(ThreadContextHolder.class));
    }
    contexts = new ArrayList<ThreadContext>(components.size());
    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {

        public Void run() {
            for (int i = 0, length = components.size(); i < length; i++) {
                ThreadContextHolder holder = components.get(i);
                ThreadContext tc = holder.getThreadContext();
                if (tc == null) {
                    // This ThreadContextHolder has nothing valuable to share so we skip it
                    continue;
                }
                contexts.add(tc);
                tc.store();
            }
            return null;
        }
    });
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) ArrayList(java.util.ArrayList)

Example 4 with ExoContainer

use of org.exoplatform.container.ExoContainer in project kernel by exoplatform.

the class TestRegistration method testFoo.

public void testFoo() throws Exception {
    RootContainer root = createRootContainer("registration-configuration.xml");
    assertNotNull(root.getMBeanServer());
    Object instance = root.getComponentInstance("Foo");
    assertNotNull(instance);
    MBeanServer server = root.getMBeanServer();
    Set<ObjectInstance> set = server.queryMBeans(ObjectName.getInstance("exo:object=Foo"), null);
    assertEquals(1, set.size());
    ObjectInstance oi = set.iterator().next();
    ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
    ExoContainer currentContainer = new ExoContainer();
    ExoContainerContext.setCurrentContainer(currentContainer);
    try {
        ExoContainerFinder proxyObject = MBeanServerInvocationHandler.newProxyInstance(server, oi.getObjectName(), ExoContainerFinder.class, false);
        assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
        assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == currentContainer);
        ExoContainerContext.setCurrentContainer(oldContainer);
        assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
        assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == oldContainer);
        ExoContainerContext.setCurrentContainer(null);
        assertTrue("We expect to get the current exo container", oldContainer == proxyObject.getCurrentExoContainer());
        assertTrue("We expect to get the previous exo container", ExoContainerContext.getCurrentContainerIfPresent() == oldContainer);
    } finally {
        ExoContainerContext.setCurrentContainer(oldContainer);
    }
    // Manual
    root.registerComponentInstance("Bar", new ManagedWithObjectNameTemplate("Bar"));
    Object instance2 = root.getComponentInstance("Bar");
    assertNotNull(instance2);
    Set set2 = server.queryMBeans(ObjectName.getInstance("exo:object=Bar"), null);
    assertEquals(1, set2.size());
}
Also used : ManagedWithObjectNameTemplate(org.exoplatform.container.jmx.support.ManagedWithObjectNameTemplate) Set(java.util.Set) ExoContainer(org.exoplatform.container.ExoContainer) ExoContainerFinder(org.exoplatform.container.jmx.support.ExoContainerFinder) ObjectInstance(javax.management.ObjectInstance) RootContainer(org.exoplatform.container.RootContainer) MBeanServer(javax.management.MBeanServer)

Example 5 with ExoContainer

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

Aggregations

ExoContainer (org.exoplatform.container.ExoContainer)18 PortalContainer (org.exoplatform.container.PortalContainer)7 RootContainer (org.exoplatform.container.RootContainer)4 ArrayList (java.util.ArrayList)3 ConcurrentContainer (org.exoplatform.container.ConcurrentContainer)2 Container (org.exoplatform.container.spi.Container)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Singleton (javax.inject.Singleton)1 MBeanServer (javax.management.MBeanServer)1 ObjectInstance (javax.management.ObjectInstance)1 CachingContainer (org.exoplatform.container.CachingContainer)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 ComponentLifecycle (org.exoplatform.container.component.ComponentLifecycle)1 ComponentRequestLifecycle (org.exoplatform.container.component.ComponentRequestLifecycle)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1