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