1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.Time;
25  import com.liferay.portal.servlet.PortalSessionContext;
26  
27  import java.util.Collection;
28  
29  import javax.servlet.http.HttpSession;
30  
31  /**
32   * <a href="MaintenanceUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Alexander Chow
35   *
36   */
37  public class MaintenanceUtil {
38  
39      public static void appendStatus(String status) {
40          _instance._appendStatus(status);
41      }
42  
43      public static void cancel() {
44          _instance._cancel();
45      }
46  
47      public static String getClassName() {
48          return _instance._getClassName();
49      }
50  
51      public static String getSessionId() {
52          return _instance._getSessionId();
53      }
54  
55      public static String getStatus() {
56          return _instance._getStatus();
57      }
58  
59      public static boolean isMaintaining() {
60          return _instance._isMaintaining();
61      }
62  
63      public static void maintain(String sessionId, String className) {
64          _instance._maintain(sessionId, className);
65      }
66  
67      private MaintenanceUtil() {
68      }
69  
70      private void _appendStatus(String status) {
71          if (_log.isDebugEnabled()) {
72              _log.debug(status);
73          }
74  
75          _status.append(Time.getRFC822() + " " + status + "<br />");
76      }
77  
78      private void _cancel() {
79          HttpSession session = PortalSessionContext.get(_sessionId);
80  
81          if (session != null) {
82              session.invalidate();
83          }
84          else {
85              if (_log.isWarnEnabled()) {
86                  _log.warn("Session " + _sessionId + " is null");
87              }
88          }
89  
90          _maintaining = false;
91      }
92  
93      private String _getClassName() {
94          return _className;
95      }
96  
97      private String _getSessionId() {
98          return _sessionId;
99      }
100 
101     private String _getStatus() {
102         return _status.toString();
103     }
104 
105     private boolean _isMaintaining() {
106         return _maintaining;
107     }
108 
109     private void _maintain(String sessionId, String className) {
110         _sessionId = sessionId;
111         _className = className;
112         _maintaining = true;
113         _status = new StringBuffer();
114 
115         _appendStatus("Executing " + _className);
116 
117         Collection<HttpSession> sessions = PortalSessionContext.values();
118 
119         for (HttpSession session : sessions) {
120             if (!sessionId.equals(session.getId())) {
121                 session.invalidate();
122             }
123         }
124     }
125 
126     private static Log _log = LogFactoryUtil.getLog(MaintenanceUtil.class);
127 
128     private static MaintenanceUtil _instance = new MaintenanceUtil();
129 
130     private String _className;
131     private boolean _maintaining = false;
132     private String _sessionId;
133     private StringBuffer _status = new StringBuffer();
134 
135 }