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.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    public class CacheEventListener implements net.sf.ehcache.event.CacheEventListener {
028    
029        private static final transient Logger LOG = LoggerFactory.getLogger(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            LOG.debug("Element {} is being evicted from cache {}", element, cache.getName());
043        }
044    
045        public void notifyElementExpired(Ehcache cache, Element element) {
046            LOG.debug("Element {} has expired in cache {}", element, cache.getName());
047        }
048    
049        public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
050            LOG.debug("Element {} has just been added/put in cache {}", element, cache.getName());
051            dispatchExchange(cache, element, "ADD");
052        }
053    
054        public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
055            LOG.debug("Element {} has just been removed from cache {}", element, cache.getName());
056            dispatchExchange(cache, element, "DELETE");        
057        }
058    
059        public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
060            LOG.debug("Element {} has just been updated in cache {}", element, cache.getName());
061            dispatchExchange(cache, element, "UPDATE");            
062        }
063    
064        public void notifyRemoveAll(Ehcache cache) {
065            LOG.debug("Cache {} is being emptied and all elements removed", cache.getName());
066            dispatchExchange(cache, null, "DELETEALL");
067        }
068    
069        private void dispatchExchange(Ehcache cache, Element element, String operation) {
070            Exchange exchange;
071            LOG.debug("Consumer Dispatching the Exchange containing the Element {} in cache {}", element, cache.getName());
072            
073            if (element == null) {
074                exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, "", "");
075            } else {
076                exchange = cacheConsumer.getEndpoint().createCacheExchange(operation, (String) element.getObjectKey(), element.getObjectValue());
077            }
078            try {
079                cacheConsumer.getProcessor().process(exchange);
080            } catch (Exception e) {
081                throw new CacheException("Error in consumer while dispatching exchange containing key "
082                    + (element != null ? element.getObjectKey() : null) + " for further processing", e);
083            }
084        }
085        
086        public CacheConsumer getCacheConsumer() {
087            return cacheConsumer;
088        }
089    
090        public void setCacheConsumer(CacheConsumer cacheConsumer) {
091            this.cacheConsumer = cacheConsumer;
092        }
093    
094        public void dispose() {
095            // noop
096        }
097    
098        public Object clone() throws CloneNotSupportedException {
099            return super.clone();
100        }
101        
102    }