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.RuntimeCamelException;
025 import org.apache.camel.util.URISupport;
026
027 public class CacheConfiguration implements Cloneable {
028 private String cacheName;
029 private int maxElementsInMemory = 1000;
030 private MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU;
031 private boolean overflowToDisk = true;
032 private String diskStorePath;
033 private boolean eternal;
034 private long timeToLiveSeconds = 300;
035 private long timeToIdleSeconds = 300;
036 private boolean diskPersistent;
037 private long diskExpiryThreadIntervalSeconds;
038 private CacheEventListenerRegistry eventListenerRegistry = new CacheEventListenerRegistry();
039 private CacheLoaderRegistry cacheLoaderRegistry = new CacheLoaderRegistry();
040
041 public CacheConfiguration() {
042 }
043
044 public CacheConfiguration(URI uri) throws Exception {
045 parseURI(uri);
046 }
047
048 public CacheConfiguration copy() {
049 try {
050 CacheConfiguration copy = (CacheConfiguration) clone();
051 // override any properties where a reference copy isn't what we want
052 return copy;
053 } catch (CloneNotSupportedException e) {
054 throw new RuntimeCamelException(e);
055 }
056 }
057
058 public void parseURI(URI uri) throws Exception {
059 String protocol = uri.getScheme();
060
061 if (!protocol.equalsIgnoreCase("cache")) {
062 throw new IllegalArgumentException("Unrecognized Cache protocol: " + protocol + " for uri: " + uri);
063 }
064
065 setCacheName(uri.getHost());
066
067 Map cacheSettings = URISupport.parseParameters(uri);
068 if (cacheSettings.containsKey("maxElementsInMemory")) {
069 setMaxElementsInMemory(Integer.valueOf((String) cacheSettings.get("maxElementsInMemory")).intValue());
070 }
071 if (cacheSettings.containsKey("overflowToDisk")) {
072 setOverflowToDisk(Boolean.valueOf((String) cacheSettings.get("overflowToDisk")));
073 }
074 if (cacheSettings.containsKey("diskStorePath")) {
075 setDiskStorePath((String)cacheSettings.get("diskStorePath"));
076 }
077 if (cacheSettings.containsKey("eternal")) {
078 setEternal(Boolean.valueOf((String) cacheSettings.get("eternal")));
079 }
080 if (cacheSettings.containsKey("timeToLiveSeconds")) {
081 setTimeToLiveSeconds(Long.valueOf((String) cacheSettings.get("timeToLiveSeconds")).longValue());
082 }
083 if (cacheSettings.containsKey("timeToIdleSeconds")) {
084 setTimeToIdleSeconds(Long.valueOf((String) cacheSettings.get("timeToIdleSeconds")).longValue());
085 }
086 if (cacheSettings.containsKey("diskPersistent")) {
087 setDiskPersistent(Boolean.valueOf((String) cacheSettings.get("diskPersistent")));
088 }
089 if (cacheSettings.containsKey("diskExpiryThreadIntervalSeconds")) {
090 setDiskExpiryThreadIntervalSeconds(Long.valueOf((String) cacheSettings.get("diskExpiryThreadIntervalSeconds")).longValue());
091 }
092 if (cacheSettings.containsKey("memoryStoreEvictionPolicy")) {
093 String policy = (String) cacheSettings.get("memoryStoreEvictionPolicy");
094 // remove leading if any given as fromString uses LRU, LFU or FIFO
095 policy = policy.replace("MemoryStoreEvictionPolicy.", "");
096 setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.fromString(policy));
097 }
098 }
099
100 public String getCacheName() {
101 return cacheName;
102 }
103
104 public void setCacheName(String cacheName) {
105 this.cacheName = cacheName;
106 }
107
108 public int getMaxElementsInMemory() {
109 return maxElementsInMemory;
110 }
111
112 public void setMaxElementsInMemory(int maxElementsInMemory) {
113 this.maxElementsInMemory = maxElementsInMemory;
114 }
115
116 public MemoryStoreEvictionPolicy getMemoryStoreEvictionPolicy() {
117 return memoryStoreEvictionPolicy;
118 }
119
120 public void setMemoryStoreEvictionPolicy(
121 MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
122 this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
123 }
124
125 public boolean isOverflowToDisk() {
126 return overflowToDisk;
127 }
128
129 public void setOverflowToDisk(boolean overflowToDisk) {
130 this.overflowToDisk = overflowToDisk;
131 }
132
133 public String getDiskStorePath() {
134 return diskStorePath;
135 }
136
137 public void setDiskStorePath(String diskStorePath) {
138 this.diskStorePath = diskStorePath;
139 }
140
141 public boolean isEternal() {
142 return eternal;
143 }
144
145 public void setEternal(boolean eternal) {
146 this.eternal = eternal;
147 }
148
149 public long getTimeToLiveSeconds() {
150 return timeToLiveSeconds;
151 }
152
153 public void setTimeToLiveSeconds(long timeToLiveSeconds) {
154 this.timeToLiveSeconds = timeToLiveSeconds;
155 }
156
157 public long getTimeToIdleSeconds() {
158 return timeToIdleSeconds;
159 }
160
161 public void setTimeToIdleSeconds(long timeToIdleSeconds) {
162 this.timeToIdleSeconds = timeToIdleSeconds;
163 }
164
165 public boolean isDiskPersistent() {
166 return diskPersistent;
167 }
168
169 public void setDiskPersistent(boolean diskPersistent) {
170 this.diskPersistent = diskPersistent;
171 }
172
173 public long getDiskExpiryThreadIntervalSeconds() {
174 return diskExpiryThreadIntervalSeconds;
175 }
176
177 public void setDiskExpiryThreadIntervalSeconds(long diskExpiryThreadIntervalSeconds) {
178 this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
179 }
180
181 public void setEventListenerRegistry(CacheEventListenerRegistry eventListenerRegistry) {
182 this.eventListenerRegistry = eventListenerRegistry;
183 }
184
185 public CacheEventListenerRegistry getEventListenerRegistry() {
186 return eventListenerRegistry;
187 }
188
189 public void setCacheLoaderRegistry(CacheLoaderRegistry cacheLoaderRegistry) {
190 this.cacheLoaderRegistry = cacheLoaderRegistry;
191 }
192
193 public CacheLoaderRegistry getCacheLoaderRegistry() {
194 return cacheLoaderRegistry;
195 }
196
197 }