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