KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > form > CmsCaptchaServiceCache


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsCaptchaServiceCache.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone.form;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.CmsEvent;
36 import org.opencms.main.I_CmsEventListener;
37 import org.opencms.main.OpenCms;
38
39 import java.util.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import com.octo.captcha.service.image.ImageCaptchaService;
43
44 /**
45  * Caches captcha services.
46  * <p>
47  *
48  * @author Thomas Weckert
49  *
50  * @author Achim Westermann
51  *
52  * @version $Revision: 1.5 $
53  */

54 public final class CmsCaptchaServiceCache implements I_CmsEventListener {
55
56     /** The shared instance of the captcha service cache. */
57     private static CmsCaptchaServiceCache sharedInstance = null;
58
59     /** Stores the captcha services. */
60     private Map JavaDoc m_captchaServices = null;
61
62     /**
63      * Default constructor.
64      * <p>
65      */

66     private CmsCaptchaServiceCache() {
67
68         super();
69
70         // add this class as an event handler to the Cms event listener
71
OpenCms.addCmsEventListener(this, new int[] {
72             I_CmsEventListener.EVENT_CLEAR_CACHES,
73             I_CmsEventListener.EVENT_CLEAR_ONLINE_CACHES,
74             I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES,
75             I_CmsEventListener.EVENT_PUBLISH_PROJECT});
76
77         m_captchaServices = new HashMap JavaDoc();
78     }
79
80     /**
81      * Returns the shared instance of the captcha service cache.
82      * <p>
83      *
84      * @return the shared instance of the captcha service cache
85      */

86     public static synchronized CmsCaptchaServiceCache getSharedInstance() {
87
88         if (sharedInstance == null) {
89             sharedInstance = new CmsCaptchaServiceCache();
90         }
91
92         return sharedInstance;
93     }
94
95     /**
96      * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent)
97      */

98     public void cmsEvent(CmsEvent event) {
99
100         switch (event.getType()) {
101             case I_CmsEventListener.EVENT_CLEAR_CACHES:
102             case I_CmsEventListener.EVENT_CLEAR_ONLINE_CACHES:
103             case I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES:
104             case I_CmsEventListener.EVENT_PUBLISH_PROJECT:
105                 clearCaptchaServices();
106                 break;
107
108             default:
109                 // noop
110
break;
111         }
112     }
113
114     /**
115      * Returns the captcha service specified by the settings.
116      * <p>
117      *
118      * @param captchaSettings the settings to render captcha images.
119      *
120      * @param cms needed for context information when getting the key for caching.
121      *
122      * @return the captcha service.
123      */

124     public synchronized ImageCaptchaService getCaptchaService(CmsCaptchaSettings captchaSettings, CmsObject cms) {
125
126         String JavaDoc key = null;
127
128         if (m_captchaServices == null) {
129             m_captchaServices = new HashMap JavaDoc();
130         }
131
132
133         key = captchaSettings.toRequestParams(cms);
134         CmsCaptchaService captchaService = (CmsCaptchaService)m_captchaServices.get(key);
135         if (captchaService == null) {
136             captchaService = new CmsCaptchaService(captchaSettings);
137             m_captchaServices.put(key, captchaService);
138         } else {
139             // install the parameters to the internal engine
140
// captchaService.setSettings(captchaSettings);
141
}
142
143         return captchaService;
144     }
145
146     /**
147      * Clears the map storing the captcha services.
148      * <p>
149      */

150     private void clearCaptchaServices() {
151
152         if (m_captchaServices != null) {
153             m_captchaServices.clear();
154         }
155     }
156
157 }
158
Popular Tags