|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 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; |
| 20 | +import java.io.InputStream; |
| 21 | + |
| 22 | +import net.sf.ehcache.CacheException; |
| 23 | +import net.sf.ehcache.CacheManager; |
| 24 | +import net.sf.ehcache.config.Configuration; |
| 25 | +import net.sf.ehcache.config.ConfigurationFactory; |
| 26 | + |
| 27 | +import org.springframework.core.io.Resource; |
| 28 | + |
| 29 | +/** |
| 30 | + * Convenient builder methods for EhCache 2.5+ {@link CacheManager} setup, |
| 31 | + * providing easy programmatic bootstrapping from a Spring-provided resource. |
| 32 | + * This is primarily intended for use within {@code @Bean} methods in a |
| 33 | + * Spring configuration class. |
| 34 | + * |
| 35 | + * <p>These methods are a simple alternative to custom {@link CacheManager} setup |
| 36 | + * code. For any advanced purposes, consider using {@link #parseConfiguration}, |
| 37 | + * customizing the configuration object, and then calling the |
| 38 | + * {@link CacheManager#CacheManager(Configuration)} constructor. |
| 39 | + * |
| 40 | + * @author Juergen Hoeller |
| 41 | + * @since 4.1 |
| 42 | + */ |
| 43 | +public abstract class EhCacheManagerUtils { |
| 44 | + |
| 45 | + /** |
| 46 | + * Build an EhCache {@link CacheManager} from the default configuration. |
| 47 | + * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path |
| 48 | + * (that is, default EhCache initialization - as defined in the EhCache docs - will apply). |
| 49 | + * If no configuration file can be found, a fail-safe fallback configuration will be used. |
| 50 | + * @return the new EhCache CacheManager |
| 51 | + * @throws CacheException in case of configuration parsing failure |
| 52 | + */ |
| 53 | + public static CacheManager buildCacheManager() throws CacheException { |
| 54 | + return new CacheManager(ConfigurationFactory.parseConfiguration()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Build an EhCache {@link CacheManager} from the default configuration. |
| 59 | + * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path |
| 60 | + * (that is, default EhCache initialization - as defined in the EhCache docs - will apply). |
| 61 | + * If no configuration file can be found, a fail-safe fallback configuration will be used. |
| 62 | + * @param name the desired name of the cache manager |
| 63 | + * @return the new EhCache CacheManager |
| 64 | + * @throws CacheException in case of configuration parsing failure |
| 65 | + */ |
| 66 | + public static CacheManager buildCacheManager(String name) throws CacheException { |
| 67 | + Configuration configuration = ConfigurationFactory.parseConfiguration(); |
| 68 | + configuration.setName(name); |
| 69 | + return new CacheManager(configuration); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Build an EhCache {@link CacheManager} from the given configuration resource. |
| 74 | + * @param configLocation the location of the configuration file (as a Spring resource) |
| 75 | + * @return the new EhCache CacheManager |
| 76 | + * @throws CacheException in case of configuration parsing failure |
| 77 | + */ |
| 78 | + public static CacheManager buildCacheManager(Resource configLocation) throws CacheException { |
| 79 | + return new CacheManager(parseConfiguration(configLocation)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Build an EhCache {@link CacheManager} from the given configuration resource. |
| 84 | + * @param name the desired name of the cache manager |
| 85 | + * @param configLocation the location of the configuration file (as a Spring resource) |
| 86 | + * @return the new EhCache CacheManager |
| 87 | + * @throws CacheException in case of configuration parsing failure |
| 88 | + */ |
| 89 | + public static CacheManager buildCacheManager(String name, Resource configLocation) throws CacheException { |
| 90 | + Configuration configuration = parseConfiguration(configLocation); |
| 91 | + configuration.setName(name); |
| 92 | + return new CacheManager(configuration); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Parse EhCache configuration from the given resource, for further use with |
| 97 | + * custom {@link CacheManager} creation. |
| 98 | + * @param configLocation the location of the configuration file (as a Spring resource) |
| 99 | + * @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 | +} |
0 commit comments