1 24 package org.ofbiz.webtools; 25 26 import java.util.Iterator ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.ofbiz.base.util.cache.UtilCache; 32 import org.ofbiz.security.Security; 33 34 41 public class UtilCacheEvents { 42 43 48 public static String removeElementEvent(HttpServletRequest request, HttpServletResponse 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 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 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 e) { 71 return "error"; 72 } 73 74 UtilCache utilCache = (UtilCache) UtilCache.utilCacheTable.get(name); 75 76 if (utilCache != null) { 77 Object key = null; 78 79 if (utilCache.getMaxSize() > 0) { 80 try { 81 key = utilCache.cacheLineTable.getKeyFromMemory(number); 82 } catch (Exception e) {} 83 } else { 84 Iterator 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 118 public static String clearEvent(HttpServletRequest request, HttpServletResponse 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 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 148 public static String clearAllEvent(HttpServletRequest request, HttpServletResponse 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 165 public static String clearAllExpiredEvent(HttpServletRequest request, HttpServletResponse 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 182 public static String updateEvent(HttpServletRequest request, HttpServletResponse 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 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 maxSizeStr = request.getParameter("UTIL_CACHE_MAX_SIZE"); 196 String expireTimeStr = request.getParameter("UTIL_CACHE_EXPIRE_TIME"); 197 String useSoftReferenceStr = request.getParameter("UTIL_CACHE_USE_SOFT_REFERENCE"); 198 199 Long maxSize = null, expireTime = null; 200 201 try { 202 maxSize = Long.valueOf(maxSizeStr); 203 } catch (Exception e) {} 204 try { 205 expireTime = Long.valueOf(expireTimeStr); 206 } catch (Exception 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 |