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.InstanceNotFoundException; 026import javax.management.JMException; 027import javax.management.MBeanException; 028import javax.management.MBeanServer; 029import javax.management.MalformedObjectNameException; 030import javax.management.ObjectName; 031import javax.management.ReflectionException; 032import javax.servlet.ServletContext; 033import javax.servlet.http.HttpServletRequest; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.jboss.seam.Seam; 038import org.jboss.seam.core.Init; 039import org.jboss.seam.init.Initialization; 040import org.nuxeo.launcher.config.ConfigurationGenerator; 041import org.nuxeo.runtime.api.Framework; 042import org.nuxeo.runtime.management.ServerLocator; 043 044/** 045 * Helper class to manage Seam Hot Reload Most of the code comes from Jboss Seam 2.0.3-RC1 Debug package 046 * (HotDeployFilter) 047 */ 048public class SeamHotReloadHelper { 049 050 private static final Log log = LogFactory.getLog(SeamHotReloadHelper.class); 051 052 @Deprecated 053 public static final String SEAM_HOT_RELOAD_SYSTEM_PROP = ConfigurationGenerator.SEAM_DEBUG_SYSTEM_PROP; 054 055 public static boolean isHotReloadEnabled() { 056 String sysProp = System.getProperty(ConfigurationGenerator.SEAM_DEBUG_SYSTEM_PROP, "false"); 057 return Boolean.TRUE.equals(Boolean.valueOf(sysProp)); 058 } 059 060 public static void flush() { 061 Seam.clearComponentNameCache(); 062 try { 063 Field f = Seam.class.getDeclaredField("CLASSLOADERS_LOADED"); 064 f.setAccessible(true); 065 ((Set<?>) f.get(null)).clear(); 066 } catch (ReflectiveOperationException e) { 067 log.warn("Can't flush seam class loader cache", e); 068 } 069 try { 070 // TODO: check if this would be needed for Studio hot reload (?) 071 flushWebResources(); 072 } catch (ReflectiveOperationException | JMException e) { 073 log.error("Cannot flush web resources, did you start with the sdk profile active ?", e); 074 } 075 } 076 077 public static Set<String> reloadSeamComponents(HttpServletRequest httpRequest) { 078 079 ServletContext servletContext = httpRequest.getSession().getServletContext(); 080 081 Init init = (Init) servletContext.getAttribute(Seam.getComponentName(Init.class)); 082 if (init != null && init.hasHotDeployableComponents()) { 083 try { 084 new Initialization(servletContext).redeploy(httpRequest); 085 } catch (InterruptedException e) { 086 log.error("Error during hot redeploy", e); 087 } 088 } 089 090 // re-initialized by re-deployment, why removing ? 091 // servletContext.removeAttribute(Seam.getComponentName(Pages.class)); 092 return init.getHotDeployableComponents(); 093 } 094 095 public static Set<String> getHotDeployableComponents(HttpServletRequest httpRequest) { 096 ServletContext servletContext = httpRequest.getSession().getServletContext(); 097 Init init = (Init) servletContext.getAttribute(Seam.getComponentName(Init.class)); 098 return init.getHotDeployableComponents(); 099 100 } 101 102 /** 103 * Flushes resources for tomcat cache 104 * 105 * @since 5.5 106 */ 107 protected static void flushWebResources() throws ReflectiveOperationException, JMException { 108 ObjectName on = new ObjectName("org.nuxeo:type=sdk,name=web-resources"); 109 MBeanServer mbs = Framework.getLocalService(ServerLocator.class).lookupServer(); 110 if (mbs.isRegistered(on)) { 111 // only in tomcat container 112 mbs.invoke(on, "flushWebResources", new Object[0], new String[0]); 113 } 114 } 115 116}