in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/admin/JCSAdminBean.java [326:382]
public void removeItem(final String cacheName, final String key) throws IOException
{
if (cacheName == null)
{
throw new IllegalArgumentException("The cache name specified was null.");
}
if (key == null)
{
throw new IllegalArgumentException("The key specified was null.");
}
if (RemoteCacheServerFactory.getRemoteCacheServer() == null)
{
// Not running in a remote cache server.
// Remove objects from the cache directly, as no need to broadcast removes to client machines...
cacheHub.getCache(cacheName).remove(key);
}
else
{
// Running in a remote cache server.
// Remove objects via the RemoteCacheServer API, so that removes will be broadcast to client machines...
try
{
Object keyToRemove = null;
final CompositeCache<?, ?> cache = CompositeCacheManager.getInstance().getCache(cacheName);
// A String key was supplied, but to remove elements via the RemoteCacheServer API, we need the
// actual key object as stored in the cache (i.e. a Serializable object). To find the key in this form,
// we iterate through all keys stored in the memory cache until we find one whose toString matches
// the string supplied...
final Set<?> allKeysInCache = cache.getMemoryCache().getKeySet();
for (final Object keyInCache : allKeysInCache)
{
if (keyInCache.toString().equals(key))
{
if (keyToRemove != null) {
// A key matching the one specified was already found...
throw new IllegalStateException("Unexpectedly found duplicate keys in the cache region matching the key specified.");
}
keyToRemove = keyInCache;
}
}
if (keyToRemove == null)
{
throw new IllegalStateException("No match for this key could be found in the set of keys retrieved from the memory cache.");
}
// At this point, we have retrieved the matching K key.
// Call remoteCacheServer.remove(String, Serializable)...
final RemoteCacheServer<Serializable, Serializable> remoteCacheServer = RemoteCacheServerFactory.getRemoteCacheServer();
remoteCacheServer.remove(cacheName, key);
}
catch (final Exception e)
{
throw new IllegalStateException("Failed to remove element with key [" + key + ", " + key.getClass() + "] from cache region [" + cacheName + "]: " + e, e);
}
}
}