001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.util.Date;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class ShutdownUtil {
025    
026            public static void cancel() {
027                    _instance._cancel();
028            }
029    
030            public static long getInProcess() {
031                    return _instance._getInProcess();
032            }
033    
034            public static String getMessage() {
035                    return _instance._getMessage();
036            }
037    
038            public static boolean isInProcess() {
039                    return _instance._isInProcess();
040            }
041    
042            public static boolean isShutdown() {
043                    return _instance._isShutdown();
044            }
045    
046            public static void shutdown(long milliseconds) {
047                    shutdown(milliseconds, StringPool.BLANK);
048            }
049    
050            public static void shutdown(long milliseconds, String message) {
051                    _instance._shutdown(milliseconds, message);
052            }
053    
054            private ShutdownUtil() {
055            }
056    
057            private void _cancel() {
058                    _date = null;
059                    _message = null;
060            }
061    
062            private long _getInProcess() {
063                    long milliseconds = 0;
064    
065                    if (_date != null) {
066                            milliseconds = _date.getTime() - System.currentTimeMillis();
067                    }
068    
069                    return milliseconds;
070            }
071    
072            private String _getMessage() {
073                    return _message;
074            }
075    
076            private boolean _isInProcess() {
077                    if (_date == null) {
078                            return false;
079                    }
080                    else {
081                            if (_date.after(new Date())) {
082                                    return true;
083                            }
084                            else {
085                                    return false;
086                            }
087                    }
088            }
089    
090            private boolean _isShutdown() {
091                    if (_date == null) {
092                            return false;
093                    }
094                    else {
095                            if (_date.before(new Date())) {
096                                    return true;
097                            }
098                            else {
099                                    return false;
100                            }
101                    }
102            }
103    
104            private void _shutdown(long milliseconds, String message) {
105                    _date = new Date(System.currentTimeMillis() + milliseconds);
106                    _message = message;
107            }
108    
109            private static ShutdownUtil _instance = new ShutdownUtil();
110    
111            private Date _date;
112            private String _message;
113    
114    }