Search in sources :

Example 16 with PortalContainer

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

the class ContextManagerListener method requestDestroyed.

/**
 * {@inheritDoc}
 */
public void requestDestroyed(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;
        }
        onRequestDestroyed(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 17 with PortalContainer

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

the class AbstractHttpServlet method service.

/**
 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
public final void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    final ExoContainer oldContainer = ExoContainerContext.getCurrentContainer();
    // Keep the old ClassLoader
    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    ExoContainer container = null;
    boolean hasBeenSet = false;
    try {
        container = getContainer();
        if (!container.equals(oldContainer)) {
            if (container instanceof PortalContainer) {
                PortalContainer.setInstance((PortalContainer) container);
            }
            ExoContainerContext.setCurrentContainer(container);
            hasBeenSet = true;
        }
        if (requirePortalEnvironment()) {
            final String ctxName = ContainerUtil.getServletContextName(config.getServletContext());
            if (!PortalContainer.isPortalContainerNameDisabled(ctxName) && container instanceof PortalContainer) {
                if (PortalContainer.getInstanceIfPresent() == null) {
                    // The portal container has not been set
                    PortalContainer.setInstance((PortalContainer) container);
                    hasBeenSet = true;
                }
                // Set the full classloader of the portal container
                Thread.currentThread().setContextClassLoader(((PortalContainer) container).getPortalClassLoader());
            } else {
                onPortalEnvironmentError(req, res);
                return;
            }
        }
        onService(container, req, res);
    } 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);
        }
        if (requirePortalEnvironment()) {
            // Re-set the old classloader
            Thread.currentThread().setContextClassLoader(currentClassLoader);
        }
    }
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) PortalContainer(org.exoplatform.container.PortalContainer)

Example 18 with PortalContainer

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

the class AbstractHttpSessionListener method sessionDestroyed.

/**
 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
 */
public final void sessionDestroyed(HttpSessionEvent event) {
    final ExoContainer oldContainer = ExoContainerContext.getCurrentContainerIfPresent();
    // Keep the old ClassLoader
    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    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;
        }
        if (requirePortalEnvironment()) {
            final String ctxName = ContainerUtil.getServletContextName(event.getSession().getServletContext());
            if (!PortalContainer.isPortalContainerNameDisabled(ctxName) && container instanceof PortalContainer) {
                if (PortalContainer.getInstanceIfPresent() == null) {
                    // The portal container has not been set
                    PortalContainer.setInstance((PortalContainer) container);
                    hasBeenSet = true;
                }
                // Set the full classloader of the portal container
                Thread.currentThread().setContextClassLoader(((PortalContainer) container).getPortalClassLoader());
            } else {
                if (PropertyManager.isDevelopping()) {
                    LOG.info("The portal environment could not be set for the webapp '" + ctxName + "' because this servlet context has not been defined as a " + "dependency of any portal container or it is a disabled portal" + " container, the sessionDestroyed event will be ignored");
                }
                return;
            }
        }
        onSessionDestroyed(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);
        }
        if (requirePortalEnvironment()) {
            // Re-set the old classloader
            Thread.currentThread().setContextClassLoader(currentClassLoader);
        }
    }
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) PortalContainer(org.exoplatform.container.PortalContainer)

Example 19 with PortalContainer

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

the class Deserializer method resolveVariables.

/**
 * Resolve the variables of type ${my.var} for the current context which is composed
 * of the system properties, the portal container settings and the given settings
 * @param input the input value
 * @param props a set of parameters to add for the variable resolution
 * @return the resolve value
 */
