View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical.ehcache;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import net.sf.ehcache.CacheException;
28  import net.sf.ehcache.CacheManager;
29  import net.sf.ehcache.config.Configuration;
30  import net.sf.ehcache.config.ConfigurationFactory;
31  import net.sf.ehcache.config.DiskStoreConfiguration;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.springframework.beans.factory.DisposableBean;
35  import org.springframework.beans.factory.FactoryBean;
36  import org.springframework.beans.factory.InitializingBean;
37  import org.springframework.core.io.Resource;
38  
39  import java.io.File;
40  import java.io.IOException;
41  
42  /**
43   * This class as been writing using this post :
44   * http://stackoverflow.com/questions/1533409/spring-context-property-placholder-ehcahe-configuration
45   * Its aims to manage the EhCache diskstore path in a config file.
46   * <p/>
47   * <p/>
48   * {@link org.springframework.beans.factory.FactoryBean} that exposes an EHCache {@link net.sf.ehcache.CacheManager}
49   * instance (independent or shared), configured from a specified config location.
50   * <p/>
51   * <p>If no config location is specified, a CacheManager will be configured from
52   * "ehcache.xml" in the root of the class path (that is, default EHCache initialization
53   * - as defined in the EHCache docs - will apply).
54   * <p/>
55   * <p>Setting up a separate EhCacheManagerFactoryBean is also advisable when using
56   * EhCacheFactoryBean, as it provides a (by default) independent CacheManager instance
57   * and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is
58   * also necessary for loading EHCache configuration from a non-default config location.
59   * <p/>
60   * <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
61   *
62   * @author Dmitriy Kopylenko
63   * @author Juergen Hoeller
64   * @author Benoit Lavenier
65   * @author Tony CHEMIT
66   * @see #setConfigLocation
67   * @see #setShared
68   * @see CacheManager
69  
70   * @since 3.1.2
71   */
72  public class EhCacheManagerFactoryBean implements FactoryBean<CacheManager>, InitializingBean, DisposableBean {
73      protected final Log logger = LogFactory.getLog(getClass());
74  
75      private String diskStorePath = null;
76  
77      private Resource configLocation;
78  
79      private boolean shared = false;
80  
81      private String cacheManagerName;
82  
83      private CacheManager cacheManager;
84  
85      /**
86       * <p>Getter for the field <code>diskStorePath</code>.</p>
87       *
88       * @return a {@link java.lang.String} object.
89       */
90      public String getDiskStorePath() {
91          return diskStorePath;
92      }
93  
94      /**
95       * <p>Setter for the field <code>diskStorePath</code>.</p>
96       *
97       * @param diskStorePath a {@link java.lang.String} object.
98       */
99      public void setDiskStorePath(String diskStorePath) {
100         this.diskStorePath = diskStorePath;
101     }
102 
103     /**
104      * Set the location of the EHCache config file. A typical value is "/WEB-INF/ehcache.xml".
105      * <p>Default is "ehcache.xml" in the root of the class path, or if not found,
106      * "ehcache-failsafe.xml" in the EHCache jar (default EHCache initialization).
107      *
108      * @see CacheManager#create(java.io.InputStream)
109      * @see CacheManager#CacheManager(java.io.InputStream)
110      * @param configLocation a {@link org.springframework.core.io.Resource} object.
111      */
112     public void setConfigLocation(Resource configLocation) {
113         this.configLocation = configLocation;
114     }
115 
116     /**
117      * Set whether the EHCache CacheManager should be shared (as a singleton at the VM level)
118      * or independent (typically local within the application). Default is "false", creating
119      * an independent instance.
120      *
121      * @see CacheManager#create()
122      * @see CacheManager#CacheManager()
123      * @param shared a boolean.
124      */
125     public void setShared(boolean shared) {
126         this.shared = shared;
127     }
128 
129     /**
130      * Set the name of the EHCache CacheManager (if a specific name is desired).
131      *
132      * @see CacheManager#setName(String)
133      * @param cacheManagerName a {@link java.lang.String} object.
134      */
135     public void setCacheManagerName(String cacheManagerName) {
136         this.cacheManagerName = cacheManagerName;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public void afterPropertiesSet() throws IOException, CacheException {
142 
143         Configuration config;
144 
145         if (this.configLocation != null) {
146             config = ConfigurationFactory.parseConfiguration(this.configLocation.getInputStream());
147         } else {
148             config = ConfigurationFactory.parseConfiguration();
149         }
150 
151         DiskStoreConfiguration diskStoreConfiguration = config.getDiskStoreConfiguration();
152         boolean noDiskStoreConfiguration = diskStoreConfiguration == null;
153         if (noDiskStoreConfiguration) {
154             diskStoreConfiguration = new DiskStoreConfiguration();
155         }
156         
157         
158         File diskStore = new File(getDiskStorePath());
159         if (!diskStore.isDirectory() || !diskStore.exists()) {
160 	        logger.info("Using EHCache disk store at " + getDiskStorePath() + " - directory not exists yet");
161         }
162         else if (logger.isDebugEnabled()) {
163 	        logger.debug("Using EHCache disk store at " + getDiskStorePath() + " (replace old config: " + diskStoreConfiguration.getOriginalPath()
164                     + ")");	
165         }
166         
167         diskStoreConfiguration.setPath(getDiskStorePath());
168         if (noDiskStoreConfiguration) {
169             config.addDiskStore(diskStoreConfiguration);
170         }
171         if (this.cacheManagerName != null) {
172             config.setName(this.cacheManagerName);
173         }
174         if (this.shared) {
175             this.cacheManager = CacheManager.create(config);
176         } else {
177             this.cacheManager = new CacheManager(config);
178         }
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public CacheManager getObject() {
184         return this.cacheManager;
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public Class<? extends CacheManager> getObjectType() {
190         return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class);
191     }
192 
193     /** {@inheritDoc} */
194     @Override
195     public boolean isSingleton() {
196         return true;
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public void destroy() {
202         logger.debug("Shutting down EHCache CacheManager");
203         this.cacheManager.shutdown();
204     }
205 }