001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.cache.ehcache;
018:
019: import java.io.IOException;
020:
021: import net.sf.ehcache.CacheException;
022: import net.sf.ehcache.CacheManager;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import org.springframework.beans.factory.DisposableBean;
027: import org.springframework.beans.factory.FactoryBean;
028: import org.springframework.beans.factory.InitializingBean;
029: import org.springframework.core.io.Resource;
030:
031: /**
032: * FactoryBean that exposes an EHCache {@link net.sf.ehcache.CacheManager} instance
033: * (independent or shared), configured from a specified config location.
034: *
035: * <p>If no config location is specified, a CacheManager will be configured from
036: * "ehcache.xml" in the root of the class path (that is, default EHCache initialization
037: * - as defined in the EHCache docs - will apply).
038: *
039: * <p>Setting up a separate EhCacheManagerFactoryBean is also advisable when using
040: * EhCacheFactoryBean, as it provides a (by default) independent CacheManager instance
041: * and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is
042: * also necessary for loading EHCache configuration from a non-default config location.
043: *
044: * <p>Note: As of Spring 2.0, this FactoryBean will by default create an independent
045: * CacheManager instance, which requires EHCache 1.2 or higher. Set the "shared"
046: * flag to "true" to create a CacheManager instance that is shared at the VM level
047: * (which is also compatible with EHCache 1.1).
048: *
049: * @author Dmitriy Kopylenko
050: * @author Juergen Hoeller
051: * @since 1.1.1
052: * @see #setConfigLocation
053: * @see #setShared
054: * @see EhCacheFactoryBean
055: * @see net.sf.ehcache.CacheManager
056: */
057: public class EhCacheManagerFactoryBean implements FactoryBean,
058: InitializingBean, DisposableBean {
059:
060: protected final Log logger = LogFactory.getLog(getClass());
061:
062: private Resource configLocation;
063:
064: private boolean shared = false;
065:
066: private CacheManager cacheManager;
067:
068: /**
069: * Set the location of the EHCache config file. A typical value is "/WEB-INF/ehcache.xml".
070: * <p>Default is "ehcache.xml" in the root of the class path, or if not found,
071: * "ehcache-failsafe.xml" in the EHCache jar (default EHCache initialization).
072: */
073: public void setConfigLocation(Resource configLocation) {
074: this .configLocation = configLocation;
075: }
076:
077: /**
078: * Set whether the EHCache CacheManager should be shared (as a singleton at the VM level)
079: * or independent (typically local within the application). Default is "false", creating
080: * an independent instance.
081: * <p>Note that independent CacheManager instances are only available on EHCache 1.2 and
082: * higher. Switch this flag to "true" if you intend to run against an EHCache 1.1 jar.
083: */
084: public void setShared(boolean shared) {
085: this .shared = shared;
086: }
087:
088: public void afterPropertiesSet() throws IOException, CacheException {
089: logger.info("Initializing EHCache CacheManager");
090: if (this .shared) {
091: // Shared CacheManager singleton at the VM level.
092: if (this .configLocation != null) {
093: this .cacheManager = CacheManager
094: .create(this .configLocation.getInputStream());
095: } else {
096: this .cacheManager = CacheManager.create();
097: }
098: } else {
099: // Independent CacheManager instance (the default).
100: if (this .configLocation != null) {
101: this .cacheManager = new CacheManager(
102: this .configLocation.getInputStream());
103: } else {
104: this .cacheManager = new CacheManager();
105: }
106: }
107: }
108:
109: public Object getObject() {
110: return this .cacheManager;
111: }
112:
113: public Class getObjectType() {
114: return (this .cacheManager != null ? this .cacheManager
115: .getClass() : CacheManager.class);
116: }
117:
118: public boolean isSingleton() {
119: return true;
120: }
121:
122: public void destroy() {
123: logger.info("Shutting down EHCache CacheManager");
124: this.cacheManager.shutdown();
125: }
126:
127: }
|