001: /*
002: * $Id: UtilCacheEvents.java,v 1.1 2003/08/18 22:03:18 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.webtools;
025:
026: import java.util.Iterator;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.ofbiz.base.util.UtilCache;
032: import org.ofbiz.security.Security;
033:
034: /**
035: * Contains events for the UtilCache class; must be external to access security resources
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.1 $
039: * @since 2.0
040: */
041: public class UtilCacheEvents {
042:
043: /** An HTTP WebEvent handler the specified element from the specified cache
044: * @param request The HTTP request object for the current JSP or Servlet request.
045: * @param response The HTTP response object for the current JSP or Servlet request.
046: * @return
047: */
048: public static String removeElementEvent(HttpServletRequest request,
049: HttpServletResponse response) {
050: Security security = (Security) request.getAttribute("security");
051: if (!security.hasPermission("UTIL_CACHE_EDIT", request
052: .getSession())) {
053: request
054: .setAttribute(
055: "_ERROR_MESSAGE_",
056: "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
057: return "error";
058: }
059:
060: String name = request.getParameter("UTIL_CACHE_NAME");
061: if (name == null) {
062: request
063: .setAttribute("_ERROR_MESSAGE_",
064: "Could not remove cache line/element, no cache name specified.");
065: return "error";
066: }
067: String numString = request
068: .getParameter("UTIL_CACHE_ELEMENT_NUMBER");
069:
070: if (numString == null) {
071: request
072: .setAttribute("_ERROR_MESSAGE_",
073: "Could not remove cache line/element, no element number specified.");
074: return "error";
075: }
076: int number;
077:
078: try {
079: number = Integer.parseInt(numString);
080: } catch (Exception e) {
081: return "error";
082: }
083:
084: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
085: .get(name);
086:
087: if (utilCache != null) {
088: Object key = null;
089:
090: if (utilCache.getMaxSize() > 0) {
091: try {
092: key = utilCache.keyLRUList.get(number);
093: } catch (Exception e) {
094: }
095: } else {
096: // no LRU, try looping through the keySet to see if we find the specified index...
097: Iterator ksIter = utilCache.cacheLineTable.keySet()
098: .iterator();
099: int curNum = 0;
100:
101: while (ksIter.hasNext()) {
102: if (number == curNum) {
103: key = ksIter.next();
104: break;
105: } else {
106: ksIter.next();
107: }
108: curNum++;
109: }
110: }
111:
112: if (key != null) {
113: utilCache.remove(key);
114: request.setAttribute("_EVENT_MESSAGE_",
115: "Removed element from cache with key: "
116: + key.toString());
117: } else {
118: request.setAttribute("_ERROR_MESSAGE_",
119: "Could not remove cache element, element not found with cache name: "
120: + name + ", element number: "
121: + numString);
122: return "error";
123: }
124: } else {
125: request.setAttribute("_ERROR_MESSAGE_",
126: "Could not remove cache element, cache not found with name: "
127: + name);
128: return "error";
129: }
130: return "success";
131: }
132:
133: /** An HTTP WebEvent handler that clears the named cache
134: * @param request The HTTP request object for the current JSP or Servlet request.
135: * @param response The HTTP response object for the current JSP or Servlet request.
136: * @return
137: */
138: public static String clearEvent(HttpServletRequest request,
139: HttpServletResponse response) {
140: Security security = (Security) request.getAttribute("security");
141: if (!security.hasPermission("UTIL_CACHE_EDIT", request
142: .getSession())) {
143: request
144: .setAttribute(
145: "_ERROR_MESSAGE_",
146: "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
147: return "error";
148: }
149:
150: String name = request.getParameter("UTIL_CACHE_NAME");
151:
152: if (name == null) {
153: request.setAttribute("_ERROR_MESSAGE_",
154: "Could not clear cache, no name specified.");
155: return "error";
156: }
157: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
158: .get(name);
159:
160: if (utilCache != null) {
161: utilCache.clear();
162: request.setAttribute("_EVENT_MESSAGE_",
163: "Cleared cache with name: " + name);
164: } else {
165: request.setAttribute("_ERROR_MESSAGE_",
166: "Could not clear cache, cache not found with name: "
167: + name);
168: return "error";
169: }
170: return "success";
171: }
172:
173: /** An HTTP WebEvent handler that clears all caches
174: * @param request The HTTP request object for the current JSP or Servlet request.
175: * @param response The HTTP response object for the current JSP or Servlet request.
176: * @return
177: */
178: public static String clearAllEvent(HttpServletRequest request,
179: HttpServletResponse response) {
180: Security security = (Security) request.getAttribute("security");
181: if (!security.hasPermission("UTIL_CACHE_EDIT", request
182: .getSession())) {
183: request
184: .setAttribute(
185: "_ERROR_MESSAGE_",
186: "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
187: return "error";
188: }
189:
190: UtilCache.clearAllCaches();
191: request.setAttribute("_EVENT_MESSAGE_", "Cleared all caches.");
192: return "success";
193: }
194:
195: /** An HTTP WebEvent handler that clears all caches
196: * @param request The HTTP request object for the current JSP or Servlet request.
197: * @param response The HTTP response object for the current JSP or Servlet request.
198: * @return
199: */
200: public static String clearAllExpiredEvent(
201: HttpServletRequest request, HttpServletResponse response) {
202: Security security = (Security) request.getAttribute("security");
203: if (!security.hasPermission("UTIL_CACHE_EDIT", request
204: .getSession())) {
205: request
206: .setAttribute(
207: "_ERROR_MESSAGE_",
208: "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
209: return "error";
210: }
211:
212: UtilCache.clearExpiredFromAllCaches();
213: request.setAttribute("_EVENT_MESSAGE_",
214: "Cleared all expried elements from all caches.");
215: return "success";
216: }
217:
218: /** An HTTP WebEvent handler that updates the named cache
219: * @param request The HTTP request object for the current JSP or Servlet request.
220: * @param response The HTTP response object for the current JSP or Servlet request.
221: * @return
222: */
223: public static String updateEvent(HttpServletRequest request,
224: HttpServletResponse response) {
225: Security security = (Security) request.getAttribute("security");
226: if (!security.hasPermission("UTIL_CACHE_EDIT", request
227: .getSession())) {
228: request
229: .setAttribute(
230: "_ERROR_MESSAGE_",
231: "You do not have permission to perform this operation, UTIL_CACHE_EDIT required.");
232: return "error";
233: }
234:
235: String name = request.getParameter("UTIL_CACHE_NAME");
236:
237: if (name == null) {
238: request
239: .setAttribute("_ERROR_MESSAGE_",
240: "Could not update cache settings, no name specified");
241: return "error";
242: }
243: String maxSizeStr = request.getParameter("UTIL_CACHE_MAX_SIZE");
244: String expireTimeStr = request
245: .getParameter("UTIL_CACHE_EXPIRE_TIME");
246: String useSoftReferenceStr = request
247: .getParameter("UTIL_CACHE_USE_SOFT_REFERENCE");
248:
249: Long maxSize = null, expireTime = null;
250:
251: try {
252: maxSize = Long.valueOf(maxSizeStr);
253: } catch (Exception e) {
254: }
255: try {
256: expireTime = Long.valueOf(expireTimeStr);
257: } catch (Exception e) {
258: }
259:
260: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
261: .get(name);
262:
263: if (utilCache != null) {
264: if (maxSize != null)
265: utilCache.setMaxSize(maxSize.longValue());
266: if (expireTime != null)
267: utilCache.setExpireTime(expireTime.longValue());
268: if (useSoftReferenceStr != null) {
269: utilCache.setUseSoftReference("true"
270: .equals(useSoftReferenceStr));
271: }
272: }
273: return "success";
274: }
275: }
|