View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  
13  /*
14   * jcaptcha, the open source java framework for captcha definition and integration
15   * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
16   * See the LICENSE.txt file distributed with this package.
17   */
18  
19  package com.octo.captcha.engine.bufferedengine;
20  
21  import com.octo.captcha.engine.CaptchaEngineException;
22  import org.apache.commons.collections.map.HashedMap;
23  
24  import java.util.Locale;
25  import java.util.Map;
26  
27  /***
28   * Class that contains informations to configure the BufferedEngineContainer.
29   *
30   * @author Benoit Doumas
31   */
32  public class ContainerConfiguration {
33      private Integer feedSize;
34  
35      private Integer swapSize;
36  
37      private Integer maxVolatileMemorySize;
38  
39      private Integer maxPersistentMemorySize;
40  
41      private HashedMap localeRatio = new HashedMap();
42  
43      private boolean serveOnlyConfiguredLocales = false;
44  
45      private Locale defaultLocale = Locale.getDefault();
46  
47      private Integer feedBatchSize;
48  
49      /***
50       * Contructs a ContainerConfiguration with custom feed size and sawp size
51       *
52       * @param localeRatio             locales and their ratio that should be feeded
53       * @param maxVolatileMemorySize   in memory captcha buffer max size
54       * @param maxPersistentMemorySize persistent captcha buffer max size
55       * @param swapSize                number of captchas that will be swapped from persistent to memory buffer each
56       *                                time
57       * @param feedSize                number of captchas that will be generated and stored to persistent buffer each
58       *                                time
59       */
60      public ContainerConfiguration(Map localeRatio, int maxVolatileMemorySize, int maxPersistentMemorySize,
61                                    int swapSize, int feedSize) {
62          this.localeRatio.putAll(localeRatio);
63          this.maxVolatileMemorySize = new Integer(maxVolatileMemorySize);
64          this.maxPersistentMemorySize = new Integer(maxPersistentMemorySize);
65          this.feedSize = new Integer(feedSize);
66          this.swapSize = new Integer(swapSize);
67          this.feedBatchSize = new Integer(feedSize);
68      }
69  
70  
71      /***
72       * Contructs a ContainerConfiguration with custom feed size and sawp size
73       *
74       * @param localeRatio                locales and their ratio that should be feeded
75       * @param maxVolatileMemorySize      in memory captcha buffer max size
76       * @param maxPersistentMemorySize    persistent captcha buffer max size
77       * @param swapSize                   number of captchas that will be swapped from persistent to memory buffer each
78       *                                   time
79       * @param feedSize                   number of captchas that will be generated and stored to persistent buffer each
80       *                                   time
81       * @param serveOnlyConfiguredLocales if set to true, serve only locales that are in configuration (only by
82       *                                   language)
83       * @param defaultLocale              the default locale used by this engineContainer.
84       */
85      public ContainerConfiguration(Map localeRatio, int maxVolatileMemorySize, int maxPersistentMemorySize,
86                                    int swapSize, int feedSize, boolean serveOnlyConfiguredLocales, Locale defaultLocale) {
87          this(localeRatio, maxVolatileMemorySize, maxPersistentMemorySize, swapSize, feedSize);
88          this.serveOnlyConfiguredLocales = serveOnlyConfiguredLocales;
89          this.defaultLocale = defaultLocale != null ? defaultLocale : this.defaultLocale;
90          validateDefaultLocale(serveOnlyConfiguredLocales, defaultLocale);
91  
92      }
93  
94      /***
95       * Contructs a ContainerConfiguration with custom feed size and sawp size
96       *
97       * @param localeRatio                locales and their ratio that should be feeded
98       * @param maxVolatileMemorySize      in memory captcha buffer max size
99       * @param maxPersistentMemorySize    persistent captcha buffer max size
100      * @param swapSize                   number of captchas that will be swapped from persistent to memory buffer each
101      *                                   time
102      * @param feedSize                   number of captchas that will be generated and stored to persistent buffer each
103      *                                   time
104      * @param feedBatchSize              max number of batch captchas to build when feeding, in order to preserve
105      *                                   memory
106      * @param serveOnlyConfiguredLocales if set to true, serve only locales that are in configuration (only by
107      *                                   language)
108      * @param defaultLocale              the default locale used by this engineContainer.
109      */
110     public ContainerConfiguration(Map localeRatio, int maxVolatileMemorySize, int maxPersistentMemorySize,
111                                   int swapSize, int feedSize, int feedBatchSize, boolean serveOnlyConfiguredLocales, Locale defaultLocale) {
112         this(localeRatio, maxVolatileMemorySize, maxPersistentMemorySize, swapSize, feedSize, serveOnlyConfiguredLocales, defaultLocale);
113         this.feedBatchSize = new Integer(feedBatchSize);
114     }
115 
116 
117     public Integer getFeedBatchSize() {
118         return feedBatchSize;
119     }
120 
121     public void setFeedBatchSize(Integer feedBatchSize) {
122         this.feedBatchSize = feedBatchSize;
123     }
124 
125     public boolean isServeOnlyConfiguredLocales() {
126         return serveOnlyConfiguredLocales;
127     }
128 
129     public void setServeOnlyConfiguredLocales(boolean serveOnlyConfiguredLocales) {
130         validateDefaultLocale(serveOnlyConfiguredLocales, defaultLocale);
131         this.serveOnlyConfiguredLocales = serveOnlyConfiguredLocales;
132     }
133 
134     public Locale getDefaultLocale() {
135         return defaultLocale;
136     }
137 
138     public void setDefaultLocale(Locale defaultLocale) {
139         validateDefaultLocale(this.serveOnlyConfiguredLocales, defaultLocale);
140 
141         this.defaultLocale = defaultLocale;
142     }
143 
144     private void validateDefaultLocale(boolean serveOnlyConfiguredLocales, Locale defaultLocale) {
145         if ((!this.getLocaleRatio().containsKey(defaultLocale)) && serveOnlyConfiguredLocales) {
146             throw new CaptchaEngineException("impossible build a ContainerConfiguration with a default locale that" +
147                     " is not in its localeRatio and that serve only configured locales : locale " + defaultLocale);
148         }
149     }
150 
151     /***
152      * @return
153      */
154     public Integer getFeedSize() {
155         return feedSize;
156     }
157 
158     /***
159      * @param feedSize
160      */
161     public void setFeedSize(Integer feedSize) {
162         this.feedSize = feedSize;
163     }
164 
165     /***
166      * @return Map of the ratio of locales
167      */
168     public HashedMap getLocaleRatio() {
169         return localeRatio;
170     }
171 
172     /***
173      * @param localeRatio Map of the ratio of locales, to produce captcha and to swap captchas
174      */
175     public void setLocaleRatio(Map localeRatio) {
176         this.localeRatio = new HashedMap(localeRatio.size());
177         this.localeRatio.putAll(localeRatio);
178     }
179 
180     /***
181      * @return Get maximum size for the disk buffer
182      */
183     public Integer getMaxPersistentMemorySize() {
184         return maxPersistentMemorySize;
185     }
186 
187     /***
188      * @param maxPersistentMemorySize Set maximum size for the disk buffer
189      */
190     public void setMaxPersistentMemorySize(Integer maxPersistentMemorySize) {
191         this.maxPersistentMemorySize = maxPersistentMemorySize;
192     }
193 
194     /***
195      * @return Get maximun size for the volatile buffer
196      */
197     public Integer getMaxVolatileMemorySize() {
198         return maxVolatileMemorySize;
199     }
200 
201     /***
202      * @param maxVolatileMemorySize Set maximun size for the volatile buffer
203      */
204     public void setMaxVolatileMemorySize(Integer maxVolatileMemorySize) {
205         this.maxVolatileMemorySize = maxVolatileMemorySize;
206     }
207 
208     /***
209      * @return Get number of captchas to swap between the volatil buffer and the disk buffer
210      */
211     public Integer getSwapSize() {
212         return swapSize;
213     }
214 
215     /***
216      * @param swapSize Set number of captchas to swap between the volatil buffer and the disk buffer
217      */
218     public void setSwapSize(Integer swapSize) {
219         this.swapSize = swapSize;
220     }
221 }