KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > commands > CmdClearCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19  
20 package org.openharmonise.rm.commands;
21
22 import java.lang.reflect.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.cache.*;
26 import org.openharmonise.rm.factory.*;
27 import org.openharmonise.rm.search.*;
28
29
30 /**
31  * Clears the cache specified in the parameters of the command
32  *
33  * @author Michael Bell
34  * @version $Revision: 1.3 $
35  *
36  */

37 public class CmdClearCache extends AbstractCmd {
38     private static String JavaDoc PARAM_CACHE = "cache";
39
40     /**
41      * Logger for this class
42      */

43     private static final Logger m_logger = Logger.getLogger(CmdClearCache.class.getName());
44
45     /**
46      * Creates a new instance of the command.
47      *
48      */

49     public CmdClearCache() {
50         super();
51     }
52
53     /* (non-Javadoc)
54      * @see org.openharmonise.rm.commands.AbstractCmd#execute()
55      */

56     public Object JavaDoc execute(Context context) throws CommandException {
57         String JavaDoc cache_name = this.getParameter(PARAM_CACHE);
58
59         if(m_logger.isLoggable(Level.FINE)) {
60             m_logger.logp(Level.FINE, this.getClass().getName(), "execute", "Clearing cache:" + cache_name);
61         }
62
63         if (cache_name.equalsIgnoreCase("resultset")) {
64             try {
65                 SearchResultsCache.getInstance().clearCache();
66             } catch (CacheException e) {
67                 throw new CommandExecutionException("Cache exception",e);
68             }
69         } else if (cache_name.indexOf(".") > 0) {
70             Class JavaDoc obj_class;
71             try {
72                 obj_class = Class.forName(cache_name);
73             } catch (ClassNotFoundException JavaDoc e) {
74                 throw new CommandException("Class not found - " + cache_name,e);
75             }
76
77             if (isCache(obj_class) == true) {
78                 try {
79                   Method getInstanceMethod = obj_class.getMethod("getInstance",
80                                                                  null);
81
82                   AbstractCache cache = (AbstractCache) getInstanceMethod.invoke(
83                                                   null, null);
84
85                   
86                     cache.clearCache();
87                   
88                 } catch (NoSuchMethodException JavaDoc e) {
89                   throw new CommandExecutionException("Didn't find 'getInstance' for " + cache_name);
90                 } catch (SecurityException JavaDoc e) {
91                   throw new CommandExecutionException("Not allowed to access 'getInstance' for " + cache_name);
92                 } catch (IllegalArgumentException JavaDoc e) {
93                     throw new CommandExecutionException("InvocationTargetException: Not allowed to access 'getInstance' for " + cache_name);
94                 } catch (IllegalAccessException JavaDoc e) {
95                     throw new CommandExecutionException("InvocationTargetException: Not allowed to access 'getInstance' for " + cache_name);
96                 } catch (InvocationTargetException e) {
97                     throw new CommandExecutionException("InvocationTargetException: Not allowed to access 'getInstance' for " + cache_name);
98                 }
99             } else {
100
101                 String JavaDoc sClassname = cache_name;
102                 CacheHandler chandler = CacheHandler.getInstance(getDataStoreInteface());
103                 
104                 if (sClassname != null) {
105                     try {
106                         AbstractCache cache = chandler.getCache(sClassname);
107                         cache.clearCache();
108                     } catch (Exception JavaDoc e) {
109                         m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
110                     }
111                 }
112
113             }
114         } else {
115             throw new CommandExecutionException("Didn't clear cache for " + cache_name);
116         }
117
118         return null;
119     }
120
121     /* (non-Javadoc)
122      * @see org.openharmonise.rm.commands.AbstractCmd#getName()
123      */

124     public String JavaDoc getName() {
125         return "ClearCache";
126     }
127
128     /* (non-Javadoc)
129      * @see org.openharmonise.rm.commands.AbstractCmd#isValidCommandObject(java.lang.Object)
130      */

131     public boolean isValidCommandObject(Object JavaDoc obj) {
132         //this command doesn't refer to the command object
133
return true;
134     }
135     
136     /**
137      * Returns <code>true</code> if the specified class is a sub
138      * class of <code>AbstractCache</code>.
139      *
140      * @param clss the class
141      * @return <code>true</code> if the specified class is a sub
142      * class of <code>AbstractCache</code>
143      */

144     private boolean isCache(Class JavaDoc clss) {
145         return AbstractCache.class.isAssignableFrom(clss);
146     }
147     
148 }
Popular Tags