001/*
002 * Copyright 2002-2014 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 *      https://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
017package org.springframework.cache.ehcache;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import net.sf.ehcache.CacheException;
023import net.sf.ehcache.CacheManager;
024import net.sf.ehcache.config.Configuration;
025import net.sf.ehcache.config.ConfigurationFactory;
026
027import org.springframework.core.io.Resource;
028
029/**
030 * Convenient builder methods for EhCache 2.5+ {@link CacheManager} setup,
031 * providing easy programmatic bootstrapping from a Spring-provided resource.
032 * This is primarily intended for use within {@code @Bean} methods in a
033 * Spring configuration class.
034 *
035 * <p>These methods are a simple alternative to custom {@link CacheManager} setup
036 * code. For any advanced purposes, consider using {@link #parseConfiguration},
037 * customizing the configuration object, and then calling the
038 * {@link CacheManager#CacheManager(Configuration)} constructor.
039 *
040 * @author Juergen Hoeller
041 * @since 4.1
042 */
043public abstract class EhCacheManagerUtils {
044
045        /**
046         * Build an EhCache {@link CacheManager} from the default configuration.
047         * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
048         * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
049         * If no configuration file can be found, a fail-safe fallback configuration will be used.
050         * @return the new EhCache CacheManager
051         * @throws CacheException in case of configuration parsing failure
052         */
053        public static CacheManager buildCacheManager() throws CacheException {
054                return new CacheManager(ConfigurationFactory.parseConfiguration());
055        }
056
057        /**
058         * Build an EhCache {@link CacheManager} from the default configuration.
059         * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
060         * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
061         * If no configuration file can be found, a fail-safe fallback configuration will be used.
062         * @param name the desired name of the cache manager
063         * @return the new EhCache CacheManager
064         * @throws CacheException in case of configuration parsing failure
065         */
066        public static CacheManager buildCacheManager(String name) throws CacheException {
067                Configuration configuration = ConfigurationFactory.parseConfiguration();
068                configuration.setName(name);
069                return new CacheManager(configuration);
070        }
071
072        /**
073         * Build an EhCache {@link CacheManager} from the given configuration resource.
074         * @param configLocation the location of the configuration file (as a Spring resource)
075         * @return the new EhCache CacheManager
076         * @throws CacheException in case of configuration parsing failure
077         */
078        public static CacheManager buildCacheManager(Resource configLocation) throws CacheException {
079                return new CacheManager(parseConfiguration(configLocation));
080        }
081
082        /**
083         * Build an EhCache {@link CacheManager} from the given configuration resource.
084         * @param name the desired name of the cache manager
085         * @param configLocation the location of the configuration file (as a Spring resource)
086         * @return the new EhCache CacheManager
087         * @throws CacheException in case of configuration parsing failure
088         */
089        public static CacheManager buildCacheManager(String name, Resource configLocation) throws CacheException {
090                Configuration configuration = parseConfiguration(configLocation);
091                configuration.setName(name);
092                return new CacheManager(configuration);
093        }
094
095        /**
096         * Parse EhCache configuration from the given resource, for further use with
097         * custom {@link CacheManager} creation.
098         * @param configLocation the location of the configuration file (as a Spring resource)
099         * @return the EhCache Configuration handle
100         * @throws CacheException in case of configuration parsing failure
101         * @see CacheManager#CacheManager(Configuration)
102         * @see CacheManager#create(Configuration)
103         */
104        public static Configuration parseConfiguration(Resource configLocation) throws CacheException {
105                InputStream is = null;
106                try {
107                        is = configLocation.getInputStream();
108                        return ConfigurationFactory.parseConfiguration(is);
109                }
110                catch (IOException ex) {
111                        throw new CacheException("Failed to parse EhCache configuration resource", ex);
112                }
113                finally {
114                        if (is != null) {
115                                try {
116                                        is.close();
117                                }
118                                catch (IOException ex) {
119                                        // ignore
120                                }
121                        }
122                }
123        }
124
125}