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