1
2
3
4
5
6
7 package com.octo.captcha.engine.image;
8
9 import java.security.SecureRandom;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Locale;
13 import java.util.Random;
14
15 import com.octo.captcha.Captcha;
16 import com.octo.captcha.CaptchaFactory;
17 import com.octo.captcha.engine.CaptchaEngineException;
18 import com.octo.captcha.image.ImageCaptcha;
19 import com.octo.captcha.image.ImageCaptchaFactory;
20
21 /***
22 * <p>Description: abstract base class for ImageCaptcha engines</p>.
23 *
24 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
25 * @version 1.0
26 */
27 public abstract class ImageCaptchaEngine
28 implements com.octo.captcha.engine.CaptchaEngine {
29
30 protected List factories = new ArrayList();
31 protected Random myRandom = new SecureRandom();
32
33 /***
34 * This method build a ImageCaptchaFactory.
35 *
36 * @return a CaptchaFactory
37 */
38 public com.octo.captcha.image.ImageCaptchaFactory getImageCaptchaFactory() {
39 return (com.octo.captcha.image.ImageCaptchaFactory) factories.get(
40 myRandom.nextInt(factories.size()));
41 }
42
43 /***
44 * This method use an object parameter to build a CaptchaFactory.
45 *
46 * @return a CaptchaFactory
47 */
48 public final ImageCaptcha getNextImageCaptcha() {
49 return getImageCaptchaFactory().getImageCaptcha();
50 }
51
52 /***
53 * This return a new captcha. It may be used directly.
54 *
55 * @param locale the desired locale
56 * @return a new Captcha
57 */
58 public ImageCaptcha getNextImageCaptcha(Locale locale) {
59 return getImageCaptchaFactory().getImageCaptcha(locale);
60 }
61
62 public final Captcha getNextCaptcha() {
63 return getImageCaptchaFactory().getImageCaptcha();
64 }
65
66 /***
67 * This return a new captcha. It may be used directly.
68 *
69 * @param locale the desired locale
70 * @return a new Captcha
71 */
72 public Captcha getNextCaptcha(Locale locale) {
73 return getImageCaptchaFactory().getImageCaptcha(locale);
74 }
75
76
77 /***
78 * @return captcha factories used by this engine
79 */
80 public CaptchaFactory[] getFactories() {
81 return (CaptchaFactory[]) this.factories.toArray(new CaptchaFactory[factories.size()]);
82 }
83
84 /***
85 * @param factories new captcha factories for this engine
86 */
87 public void setFactories(CaptchaFactory[] factories) throws CaptchaEngineException {
88 checkNotNullOrEmpty(factories);
89 ArrayList tempFactories = new ArrayList();
90
91 for (int i = 0; i < factories.length; i++) {
92 if (!ImageCaptchaFactory.class.isAssignableFrom(factories[i].getClass())) {
93 throw new CaptchaEngineException("This factory is not an image captcha factory " + factories[i].getClass());
94 }
95 tempFactories.add(factories[i]);
96 }
97
98 this.factories = tempFactories;
99 }
100
101 protected void checkNotNullOrEmpty(CaptchaFactory[] factories) {
102 if (factories == null || factories.length == 0) {
103 throw new CaptchaEngineException("impossible to set null or empty factories");
104 }
105 }
106 }