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        // Container specific ChannelFactory
069        private final ChannelFactory channelFactory;
070            
071            public GravityConfig(GraniteConfig graniteConfig, ChannelFactory channelFactory) {
072                    
073                    parseConfig(graniteConfig.getGravityConfig());
074                    
075                    this.channelFactory = channelFactory;
076            }
077            
078            private void parseConfig(XMap config) {
079                    if (config != null) {
080                            gravityFactory = config.get("@factory", String.class, DEFAULT_GRAVITY_FACTORY);
081    
082                            // Channel configuration.
083                            channelIdleTimeoutMillis = config.get("@channel-idle-timeout-millis", Long.TYPE, DEFAULT_CHANNEL_IDLE_TIMEOUT_MILLIS);
084                            longPollingTimeoutMillis = config.get("@long-polling-timeout-millis", Long.TYPE, DEFAULT_LONG_POLLING_TIMEOUT_MILLIS);
085                            retryOnError = config.get("@retry-on-error", Boolean.TYPE, DEFAULT_RETRY_ON_ERROR);
086                            maxMessagesQueuedPerChannel = config.get("@max-messages-queued-per-channel", Integer.TYPE, DEFAULT_MAX_MESSAGES_QUEUED_PER_CHANNEL);
087    
088                            // Advices sent to clients.
089                            reconnectIntervalMillis = config.get("@reconnect-interval-millis", Long.TYPE, DEFAULT_RECONNECT_INTERVAL_MILLIS);
090                            reconnectMaxAttempts = config.get("@reconnect-max-attempts", Integer.TYPE, DEFAULT_RECONNECT_MAX_ATTEMPTS);
091                            
092                            // Free configuration options.
093                            extra = config.getOne("configuration");
094                            
095                            // Thread pool configuration.
096                            corePoolSize = config.get("thread-pool/@core-pool-size", Integer.TYPE, DEFAULT_CORE_POOL_SIZE);
097                            maximumPoolSize = config.get("thread-pool/@maximum-pool-size", Integer.TYPE, DEFAULT_MAXIMUM_POOL_SIZE);
098                            keepAliveTimeMillis = config.get("thread-pool/@keep-alive-time-millis", Long.TYPE, DEFAULT_KEEP_ALIVE_TIME_MILLIS);
099                            queueCapacity = config.get("thread-pool/@queue-capacity", Integer.TYPE, DEFAULT_QUEUE_CAPACITY);
100                    }
101            }
102    
103            public void onReload(ServletContext context, GraniteConfig config) {
104                    parseConfig(config.getGravityConfig());
105                    GravityManager.reconfigure(context, this);
106            }
107    
108            public String getGravityFactory() {
109                    return gravityFactory;
110            }
111    
112            public long getChannelIdleTimeoutMillis() {
113                    return channelIdleTimeoutMillis;
114            }
115            public void setChannelIdleTimeoutMillis(long channelIdleTimeoutMillis) {
116                    this.channelIdleTimeoutMillis = channelIdleTimeoutMillis;
117            }
118    
119            public long getLongPollingTimeoutMillis() {
120                    return longPollingTimeoutMillis;
121            }
122            public void setLongPollingTimeoutMillis(long longPollingTimeoutMillis) {
123                    this.longPollingTimeoutMillis = longPollingTimeoutMillis;
124            }
125    
126            public boolean isRetryOnError() {
127                    return retryOnError;
128            }
129            public void setRetryOnError(boolean retryOnError) {
130                    this.retryOnError = retryOnError;
131            }
132    
133            public int getMaxMessagesQueuedPerChannel() {
134                    return maxMessagesQueuedPerChannel;
135            }
136            public void setMaxMessagesQueuedPerChannel(int maxMessagesQueuedPerChannel) {
137                    this.maxMessagesQueuedPerChannel = maxMessagesQueuedPerChannel;
138            }
139    
140            public long getReconnectIntervalMillis() {
141                    return reconnectIntervalMillis;
142            }
143            public void setReconnectIntervalMillis(long reconnectIntervalMillis) {
144                    this.reconnectIntervalMillis = reconnectIntervalMillis;
145            }
146    
147            public int getReconnectMaxAttempts() {
148                    return reconnectMaxAttempts;
149            }
150            public void setReconnectMaxAttempts(int reconnectMaxAttempts) {
151                    this.reconnectMaxAttempts = reconnectMaxAttempts;
152            }
153    
154            public XMap getExtra() {
155                    return (extra != null ? extra : XMap.EMPTY_XMAP);
156            }
157    
158            public int getCorePoolSize() {
159                    return corePoolSize;
160            }
161            public void setCorePoolSize(int corePoolSize) {
162                    this.corePoolSize = corePoolSize;
163            }
164    
165            public int getMaximumPoolSize() {
166                    return maximumPoolSize;
167            }
168            public void setMaximumPoolSize(int maximumPoolSize) {
169                    this.maximumPoolSize = maximumPoolSize;
170            }
171    
172            public long getKeepAliveTimeMillis() {
173                    return keepAliveTimeMillis;
174            }
175            public void setKeepAliveTimeMillis(long keepAliveTimeMillis) {
176                    this.keepAliveTimeMillis = keepAliveTimeMillis;
177            }
178    
179            public int getQueueCapacity() {
180                    return queueCapacity;
181            }
182            public void setQueueCapacity(int queueCapacity) {
183                    this.queueCapacity = queueCapacity;
184            }
185    
186            public ChannelFactory getChannelFactory() {
187                    return channelFactory;
188            }
189    }