001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.gravity;
022    
023    import javax.servlet.ServletContext;
024    
025    import org.granite.config.GraniteConfig;
026    import org.granite.config.GraniteConfigReloadListener;
027    import org.granite.util.XMap;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public class GravityConfig implements GraniteConfigReloadListener {
033    
034            public static final String DEFAULT_GRAVITY_FACTORY = DefaultGravityFactory.class.getName();
035            public static final long DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS = 30 * 60000L;
036            public static final long DEFAULT_LONG_POLLING_TIMEOUT_MILLIS = 20000L;
037            public static final boolean DEFAULT_RETRY_ON_ERROR = true;
038            public static final int DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL = Integer.MAX_VALUE;
039            public static final long DEFAULT_RECONNECT_INTERVAL_MILLIS = 30000L;
040            public static final int DEFAULT_RECONNECT_MAX_ATTEMPTS = 60;
041            public static final int DEFAULT_CORE_POOL_SIZE = 5;
042            public static final int DEFAULT_MAXIMUM_POOL_SIZE = 20;
043            public static final long DEFAULT_KEEP_ALIVE_TIME_MILLIS = 10000L;
044            public static final int DEFAULT_QUEUE_CAPACITY = Integer.MAX_VALUE;
045            
046        // General Gravity configuration.
047            private String gravityFactory = DEFAULT_GRAVITY_FACTORY;
048    
049            // Channel configuration.
050            private long channelIdleTimeoutMillis = DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS;
051        private long longPollingTimeoutMillis = DEFAULT_LONG_POLLING_TIMEOUT_MILLIS;
052            private boolean retryOnError = DEFAULT_RETRY_ON_ERROR;
053            private int maxMessagesQueuedPerChannel = DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL;
054    
055        // Client advices.
056        private long reconnectIntervalMillis = DEFAULT_RECONNECT_INTERVAL_MILLIS;
057        private int reconnectMaxAttempts = DEFAULT_RECONNECT_MAX_ATTEMPTS;
058        
059        // Free configuration options.
060        private XMap extra = null;
061    
062        // Thread pool configuration.
063        private int corePoolSize = DEFAULT_CORE_POOL_SIZE;
064        private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE;
065        private long keepAliveTimeMillis = DEFAULT_KEEP_ALIVE_TIME_MILLIS;
066        private int queueCapacity = DEFAULT_QUEUE_CAPACITY;
067    
068            public GravityConfig(GraniteConfig graniteConfig) {
069                    
070                    parseConfig(graniteConfig.getGravityConfig());
071            }
072            
073            private void parseConfig(XMap config) {
074                    if (config != null) {
075                            gravityFactory = config.get("@factory", String.class, DEFAULT_GRAVITY_FACTORY);
076    
077                            // Channel configuration.
078                            channelIdleTimeoutMillis = config.get("@channel-idle-timeout-millis", Long.TYPE, DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS);
079                            longPollingTimeoutMillis = config.get("@long-polling-timeout-millis", Long.TYPE, DEFAULT_LONG_POLLING_TIMEOUT_MILLIS);
080                            retryOnError = config.get("@retry-on-error", Boolean.TYPE, DEFAULT_RETRY_ON_ERROR);
081                            maxMessagesQueuedPerChannel = config.get("@max-messages-queued-per-channel", Integer.TYPE, DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL);
082    
083                            // Advices sent to clients.
084                            reconnectIntervalMillis = config.get("@reconnect-interval-millis", Long.TYPE, DEFAULT_RECONNECT_INTERVAL_MILLIS);
085                            reconnectMaxAttempts = config.get("@reconnect-max-attempts", Integer.TYPE, DEFAULT_RECONNECT_MAX_ATTEMPTS);
086                            
087                            // Free configuration options.
088                            extra = config.getOne("configuration");
089                            
090                            // Thread pool configuration.
091                            corePoolSize = config.get("thread-pool/@core-pool-size", Integer.TYPE, DEFAULT_CORE_POOL_SIZE);
092                            maximumPoolSize = config.get("thread-pool/@maximum-pool-size", Integer.TYPE, DEFAULT_MAXIMUM_POOL_SIZE);
093                            keepAliveTimeMillis = config.get("thread-pool/@keep-alive-time-millis", Long.TYPE, DEFAULT_KEEP_ALIVE_TIME_MILLIS);
094                            queueCapacity = config.get("thread-pool/@queue-capacity", Integer.TYPE, DEFAULT_QUEUE_CAPACITY);
095                    }
096            }
097    
098            public void onReload(ServletContext context, GraniteConfig config) {
099                    parseConfig(config.getGravityConfig());
100                    GravityManager.reconfigure(context, this);
101            }
102    
103            public String getGravityFactory() {
104                    return gravityFactory;
105            }
106    
107            public long getChannelIdleTimeoutMillis() {
108                    return channelIdleTimeoutMillis;
109            }
110            public void setChannelIdleTimeoutMillis(long channelIdleTimeoutMillis) {
111                    this.channelIdleTimeoutMillis = channelIdleTimeoutMillis;
112            }
113    
114            public long getLongPollingTimeoutMillis() {
115                    return longPollingTimeoutMillis;
116            }
117            public void setLongPollingTimeoutMillis(long longPollingTimeoutMillis) {
118                    this.longPollingTimeoutMillis = longPollingTimeoutMillis;
119            }
120    
121            public boolean isRetryOnError() {
122                    return retryOnError;
123            }
124            public void setRetryOnError(boolean retryOnError) {
125                    this.retryOnError = retryOnError;
126            }
127    
128            public int getMaxMessagesQueuedPerChannel() {
129                    return maxMessagesQueuedPerChannel;
130            }
131            public void setMaxMessagesQueuedPerChannel(int maxMessagesQueuedPerChannel) {
132                    this.maxMessagesQueuedPerChannel = maxMessagesQueuedPerChannel;
133            }
134    
135            public long getReconnectIntervalMillis() {
136                    return reconnectIntervalMillis;
137            }
138            public void setReconnectIntervalMillis(long reconnectIntervalMillis) {
139                    this.reconnectIntervalMillis = reconnectIntervalMillis;
140            }
141    
142            public int getReconnectMaxAttempts() {
143                    return reconnectMaxAttempts;
144            }
145            public void setReconnectMaxAttempts(int reconnectMaxAttempts) {
146                    this.reconnectMaxAttempts = reconnectMaxAttempts;
147            }
148    
149            public XMap getExtra() {
150                    return (extra != null ? extra : XMap.EMPTY_XMAP);
151            }
152    
153            public int getCorePoolSize() {
154                    return corePoolSize;
155            }
156            public void setCorePoolSize(int corePoolSize) {
157                    this.corePoolSize = corePoolSize;
158            }
159    
160            public int getMaximumPoolSize() {
161                    return maximumPoolSize;
162            }
163            public void setMaximumPoolSize(int maximumPoolSize) {
164                    this.maximumPoolSize = maximumPoolSize;
165            }
166    
167            public long getKeepAliveTimeMillis() {
168                    return keepAliveTimeMillis;
169            }
170            public void setKeepAliveTimeMillis(long keepAliveTimeMillis) {
171                    this.keepAliveTimeMillis = keepAliveTimeMillis;
172            }
173    
174            public int getQueueCapacity() {
175                    return queueCapacity;
176            }
177            public void setQueueCapacity(int queueCapacity) {
178                    this.queueCapacity = queueCapacity;
179            }
180    }