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   package com.octo.captcha.service.captchastore;
8   
9   import java.util.Collection;
10  import java.util.Collections;
11  import java.util.Locale;
12  
13  import org.jboss.cache.Cache;
14  import org.jboss.cache.CacheException;
15  import org.jboss.cache.CacheFactory;
16  import org.jboss.cache.DefaultCacheFactory;
17  import org.jboss.cache.Fqn;
18  import org.jboss.cache.Node;
19  
20  import com.octo.captcha.Captcha;
21  import com.octo.captcha.service.CaptchaServiceException;
22  
23  /***
24   * JBossCache 2.0.0 implementation of the captcha store. Needs JDK 5.0
25   * @see http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossCache
26   * @author <a href="mailto:antoine.veret@gmail.com">Antoine Véret</a>
27   * @version 1.0
28   */
29  public class JBossCacheCaptchaStore implements CaptchaStore {
30  
31  	public static final String JCAPTCHA_JBOSSCACHE_CONFIG = "jcaptcha.jbosscache.config";
32      private static final String DEFAULT_CACHE_NAME = "/captcha";
33      private Fqn cacheQualifiedName;
34      private Cache cache;
35  
36      public JBossCacheCaptchaStore() {
37          this(DEFAULT_CACHE_NAME);
38      }
39      
40      public JBossCacheCaptchaStore(String cacheQualifiedName) {
41          this.cacheQualifiedName = Fqn.fromString(cacheQualifiedName);
42      }
43  
44      public boolean hasCaptcha(String s) {
45  
46      	try {
47              Object result = cache.get(cacheQualifiedName, s);
48              if (result != null) {
49                  return true;
50              }
51              else
52                  return false;
53  
54          } catch (CacheException e) {
55              throw new CaptchaServiceException(e);
56          }        
57      }
58  
59      public void storeCaptcha(String s, Captcha captcha) throws CaptchaServiceException {
60  
61          try {
62              cache.put(cacheQualifiedName, s, new CaptchaAndLocale(captcha));
63          } catch (CacheException e) {
64              throw new CaptchaServiceException(e);
65          }
66      }
67  
68      public void storeCaptcha(String s, Captcha captcha, Locale locale) throws CaptchaServiceException {
69  
70          try {
71              cache.put(cacheQualifiedName, s, new CaptchaAndLocale(captcha, locale));
72          } catch (CacheException e) {
73              throw new CaptchaServiceException(e);
74          }
75      }
76  
77      public boolean removeCaptcha(String s) {
78          try {
79              Object captcha = cache.remove(cacheQualifiedName, s);
80              if (captcha != null)
81                  return true;
82              else
83                  return false;
84          } catch (CacheException e) {
85              throw new CaptchaServiceException(e);
86          }
87      }
88  
89      public Captcha getCaptcha(String s) throws CaptchaServiceException {
90  
91          try {
92              Object result = cache.get(cacheQualifiedName, s);
93              if (result != null) {
94                  CaptchaAndLocale captchaAndLocale = (CaptchaAndLocale) result;
95                  return captchaAndLocale.getCaptcha();
96              }
97              else
98                  return null;
99  
100         } catch (CacheException e) {
101             throw new CaptchaServiceException(e);
102         }
103     }
104 
105     public Locale getLocale(String s) throws CaptchaServiceException {
106 
107         try {
108             Object result = cache.get(cacheQualifiedName, s);
109             if (result != null) {
110                 CaptchaAndLocale captchaAndLocale = (CaptchaAndLocale) result;
111                 return captchaAndLocale.getLocale();
112             }
113             else
114                 return null;
115 
116         } catch (CacheException e) {
117             throw new CaptchaServiceException(e);
118         }
119     }
120 
121     public int getSize() {
122 
123     	try {
124     		Node root = cache.getRoot();
125     		if (root != null) {
126     			Node captchas = root.getChild(cacheQualifiedName);
127     			if (captchas != null)
128     				return captchas.dataSize();
129     		}
130     		return 0;
131     	} catch (CacheException e) {
132             throw new CaptchaServiceException(e);
133         }	
134     }
135 
136     public Collection getKeys() {
137 
138     	try {
139     		Node root = cache.getRoot();
140     		if (root != null) {
141     			Node captchas = root.getChild(cacheQualifiedName);
142     			if (captchas != null) {
143     				Collection keys = captchas.getKeys(); 
144     		        if (keys != null)
145     		        	return keys;
146     			}
147     		}
148     		return Collections.EMPTY_SET;
149     	} catch (CacheException e) {
150             throw new CaptchaServiceException(e);
151         }
152     }
153 
154     public void empty() {
155         try {
156         	Node root = cache.getRoot();
157     		if (root != null) {
158     			Node captchas = root.getChild(cacheQualifiedName);
159     			if (captchas != null) {
160     				captchas.clearData();
161     			}
162     		}        	
163             cache.removeNode(cacheQualifiedName);
164         } catch (CacheException e) {
165             throw new CaptchaServiceException(e);
166         }
167     }
168     
169     /* (non-Javadoc)
170 	 * @see com.octo.captcha.service.captchastore.CaptchaStore#initAndStart()
171 	 */
172 	public void initAndStart() {
173 		
174 		String configFileName = System.getProperty(JCAPTCHA_JBOSSCACHE_CONFIG);
175         if (configFileName == null)
176             throw new RuntimeException("The system property " + JCAPTCHA_JBOSSCACHE_CONFIG + " have to be set");
177 		
178 		CacheFactory factory = DefaultCacheFactory.getInstance();
179 	    cache = factory.createCache(configFileName);								
180 	}
181 
182 	/* (non-Javadoc)
183 	 * @see com.octo.captcha.service.captchastore.CaptchaStore#shutdownAndClean()
184 	 */
185 	public void cleanAndShutdown() {
186 		cache.stop();
187 		cache.destroy();
188 	}
189 }