001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.webtools;
019:
020: import java.util.Iterator;
021: import java.util.Locale;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.ofbiz.base.util.UtilHttp;
027: import org.ofbiz.base.util.UtilMisc;
028: import org.ofbiz.base.util.UtilProperties;
029: import org.ofbiz.base.util.cache.UtilCache;
030: import org.ofbiz.security.Security;
031:
032: /**
033: * Contains events for the UtilCache class; must be external to access security resources
034: */
035: public class UtilCacheEvents {
036:
037: public static final String err_resource = "WebtoolsErrorUiLabels";
038:
039: /** An HTTP WebEvent handler the specified element from the specified cache
040: * @param request The HTTP request object for the current JSP or Servlet request.
041: * @param response The HTTP response object for the current JSP or Servlet request.
042: * @return
043: */
044: public static String removeElementEvent(HttpServletRequest request,
045: HttpServletResponse response) {
046: String errMsg = "";
047: Locale locale = UtilHttp.getLocale(request);
048:
049: Security security = (Security) request.getAttribute("security");
050: if (!security.hasPermission("UTIL_CACHE_EDIT", request
051: .getSession())) {
052: errMsg = UtilProperties.getMessage(
053: UtilCacheEvents.err_resource,
054: "utilCacheEvents.permissionEdit", locale)
055: + ".";
056: request.setAttribute("_ERROR_MESSAGE_", errMsg);
057: return "error";
058: }
059:
060: String name = request.getParameter("UTIL_CACHE_NAME");
061: if (name == null) {
062: errMsg = UtilProperties.getMessage(
063: UtilCacheEvents.err_resource,
064: "utilCacheEvents.noCacheNameSpecified", locale)
065: + ".";
066: request.setAttribute("_ERROR_MESSAGE_", errMsg);
067: return "error";
068: }
069: String numString = request
070: .getParameter("UTIL_CACHE_ELEMENT_NUMBER");
071:
072: if (numString == null) {
073: errMsg = UtilProperties.getMessage(
074: UtilCacheEvents.err_resource,
075: "utilCacheEvents.noElementNumberSpecified", locale)
076: + ".";
077: request.setAttribute("_ERROR_MESSAGE_", "");
078: return "error";
079: }
080: int number;
081:
082: try {
083: number = Integer.parseInt(numString);
084: } catch (Exception e) {
085: return "error";
086: }
087:
088: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
089: .get(name);
090:
091: if (utilCache != null) {
092: Object key = null;
093:
094: if (utilCache.getMaxSize() > 0) {
095: try {
096: key = utilCache.cacheLineTable
097: .getKeyFromMemory(number);
098: } catch (Exception e) {
099: }
100: } else {
101: // no LRU, try looping through the keySet to see if we find the specified index...
102: Iterator ksIter = utilCache.cacheLineTable.keySet()
103: .iterator();
104: int curNum = 0;
105:
106: while (ksIter.hasNext()) {
107: if (number == curNum) {
108: key = ksIter.next();
109: break;
110: } else {
111: ksIter.next();
112: }
113: curNum++;
114: }
115: }
116:
117: if (key != null) {
118: utilCache.remove(key);
119: errMsg = UtilProperties.getMessage(
120: UtilCacheEvents.err_resource,
121: "utilCache.removeElementWithKey", UtilMisc
122: .toMap("key", key.toString()), locale)
123: + ".";
124: request.setAttribute("_EVENT_MESSAGE_", errMsg);
125: } else {
126: errMsg = UtilProperties.getMessage(
127: UtilCacheEvents.err_resource,
128: "utilCache.couldNotRemoveElementNumber",
129: UtilMisc.toMap("name", name, "numString",
130: numString), locale)
131: + ".";
132: request.setAttribute("_ERROR_MESSAGE_", errMsg);
133: return "error";
134: }
135: } else {
136: errMsg = UtilProperties.getMessage(
137: UtilCacheEvents.err_resource,
138: "utilCache.couldNotRemoveElement", UtilMisc.toMap(
139: "name", name), locale)
140: + ".";
141: request.setAttribute("_ERROR_MESSAGE_", errMsg);
142: return "error";
143: }
144: return "success";
145: }
146:
147: /** An HTTP WebEvent handler that clears the named cache
148: * @param request The HTTP request object for the current JSP or Servlet request.
149: * @param response The HTTP response object for the current JSP or Servlet request.
150: * @return
151: */
152: public static String clearEvent(HttpServletRequest request,
153: HttpServletResponse response) {
154: String errMsg = "";
155: Locale locale = UtilHttp.getLocale(request);
156:
157: Security security = (Security) request.getAttribute("security");
158: if (!security.hasPermission("UTIL_CACHE_EDIT", request
159: .getSession())) {
160: errMsg = UtilProperties.getMessage(
161: UtilCacheEvents.err_resource,
162: "utilCacheEvents.permissionEdit", locale)
163: + ".";
164: request.setAttribute("_ERROR_MESSAGE_", errMsg);
165: return "error";
166: }
167:
168: String name = request.getParameter("UTIL_CACHE_NAME");
169:
170: if (name == null) {
171: errMsg = UtilProperties.getMessage(
172: UtilCacheEvents.err_resource,
173: "utilCache.couldNotClearCache", locale)
174: + ".";
175: request.setAttribute("_ERROR_MESSAGE_", errMsg);
176: return "error";
177: }
178: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
179: .get(name);
180:
181: if (utilCache != null) {
182: utilCache.clear();
183: errMsg = UtilProperties.getMessage(
184: UtilCacheEvents.err_resource,
185: "utilCache.clearCache", UtilMisc
186: .toMap("name", name), locale)
187: + ".";
188: request.setAttribute("_EVENT_MESSAGE_", errMsg);
189: } else {
190: errMsg = UtilProperties.getMessage(
191: UtilCacheEvents.err_resource,
192: "utilCache.couldNotClearCacheNotFoundName",
193: UtilMisc.toMap("name", name), locale)
194: + ".";
195: request.setAttribute("_ERROR_MESSAGE_", errMsg);
196: return "error";
197: }
198: return "success";
199: }
200:
201: /** An HTTP WebEvent handler that clears all caches
202: * @param request The HTTP request object for the current JSP or Servlet request.
203: * @param response The HTTP response object for the current JSP or Servlet request.
204: * @return
205: */
206: public static String clearAllEvent(HttpServletRequest request,
207: HttpServletResponse response) {
208: String errMsg = "";
209: Locale locale = UtilHttp.getLocale(request);
210:
211: Security security = (Security) request.getAttribute("security");
212: if (!security.hasPermission("UTIL_CACHE_EDIT", request
213: .getSession())) {
214: errMsg = UtilProperties.getMessage(
215: UtilCacheEvents.err_resource,
216: "utilCacheEvents.permissionEdit", locale)
217: + ".";
218: request.setAttribute("_ERROR_MESSAGE_", errMsg);
219: return "error";
220: }
221:
222: UtilCache.clearAllCaches();
223: errMsg = UtilProperties.getMessage(
224: UtilCacheEvents.err_resource,
225: "utilCache.clearAllCaches", locale)
226: + ".";
227: request.setAttribute("_EVENT_MESSAGE_", errMsg);
228: return "success";
229: }
230:
231: /** An HTTP WebEvent handler that clears all caches
232: * @param request The HTTP request object for the current JSP or Servlet request.
233: * @param response The HTTP response object for the current JSP or Servlet request.
234: * @return
235: */
236: public static String clearAllExpiredEvent(
237: HttpServletRequest request, HttpServletResponse response) {
238: String errMsg = "";
239: Locale locale = UtilHttp.getLocale(request);
240:
241: Security security = (Security) request.getAttribute("security");
242: if (!security.hasPermission("UTIL_CACHE_EDIT", request
243: .getSession())) {
244: errMsg = UtilProperties.getMessage(
245: UtilCacheEvents.err_resource,
246: "utilCacheEvents.permissionEdit", locale)
247: + ".";
248: request.setAttribute("_ERROR_MESSAGE_", errMsg);
249: return "error";
250: }
251:
252: UtilCache.clearExpiredFromAllCaches();
253: errMsg = UtilProperties.getMessage(
254: UtilCacheEvents.err_resource,
255: "utilCache.clearAllExpiredElements", locale)
256: + ".";
257: request.setAttribute("_EVENT_MESSAGE_", errMsg);
258: return "success";
259: }
260:
261: /** An HTTP WebEvent handler that updates the named cache
262: * @param request The HTTP request object for the current JSP or Servlet request.
263: * @param response The HTTP response object for the current JSP or Servlet request.
264: * @return
265: */
266: public static String updateEvent(HttpServletRequest request,
267: HttpServletResponse response) {
268: String errMsg = "";
269: Locale locale = UtilHttp.getLocale(request);
270:
271: Security security = (Security) request.getAttribute("security");
272: if (!security.hasPermission("UTIL_CACHE_EDIT", request
273: .getSession())) {
274: errMsg = UtilProperties.getMessage(
275: UtilCacheEvents.err_resource,
276: "utilCacheEvents.permissionEdit", locale)
277: + ".";
278: request.setAttribute("_EVENT_MESSAGE_", errMsg);
279: return "error";
280: }
281:
282: String name = request.getParameter("UTIL_CACHE_NAME");
283:
284: if (name == null) {
285: errMsg = UtilProperties.getMessage(
286: UtilCacheEvents.err_resource,
287: "utilCache.couldNotUpdateCacheSetting", locale)
288: + ".";
289: request.setAttribute("_ERROR_MESSAGE_", errMsg);
290: return "error";
291: }
292: String maxSizeStr = request.getParameter("UTIL_CACHE_MAX_SIZE");
293: String expireTimeStr = request
294: .getParameter("UTIL_CACHE_EXPIRE_TIME");
295: String useSoftReferenceStr = request
296: .getParameter("UTIL_CACHE_USE_SOFT_REFERENCE");
297:
298: Long maxSize = null, expireTime = null;
299:
300: try {
301: maxSize = Long.valueOf(maxSizeStr);
302: } catch (Exception e) {
303: }
304: try {
305: expireTime = Long.valueOf(expireTimeStr);
306: } catch (Exception e) {
307: }
308:
309: UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable
310: .get(name);
311:
312: if (utilCache != null) {
313: if (maxSize != null)
314: utilCache.setMaxSize(maxSize.intValue());
315: if (expireTime != null)
316: utilCache.setExpireTime(expireTime.longValue());
317: if (useSoftReferenceStr != null) {
318: utilCache.setUseSoftReference("true"
319: .equals(useSoftReferenceStr));
320: }
321: }
322: return "success";
323: }
324: }
|