001/*
002 * (C) Copyright 2011 Nuxeo SA (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 *     matic
016 */
017package org.nuxeo.ecm.webapp.seam;
018
019import java.io.IOException;
020import java.lang.management.ManagementFactory;
021import java.net.HttpURLConnection;
022import java.net.URL;
023import java.util.StringTokenizer;
024
025import javax.management.JMX;
026import javax.management.MBeanServer;
027import javax.management.MXBean;
028import javax.management.MalformedObjectNameException;
029import javax.management.ObjectInstance;
030import javax.management.ObjectName;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.reload.ReloadEventNames;
036import org.nuxeo.runtime.services.event.Event;
037import org.nuxeo.runtime.services.event.EventListener;
038
039/**
040 * @author matic
041 * @since 5.5
042 */
043// TODO: protect methods by checking if seam hot reload is enabled (?)
044public class NuxeoSeamFlusher implements EventListener {
045
046    protected Log log = LogFactory.getLog(NuxeoSeamFlusher.class);
047
048    @Override
049    public boolean aboutToHandleEvent(Event event) {
050        return true;
051    }
052
053    @Override
054    public void handleEvent(Event event) {
055        if (NuxeoSeamWebGate.isInitialized() == false) {
056            return;
057        }
058        String id = event.getId();
059        if (ReloadEventNames.FLUSH_SEAM_EVENT_ID.equals(id)) {
060            SeamHotReloadHelper.flush();
061            try {
062                invalidateWebSessions();
063            } catch (IOException e) {
064                log.error("Cannot invalidate seam web sessions", e);
065            }
066        } else if (ReloadEventNames.RELOAD_SEAM_EVENT_ID.equals(id)) {
067            try {
068                if (postSeamReload() == false) {
069                    log.error("Cannot post hot-reload seam components on loopback url");
070                }
071            } catch (IOException e) {
072                log.error("Cannot hot-reload seam components", e);
073            }
074        }
075    }
076
077    protected boolean postSeamReload() throws IOException {
078        String loopbackURL = Framework.getProperty("nuxeo.loopback.url");
079        URL location = new URL(loopbackURL + "/restAPI/seamReload");
080        HttpURLConnection uc = (HttpURLConnection) location.openConnection();
081        uc.setRequestMethod("POST");
082        return uc.getResponseCode() == HttpURLConnection.HTTP_OK;
083    }
084
085    @MXBean
086    public interface WebSessionFlusher {
087
088        String listSessionIds();
089
090        void expireSession(String id);
091
092    }
093
094    protected void invalidateWebSessions() throws IOException {
095        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
096        ObjectName name;
097        try {
098            name = new ObjectName("Catalina:type=Manager,context=/nuxeo,host=*");
099        } catch (MalformedObjectNameException e) {
100            throw new IOException(e);
101        }
102        for (ObjectInstance oi : mbs.queryMBeans(name, null)) {
103            WebSessionFlusher flusher = JMX.newMBeanProxy(mbs, oi.getObjectName(), WebSessionFlusher.class);
104            StringTokenizer tokenizer = new StringTokenizer(flusher.listSessionIds(), " ");
105            while (tokenizer.hasMoreTokens()) {
106                String id = tokenizer.nextToken();
107                flusher.expireSession(id);
108            }
109        }
110    }
111
112}