001    /**
002     * Copyright (c) 2000-2010 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.util.Time;
020    import com.liferay.portal.servlet.PortalSessionContext;
021    
022    import java.util.Collection;
023    
024    import javax.servlet.http.HttpSession;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class MaintenanceUtil {
030    
031            public static void appendStatus(String status) {
032                    _instance._appendStatus(status);
033            }
034    
035            public static void cancel() {
036                    _instance._cancel();
037            }
038    
039            public static String getClassName() {
040                    return _instance._getClassName();
041            }
042    
043            public static String getSessionId() {
044                    return _instance._getSessionId();
045            }
046    
047            public static String getStatus() {
048                    return _instance._getStatus();
049            }
050    
051            public static boolean isMaintaining() {
052                    return _instance._isMaintaining();
053            }
054    
055            public static void maintain(String sessionId, String className) {
056                    _instance._maintain(sessionId, className);
057            }
058    
059            private MaintenanceUtil() {
060            }
061    
062            private void _appendStatus(String status) {
063                    if (_log.isDebugEnabled()) {
064                            _log.debug(status);
065                    }
066    
067                    _status.append(Time.getRFC822() + " " + status + "<br />");
068            }
069    
070            private void _cancel() {
071                    HttpSession session = PortalSessionContext.get(_sessionId);
072    
073                    if (session != null) {
074                            session.invalidate();
075                    }
076                    else {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn("Session " + _sessionId + " is null");
079                            }
080                    }
081    
082                    _maintaining = false;
083            }
084    
085            private String _getClassName() {
086                    return _className;
087            }
088    
089            private String _getSessionId() {
090                    return _sessionId;
091            }
092    
093            private String _getStatus() {
094                    return _status.toString();
095            }
096    
097            private boolean _isMaintaining() {
098                    return _maintaining;
099            }
100    
101            private void _maintain(String sessionId, String className) {
102                    _sessionId = sessionId;
103                    _className = className;
104                    _maintaining = true;
105                    _status = new StringBuffer();
106    
107                    _appendStatus("Executing " + _className);
108    
109                    Collection<HttpSession> sessions = PortalSessionContext.values();
110    
111                    for (HttpSession session : sessions) {
112                            if (!sessionId.equals(session.getId())) {
113                                    session.invalidate();
114                            }
115                    }
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(MaintenanceUtil.class);
119    
120            private static MaintenanceUtil _instance = new MaintenanceUtil();
121    
122            private String _className;
123            private boolean _maintaining = false;
124            private String _sessionId;
125            private StringBuffer _status = new StringBuffer();
126    
127    }