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.increment;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.util.PropsUtil;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class BufferedIncrementConfiguration {
028    
029            public BufferedIncrementConfiguration(String configuration) {
030                    Filter filter = new Filter(configuration);
031    
032                    _enabled = GetterUtil.getBoolean(
033                            PropsUtil.get(PropsKeys.BUFFERED_INCREMENT_ENABLED, filter));
034                    _standbyQueueThreshold = GetterUtil.getInteger(
035                            PropsUtil.get(
036                                    PropsKeys.BUFFERED_INCREMENT_STANDBY_QUEUE_THRESHOLD, filter));
037                    _standbyTimeUpperLimit = GetterUtil.getLong(
038                            PropsUtil.get(
039                                    PropsKeys.BUFFERED_INCREMENT_STANDBY_TIME_UPPER_LIMIT, filter));
040    
041                    long threadpoolKeepAliveTime = GetterUtil.getLong(
042                            PropsUtil.get(
043                                    PropsKeys.BUFFERED_INCREMENT_THREADPOOL_KEEP_ALIVE_TIME,
044                                    filter));
045    
046                    if (threadpoolKeepAliveTime < 0) {
047                            if (_log.isWarnEnabled()) {
048                                    _log.warn(
049                                            PropsKeys.BUFFERED_INCREMENT_THREADPOOL_KEEP_ALIVE_TIME +
050                                                    "[" + configuration + "]=" + threadpoolKeepAliveTime +
051                                                            ". Auto reset to 0.");
052                            }
053    
054                            threadpoolKeepAliveTime = 0;
055                    }
056    
057                    _threadpoolKeepAliveTime = threadpoolKeepAliveTime;
058    
059                    int threadpoolMaxSize = GetterUtil.getInteger(
060                            PropsUtil.get(
061                                    PropsKeys.BUFFERED_INCREMENT_THREADPOOL_MAX_SIZE, filter));
062    
063                    if (threadpoolMaxSize < 1) {
064                            if (_log.isWarnEnabled()) {
065                                    _log.warn(
066                                            PropsKeys.BUFFERED_INCREMENT_THREADPOOL_MAX_SIZE +
067                                                    "[" + configuration + "]=" + threadpoolMaxSize +
068                                                            ". Auto reset to 1.");
069                            }
070    
071                            threadpoolMaxSize = 1;
072                    }
073    
074                    _threadpoolMaxSize = threadpoolMaxSize;
075    
076                    if ((_standbyQueueThreshold > 0) && (_standbyTimeUpperLimit > 0)) {
077                            _standbyEnabled = true;
078                    }
079                    else {
080                            _standbyEnabled = false;
081                    }
082            }
083    
084            public long calculateStandbyTime(int queueLength) {
085                    if (queueLength < 0) {
086                            throw new IllegalArgumentException(
087                                    "Negative queue length " + queueLength);
088                    }
089    
090                    if (!_standbyEnabled) {
091                            throw new IllegalStateException("Standby is disabled");
092                    }
093    
094                    if (queueLength > _standbyQueueThreshold) {
095                            return 0;
096                    }
097    
098                    return (_standbyQueueThreshold - queueLength) * _standbyTimeUpperLimit *
099                            1000 / _standbyQueueThreshold;
100            }
101    
102            public int getStandbyQueueThreshold() {
103                    return _standbyQueueThreshold;
104            }
105    
106            public long getStandbyTimeUpperLimit() {
107                    return _standbyTimeUpperLimit;
108            }
109    
110            public long getThreadpoolKeepAliveTime() {
111                    return _threadpoolKeepAliveTime;
112            }
113    
114            public int getThreadpoolMaxSize() {
115                    return _threadpoolMaxSize;
116            }
117    
118            public boolean isEnabled() {
119                    return _enabled;
120            }
121    
122            public boolean isStandbyEnabled() {
123                    return _standbyEnabled;
124            }
125    
126            private static Log _log = LogFactoryUtil.getLog(
127                    BufferedIncrementConfiguration.class);
128    
129            private final boolean _enabled;
130            private final boolean _standbyEnabled;
131            private final int _standbyQueueThreshold;
132            private final long _standbyTimeUpperLimit;
133            private final long _threadpoolKeepAliveTime;
134            private final int _threadpoolMaxSize;
135    
136    }