001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.gravity;
023
024import javax.servlet.ServletContext;
025
026import org.granite.config.GraniteConfig;
027import org.granite.config.GraniteConfigReloadListener;
028import org.granite.util.XMap;
029
030/**
031 * @author Franck WOLFF
032 */
033public class GravityConfig implements GraniteConfigReloadListener {
034
035        public static final String DEFAULT_GRAVITY_FACTORY = DefaultGravityFactory.class.getName();
036        public static final long DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS = 30 * 60000L;
037        public static final long DEFAULT_LONG_POLLING_TIMEOUT_MILLIS = 20000L;
038        public static final boolean DEFAULT_RETRY_ON_ERROR = true;
039        public static final int DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL = Integer.MAX_VALUE;
040        public static final long DEFAULT_RECONNECT_INTERVAL_MILLIS = 30000L;
041        public static final int DEFAULT_RECONNECT_MAX_ATTEMPTS = 60;
042        public static final boolean DEFAULT_ENCODE_MESSAGE_BODY = false;
043        public static final int DEFAULT_CORE_POOL_SIZE = 5;
044        public static final int DEFAULT_MAXIMUM_POOL_SIZE = 20;
045        public static final long DEFAULT_KEEP_ALIVE_TIME_MILLIS = 10000L;
046        public static final int DEFAULT_QUEUE_CAPACITY = Integer.MAX_VALUE;
047        
048        public static final int DEFAULT_UDP_SEND_BUFFER_SIZE = 64 * 1024; // 64K.
049        
050    // General Gravity configuration.
051        private String gravityFactory = DEFAULT_GRAVITY_FACTORY;
052
053        // Channel configuration.
054        private long channelIdleTimeoutMillis = DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS;
055    private long longPollingTimeoutMillis = DEFAULT_LONG_POLLING_TIMEOUT_MILLIS;
056        private boolean retryOnError = DEFAULT_RETRY_ON_ERROR;
057        private int maxMessagesQueuedPerChannel = DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL;
058
059    // Client advices.
060    private long reconnectIntervalMillis = DEFAULT_RECONNECT_INTERVAL_MILLIS;
061    private int reconnectMaxAttempts = DEFAULT_RECONNECT_MAX_ATTEMPTS;
062    private boolean encodeMessageBody = DEFAULT_ENCODE_MESSAGE_BODY;
063    
064    // Free configuration options.
065    private XMap extra = null;
066
067    // Thread pool configuration.
068    private int corePoolSize = DEFAULT_CORE_POOL_SIZE;
069    private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
070    private long keepAliveTimeMillis = DEFAULT_KEEP_ALIVE_TIME_MILLIS;
071    private int queueCapacity = DEFAULT_QUEUE_CAPACITY;
072
073    // UDP configuration.
074    private boolean useUdp = false;
075    private int udpPort = 0;
076    private boolean udpNio = true;
077    private boolean udpConnected = true;
078    private int udpSendBufferSize = DEFAULT_UDP_SEND_BUFFER_SIZE;
079
080        public GravityConfig(GraniteConfig graniteConfig) {
081                parseConfig(graniteConfig.getGravityConfig());
082        }
083        
084        private void parseConfig(XMap config) {
085                if (config != null) {
086                        gravityFactory = config.get("@factory", String.class, DEFAULT_GRAVITY_FACTORY);
087
088                        // Channel configuration.
089                        channelIdleTimeoutMillis = config.get("@channel-idle-timeout-millis", Long.TYPE, DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS);
090                        longPollingTimeoutMillis = config.get("@long-polling-timeout-millis", Long.TYPE, DEFAULT_LONG_POLLING_TIMEOUT_MILLIS);
091                        retryOnError = config.get("@retry-on-error", Boolean.TYPE, DEFAULT_RETRY_ON_ERROR);
092                        maxMessagesQueuedPerChannel = config.get("@max-messages-queued-per-channel", Integer.TYPE, DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL);
093
094                        // Advices sent to clients.
095                        reconnectIntervalMillis = config.get("@reconnect-interval-millis", Long.TYPE, DEFAULT_RECONNECT_INTERVAL_MILLIS);
096                        reconnectMaxAttempts = config.get("@reconnect-max-attempts", Integer.TYPE, DEFAULT_RECONNECT_MAX_ATTEMPTS);
097                        encodeMessageBody = config.get("@encode-message-body", Boolean.TYPE, DEFAULT_ENCODE_MESSAGE_BODY);
098                        
099                        // Free configuration options.
100                        extra = config.getOne("configuration");
101                        
102                        // Thread pool configuration.
103                        corePoolSize = config.get("thread-pool/@core-pool-size", Integer.TYPE, DEFAULT_CORE_POOL_SIZE);
104                        maximumPoolSize = config.get("thread-pool/@maximum-pool-size", Integer.TYPE, DEFAULT_MAXIMUM_POOL_SIZE);
105                        keepAliveTimeMillis = config.get("thread-pool/@keep-alive-time-millis", Long.TYPE, DEFAULT_KEEP_ALIVE_TIME_MILLIS);
106                        queueCapacity = config.get("thread-pool/@queue-capacity", Integer.TYPE, DEFAULT_QUEUE_CAPACITY);
107                        
108                        // UDP configuration.
109                        useUdp = config.containsKey("udp");
110                        if (useUdp) {
111                                udpPort = config.get("udp/@port", Integer.TYPE, 0);
112                                udpNio = config.get("udp/@nio", Boolean.TYPE, true);
113                                udpConnected = config.get("udp/@connected", Boolean.TYPE, true);
114                                udpSendBufferSize = config.get("udp/@send-buffer-size", Integer.TYPE, DEFAULT_UDP_SEND_BUFFER_SIZE);
115                        }
116                }
117        }
118
119        public void onReload(ServletContext context, GraniteConfig config) {
120                parseConfig(config.getGravityConfig());
121                GravityManager.reconfigure(context, this);
122        }
123
124        public String getGravityFactory() {
125                return gravityFactory;
126        }
127
128        public long getChannelIdleTimeoutMillis() {
129                return channelIdleTimeoutMillis;
130        }
131        public void setChannelIdleTimeoutMillis(long channelIdleTimeoutMillis) {
132                this.channelIdleTimeoutMillis = channelIdleTimeoutMillis;
133        }
134
135        public long getLongPollingTimeoutMillis() {
136                return longPollingTimeoutMillis;
137        }
138        public void setLongPollingTimeoutMillis(long longPollingTimeoutMillis) {
139                this.longPollingTimeoutMillis = longPollingTimeoutMillis;
140        }
141
142        public boolean isRetryOnError() {
143                return retryOnError;
144        }
145        public void setRetryOnError(boolean retryOnError) {
146                this.retryOnError = retryOnError;
147        }
148
149        public int getMaxMessagesQueuedPerChannel() {
150                return maxMessagesQueuedPerChannel;
151        }
152        public void setMaxMessagesQueuedPerChannel(int maxMessagesQueuedPerChannel) {
153                this.maxMessagesQueuedPerChannel = maxMessagesQueuedPerChannel;
154        }
155
156        public long getReconnectIntervalMillis() {
157                return reconnectIntervalMillis;
158        }
159        public void setReconnectIntervalMillis(long reconnectIntervalMillis) {
160                this.reconnectIntervalMillis = reconnectIntervalMillis;
161        }
162
163        public int getReconnectMaxAttempts() {
164                return reconnectMaxAttempts;
165        }
166        public void setReconnectMaxAttempts(int reconnectMaxAttempts) {
167                this.reconnectMaxAttempts = reconnectMaxAttempts;
168        }
169
170        public boolean isEncodeMessageBody() {
171                return encodeMessageBody;
172        }
173
174        public void setEncodeMessageBody(boolean encodeMessageBody) {
175                this.encodeMessageBody = encodeMessageBody;
176        }
177
178        public XMap getExtra() {
179                return (extra != null ? extra : XMap.EMPTY_XMAP);
180        }
181
182        public int getCorePoolSize() {
183                return corePoolSize;
184        }
185        public void setCorePoolSize(int corePoolSize) {
186                this.corePoolSize = corePoolSize;
187        }
188
189        public int getMaximumPoolSize() {
190                return maximumPoolSize;
191        }
192        public void setMaximumPoolSize(int maximumPoolSize) {
193                this.maximumPoolSize = maximumPoolSize;
194        }
195
196        public long getKeepAliveTimeMillis() {
197                return keepAliveTimeMillis;
198        }
199        public void setKeepAliveTimeMillis(long keepAliveTimeMillis) {
200                this.keepAliveTimeMillis = keepAliveTimeMillis;
201        }
202
203        public int getQueueCapacity() {
204                return queueCapacity;
205        }
206        public void setQueueCapacity(int queueCapacity) {
207                this.queueCapacity = queueCapacity;
208        }
209
210        public boolean isUseUdp() {
211                return useUdp;
212        }
213        public void setUseUdp(boolean useUdp) {
214                this.useUdp = useUdp;
215        }
216
217        public int getUdpPort() {
218                return udpPort;
219        }
220        public void setUdpPort(int udpPort) {
221                this.udpPort = udpPort;
222        }
223
224        public boolean isUdpNio() {
225                return udpNio;
226        }
227        public void setUdpNio(boolean udpNio) {
228                this.udpNio = udpNio;
229        }
230
231        public boolean isUdpConnected() {
232                return udpConnected;
233        }
234        public void setUdpConnected(boolean udpConnected) {
235                this.udpConnected = udpConnected;
236        }
237
238        public int getUdpSendBufferSize() {
239                return udpSendBufferSize;
240        }
241        public void setUdpSendBufferSize(int udpBufferSize) {
242                this.udpSendBufferSize = udpBufferSize;
243        }
244}