public static String resolveVariables(String input, Map<String, Object> props) {
    final int NORMAL = 0;
    final int SEEN_DOLLAR = 1;
    final int IN_BRACKET = 2;
    if (input == null)
        return input;
    char[] chars = input.toCharArray();
    StringBuilder buffer = new StringBuilder();
    boolean properties = false;
    int state = NORMAL;
    int start = 0;
    boolean precedingBackslash = false;
    for (int i = 0; i < chars.length; ++i) {
        char c = chars[i];
        if (c == '$' && state != IN_BRACKET)
            state = SEEN_DOLLAR;
        else if (c == '{' && state == SEEN_DOLLAR && !precedingBackslash) {
            buffer.append(input.substring(start, i - 1));
            state = IN_BRACKET;
            start = i - 1;
        } else if (state == SEEN_DOLLAR)
            state = NORMAL;
        else if (c == '}' && state == IN_BRACKET && !precedingBackslash) {
            if (start + 2 == i) {
                buffer.append("${}");
            } else {
                String value = null;
                String key = input.substring(start + 2, i);
                String defaultValue = null;
                int index = key.indexOf(':');
                if (index > -1) {
                    defaultValue = key.substring(index + 1);
                    if (defaultValue != null && !defaultValue.isEmpty()) {
                        defaultValue = defaultValue.replace("\\{", "{").replace("\\}", "}");
                    }
                    key = key.substring(0, index);
                }
                if (key.equals(Deserializer.EXO_CONTAINER_PROP_NAME)) {
                    // The requested key is the name of current container
                    ExoContainer container = ExoContainerContext.getCurrentContainerIfPresent();
                    if (container instanceof PortalContainer) {
                        // The current container is a portal container
                        RootContainer rootContainer = (RootContainer) ExoContainerContext.getTopContainer();
                        value = rootContainer.isPortalContainerConfigAware() ? "_" + container.getContext().getName() : "";
                    }
                } else if (key.startsWith(Deserializer.PORTAL_CONTAINER_VARIABLE_PREFIX)) {
                    // We try to get a value tied to the current portal container.
                    ExoContainer container = ExoContainerContext.getCurrentContainerIfPresent();
                    if (container instanceof PortalContainer) {
                        // The current container is a portal container
                        Object oValue = ((PortalContainer) container).getSetting(key.substring(Deserializer.PORTAL_CONTAINER_VARIABLE_PREFIX.length()));
                        value = oValue == null ? null : oValue.toString();
                    }
                } else {
                    if (props != null) {
                        // Some parameters have been given thus we need to check inside first
                        Object oValue = props.get(key);
                        value = oValue == null ? null : oValue.toString();
                    }
                    if (value == null) {
                        // No value could be found so far, thus we try to get it from the
                        // system properties
                        value = PrivilegedSystemHelper.getProperty(key);
                    }
                }
                if (value == null && defaultValue != null) {
                    value = defaultValue;
                }
                if (value != null) {
                    properties = true;
                    buffer.append(value);
                }
            }
            start = i + 1;
            state = NORMAL;
        }
        if (c == '\\') {
            precedingBackslash = !precedingBackslash;
        } else {
            precedingBackslash = false;
        }
    }
    if (properties == false)
        return input;
    if (start != chars.length)
        buffer.append(input.substring(start, chars.length));
    return buffer.toString();
}
Also used : ExoContainer(org.exoplatform.container.ExoContainer) RootContainer(org.exoplatform.container.RootContainer) PortalContainer(org.exoplatform.container.PortalContainer)

Example 20 with PortalContainer

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

the class TestSpringContainer method testIntegration.

