View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
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 net.sf.ehcache.config;
18  
19  import net.sf.ehcache.ObjectExistsException;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
24  
25  /**
26   * A bean, used by BeanUtils, to set configuration from an XML configuration file.
27   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
28   * @version $Id: Configuration.java 512 2007-07-10 09:18:45Z gregluck $
29   */
30  public final class Configuration {
31  
32      private DiskStoreConfiguration diskStoreConfiguration;
33      private CacheConfiguration defaultCacheConfiguration;
34      private FactoryConfiguration cacheManagerPeerProviderFactoryConfiguration;
35      private FactoryConfiguration cacheManagerPeerListenerFactoryConfiguration;
36      private FactoryConfiguration cacheManagerEventListenerFactoryConfiguration;
37      private final Map cacheConfigurations = new HashMap();
38      private String configurationSource;
39  
40      /**
41       * Empty constructor, which is used by {@link ConfigurationFactory}, and can be also sued programmatically.
42       * <p/>
43       * If you are using it programmtically you need to call the relevant add and setter methods in this class to
44       * populate everything.
45       */
46      public Configuration() { }
47  
48  
49      /**
50       * Allows {@link BeanHandler} to add disk store location to the configuration.
51       */
52      public final void addDiskStore(DiskStoreConfiguration diskStoreConfigurationParameter) throws ObjectExistsException {
53          if (diskStoreConfiguration != null) {
54              throw new ObjectExistsException("The Disk Store has already been configured");
55          }
56          diskStoreConfiguration = diskStoreConfigurationParameter;
57      }
58  
59      
60  
61      /**
62       * Allows {@link BeanHandler} to add the CacheManagerEventListener to the configuration.
63       */
64      public final void addCacheManagerEventListenerFactory(FactoryConfiguration
65              cacheManagerEventListenerFactoryConfiguration) throws ObjectExistsException {
66          if (this.cacheManagerEventListenerFactoryConfiguration == null) {
67              this.cacheManagerEventListenerFactoryConfiguration = cacheManagerEventListenerFactoryConfiguration;
68          }
69      }
70  
71  
72  
73      /**
74       * Adds a CachePeerProviderFactoryConfiguration.
75       */
76      public final void addCacheManagerPeerProviderFactory(FactoryConfiguration factory) {
77          if (cacheManagerPeerProviderFactoryConfiguration == null) {
78              cacheManagerPeerProviderFactoryConfiguration = factory;
79          }
80      }
81  
82      /**
83       * Adds a CachePeerProviderFactoryConfiguration.
84       * cachePeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
85       * properties="hostName=localhost, port=5000"
86       */
87      public final void addCacheManagerPeerListenerFactory(FactoryConfiguration factory) {
88          if (cacheManagerPeerListenerFactoryConfiguration == null) {
89              cacheManagerPeerListenerFactoryConfiguration = factory;
90          }
91      }
92  
93  
94      /**
95       * Allows {@link BeanHandler} to add a default configuration to the configuration.
96       */
97      public final void addDefaultCache(CacheConfiguration defaultCacheConfiguration) throws ObjectExistsException {
98          if (this.defaultCacheConfiguration != null) {
99              throw new ObjectExistsException("The Default Cache has already been configured");
100         }
101         this.defaultCacheConfiguration = defaultCacheConfiguration;
102     }
103 
104     /**
105      * Allows {@link BeanHandler} to add Cache Configurations to the configuration.
106      */
107     public final void addCache(CacheConfiguration cacheConfiguration) throws ObjectExistsException {
108         if (cacheConfigurations.get(cacheConfiguration.name) != null) {
109             throw new ObjectExistsException("Cannot create cache: " + cacheConfiguration.name
110                     + " with the same name as an existing one.");
111         }
112         if (cacheConfiguration.name.equalsIgnoreCase(net.sf.ehcache.Cache.DEFAULT_CACHE_NAME)) {
113             throw new ObjectExistsException("The Default Cache has already been configured");
114         }
115 
116         cacheConfigurations.put(cacheConfiguration.name, cacheConfiguration);
117     }
118 
119     /**
120      * Gets a Map of cacheConfigurations.
121      */
122     public final Set getCacheConfigurationsKeySet() {
123         return cacheConfigurations.keySet();
124     }
125 
126     /**
127      * @return the configuration's default cache configuration
128      */
129     public final CacheConfiguration getDefaultCacheConfiguration() {
130         return defaultCacheConfiguration;
131     }
132 
133     /**
134      *
135      * @param defaultCacheConfiguration
136      */
137     public final void setDefaultCacheConfiguration(CacheConfiguration defaultCacheConfiguration) {
138         this.defaultCacheConfiguration = defaultCacheConfiguration;
139     }
140 
141 
142     /**
143      * Gets the disk store configuration.
144      */
145     public final DiskStoreConfiguration getDiskStoreConfiguration() {
146         return diskStoreConfiguration;
147     }
148 
149     /**
150      * Gets the CacheManagerPeerProvider factory configuration.
151      */
152     public final FactoryConfiguration getCacheManagerPeerProviderFactoryConfiguration() {
153         return cacheManagerPeerProviderFactoryConfiguration;
154     }
155 
156     /**
157      * Gets the CacheManagerPeerListener factory configuration.
158      */
159     public final FactoryConfiguration getCacheManagerPeerListenerFactoryConfiguration() {
160         return cacheManagerPeerListenerFactoryConfiguration;
161     }
162 
163     /**
164      * Gets the CacheManagerEventListener factory configuration.
165      */
166     public final FactoryConfiguration getCacheManagerEventListenerFactoryConfiguration() {
167         return cacheManagerEventListenerFactoryConfiguration;
168     }
169 
170     /**
171      * Gets a Map of cache configurations, keyed by name.
172      */
173     public final Map getCacheConfigurations() {
174         return cacheConfigurations;
175     }
176 
177     /**
178      * Sets the configuration source.
179      * @param configurationSource  an informative description of the source, preferably
180      * including the resource name and location.
181      */
182     public final void setSource(String configurationSource) {
183         this.configurationSource = configurationSource;
184     }
185 
186     /**
187      * Gets a description of the source from which this configuration was created.
188      */
189     public final String getConfigurationSource() {
190         return configurationSource;
191     }
192 }