001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.PortalSessionContext;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.Time;
022    
023    import java.util.Collection;
024    
025    import javax.servlet.http.HttpSession;
026    
027    /**
028     * @author Alexander Chow
029     */
030    public class MaintenanceUtil {
031    
032            public static void appendStatus(String status) {
033                    _instance._appendStatus(status);
034            }
035    
036            public static void cancel() {
037                    _instance._cancel();
038            }
039    
040            public static String getClassName() {
041                    return _instance._getClassName();
042            }
043    
044            public static String getSessionId() {
045                    return _instance._getSessionId();
046            }
047    
048            public static String getStatus() {
049                    return _instance._getStatus();
050            }
051    
052            public static boolean isMaintaining() {
053                    return _instance._isMaintaining();
054            }
055    
056            public static void maintain(String sessionId, String className) {
057                    _instance._maintain(sessionId, className);
058            }
059    
060            private MaintenanceUtil() {
061            }
062    
063            private void _appendStatus(String status) {
064                    if (_log.isDebugEnabled()) {
065                            _log.debug(status);
066                    }
067    
068                    _status.append(
069                            Time.getRFC822() + " " + HtmlUtil.escape(status) + "<br />");
070            }
071    
072            private void _cancel() {
073                    HttpSession session = PortalSessionContext.get(_sessionId);
074    
075                    if (session != null) {
076                            session.invalidate();
077                    }
078                    else {
079                            if (_log.isWarnEnabled()) {
080                                    _log.warn("Session " + _sessionId + " is null");
081                            }
082                    }
083    
084                    _maintaining = false;
085            }
086    
087            private String _getClassName() {
088                    return _className;
089            }
090    
091            private String _getSessionId() {
092                    return _sessionId;
093            }
094    
095            private String _getStatus() {
096                    return _status.toString();
097            }
098    
099            private boolean _isMaintaining() {
100                    return _maintaining;
101            }
102    
103            private void _maintain(String sessionId, String className) {
104                    _sessionId = sessionId;
105                    _className = className;
106                    _maintaining = true;
107                    _status = new StringBuffer();
108    
109                    _appendStatus("Executing " + _className);
110    
111                    Collection<HttpSession> sessions = PortalSessionContext.values();
112    
113                    for (HttpSession session : sessions) {
114                            if (!sessionId.equals(session.getId())) {
115                                    try {
116                                            session.invalidate();
117                                    }
118                                    catch (IllegalStateException ise) {
119                                    }
120                            }
121                    }
122            }
123    
124            private static final Log _log = LogFactoryUtil.getLog(
125                    MaintenanceUtil.class);
126    
127            private static final MaintenanceUtil _instance = new MaintenanceUtil();
128    
129            private String _className;
130            private boolean _maintaining;
131            private String _sessionId;
132            private StringBuffer _status = new StringBuffer();
133    
134    }