KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > cache > ehcache > EhCacheManagerFactoryBean


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.cache.ehcache;
18
19 import java.io.IOException JavaDoc;
20
21 import net.sf.ehcache.CacheException;
22 import net.sf.ehcache.CacheManager;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.beans.factory.DisposableBean;
27 import org.springframework.beans.factory.FactoryBean;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.io.Resource;
30
31 /**
32  * FactoryBean that exposes an EHCache {@link net.sf.ehcache.CacheManager} instance
33  * (independent or shared), configured from a specified config location.
34  *
35  * <p>If no config location is specified, a CacheManager will be configured from
36  * "ehcache.xml" in the root of the class path (that is, default EHCache initialization
37  * - as defined in the EHCache docs - will apply).
38  *
39  * <p>Setting up a separate EhCacheManagerFactoryBean is also advisable when using
40  * EhCacheFactoryBean, as it provides a (by default) independent CacheManager instance
41  * and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is
42  * also necessary for loading EHCache configuration from a non-default config location.
43  *
44  * <p>Note: As of Spring 2.0, this FactoryBean will by default create an independent
45  * CacheManager instance, which requires EHCache 1.2 or higher. Set the "shared"
46  * flag to "true" to create a CacheManager instance that is shared at the VM level
47  * (which is also compatible with EHCache 1.1).
48  *
49  * @author Dmitriy Kopylenko
50  * @author Juergen Hoeller
51  * @since 1.1.1
52  * @see #setConfigLocation
53  * @see #setShared
54  * @see EhCacheFactoryBean
55  * @see net.sf.ehcache.CacheManager
56  */

57 public class EhCacheManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
58
59     protected final Log logger = LogFactory.getLog(getClass());
60
61     private Resource configLocation;
62
63     private boolean shared = false;
64
65     private CacheManager cacheManager;
66
67
68     /**
69      * Set the location of the EHCache config file. A typical value is "/WEB-INF/ehcache.xml".
70      * <p>Default is "ehcache.xml" in the root of the class path, or if not found,
71      * "ehcache-failsafe.xml" in the EHCache jar (default EHCache initialization).
72      */

73     public void setConfigLocation(Resource configLocation) {
74         this.configLocation = configLocation;
75     }
76
77     /**
78      * Set whether the EHCache CacheManager should be shared (as a singleton at the VM level)
79      * or independent (typically local within the application). Default is "false", creating
80      * an independent instance.
81      * <p>Note that independent CacheManager instances are only available on EHCache 1.2 and
82      * higher. Switch this flag to "true" if you intend to run against an EHCache 1.1 jar.
83      */

84     public void setShared(boolean shared) {
85         this.shared = shared;
86     }
87
88
89     public void afterPropertiesSet() throws IOException JavaDoc, CacheException {
90         logger.info("Initializing EHCache CacheManager");
91         if (this.shared) {
92             // Shared CacheManager singleton at the VM level.
93
if (this.configLocation != null) {
94                 this.cacheManager = CacheManager.create(this.configLocation.getInputStream());
95             }
96             else {
97                 this.cacheManager = CacheManager.create();
98             }
99         }
100         else {
101             // Independent CacheManager instance (the default).
102
if (this.configLocation != null) {
103                 this.cacheManager = new CacheManager(this.configLocation.getInputStream());
104             }
105             else {
106                 this.cacheManager = new CacheManager();
107             }
108         }
109     }
110
111
112     public Object JavaDoc getObject() {
113         return this.cacheManager;
114     }
115
116     public Class JavaDoc getObjectType() {
117         return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class);
118     }
119
120     public boolean isSingleton() {
121         return true;
122     }
123
124
125     public void destroy() {
126         logger.info("Shutting down EHCache CacheManager");
127         this.cacheManager.shutdown();
128     }
129
130 }
131
Popular Tags