KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webtools > UtilCacheEvents


1 /*
2  * $Id: UtilCacheEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.webtools;
25
26 import java.util.Iterator JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.ofbiz.base.util.cache.UtilCache;
32 import org.ofbiz.security.Security;
33
34 /**
35  * Contains events for the UtilCache class; must be external to access security resources
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class UtilCacheEvents {
42
43     /** An HTTP WebEvent handler the specified element from the specified cache
44      * @param request The HTTP request object for the current JSP or Servlet request.
45      * @param response The HTTP response object for the current JSP or Servlet request.
46      * @return
47      */

48     public static String JavaDoc removeElementEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
49         Security security = (Security) request.getAttribute("security");
50         if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
51             request.setAttribute("_ERROR_MESSAGE_", "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
52             return "error";
53         }
54
55         String JavaDoc name = request.getParameter("UTIL_CACHE_NAME");
56         if (name == null) {
57             request.setAttribute("_ERROR_MESSAGE_", "Could not remove cache line/element, no cache name specified.");
58             return "error";
59         }
60         String JavaDoc numString = request.getParameter("UTIL_CACHE_ELEMENT_NUMBER");
61
62         if (numString == null) {
63             request.setAttribute("_ERROR_MESSAGE_", "Could not remove cache line/element, no element number specified.");
64             return "error";
65         }
66         int number;
67
68         try {
69             number = Integer.parseInt(numString);
70         } catch (Exception JavaDoc e) {
71             return "error";
72         }
73
74         UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get(name);
75
76         if (utilCache != null) {
77             Object JavaDoc key = null;
78
79             if (utilCache.getMaxSize() > 0) {
80                 try {
81                     key = utilCache.cacheLineTable.getKeyFromMemory(number);
82                 } catch (Exception JavaDoc e) {}
83             } else {
84                 // no LRU, try looping through the keySet to see if we find the specified index...
85
Iterator JavaDoc ksIter = utilCache.cacheLineTable.keySet().iterator();
86                 int curNum = 0;
87
88                 while (ksIter.hasNext()) {
89                     if (number == curNum) {
90                         key = ksIter.next();
91                         break;
92                     } else {
93                         ksIter.next();
94                     }
95                     curNum++;
96                 }
97             }
98
99             if (key != null) {
100                 utilCache.remove(key);
101                 request.setAttribute("_EVENT_MESSAGE_", "Removed element from cache with key: " + key.toString());
102             } else {
103                 request.setAttribute("_ERROR_MESSAGE_", "Could not remove cache element, element not found with cache name: " + name + ", element number: " + numString);
104                 return "error";
105             }
106         } else {
107             request.setAttribute("_ERROR_MESSAGE_", "Could not remove cache element, cache not found with name: " + name);
108             return "error";
109         }
110         return "success";
111     }
112
113     /** An HTTP WebEvent handler that clears the named cache
114      * @param request The HTTP request object for the current JSP or Servlet request.
115      * @param response The HTTP response object for the current JSP or Servlet request.
116      * @return
117      */

118     public static String JavaDoc clearEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
119         Security security = (Security) request.getAttribute("security");
120         if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
121             request.setAttribute("_ERROR_MESSAGE_", "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
122             return "error";
123         }
124
125         String JavaDoc name = request.getParameter("UTIL_CACHE_NAME");
126
127         if (name == null) {
128             request.setAttribute("_ERROR_MESSAGE_", "Could not clear cache, no name specified.");
129             return "error";
130         }
131         UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get(name);
132
133         if (utilCache != null) {
134             utilCache.clear();
135             request.setAttribute("_EVENT_MESSAGE_", "Cleared cache with name: " + name);
136         } else {
137             request.setAttribute("_ERROR_MESSAGE_", "Could not clear cache, cache not found with name: " + name);
138             return "error";
139         }
140         return "success";
141     }
142
143     /** An HTTP WebEvent handler that clears all caches
144      * @param request The HTTP request object for the current JSP or Servlet request.
145      * @param response The HTTP response object for the current JSP or Servlet request.
146      * @return
147      */

148     public static String JavaDoc clearAllEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
149         Security security = (Security) request.getAttribute("security");
150         if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
151             request.setAttribute("_ERROR_MESSAGE_", "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
152             return "error";
153         }
154
155         UtilCache.clearAllCaches();
156         request.setAttribute("_EVENT_MESSAGE_", "Cleared all caches.");
157         return "success";
158     }
159
160     /** An HTTP WebEvent handler that clears all caches
161      * @param request The HTTP request object for the current JSP or Servlet request.
162      * @param response The HTTP response object for the current JSP or Servlet request.
163      * @return
164      */

165     public static String JavaDoc clearAllExpiredEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
166         Security security = (Security) request.getAttribute("security");
167         if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
168             request.setAttribute("_ERROR_MESSAGE_", "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
169             return "error";
170         }
171
172         UtilCache.clearExpiredFromAllCaches();
173         request.setAttribute("_EVENT_MESSAGE_", "Cleared all expried elements from all caches.");
174         return "success";
175     }
176
177     /** An HTTP WebEvent handler that updates the named cache
178      * @param request The HTTP request object for the current JSP or Servlet request.
179      * @param response The HTTP response object for the current JSP or Servlet request.
180      * @return
181      */

182     public static String JavaDoc updateEvent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
183         Security security = (Security) request.getAttribute("security");
184         if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
185             request.setAttribute("_ERROR_MESSAGE_", "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
186             return "error";
187         }
188
189         String JavaDoc name = request.getParameter("UTIL_CACHE_NAME");
190
191         if (name == null) {
192             request.setAttribute("_ERROR_MESSAGE_", "Could not update cache settings, no name specified");
193             return "error";
194         }
195         String JavaDoc maxSizeStr = request.getParameter("UTIL_CACHE_MAX_SIZE");
196         String JavaDoc expireTimeStr = request.getParameter("UTIL_CACHE_EXPIRE_TIME");
197         String JavaDoc useSoftReferenceStr = request.getParameter("UTIL_CACHE_USE_SOFT_REFERENCE");
198
199         Long JavaDoc maxSize = null, expireTime = null;
200
201         try {
202             maxSize = Long.valueOf(maxSizeStr);
203         } catch (Exception JavaDoc e) {}
204         try {
205             expireTime = Long.valueOf(expireTimeStr);
206         } catch (Exception JavaDoc e) {}
207
208         UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get(name);
209
210         if (utilCache != null) {
211             if (maxSize != null)
212                 utilCache.setMaxSize(maxSize.intValue());
213             if (expireTime != null)
214                 utilCache.setExpireTime(expireTime.longValue());
215             if (useSoftReferenceStr != null) {
216                 utilCache.setUseSoftReference("true".equals(useSoftReferenceStr));
217             }
218         }
219         return "success";
220     }
221 }
222
Popular Tags