001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 */
018
019package org.nuxeo.ecm.webapp.seam;
020
021import java.lang.management.ManagementFactory;
022import java.util.HashSet;
023import java.util.Iterator;
024import java.util.Set;
025
026import javax.management.JMX;
027import javax.management.MBeanServer;
028import javax.management.MXBean;
029import javax.management.MalformedObjectNameException;
030import javax.management.ObjectInstance;
031import javax.management.ObjectName;
032import javax.servlet.ServletContextEvent;
033import javax.servlet.ServletContextListener;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037
038/**
039 * Listener for Seam cache flush requests
040 *
041 * @since 5.5
042 */
043public class NuxeoSeamWebGate implements ServletContextListener {
044
045    protected static NuxeoSeamWebGate instance;
046
047    protected static Log log = LogFactory.getLog(NuxeoSeamWebGate.class);
048
049    protected boolean initialized;
050
051    public NuxeoSeamWebGate() {
052        instance = this;
053    }
054
055    @Override
056    public void contextDestroyed(ServletContextEvent arg0) {
057        initialized = false;
058    }
059
060    @Override
061    public void contextInitialized(ServletContextEvent arg0) {
062        initialized = true;
063    }
064
065    @MXBean
066    public interface WebConnector {
067        String getStateName();
068    }
069
070    protected final Set<WebConnector> waitingConnectors = fetchConnectors();
071
072    protected Set<WebConnector> fetchConnectors() {
073        Set<WebConnector> connectors = new HashSet<WebConnector>();
074        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
075        ObjectName names;
076        try {
077            names = new ObjectName("Catalina:type=Connector,port=*,address=*");
078        } catch (MalformedObjectNameException e) {
079            log.error("Cannot query for tomcat connectors", e);
080            return connectors;
081        }
082        Set<ObjectInstance> ois = mbs.queryMBeans(names, null);
083        for (ObjectInstance oi : ois) {
084            WebConnector connector = JMX.newMBeanProxy(mbs, oi.getObjectName(), WebConnector.class);
085            connectors.add(connector);
086        }
087        return connectors;
088    }
089
090    protected synchronized boolean checkConnectorsUp() {
091        Iterator<WebConnector> it = waitingConnectors.iterator();
092        while (it.hasNext()) {
093            WebConnector connector = it.next();
094            if ("STARTED".equals(connector.getStateName())) {
095                it.remove();
096            }
097        }
098        return waitingConnectors.isEmpty();
099    }
100
101    public static boolean isInitialized() {
102        if (instance == null) {
103            return false;
104        }
105        if (instance.initialized == false) {
106            return false;
107        }
108        return instance.checkConnectorsUp();
109    }
110
111}