1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
40  public class MaintenanceUtil {
41  
42      public static void appendStatus(String status) {
43          _instance._appendStatus(status);
44      }
45  
46      public static void cancel() {
47          _instance._cancel();
48      }
49  
50      public static String getClassName() {
51          return _instance._getClassName();
52      }
53  
54      public static String getSessionId() {
55          return _instance._getSessionId();
56      }
57  
58      public static String getStatus() {
59          return _instance._getStatus();
60      }
61  
62      public static boolean isMaintaining() {
63          return _instance._isMaintaining();
64      }
65  
66      public static void maintain(String sessionId, String className) {
67          _instance._maintain(sessionId, className);
68      }
69  
70      private MaintenanceUtil() {
71      }
72  
73      private void _appendStatus(String status) {
74          if (_log.isDebugEnabled()) {
75              _log.debug(status);
76          }
77  
78          _status.append(Time.getRFC822() + " " + status + "<br />");
79      }
80  
81      private void _cancel() {
82          HttpSession session = PortalSessionContext.get(_sessionId);
83  
84          if (session != null) {
85              session.invalidate();
86          }
87          else {
88              if (_log.isWarnEnabled()) {
89                  _log.warn("Session " + _sessionId + " is null");
90              }
91          }
92  
93          _maintaining = false;
94      }
95  
96      private String _getClassName() {
97          return _className;
98      }
99  
100     private String _getSessionId() {
101         return _sessionId;
102     }
103 
104     private String _getStatus() {
105         return _status.toString();
106     }
107 
108     private boolean _isMaintaining() {
109         return _maintaining;
110     }
111 
112     private void _maintain(String sessionId, String className) {
113         _sessionId = sessionId;
114         _className = className;
115         _maintaining = true;
116         _status = new StringBuffer();
117 
118         _appendStatus("Executing " + _className);
119 
120         Collection<HttpSession> sessions = PortalSessionContext.values();
121 
122         for (HttpSession session : sessions) {
123             if (!sessionId.equals(session.getId())) {
124                 session.invalidate();
125             }
126         }
127     }
128 
129     private static Log _log = LogFactoryUtil.getLog(MaintenanceUtil.class);
130 
131     private static MaintenanceUtil _instance = new MaintenanceUtil();
132 
133     private String _className;
134     private boolean _maintaining = false;
135     private String _sessionId;
136     private StringBuffer _status = new StringBuffer();
137 
138 }