View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache.ehcache;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.cms.util.MBeanUtil;
38  import info.magnolia.module.cache.Cache;
39  import info.magnolia.module.cache.CacheFactory;
40  
41  import java.util.Arrays;
42  import java.util.List;
43  
44  import javax.management.MBeanServer;
45  
46  import info.magnolia.module.cache.mbean.CacheMonitor;
47  import net.sf.ehcache.CacheManager;
48  import net.sf.ehcache.Ehcache;
49  import net.sf.ehcache.config.CacheConfiguration;
50  import net.sf.ehcache.config.Configuration;
51  import net.sf.ehcache.config.ConfigurationFactory;
52  import net.sf.ehcache.constructs.blocking.BlockingCache;
53  import net.sf.ehcache.management.ManagementService;
54  
55  /**
56   * A CacheFactory based on ehcache, which wraps BlockingCache instances.
57   *
58   * @author gjoseph
59   * @version $Revision: $ ($Author: $)
60   */
61  public class EhCacheFactory implements CacheFactory {
62      private final CacheMonitor cacheMonitor;
63  
64      private CacheManager cacheManager;
65      private CacheConfiguration defaultCacheConfiguration;
66      private String diskStorePath;
67      // 0 == do not timeout ever, set to 10s by default.
68      private int blockingTimeout = 10000;
69  
70      public EhCacheFactory(CacheMonitor cacheMonitor) {
71          this.cacheMonitor = cacheMonitor;
72          diskStorePath = Path.getCacheDirectoryPath();
73      }
74  
75      public CacheConfiguration getDefaultCacheConfiguration() {
76          return defaultCacheConfiguration;
77      }
78  
79      public void setDefaultCacheConfiguration(CacheConfiguration defaultCacheConfiguration) {
80          this.defaultCacheConfiguration = defaultCacheConfiguration;
81      }
82  
83      public String getDiskStorePath() {
84          return diskStorePath;
85      }
86  
87      public void setDiskStorePath(String diskStorePath) {
88          this.diskStorePath = diskStorePath;
89      }
90  
91      public int getBlockingTimeout() {
92          return blockingTimeout;
93      }
94  
95      public void setBlockingTimeout(int blockingTimeout) {
96          this.blockingTimeout = blockingTimeout;
97      }
98  
99      public List getCacheNames() {
100         return Arrays.asList(cacheManager.getCacheNames());
101     }
102 
103     @Override
104     public Cache getCache(String name) {
105         final Ehcache ehcache = cacheManager.getEhcache(name);
106         if (ehcache == null) {
107             createCache(name);
108             return getCache(name);
109         }
110         return new EhCacheWrapper(ehcache, cacheMonitor, name);
111     }
112 
113     protected void createCache(String name) {
114         synchronized (this.getClass()) {
115             cacheManager.addCache(name);
116 
117             final Ehcache cache = cacheManager.getEhcache(name);
118             // default EHCache is non blocking. Since our impl always requires blocking cache wrap it here.
119             final BlockingCache newBlockingCache = new BlockingCache(cache);
120             newBlockingCache.setTimeoutMillis(getBlockingTimeout());
121             cacheManager.replaceCacheWithDecoratedCache(cache, newBlockingCache);
122         }
123     }
124 
125     @Override
126     public void start() {
127         final Configuration cfg = ConfigurationFactory.parseConfiguration();
128         cfg.setSource("ehcache defaults");
129         if (defaultCacheConfiguration != null) {
130             cfg.setDefaultCacheConfiguration(defaultCacheConfiguration);
131             cfg.setSource(cfg.getConfigurationSource() + " + Magnolia-based defaultCacheConfiguration");
132         }
133         if (diskStorePath != null) {
134             cfg.getDiskStoreConfiguration().setPath(diskStorePath);
135             cfg.setSource(cfg.getConfigurationSource() + " + Magnolia-based diskStorePath");
136         }
137         cacheManager = new CacheManager(cfg);
138 
139         // TODO cacheManager.setName(...magnolia instance name ...);
140 
141         final MBeanServer mBeanServer = MBeanUtil.getMBeanServer();
142         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);
143     }
144 
145     @Override
146     public void stop() {
147         cacheManager.shutdown();
148     }
149 
150     public CacheManager getWrappedCacheManager() {
151         return cacheManager;
152     }
153 }