View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.jcache;
17  
18  import net.sf.ehcache.Ehcache;
19  import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
20  import net.sf.ehcache.util.ClassLoaderUtil;
21  import net.sf.ehcache.util.PropertyUtil;
22  import net.sf.jsr107cache.Cache;
23  import net.sf.jsr107cache.CacheException;
24  import net.sf.jsr107cache.CacheFactory;
25  import net.sf.jsr107cache.CacheLoader;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import java.util.Map;
30  
31  /**
32   * A CacheFactory implementation for JCache.
33   *
34   * This factory uses ehcache in singleton CacheManager mode i.e. one per classloader.
35   *
36   * @author Greg Luck
37   * @version $Id: JCacheFactory.java 512 2007-07-10 09:18:45Z gregluck $
38   */
39  public class JCacheFactory implements CacheFactory {
40  
41  
42      private static final Log LOG = LogFactory.getLog(JCacheFactory.class.getName());
43  
44      /**
45       * Creates a new implementation specific Cache object using the environment parameters.
46       *
47       * The created cache is not accessible from the JCache CacheManager until it has been registered with the manager.
48       *
49       * Create caches are registered with a singleton ehcache CacheManager.
50       *
51       * @param environment String values for the following properties:
52       *            String name,
53       *            int maxElementsInMemory,
54       *            MemoryStoreEvictionPolicy memoryStoreEvictionPolicy (one of LFU, LRU or FIFO)
55       *            boolean overflowToDisk,
56       *            boolean eternal,
57       *            long timeToLiveSeconds,
58       *            long timeToIdleSeconds,
59       *            boolean diskPersistent,
60       *            long diskExpiryThreadIntervalSeconds,
61       *            int maxElementsOnDisk,
62       *            String cacheLoaderFactoryClassName
63       *
64       *
65       * Note that the following cannot be set using this factory method:
66       * <ol>
67       * <li>diskStorePath - this is set on the CacheManager and ignored here
68       * <li>RegisteredEventListeners - register any of these after cache creation
69       * <li>BootstrapCacheLoader - not supported here
70       * </ol>
71       * If you need this functionality create a JCache by decorating an Ehcache and put the
72       * resulting JCache in the manager.
73       * 
74       * @return a newly created JCache registered in the singleton CacheManager
75       * @throws CacheException
76       */
77      public Cache createCache(Map environment) throws CacheException {
78  
79  
80          CacheLoader cacheLoader = null;
81          Ehcache cache = null;
82          try {
83              String name = PropertyUtil.extractAndLogProperty("name", environment);
84  
85              String maxElementsInMemoryString = PropertyUtil.extractAndLogProperty("maxElementsInMemory", environment);
86              int maxElementsInMemory = Integer.parseInt(maxElementsInMemoryString);
87  
88              String memoryStoreEvictionPolicyString = PropertyUtil.extractAndLogProperty("memoryStoreEvictionPolicy", environment);
89              MemoryStoreEvictionPolicy memoryStoreEvictionPolicy =
90                      MemoryStoreEvictionPolicy.fromString(memoryStoreEvictionPolicyString);
91  
92              String overflowToDiskString = PropertyUtil.extractAndLogProperty("overflowToDisk", environment);
93              boolean overflowToDisk = PropertyUtil.parseBoolean(overflowToDiskString);
94  
95              String eternalString = PropertyUtil.extractAndLogProperty("eternal", environment);
96              boolean eternal = PropertyUtil.parseBoolean(eternalString);
97  
98              String timeToLiveSecondsString = PropertyUtil.extractAndLogProperty("timeToLiveSeconds", environment);
99              long timeToLiveSeconds = Long.parseLong(timeToLiveSecondsString);
100 
101             String timeToIdleSecondsString = PropertyUtil.extractAndLogProperty("timeToIdleSeconds", environment);
102             long timeToIdleSeconds = Long.parseLong(timeToIdleSecondsString);
103 
104             String diskPersistentString = PropertyUtil.extractAndLogProperty("diskPersistentSeconds", environment);
105             boolean diskPersistent = PropertyUtil.parseBoolean(diskPersistentString);
106 
107             long diskExpiryThreadIntervalSeconds = 0;
108             String diskExpiryThreadIntervalSecondsString =
109                     PropertyUtil.extractAndLogProperty("diskExpiryThreadIntervalSeconds", environment);
110             if (diskExpiryThreadIntervalSecondsString != null) {
111                 diskExpiryThreadIntervalSeconds = Long.parseLong(diskExpiryThreadIntervalSecondsString);
112             }
113 
114             int maxElementsOnDisk = 0;
115             String maxElementsOnDiskString =
116                             PropertyUtil.extractAndLogProperty("maxElementsOnDisk", environment);
117             if (maxElementsOnDiskString != null) {
118                 maxElementsOnDisk = Integer.parseInt(maxElementsOnDiskString);
119             }
120 
121             cacheLoader = null;
122             String cacheLoaderFactoryClassName =
123                             PropertyUtil.extractAndLogProperty("cacheLoaderFactoryClassName", environment);
124             if (cacheLoaderFactoryClassName == null) {
125                 LOG.debug("cacheLoaderFactoryClassName not configured. Skipping...");
126             } else {
127                 CacheLoaderFactory factory = (CacheLoaderFactory)
128                         ClassLoaderUtil.createNewInstance(cacheLoaderFactoryClassName);
129                 cacheLoader = factory.createCacheLoader(environment);
130             }
131 
132 
133             cache = new net.sf.ehcache.Cache(name, maxElementsInMemory, memoryStoreEvictionPolicy,
134                     overflowToDisk, null, eternal,
135                     timeToLiveSeconds, timeToIdleSeconds, diskPersistent, diskExpiryThreadIntervalSeconds,
136                     null, null, maxElementsOnDisk);
137 
138             net.sf.ehcache.CacheManager.getInstance().addCache(cache);
139         } catch (net.sf.ehcache.CacheException e) {
140             throw new CacheException(e.getMessage(), e);
141         }
142 
143 
144         return new JCache(cache, cacheLoader);
145 
146     }
147 }