1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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                 try {
116                     session.invalidate();
117                 }
118                 catch (IllegalStateException ise) {
119                 }
120             }
121         }
122     }
123 
124     private static Log _log = LogFactoryUtil.getLog(MaintenanceUtil.class);
125 
126     private static MaintenanceUtil _instance = new MaintenanceUtil();
127 
128     private String _className;
129     private boolean _maintaining = false;
130     private String _sessionId;
131     private StringBuffer _status = new StringBuffer();
132 
133 }