@SuppressWarnings("unchecked")
private void testIntegration(RootContainer container) {
    assertNotNull(container);
    ComponentAdapter<A> adapterA = container.getComponentAdapterOfType(A.class);
    assertNotNull(adapterA);
    assertSame(adapterA, container.getComponentAdapterOfType(A.class));
    ComponentAdapter<B> adapterB = container.getComponentAdapterOfType(B.class);
    assertNotNull(adapterB);
    ComponentAdapter<C> adapterC = container.getComponentAdapterOfType(C.class);
    assertNotNull(adapterC);
    ComponentAdapter<D> adapterD = container.getComponentAdapterOfType(D.class);
    assertNotNull(adapterD);
    assertSame(adapterD, container.getComponentAdapterOfType(D.class));
    ComponentAdapter<E> adapterE = container.getComponentAdapterOfType(E.class);
    assertNotNull(adapterE);
    adapterE = (ComponentAdapter<E>) container.getComponentAdapter("MyClassE");
    assertNotNull(adapterE);
    assertSame(adapterE, container.getComponentAdapter("MyClassE"));
    ComponentAdapter<F> adapterF = container.getComponentAdapterOfType(F.class);
    assertNotNull(adapterF);
    ComponentAdapter<G> adapterG = container.getComponentAdapterOfType(G.class);
    assertNotNull(adapterG);
    A a = container.getComponentInstanceOfType(A.class);
    assertNotNull(a);
    assertSame(a, container.getComponentInstanceOfType(A.class));
    assertSame(a, adapterA.getComponentInstance());
    B b = container.getComponentInstanceOfType(B.class);
    assertNotNull(b);
    assertSame(b, container.getComponentInstanceOfType(B.class));
    assertSame(b, adapterB.getComponentInstance());
    C c = container.getComponentInstanceOfType(C.class);
    assertNotNull(c);
    assertNotSame(c, container.getComponentInstanceOfType(C.class));
    assertNotSame(c, adapterC.getComponentInstance());
    assertSame(a, c.a);
    assertSame(b, c.b);
    assertSame(a, ((C) adapterC.getComponentInstance()).a);
    assertSame(b, ((C) adapterC.getComponentInstance()).b);
    assertSame(a, container.getComponentInstanceOfType(C.class).a);
    assertSame(b, container.getComponentInstanceOfType(C.class).b);
    assertNotNull(c.a2);
    assertNotNull(c.a2_2);
    assertNotSame(c.a2, c.a2_2);
    assertNotNull(c.d);
    assertSame(c.d, c.d2);
    D d = container.getComponentInstanceOfType(D.class);
    assertNotNull(d);
    assertSame(d, container.getComponentInstanceOfType(D.class));
    assertSame(d, adapterD.getComponentInstance());
    assertSame(a, d.a);
    assertSame(b, d.b);
    assertTrue(d.g instanceof G1);
    assertTrue(d.g_2 instanceof G2);
    assertTrue(d.g2 instanceof G2);
    assertTrue(d.g2_1 instanceof G1);
    assertTrue(d.g3.get() instanceof G3);
    E e = (E) container.getComponentInstance("MyClassE");
    assertNotNull(a);
    assertSame(e, container.getComponentInstance("MyClassE"));
    assertSame(e, adapterE.getComponentInstance());
    F f = container.getComponentInstanceOfType(F.class);
    assertNotNull(f);
    assertSame(f, container.getComponentInstanceOfType(F.class));
    assertSame(f, adapterF.getComponentInstance());
    assertSame(e, f.e);
    assertTrue(f.e instanceof E1);
    assertTrue(f.m instanceof E1);
    assertTrue(f.e2 instanceof E2);
    assertNotNull(f.e3.get());
    assertTrue(f.e3.get() instanceof E);
    assertFalse(f.e3.get() instanceof E1);
    assertFalse(f.e3.get() instanceof E2);
    G g = container.getComponentInstanceOfType(G.class);
    assertNotNull(g);
    assertSame(g, container.getComponentInstanceOfType(G.class));
    assertSame(g, adapterG.getComponentInstance());
    List<ComponentAdapter<Marker>> adapters = container.getComponentAdaptersOfType(Marker.class);
    assertNotNull(adapters);
    assertEquals(4, adapters.size());
    boolean foundE = false, foundF = false, foundE1 = false, foundE2 = false;
    for (ComponentAdapter<Marker> adapter : adapters) {
        if (adapter.getComponentImplementation().equals(E1.class)) {
            foundE1 = true;
            assertSame(e, adapter.getComponentInstance());
        } else if (adapter.getComponentImplementation().equals(E2.class)) {
            foundE2 = true;
            assertSame(f.e2, adapter.getComponentInstance());
        } else if (adapter.getComponentImplementation().equals(E.class)) {
            foundE = true;
            assertSame(f.e3.get(), adapter.getComponentInstance());
        } else if (adapter.getComponentImplementation().equals(F.class)) {
            foundF = true;
            assertSame(f, adapter.getComponentInstance());
        }
    }
    assertTrue(foundE);
    assertTrue(foundE1);
    assertTrue(foundE2);
    assertTrue(foundF);
    List<Marker> markers = container.getComponentInstancesOfType(Marker.class);
    assertNotNull(markers);
    assertEquals(4, markers.size());
    assertTrue(markers.contains(e));
    assertTrue(markers.contains(f.e));
    assertTrue(markers.contains(f.e2));
    assertTrue(markers.contains(f));
    ComponentAdapter<H> adapterH = container.getComponentAdapterOfType(H.class);
    assertNull(adapterH);
    PortalContainer portal = PortalContainer.getInstance();
    adapterH = portal.getComponentAdapterOfType(H.class);
    assertNotNull(adapterH);
    H h = container.getComponentInstanceOfType(H.class);
    assertNull(h);
    h = portal.getComponentInstanceOfType(H.class);
    assertNotNull(h);
    assertSame(h, portal.getComponentInstanceOfType(H.class));
    assertSame(h, adapterH.getComponentInstance());
    List<ComponentAdapter<H>> adaptersH = container.getComponentAdaptersOfType(H.class);
    assertTrue(adaptersH == null || adaptersH.isEmpty());
    adaptersH = portal.getComponentAdaptersOfType(H.class);
    assertNotNull(adaptersH);
    assertEquals(1, adaptersH.size());
    assertSame(h, adaptersH.get(0).getComponentInstance());
    List<H> allH = container.getComponentInstancesOfType(H.class);
    assertTrue(allH == null || allH.isEmpty());
    allH = portal.getComponentInstancesOfType(H.class);
    assertNotNull(allH);
    assertEquals(1, allH.size());
    assertSame(h, allH.get(0));
}
Also used : ComponentAdapter(org.exoplatform.container.spi.ComponentAdapter) PortalContainer(org.exoplatform.container.PortalContainer)

Aggregations

PortalContainer (org.exoplatform.container.PortalContainer)24 RootContainer (org.exoplatform.container.RootContainer)9 ExoContainer (org.exoplatform.container.ExoContainer)7 URL (java.net.URL)4 ContainerBuilder (org.exoplatform.container.ContainerBuilder)4 ComponentAdapter (org.exoplatform.container.spi.ComponentAdapter)3 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 ComponentPlugin (org.exoplatform.container.component.ComponentPlugin)1 ConfigurationManager (org.exoplatform.container.configuration.ConfigurationManager)1 SimpleManagementAware (org.exoplatform.container.jmx.support.SimpleManagementAware)1 ManagementContextImpl (org.exoplatform.container.management.ManagementContextImpl)1 PriorityService (org.exoplatform.mocks.PriorityService)1 ExoCacheConfig (org.exoplatform.services.cache.ExoCacheConfig)1 ExoCacheFactoryImpl (org.exoplatform.services.cache.impl.infinispan.ExoCacheFactoryImpl)1 DistributedCacheManager (org.exoplatform.services.ispn.DistributedCacheManager)1 QueueTasks (org.exoplatform.services.scheduler.QueueTasks)1 Task (org.exoplatform.services.scheduler.Task)1 KeyAffinityService (org.infinispan.affinity.KeyAffinityService)1 DistributionManager (org.infinispan.distribution.DistributionManager)1