1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Time;
20  import com.liferay.portal.servlet.PortalSessionContext;
21  
22  import java.util.Collection;
23  
24  import javax.servlet.http.HttpSession;
25  
26  /**
27   * <a href="MaintenanceUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Alexander Chow
30   */
31  public class MaintenanceUtil {
32  
33      public static void appendStatus(String status) {
34          _instance._appendStatus(status);
35      }
36  
37      public static void cancel() {
38          _instance._cancel();
39      }
40  
41      public static String getClassName() {
42          return _instance._getClassName();
43      }
44  
45      public static String getSessionId() {
46          return _instance._getSessionId();
47      }
48  
49      public static String getStatus() {
50          return _instance._getStatus();
51      }
52  
53      public static boolean isMaintaining() {
54          return _instance._isMaintaining();
55      }
56  
57      public static void maintain(String sessionId, String className) {
58          _instance._maintain(sessionId, className);
59      }
60  
61      private MaintenanceUtil() {
62      }
63  
64      private void _appendStatus(String status) {
65          if (_log.isDebugEnabled()) {
66              _log.debug(status);
67          }
68  
69          _status.append(Time.getRFC822() + " " + status + "<br />");
70      }
71  
72      private void _cancel() {
73          HttpSession session = PortalSessionContext.get(_sessionId);
74  
75          if (session != null) {
76              session.invalidate();
77          }
78          else {
79              if (_log.isWarnEnabled()) {
80                  _log.warn("Session " + _sessionId + " is null");
81              }
82          }
83  
84          _maintaining = false;
85      }
86  
87      private String _getClassName() {
88          return _className;
89      }
90  
91      private String _getSessionId() {
92          return _sessionId;
93      }
94  
95      private String _getStatus() {
96          return _status.toString();
97      }
98  
99      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                 session.invalidate();
116             }
117         }
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(MaintenanceUtil.class);
121 
122     private static MaintenanceUtil _instance = new MaintenanceUtil();
123 
124     private String _className;
125     private boolean _maintaining = false;
126     private String _sessionId;
127     private StringBuffer _status = new StringBuffer();
128 
129 }