001/* 002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 */ 016 017package org.nuxeo.ecm.webapp.seam; 018 019import java.lang.management.ManagementFactory; 020import java.util.HashSet; 021import java.util.Iterator; 022import java.util.Set; 023 024import javax.management.JMX; 025import javax.management.MBeanServer; 026import javax.management.MXBean; 027import javax.management.MalformedObjectNameException; 028import javax.management.ObjectInstance; 029import javax.management.ObjectName; 030import javax.servlet.ServletContextEvent; 031import javax.servlet.ServletContextListener; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035 036/** 037 * Listener for Seam cache flush requests 038 * 039 * @since 5.5 040 */ 041public class NuxeoSeamWebGate implements ServletContextListener { 042 043 protected static NuxeoSeamWebGate instance; 044 045 protected static Log log = LogFactory.getLog(NuxeoSeamWebGate.class); 046 047 protected boolean initialized; 048 049 public NuxeoSeamWebGate() { 050 instance = this; 051 } 052 053 @Override 054 public void contextDestroyed(ServletContextEvent arg0) { 055 initialized = false; 056 } 057 058 @Override 059 public void contextInitialized(ServletContextEvent arg0) { 060 initialized = true; 061 } 062 063 @MXBean 064 public interface WebConnector { 065 String getStateName(); 066 } 067 068 protected final Set<WebConnector> waitingConnectors = fetchConnectors(); 069 070 protected Set<WebConnector> fetchConnectors() { 071 Set<WebConnector> connectors = new HashSet<WebConnector>(); 072 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 073 ObjectName names; 074 try { 075 names = new ObjectName("Catalina:type=Connector,port=*,address=*"); 076 } catch (MalformedObjectNameException e) { 077 log.error("Cannot query for tomcat connectors", e); 078 return connectors; 079 } 080 Set<ObjectInstance> ois = mbs.queryMBeans(names, null); 081 for (ObjectInstance oi : ois) { 082 WebConnector connector = JMX.newMBeanProxy(mbs, oi.getObjectName(), WebConnector.class); 083 connectors.add(connector); 084 } 085 return connectors; 086 } 087 088 protected synchronized boolean checkConnectorsUp() { 089 Iterator<WebConnector> it = waitingConnectors.iterator(); 090 while (it.hasNext()) { 091 WebConnector connector = it.next(); 092 if ("STARTED".equals(connector.getStateName())) { 093 it.remove(); 094 } 095 } 096 return waitingConnectors.isEmpty(); 097 } 098 099 public static boolean isInitialized() { 100 if (instance == null) { 101 return false; 102 } 103 if (instance.initialized == false) { 104 return false; 105 } 106 return instance.checkConnectorsUp(); 107 } 108 109}