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.util.StringPool;
23  
24  import java.util.Date;
25  
26  /**
27   * <a href="ShutdownUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
32  public class ShutdownUtil {
33  
34      public static void cancel() {
35          _instance._cancel();
36      }
37  
38      public static long getInProcess() {
39          return _instance._getInProcess();
40      }
41  
42      public static String getMessage() {
43          return _instance._getMessage();
44      }
45  
46      public static boolean isInProcess() {
47          return _instance._isInProcess();
48      }
49  
50      public static boolean isShutdown() {
51          return _instance._isShutdown();
52      }
53  
54      public static void shutdown(long milliseconds) {
55          shutdown(milliseconds, StringPool.BLANK);
56      }
57  
58      public static void shutdown(long milliseconds, String message) {
59          _instance._shutdown(milliseconds, message);
60      }
61  
62      private ShutdownUtil() {
63      }
64  
65      private void _cancel() {
66          _date = null;
67          _message = null;
68      }
69  
70      private long _getInProcess() {
71          long milliseconds = 0;
72  
73          if (_date != null) {
74              milliseconds = _date.getTime() - System.currentTimeMillis();
75          }
76  
77          return milliseconds;
78      }
79  
80      private String _getMessage() {
81          return _message;
82      }
83  
84      private boolean _isInProcess() {
85          if (_date == null) {
86              return false;
87          }
88          else {
89              if (_date.after(new Date())) {
90                  return true;
91              }
92              else {
93                  return false;
94              }
95          }
96      }
97  
98      private boolean _isShutdown() {
99          if (_date == null) {
100             return false;
101         }
102         else {
103             if (_date.before(new Date())) {
104                 return true;
105             }
106             else {
107                 return false;
108             }
109         }
110     }
111 
112     private void _shutdown(long milliseconds, String message) {
113         _date = new Date(System.currentTimeMillis() + milliseconds);
114         _message = message;
115     }
116 
117     private static ShutdownUtil _instance = new ShutdownUtil();
118 
119     private Date _date;
120     private String _message;
121 
122 }