001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
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: */package net.sf.ehcache.config;
016:
017: import net.sf.ehcache.ObjectExistsException;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Set;
022:
023: /**
024: * A bean, used by BeanUtils, to set configuration from an XML configuration file.
025: * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
026: * @version $Id: Configuration.java 519 2007-07-27 07:11:45Z gregluck $
027: */
028: public final class Configuration {
029:
030: private DiskStoreConfiguration diskStoreConfiguration;
031: private CacheConfiguration defaultCacheConfiguration;
032: private FactoryConfiguration cacheManagerPeerProviderFactoryConfiguration;
033: private FactoryConfiguration cacheManagerPeerListenerFactoryConfiguration;
034: private FactoryConfiguration cacheManagerEventListenerFactoryConfiguration;
035: private final Map cacheConfigurations = new HashMap();
036: private String configurationSource;
037:
038: /**
039: * Empty constructor, which is used by {@link ConfigurationFactory}, and can be also sued programmatically.
040: * <p/>
041: * If you are using it programmtically you need to call the relevant add and setter methods in this class to
042: * populate everything.
043: */
044: public Configuration() {
045: }
046:
047: /**
048: * Allows {@link BeanHandler} to add disk store location to the configuration.
049: */
050: public final void addDiskStore(
051: DiskStoreConfiguration diskStoreConfigurationParameter)
052: throws ObjectExistsException {
053: if (diskStoreConfiguration != null) {
054: throw new ObjectExistsException(
055: "The Disk Store has already been configured");
056: }
057: diskStoreConfiguration = diskStoreConfigurationParameter;
058: }
059:
060: /**
061: * Allows {@link BeanHandler} to add the CacheManagerEventListener to the configuration.
062: */
063: public final void addCacheManagerEventListenerFactory(
064: FactoryConfiguration cacheManagerEventListenerFactoryConfiguration)
065: throws ObjectExistsException {
066: if (this .cacheManagerEventListenerFactoryConfiguration == null) {
067: this .cacheManagerEventListenerFactoryConfiguration = cacheManagerEventListenerFactoryConfiguration;
068: }
069: }
070:
071: /**
072: * Adds a CachePeerProviderFactoryConfiguration.
073: */
074: public final void addCacheManagerPeerProviderFactory(
075: FactoryConfiguration factory) {
076: if (cacheManagerPeerProviderFactoryConfiguration == null) {
077: cacheManagerPeerProviderFactoryConfiguration = factory;
078: }
079: }
080:
081: /**
082: * Adds a CachePeerProviderFactoryConfiguration.
083: * cachePeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
084: * properties="hostName=localhost, port=5000"
085: */
086: public final void addCacheManagerPeerListenerFactory(
087: FactoryConfiguration factory) {
088: if (cacheManagerPeerListenerFactoryConfiguration == null) {
089: cacheManagerPeerListenerFactoryConfiguration = factory;
090: }
091: }
092:
093: /**
094: * Allows {@link BeanHandler} to add a default configuration to the configuration.
095: */
096: public final void addDefaultCache(
097: CacheConfiguration defaultCacheConfiguration)
098: throws ObjectExistsException {
099: if (this .defaultCacheConfiguration != null) {
100: throw new ObjectExistsException(
101: "The Default Cache has already been configured");
102: }
103: this .defaultCacheConfiguration = defaultCacheConfiguration;
104: }
105:
106: /**
107: * Allows {@link BeanHandler} to add Cache Configurations to the configuration.
108: */
109: public final void addCache(CacheConfiguration cacheConfiguration)
110: throws ObjectExistsException {
111: if (cacheConfigurations.get(cacheConfiguration.name) != null) {
112: throw new ObjectExistsException("Cannot create cache: "
113: + cacheConfiguration.name
114: + " with the same name as an existing one.");
115: }
116: if (cacheConfiguration.name
117: .equalsIgnoreCase(net.sf.ehcache.Cache.DEFAULT_CACHE_NAME)) {
118: throw new ObjectExistsException(
119: "The Default Cache has already been configured");
120: }
121:
122: cacheConfigurations.put(cacheConfiguration.name,
123: cacheConfiguration);
124: }
125:
126: /**
127: * Gets a Map of cacheConfigurations.
128: */
129: public final Set getCacheConfigurationsKeySet() {
130: return cacheConfigurations.keySet();
131: }
132:
133: /**
134: * @return the configuration's default cache configuration
135: */
136: public final CacheConfiguration getDefaultCacheConfiguration() {
137: return defaultCacheConfiguration;
138: }
139:
140: /**
141: *
142: * @param defaultCacheConfiguration
143: */
144: public final void setDefaultCacheConfiguration(
145: CacheConfiguration defaultCacheConfiguration) {
146: this .defaultCacheConfiguration = defaultCacheConfiguration;
147: }
148:
149: /**
150: * Gets the disk store configuration.
151: */
152: public final DiskStoreConfiguration getDiskStoreConfiguration() {
153: return diskStoreConfiguration;
154: }
155:
156: /**
157: * Gets the CacheManagerPeerProvider factory configuration.
158: */
159: public final FactoryConfiguration getCacheManagerPeerProviderFactoryConfiguration() {
160: return cacheManagerPeerProviderFactoryConfiguration;
161: }
162:
163: /**
164: * Gets the CacheManagerPeerListener factory configuration.
165: */
166: public final FactoryConfiguration getCacheManagerPeerListenerFactoryConfiguration() {
167: return cacheManagerPeerListenerFactoryConfiguration;
168: }
169:
170: /**
171: * Gets the CacheManagerEventListener factory configuration.
172: */
173: public final FactoryConfiguration getCacheManagerEventListenerFactoryConfiguration() {
174: return cacheManagerEventListenerFactoryConfiguration;
175: }
176:
177: /**
178: * Gets a Map of cache configurations, keyed by name.
179: */
180: public final Map getCacheConfigurations() {
181: return cacheConfigurations;
182: }
183:
184: /**
185: * Sets the configuration source.
186: * @param configurationSource an informative description of the source, preferably
187: * including the resource name and location.
188: */
189: public final void setSource(String configurationSource) {
190: this .configurationSource = configurationSource;
191: }
192:
193: /**
194: * Gets a description of the source from which this configuration was created.
195: */
196: public final String getConfigurationSource() {
197: return configurationSource;
198: }
199: }
|