1 64 65 package com.jcorporate.expresso.services.controller; 66 67 import com.jcorporate.expresso.core.cache.CacheEntry; 68 import com.jcorporate.expresso.core.cache.CacheException; 69 import com.jcorporate.expresso.core.cache.CacheManager; 70 import com.jcorporate.expresso.core.cache.CacheSystem; 71 import com.jcorporate.expresso.core.cache.Cacheable; 72 import com.jcorporate.expresso.core.controller.ControllerException; 73 import com.jcorporate.expresso.core.controller.ControllerRequest; 74 import com.jcorporate.expresso.core.controller.ControllerResponse; 75 import com.jcorporate.expresso.core.controller.DBController; 76 import com.jcorporate.expresso.core.controller.Input; 77 import com.jcorporate.expresso.core.controller.Output; 78 import com.jcorporate.expresso.core.controller.State; 79 import com.jcorporate.expresso.core.controller.Transition; 80 import com.jcorporate.expresso.core.db.DBConnectionPool; 81 import com.jcorporate.expresso.core.db.DBException; 82 import com.jcorporate.expresso.core.dbobj.ValidValue; 83 import com.jcorporate.expresso.core.misc.ConfigManager; 84 85 import java.util.Enumeration ; 86 import java.util.Iterator ; 87 import java.util.List ; 88 import java.util.Vector ; 89 90 91 99 public class CacheControl 100 extends DBController { 101 102 105 public CacheControl() { 106 super(); 107 108 State prompt = new State("prompt", "Prompt for Command"); 109 addState(prompt); 110 setInitialState("prompt"); 111 112 State clearCache = new State("clear", "Clear Cache"); 113 clearCache.addRequiredParameter("dbContext"); 114 clearCache.addRequiredParameter("cache"); 115 clearCache.addRequiredParameter("key"); 116 clearCache.addRequiredParameter("nonotify"); 117 addState(clearCache); 118 119 State display = new State("display", "Display Cache Information"); 120 display.addRequiredParameter("dbContext"); 121 display.addRequiredParameter("cache"); 122 display.addRequiredParameter("key"); 123 addState(display); 124 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 125 126 } 127 128 137 private void addAllItems(Output o, String db, String cacheName, String key) 138 throws CacheException { 139 long curTime = System.currentTimeMillis(); 140 CacheSystem cs = CacheManager.getCacheSystem(db); 141 if (key.equalsIgnoreCase("all")) { 142 List v = cs.getItems(cacheName); 143 144 if (v == null) { 145 return; 146 } 147 148 Cacheable oneItem = null; 149 150 for (Iterator ei = v.iterator(); ei.hasNext();) { 151 oneItem = (Cacheable) ei.next(); 152 CacheEntry ce = cs.getCache(cacheName).getCacheEntry(oneItem.getKey()); 153 if (ce != null) { 154 String outputContent = "Item '" + oneItem.getKey(); 155 long expires = ce.getExpires(); 156 if (expires > 0) { 157 outputContent = outputContent + " [Expiration: " + (int) ((expires - curTime) / 60000) + " minutes]"; 158 } 159 o.addNested(new Output(outputContent)); 160 } 161 } 162 } else { 163 Cacheable oneItem = cs.getItem(cacheName, key); 164 165 if (oneItem != null) { 166 o.addNested(new Output("Item '" + oneItem.getKey())); 167 } else { 168 o.addNested(new Output("No such item with key '" + key + 169 "' in cache '" + cacheName + 170 "' in db/context '" + db + "'")); 171 } 172 } 173 } 174 175 176 182 private void runClearState(ControllerRequest request, 183 ControllerResponse response) 184 throws ControllerException { 185 try { 186 DBConnectionPool.reInitialize(); 187 } catch (DBException de) { 188 throw new ControllerException(de); 189 } 190 if (!request.getParameter("key").equalsIgnoreCase("all")) { 191 if (request.getParameter("dbContext").equalsIgnoreCase("all")) { 192 throw new ControllerException("You cannot clear a specific key in all databases"); 193 } 194 if (request.getParameter("cache").equalsIgnoreCase("all")) { 195 throw new ControllerException("You cannot clear a specific key in all caches"); 196 } 197 } 198 199 Vector dbVector = new Vector (); 200 201 if (request.getParameter("dbContext").equalsIgnoreCase("all")) { 202 for (Enumeration edb = ConfigManager.getAllConfigKeys(); 203 edb.hasMoreElements();) { 204 dbVector.addElement((String ) edb.nextElement()); 205 } 206 } else { 207 dbVector.addElement(request.getParameter("dbContext")); 208 } 209 try { 210 String oneDBName = null; 211 212 for (Enumeration ecl = dbVector.elements(); ecl.hasMoreElements();) { 213 oneDBName = (String ) ecl.nextElement(); 214 215 if (request.getParameter("cache").equalsIgnoreCase("all")) { 216 if (!request.getParameter("nonotify").equals("true")) { 217 CacheManager.clearNoNotify(oneDBName); 218 } else { 219 CacheManager.clear(oneDBName); 220 } 221 } else { 222 if (request.getParameter("key").equalsIgnoreCase("all")) { 223 if (!request.getParameter("nonotify").equals("true")) { 224 CacheManager.clearNoNotify(oneDBName); 225 } else { 226 CacheManager.clear(oneDBName, 227 request.getParameter("cache")); 228 } 229 } else { 230 Cacheable oneItem = CacheManager.getItem(oneDBName, 231 request.getParameter("cache"), 232 request.getParameter("key")); 233 234 if (oneItem != null) { 235 if (!request.getParameter("nonotify").equals("true")) { 236 CacheManager.removeItemNoNotify(oneDBName, 237 request.getParameter("cache"), 238 oneItem); 239 } else { 240 CacheManager.removeItem(oneDBName, 241 request.getParameter("cache"), 242 oneItem); 243 } 244 } 245 } 246 } 247 } 248 249 } catch (CacheException ce) { 250 throw new ControllerException("Unable to clear cache", ce); 251 } 252 253 Transition prompt = new Transition("Start Again", getClass().getName()); 254 prompt.setName("prompt"); 255 prompt.addParam(STATE_PARAM_KEY, "prompt"); 256 response.addTransition(prompt); 257 } 258 259 260 266 private void runDisplayState(ControllerRequest request, 267 ControllerResponse response) 268 throws ControllerException { 269 if (!request.getParameter("key").equalsIgnoreCase("all")) { 270 if (request.getParameter("dbContext").equalsIgnoreCase("all")) { 271 throw new ControllerException("You cannot display a specific key in all databases"); 272 } 273 if (request.getParameter("cache").equalsIgnoreCase("all")) { 274 throw new ControllerException("You cannot display a specific key in all caches"); 275 } 276 } 277 278 Vector dbVector = new Vector (); 279 280 if (request.getParameter("dbContext").equalsIgnoreCase("all")) { 281 for (Enumeration edb = ConfigManager.getAllConfigKeys(); 282 edb.hasMoreElements();) { 283 dbVector.addElement((String ) edb.nextElement()); 284 } 285 } else { 286 dbVector.addElement(request.getParameter("dbContext")); 287 } 288 try { 289 String oneDBName = null; 290 291 for (Enumeration ecl = dbVector.elements(); ecl.hasMoreElements();) { 292 oneDBName = (String ) ecl.nextElement(); 293 294 Output oneDB = new Output("Database/Context '" + oneDBName + 295 "'"); 296 response.addOutput(oneDB); 297 298 Output oneCache = null; 299 String cacheName = null; 300 301 if (request.getParameter("cache").equalsIgnoreCase("all")) { 302 Iterator c = CacheManager.getAllCacheNamesIterator(oneDBName); 303 304 if (c != null) { 305 while (c.hasNext()) { 306 cacheName = (String ) c.next(); 307 oneCache = new Output("Cache '" + cacheName + "'"); 308 oneDB.addNested(oneCache); 309 addAllItems(oneCache, oneDBName, cacheName, 310 request.getParameter("key")); 311 } 312 } 313 } else { 314 cacheName = request.getParameter("cache"); 315 oneCache = new Output("Cache '" + cacheName + "'"); 316 oneDB.addNested(oneCache); 317 addAllItems(oneCache, oneDBName, cacheName, 318 request.getParameter("key")); 319 } 320 } 321 322 } catch (CacheException ce) { 323 throw new ControllerException("Unable to clear cache", ce); 324 } 325 326 Runtime r = Runtime.getRuntime(); 327 double avail = r.totalMemory(); 328 double free = r.freeMemory(); 329 double pfree = ((free / avail) * 100.000); 330 response.addOutput(new Output("Memory avail:" + avail + ", Free:" + 331 free + ", %free " + pfree)); 332 333 Transition prompt = new Transition("Start Again", getClass().getName()); 334 prompt.setName("prompt"); 335 prompt.addParam(STATE_PARAM_KEY, "prompt"); 336 response.addTransition(prompt); 337 } 338 339 340 345 public String getTitle() { 346 return ("Cache Control"); 347 } 348 349 355 private void runPromptState(ControllerRequest request, 356 ControllerResponse response) 357 throws ControllerException { 358 Input db = new Input(); 359 db.setLabel("Database/Context (or 'all')"); 360 db.setName("dbContext"); 361 db.setDefaultValue("all"); 362 response.addInput(db); 363 364 Input cache = new Input(); 365 cache.setLabel("Cache Name (or 'all')"); 366 cache.setName("cache"); 367 cache.setDefaultValue("all"); 368 response.addInput(cache); 369 370 Input key = new Input(); 371 key.setLabel("Cache Item Key (or 'all')"); 372 key.setName("key"); 373 key.setDefaultValue("all"); 374 response.addInput(key); 375 376 Input noNotify = new Input(); 377 noNotify.setLabel("Skip Notifications of remote Caches?"); 378 noNotify.setName("nonotify"); 379 noNotify.setDefaultValue("true"); 380 Vector vv = new Vector (2); 381 vv.add(new ValidValue("true", "true")); 382 vv.add(new ValidValue("false", "false")); 383 noNotify.setValidValues(vv); 384 response.addInput(noNotify); 385 386 Transition clear = new Transition("Clear Cache", getClass().getName()); 387 clear.setName("clear"); 388 clear.addParam(STATE_PARAM_KEY, "clear"); 389 response.addTransition(clear); 390 391 Transition display = new Transition("Display Cache", 392 getClass().getName()); 393 display.setName("display"); 394 display.addParam(STATE_PARAM_KEY, "display"); 395 response.addTransition(display); 396 } 397 398 399 } 400 | Popular Tags |