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