1
2
3
4
5
6
7 package com.octo.captcha.service.captchastore;
8
9 import com.octo.captcha.Captcha;
10 import com.octo.captcha.service.CaptchaServiceException;
11
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Locale;
15 import java.util.Map;
16
17
18 /***
19 * Simple store based on a HashMap
20 */
21 public class MapCaptchaStore implements CaptchaStore {
22
23 Map store;
24
25 public MapCaptchaStore() {
26 this.store = new HashMap();
27 }
28
29 /***
30 * Check if a captcha is stored for this id
31 *
32 * @return true if a captcha for this id is stored, false otherwise
33 */
34 public boolean hasCaptcha(String id) {
35 return store.containsKey(id);
36 }
37
38 /***
39 * Store the captcha with the provided id as key. The key is assumed to be unique, so if the same key is used twice
40 * to store a captcha, the store will return an exception
41 *
42 * @param id the key
43 * @param captcha the captcha
44 *
45 * @throws CaptchaServiceException if the captcha already exists, or if an error occurs during storing routine.
46 */
47 public void storeCaptcha(String id, Captcha captcha) throws CaptchaServiceException {
48
49
50
51
52 store.put(id, new CaptchaAndLocale(captcha));
53 }
54
55 /***
56 * Store the captcha with the provided id as key. The key is assumed to be unique, so if the same key is used twice
57 * to store a captcha, the store will return an exception
58 *
59 * @param id the key
60 * @param captcha the captcha
61 * @param locale the locale used that triggers the captcha generation
62 * @throws com.octo.captcha.service.CaptchaServiceException
63 * if the captcha already exists, or if an error occurs during storing routine.
64 */
65 public void storeCaptcha(String id, Captcha captcha, Locale locale) throws CaptchaServiceException {
66 store.put(id, new CaptchaAndLocale(captcha,locale));
67 }
68
69 /***
70 * Retrieve the captcha for this key from the store.
71 *
72 * @return the captcha for this id
73 *
74 * @throws CaptchaServiceException if a captcha for this key is not found or if an error occurs during retrieving
75 * routine.
76 */
77 public Captcha getCaptcha(String id) throws CaptchaServiceException {
78 Object captchaAndLocale = store.get(id);
79 return captchaAndLocale!=null?((CaptchaAndLocale) captchaAndLocale).getCaptcha():null;
80 }
81
82 /***
83 * Retrieve the locale for this key from the store.
84 *
85 * @return the locale for this id, null if not found
86 * @throws com.octo.captcha.service.CaptchaServiceException
87 * if an error occurs during retrieving routine.
88 */
89 public Locale getLocale(String id) throws CaptchaServiceException {
90 Object captchaAndLocale = store.get(id);
91 return captchaAndLocale!=null?((CaptchaAndLocale) captchaAndLocale).getLocale():null;
92 }
93
94 /***
95 * Remove the captcha with the provided id as key.
96 *
97 * @param id the key
98 *
99 * @return true if found, false otherwise
100 *
101 * @throws CaptchaServiceException if an error occurs during remove routine
102 */
103 public boolean removeCaptcha(String id) {
104 if (store.get(id) != null) {
105 store.remove(id);
106 return true;
107 }
108 return false;
109 }
110
111 /***
112 * get the size of this store
113 */
114 public int getSize() {
115 return store.size();
116 }
117
118 /***
119 * Return all the contained keys
120 */
121 public Collection getKeys() {
122 return store.keySet();
123 }
124
125 /***
126 * Empty the store
127 */
128 public void empty() {
129 this.store = new HashMap();
130 }
131
132
133
134
135 public void initAndStart() {
136
137 }
138
139
140
141
142 public void cleanAndShutdown() {
143 store.clear();
144 }
145 }