001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.rm.commands;
021:
022: import java.lang.reflect.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.commons.cache.*;
026: import org.openharmonise.rm.factory.*;
027: import org.openharmonise.rm.search.*;
028:
029: /**
030: * Clears the cache specified in the parameters of the command
031: *
032: * @author Michael Bell
033: * @version $Revision: 1.3 $
034: *
035: */
036: public class CmdClearCache extends AbstractCmd {
037: private static String PARAM_CACHE = "cache";
038:
039: /**
040: * Logger for this class
041: */
042: private static final Logger m_logger = Logger
043: .getLogger(CmdClearCache.class.getName());
044:
045: /**
046: * Creates a new instance of the command.
047: *
048: */
049: public CmdClearCache() {
050: super ();
051: }
052:
053: /* (non-Javadoc)
054: * @see org.openharmonise.rm.commands.AbstractCmd#execute()
055: */
056: public Object execute(Context context) throws CommandException {
057: String cache_name = this .getParameter(PARAM_CACHE);
058:
059: if (m_logger.isLoggable(Level.FINE)) {
060: m_logger.logp(Level.FINE, this .getClass().getName(),
061: "execute", "Clearing cache:" + cache_name);
062: }
063:
064: if (cache_name.equalsIgnoreCase("resultset")) {
065: try {
066: SearchResultsCache.getInstance().clearCache();
067: } catch (CacheException e) {
068: throw new CommandExecutionException("Cache exception",
069: e);
070: }
071: } else if (cache_name.indexOf(".") > 0) {
072: Class obj_class;
073: try {
074: obj_class = Class.forName(cache_name);
075: } catch (ClassNotFoundException e) {
076: throw new CommandException("Class not found - "
077: + cache_name, e);
078: }
079:
080: if (isCache(obj_class) == true) {
081: try {
082: Method getInstanceMethod = obj_class.getMethod(
083: "getInstance", null);
084:
085: AbstractCache cache = (AbstractCache) getInstanceMethod
086: .invoke(null, null);
087:
088: cache.clearCache();
089:
090: } catch (NoSuchMethodException e) {
091: throw new CommandExecutionException(
092: "Didn't find 'getInstance' for "
093: + cache_name);
094: } catch (SecurityException e) {
095: throw new CommandExecutionException(
096: "Not allowed to access 'getInstance' for "
097: + cache_name);
098: } catch (IllegalArgumentException e) {
099: throw new CommandExecutionException(
100: "InvocationTargetException: Not allowed to access 'getInstance' for "
101: + cache_name);
102: } catch (IllegalAccessException e) {
103: throw new CommandExecutionException(
104: "InvocationTargetException: Not allowed to access 'getInstance' for "
105: + cache_name);
106: } catch (InvocationTargetException e) {
107: throw new CommandExecutionException(
108: "InvocationTargetException: Not allowed to access 'getInstance' for "
109: + cache_name);
110: }
111: } else {
112:
113: String sClassname = cache_name;
114: CacheHandler chandler = CacheHandler
115: .getInstance(getDataStoreInteface());
116:
117: if (sClassname != null) {
118: try {
119: AbstractCache cache = chandler
120: .getCache(sClassname);
121: cache.clearCache();
122: } catch (Exception e) {
123: m_logger.log(Level.WARNING, e
124: .getLocalizedMessage(), e);
125: }
126: }
127:
128: }
129: } else {
130: throw new CommandExecutionException(
131: "Didn't clear cache for " + cache_name);
132: }
133:
134: return null;
135: }
136:
137: /* (non-Javadoc)
138: * @see org.openharmonise.rm.commands.AbstractCmd#getName()
139: */
140: public String getName() {
141: return "ClearCache";
142: }
143:
144: /* (non-Javadoc)
145: * @see org.openharmonise.rm.commands.AbstractCmd#isValidCommandObject(java.lang.Object)
146: */
147: public boolean isValidCommandObject(Object obj) {
148: //this command doesn't refer to the command object
149: return true;
150: }
151:
152: /**
153: * Returns <code>true</code> if the specified class is a sub
154: * class of <code>AbstractCache</code>.
155: *
156: * @param clss the class
157: * @return <code>true</code> if the specified class is a sub
158: * class of <code>AbstractCache</code>
159: */
160: private boolean isCache(Class clss) {
161: return AbstractCache.class.isAssignableFrom(clss);
162: }
163:
164: }
|