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