001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.processor.cache;
018
019 import net.sf.ehcache.CacheManager;
020 import net.sf.ehcache.Ehcache;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 public class CacheValidate {
026 private static final transient Log LOG = LogFactory.getLog(CacheValidate.class);
027
028 public boolean isValid(CacheManager cacheManager, String cacheName, String key) {
029 if (LOG.isTraceEnabled()) {
030 LOG.trace("Cache Name: " + cacheName);
031 }
032
033 if (!cacheManager.cacheExists(cacheName)) {
034 if (LOG.isDebugEnabled()) {
035 LOG.debug("No existing Cache found with name: " + cacheName
036 + ". Please ensure a cache is first instantiated using a Cache Consumer or Cache Producer."
037 + " Replacement will not be performed since the cache " + cacheName + "does not presently exist");
038 }
039 return false;
040 }
041
042 if (LOG.isDebugEnabled()) {
043 LOG.debug("Found an existing cache: " + cacheName);
044 }
045
046 if (LOG.isTraceEnabled()) {
047 LOG.trace("Cache " + cacheName + " currently contains " + cacheManager.getCache(cacheName).getSize() + " elements");
048 }
049 Ehcache cache = cacheManager.getCache(cacheName);
050
051 if (!cache.isKeyInCache(key)) {
052 if (LOG.isDebugEnabled()) {
053 LOG.debug("No Key with name: " + key
054 + "presently exists in the cache. It is also possible that the key may have expired in the cache."
055 + " Replacement will not be performed until an appropriate key/value pair is added to (or) found in the cache.");
056 }
057 return false;
058 }
059
060 return true;
061 }
062
063 }