View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3    * use this file except in compliance with the License. You may obtain a copy of
4    * the License at
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package org.esigate.cache;
16  
17  import java.util.Properties;
18  
19  import net.sf.ehcache.CacheManager;
20  import net.sf.ehcache.Ehcache;
21  
22  import org.apache.http.impl.client.cache.CacheConfig;
23  import org.apache.http.impl.client.cache.ehcache.EhcacheHttpCacheStorage;
24  import org.esigate.Parameters;
25  
26  public class EhcacheCacheStorage extends CacheStorage {
27      public static final String DEFAULT_CACHE_NAME = "EsiGate";
28  
29      @Override
30      public void init(Properties properties) {
31          String cacheName = Parameters.EHCACHE_CACHE_NAME_PROPERTY.getValue(properties);
32          String configurationFileName = Parameters.EHCACHE_CONFIGURATION_FILE_PROPERTY.getValue(properties);
33          // Loaded from the Classpath, default will use /ehcache.xml or if not found /ehcache-failsafe.xml
34          CacheManager cacheManager = CacheManager.create(configurationFileName);
35          Ehcache ehcache = cacheManager.getEhcache(cacheName);
36          if (ehcache == null) {
37              cacheManager.addCache(cacheName);
38              ehcache = cacheManager.getEhcache(cacheName);
39          }
40          CacheConfig cacheConfig = CacheConfigHelper.createCacheConfig(properties);
41          setImpl(new EhcacheHttpCacheStorage(ehcache, cacheConfig));
42      }
43  
44  }