001/*
002 * (C) Copyright 2006, JBoss Inc., and individual contributors as indicated
003 * by the @authors tag. See the copyright.txt in the distribution for a
004 * full listing of individual contributors.
005 *
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the GNU Lesser General Public License
008 * (LGPL) version 2.1 which accompanies this distribution, and is available at
009 * http://www.gnu.org/licenses/lgpl.html
010 *
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * Contributors:
017 *     JBoss and Seam dev team
018 *     Thierry Delprat
019 */
020package org.nuxeo.ecm.webapp.seam;
021
022import java.lang.reflect.Field;
023import java.util.Set;
024
025import javax.management.JMException;
026import javax.management.MBeanServer;
027import javax.management.ObjectName;
028import javax.servlet.ServletContext;
029import javax.servlet.http.HttpServletRequest;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.Seam;
034import org.jboss.seam.core.Init;
035import org.jboss.seam.init.Initialization;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.launcher.config.ConfigurationGenerator;
038import org.nuxeo.runtime.api.Framework;
039import org.nuxeo.runtime.management.ServerLocator;
040
041/**
042 * Helper class to manage Seam Hot Reload Most of the code comes from Jboss Seam 2.0.3-RC1 Debug package
043 * (HotDeployFilter)
044 */
045public class SeamHotReloadHelper {
046
047    private static final Log log = LogFactory.getLog(SeamHotReloadHelper.class);
048
049    @Deprecated
050    public static final String SEAM_HOT_RELOAD_SYSTEM_PROP = ConfigurationGenerator.SEAM_DEBUG_SYSTEM_PROP;
051
052    public static boolean isHotReloadEnabled() {
053        return Framework.isBooleanPropertyTrue(ConfigurationGenerator.SEAM_DEBUG_SYSTEM_PROP);
054    }
055
056    public static void flush() {
057        Seam.clearComponentNameCache();
058        try {
059            Field f = Seam.class.getDeclaredField("CLASSLOADERS_LOADED");
060            f.setAccessible(true);
061            ((Set<?>) f.get(null)).clear();
062        } catch (ReflectiveOperationException e) {
063            log.warn("Can't flush seam class loader cache", e);
064        }
065        try {
066            // TODO: check if this would be needed for Studio hot reload (?)
067            flushWebResources();
068        } catch (ReflectiveOperationException | JMException e) {
069            log.error("Cannot flush web resources, did you start with the sdk profile active ?", e);
070        }
071    }
072
073    public static Set<String> reloadSeamComponents(HttpServletRequest httpRequest) {
074
075        ServletContext servletContext = httpRequest.getSession().getServletContext();
076
077        Init init = (Init) servletContext.getAttribute(Seam.getComponentName(Init.class));
078        if (init.hasHotDeployableComponents()) {
079            try {
080                new Initialization(servletContext).redeploy(httpRequest);
081            } catch (InterruptedException e) {
082                Thread.currentThread().interrupt();
083                throw new NuxeoException(e);
084            }
085        }
086
087        // re-initialized by re-deployment, why removing ?
088        // servletContext.removeAttribute(Seam.getComponentName(Pages.class));
089        return init.getHotDeployableComponents();
090    }
091
092    public static Set<String> getHotDeployableComponents(HttpServletRequest httpRequest) {
093        ServletContext servletContext = httpRequest.getSession().getServletContext();
094        Init init = (Init) servletContext.getAttribute(Seam.getComponentName(Init.class));
095        return init.getHotDeployableComponents();
096
097    }
098
099    /**
100     * Flushes resources for tomcat cache
101     *
102     * @since 5.5
103     */
104    protected static void flushWebResources() throws ReflectiveOperationException, JMException {
105        ObjectName on = new ObjectName("org.nuxeo:type=sdk,name=web-resources");
106        MBeanServer mbs = Framework.getService(ServerLocator.class).lookupServer();
107        if (mbs.isRegistered(on)) {
108            // only in tomcat container
109            mbs.invoke(on, "flushWebResources", new Object[0], new String[0]);
110        }
111    }
112
113}