KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > CacheControl


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

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 JavaDoc;
86 import java.util.Iterator JavaDoc;
87 import java.util.List JavaDoc;
88 import java.util.Vector JavaDoc;
89
90
91 /**
92  * This Controller allows control of the CacheManager. It allows any or all caches
93  * to be cleared, and permits the current status of the cache to be displayed. This
94  * controller is called when the cache requires and update triggered from a seperate VM.
95  *
96  * @author Michael Nash
97  * @version $Revision: 1.18 $ $Date: 2004/11/17 20:48:17 $
98  */

99 public class CacheControl
100         extends DBController {
101
102     /**
103      * Our constructor "declares" what states we handle
104      */

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     } /* CacheControl() */
127
128     /**
129      * Adds all cacheable items under the given output o
130      *
131      * @param o The parent output
132      * @param db The data context
133      * @param cacheName The name of the cache to get the outputs for
134      * @param key The key for the cache or 'all' if you want to retrieve
135      * all elements in the cache
136      */

137     private void addAllItems(Output o, String JavaDoc db, String JavaDoc cacheName, String JavaDoc key)
138             throws CacheException {
139         long curTime = System.currentTimeMillis();
140         CacheSystem cs = CacheManager.getCacheSystem(db);
141         if (key.equalsIgnoreCase("all")) {
142             List JavaDoc v = cs.getItems(cacheName);
143
144             if (v == null) {
145                 return;
146             }
147
148             Cacheable oneItem = null;
149
150             for (Iterator JavaDoc 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 JavaDoc 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     } /* addAllItems(Output, String, String, String) */
174
175
176     /**
177      * Clears the cache
178      *
179      * @param request The ControllerRequest Object
180      * @param response the ControllerResposne
181      */

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 JavaDoc dbVector = new Vector JavaDoc();
200
201         if (request.getParameter("dbContext").equalsIgnoreCase("all")) {
202             for (Enumeration JavaDoc edb = ConfigManager.getAllConfigKeys();
203                  edb.hasMoreElements();) {
204                 dbVector.addElement((String JavaDoc) edb.nextElement());
205             }
206         } else {
207             dbVector.addElement(request.getParameter("dbContext"));
208         }
209         try {
210             String JavaDoc oneDBName = null;
211
212             for (Enumeration JavaDoc ecl = dbVector.elements(); ecl.hasMoreElements();) {
213                 oneDBName = (String JavaDoc) 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             } /* for each db/context */
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     } /* clearState() */
258
259
260     /**
261      * Show the current state of the cache
262      *
263      * @param response the ControllerResponse Object
264      * @param request The ControllerRequest object
265      */

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 JavaDoc dbVector = new Vector JavaDoc();
279
280         if (request.getParameter("dbContext").equalsIgnoreCase("all")) {
281             for (Enumeration JavaDoc edb = ConfigManager.getAllConfigKeys();
282                  edb.hasMoreElements();) {
283                 dbVector.addElement((String JavaDoc) edb.nextElement());
284             }
285         } else {
286             dbVector.addElement(request.getParameter("dbContext"));
287         }
288         try {
289             String JavaDoc oneDBName = null;
290
291             for (Enumeration JavaDoc ecl = dbVector.elements(); ecl.hasMoreElements();) {
292                 oneDBName = (String JavaDoc) ecl.nextElement();
293
294                 Output oneDB = new Output("Database/Context '" + oneDBName +
295                         "'");
296                 response.addOutput(oneDB);
297
298                 Output oneCache = null;
299                 String JavaDoc cacheName = null;
300
301                 if (request.getParameter("cache").equalsIgnoreCase("all")) {
302                     Iterator JavaDoc c = CacheManager.getAllCacheNamesIterator(oneDBName);
303
304                     if (c != null) {
305                         while (c.hasNext()) {
306                             cacheName = (String JavaDoc) 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             } /* for each db/context */
321
322         } catch (CacheException ce) {
323             throw new ControllerException("Unable to clear cache", ce);
324         }
325
326         Runtime JavaDoc 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     } /* displayState() */
338
339
340     /**
341      * Return the title of this Controller
342      *
343      * @return the title of this controller
344      */

345     public String JavaDoc getTitle() {
346         return ("Cache Control");
347     } /* getTitle() */
348
349     /**
350      * Prompt for the command to perform
351      *
352      * @param response The ControllerResponse object
353      * @param request The ControllerRequest object
354      */

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 JavaDoc vv = new Vector JavaDoc(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     } /* promptState() */
397
398
399 } /* CacheControl */
400
Popular Tags