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.component.cache;
018    
019    import net.sf.ehcache.CacheException;
020    import net.sf.ehcache.Ehcache;
021    import net.sf.ehcache.Element;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    public class CacheEventListener implements net.sf.ehcache.event.CacheEventListener {
028    
029        private static final transient Log LOG = LogFactory.getLog(CacheEventListener.class);
030        CacheConsumer cacheConsumer;
031    
032        public CacheEventListener() {
033            super();
034        }
035    
036        public CacheEventListener(CacheConsumer cacheConsumer) {
037            super();
038            this.cacheConsumer = cacheConsumer;
039        }
040    
041        public void notifyElementEvicted(Ehcache cache, Element element) {
042            if (LOG.isDebugEnabled()) {
043                LOG.debug("Element" + element.toString() + " is being evicted from cache " + cache.getName());
044            }
045        }
046    
047        public void notifyElementExpired(Ehcache cache, Element element) {
048            if (LOG.isDebugEnabled()) {
049                LOG.debug("Element" + element.toString() + " has expired in cache " + cache.getName());       
050            }
051        }
052    
053        public void notifyElementPut(Ehcache cache, Element element)
054            throws CacheException {
055            if (LOG.isDebugEnabled()) {
056                LOG.debug("Element" + element.toString() + " has just been added/put in cache " + cache.getName());
057            }
058            dispatchExchange(cache, element, "ADD");
059        }
060    
061        public void notifyElementRemoved(Ehcache cache, Element element)
062            throws CacheException {
063            
064            if (LOG.isDebugEnabled()) {
065                LOG.debug("Element" + element.toString() + " has just been removed from cache " + cache.getName());
066            }
067            dispatchExchange(cache, element, "DELETE");        
068        }
069    
070        public void notifyElementUpdated(Ehcache cache, Element element)
071            throws CacheException {
072            if (LOG.isDebugEnabled()) {
073                LOG.debug("Element" + element.toString() + " has just been updated in cache " + cache.getName());
074            }
075            dispatchExchange(cache, element, "UPDATE");            
076        }
077    
078        public void notifyRemoveAll(Ehcache cache) {
079            if (LOG.isDebugEnabled()) {
080                LOG.debug("Cache " + cache.getName() + " is being emptied and all elements removed");
081            }
082            dispatchExchange(cache, null, "DELETEALL");
083            
084        }
085    
086        private void dispatchExchange(Ehcache cache, Element element, String operation) {
087            Exchange exchange;
088            
089            if (LOG.isDebugEnabled()) {
090                LOG.debug("Consumer Dispatching the Exchange containing the Element " + element.toString() + " in cache " + cache.getName());
091            }
092            if (element == null) {
093                exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, "", "");
094            } else {
095                exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, (String) element.getObjectKey(), element.getObjectValue());
096            }
097            try {
098                cacheConsumer.getProcessor().process(exchange);
099            } catch (Exception e) {
100                throw new CacheException("Error in consumer while dispatching exchange containing key "
101                    + (element != null ? element.getObjectKey() : null) + " for further processing", e);
102            }
103        }
104        
105        public CacheConsumer getCacheConsumer() {
106            return cacheConsumer;
107        }
108    
109        public void setCacheConsumer(CacheConsumer cacheConsumer) {
110            this.cacheConsumer = cacheConsumer;
111        }
112    
113        public void dispose() {
114            // noop
115        }
116    
117        public Object clone() throws CloneNotSupportedException {
118            return super.clone();
119        }
120        
121    }