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 java.net.URI;
020    import java.util.Map;
021    
022    import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
023    
024    import org.apache.camel.util.URISupport;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    public class CacheConfiguration {
029        private String cacheName;
030        private int maxElementsInMemory = 1000;
031        private MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU;
032        private boolean overflowToDisk = true;
033        private String diskStorePath;
034        private boolean eternal;
035        private long timeToLiveSeconds = 300;
036        private long timeToIdleSeconds = 300;
037        private boolean diskPersistent;
038        private long diskExpiryThreadIntervalSeconds;
039    
040        public CacheConfiguration() {
041        }
042    
043        public CacheConfiguration(URI uri) throws Exception {
044            parseURI(uri);
045        }
046        
047        public void parseURI(URI uri) throws Exception {
048            String protocol = uri.getScheme();
049            
050            if (!protocol.equalsIgnoreCase("cache")) {
051                throw new IllegalArgumentException("Unrecognized Cache protocol: " + protocol + " for uri: " + uri);
052            }
053            
054            setCacheName(uri.getHost());
055            
056            Map cacheSettings = URISupport.parseParameters(uri);
057            if (cacheSettings.containsKey("maxElementsInMemory")) {
058                setMaxElementsInMemory(Integer.valueOf((String) cacheSettings.get("maxElementsInMemory")).intValue());
059            }
060            if (cacheSettings.containsKey("overflowToDisk")) {
061                setOverflowToDisk(Boolean.valueOf((String) cacheSettings.get("overflowToDisk")));
062            }
063            if (cacheSettings.containsKey("diskStorePath")) {
064                setDiskStorePath((String)cacheSettings.get("diskStorePath"));
065            }
066            if (cacheSettings.containsKey("eternal")) {
067                setEternal(Boolean.valueOf((String) cacheSettings.get("eternal")));
068            }
069            if (cacheSettings.containsKey("timeToLiveSeconds")) {
070                setTimeToLiveSeconds(Long.valueOf((String) cacheSettings.get("timeToLiveSeconds")).longValue());
071            }
072            if (cacheSettings.containsKey("timeToIdleSeconds")) {
073                setTimeToIdleSeconds(Long.valueOf((String) cacheSettings.get("timeToIdleSeconds")).longValue());
074            }
075            if (cacheSettings.containsKey("diskPersistent")) {
076                setDiskPersistent(Boolean.valueOf((String) cacheSettings.get("diskPersistent")));
077            }
078            if (cacheSettings.containsKey("diskExpiryThreadIntervalSeconds")) {
079                setDiskExpiryThreadIntervalSeconds(Long.valueOf((String) cacheSettings.get("diskExpiryThreadIntervalSeconds")).longValue());
080            }
081            
082        }
083        
084        public String getCacheName() {
085            return cacheName;
086        }
087    
088        public void setCacheName(String cacheName) {
089            this.cacheName = cacheName;
090        }
091    
092        public int getMaxElementsInMemory() {
093            return maxElementsInMemory;
094        }
095    
096        public void setMaxElementsInMemory(int maxElementsInMemory) {
097            this.maxElementsInMemory = maxElementsInMemory;
098        }
099    
100        public MemoryStoreEvictionPolicy getMemoryStoreEvictionPolicy() {
101            return memoryStoreEvictionPolicy;
102        }
103    
104        public void setMemoryStoreEvictionPolicy(
105                MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
106            this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
107        }
108    
109        public boolean isOverflowToDisk() {
110            return overflowToDisk;
111        }
112    
113        public void setOverflowToDisk(boolean overflowToDisk) {
114            this.overflowToDisk = overflowToDisk;
115        }
116    
117        public String getDiskStorePath() {
118            return diskStorePath;
119        }
120    
121        public void setDiskStorePath(String diskStorePath) {
122            this.diskStorePath = diskStorePath;
123        }
124    
125        public boolean isEternal() {
126            return eternal;
127        }
128    
129        public void setEternal(boolean eternal) {
130            this.eternal = eternal;
131        }
132    
133        public long getTimeToLiveSeconds() {
134            return timeToLiveSeconds;
135        }
136    
137        public void setTimeToLiveSeconds(long timeToLiveSeconds) {
138            this.timeToLiveSeconds = timeToLiveSeconds;
139        }
140    
141        public long getTimeToIdleSeconds() {
142            return timeToIdleSeconds;
143        }
144    
145        public void setTimeToIdleSeconds(long timeToIdleSeconds) {
146            this.timeToIdleSeconds = timeToIdleSeconds;
147        }
148    
149        public boolean isDiskPersistent() {
150            return diskPersistent;
151        }
152    
153        public void setDiskPersistent(boolean diskPersistent) {
154            this.diskPersistent = diskPersistent;
155        }
156    
157        public long getDiskExpiryThreadIntervalSeconds() {
158            return diskExpiryThreadIntervalSeconds;
159        }
160    
161        public void setDiskExpiryThreadIntervalSeconds(long diskExpiryThreadIntervalSeconds) {
162            this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
163        }
164       
165    }