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.Cache;
020    import net.sf.ehcache.CacheManager;
021    import net.sf.ehcache.Ehcache;
022    
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.Processor;
025    import org.apache.camel.component.cache.factory.CacheManagerFactory;
026    import org.apache.camel.impl.DefaultConsumer;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    public class CacheConsumer extends DefaultConsumer {
031    
032        private static final transient Log LOG = LogFactory.getLog(CacheConsumer.class);
033        CacheEndpoint endpoint;
034        CacheConfiguration config;
035        Ehcache cache;
036        CacheManager cacheManager;
037        
038        public CacheConsumer(Endpoint endpoint, Processor processor, CacheConfiguration config) {
039            super(endpoint, processor);
040            this.endpoint = (CacheEndpoint) endpoint;
041            this.config = config;
042        }
043    
044        @Override
045        protected void doStart() throws Exception {
046            super.doStart();
047            createConsumerCacheConnection();
048        }
049    
050        @Override
051        protected void doStop() throws Exception {
052            removeConsumerCacheConnection();
053            super.doStop();
054        }
055    
056        @Override
057        public CacheEndpoint getEndpoint() {
058            return endpoint;
059        }
060        
061        protected void createConsumerCacheConnection() {
062            cacheManager = new CacheManagerFactory().instantiateCacheManager();
063            CacheEventListener cacheEventListener = new CacheEventListenerFactory().createCacheEventListener(null);
064            cacheEventListener.setCacheConsumer(this);
065    
066            if (cacheManager.cacheExists(config.getCacheName())) {
067                cache = cacheManager.getCache(config.getCacheName());
068                cache.getCacheEventNotificationService().registerListener(cacheEventListener);
069            } else {
070                cache = new Cache(config.getCacheName(), 
071                        config.getMaxElementsInMemory(),
072                        config.getMemoryStoreEvictionPolicy(), 
073                        config.isOverflowToDisk(), 
074                        config.getDiskStorePath(), 
075                        config.isEternal(), 
076                        config.getTimeToLiveSeconds(), 
077                        config.getTimeToIdleSeconds(), 
078                        config.isDiskPersistent(), 
079                        config.getDiskExpiryThreadIntervalSeconds(), 
080                        null);
081                cache.getCacheEventNotificationService().registerListener(cacheEventListener);
082                cacheManager.addCache(cache);
083    
084                if (LOG.isDebugEnabled()) {
085                    LOG.debug("Added a new cache: " + cache.getName());
086                }
087            }
088        }
089        
090        protected void removeConsumerCacheConnection() {
091            cacheManager.removeCache(config.getCacheName());
092            if (cacheManager.getCacheNames().length == 0) {
093                cacheManager.shutdown();
094            }
095        }
096    
097    }