View Javadoc

1   /**
2    *  Copyright 2003-2006 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 52 2006-04-24 14:50:03Z 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       * Allows {@link BeanHandler} to add the CacheManagerEventListener to the configuration.
61       */
62      public final void addCacheManagerEventListenerFactory(FactoryConfiguration
63              cacheManagerEventListenerFactoryConfiguration) throws ObjectExistsException {
64          if (this.cacheManagerEventListenerFactoryConfiguration == null) {
65              this.cacheManagerEventListenerFactoryConfiguration = cacheManagerEventListenerFactoryConfiguration;
66          }
67      }
68  
69      /**
70       * Adds a CachePeerProviderFactoryConfiguration.
71       */
72      public final void addCacheManagerPeerProviderFactory(FactoryConfiguration factory) {
73          if (cacheManagerPeerProviderFactoryConfiguration == null) {
74              cacheManagerPeerProviderFactoryConfiguration = factory;
75          }
76      }
77  
78      /**
79       * Adds a CachePeerProviderFactoryConfiguration.
80       * cachePeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
81       * properties="hostName=localhost, port=5000"
82       */
83      public final void addCacheManagerPeerListenerFactory(FactoryConfiguration factory) {
84          if (cacheManagerPeerListenerFactoryConfiguration == null) {
85              cacheManagerPeerListenerFactoryConfiguration = factory;
86          }
87      }
88  
89  
90      /**
91       * Allows {@link BeanHandler} to add a default configuration to the configuration.
92       */
93      public final void addDefaultCache(CacheConfiguration defaultCacheConfiguration) throws ObjectExistsException {
94          if (this.defaultCacheConfiguration != null) {
95              throw new ObjectExistsException("The Default Cache has already been configured");
96          }
97          this.defaultCacheConfiguration = defaultCacheConfiguration;
98      }
99  
100     /**
101      * Allows {@link BeanHandler} to add Cache Configurations to the configuration.
102      */
103     public final void addCache(CacheConfiguration cacheConfiguration) throws ObjectExistsException {
104         if (cacheConfigurations.get(cacheConfiguration.name) != null) {
105             throw new ObjectExistsException("Cannot create cache: " + cacheConfiguration.name
106                     + " with the same name as an existing one.");
107         }
108         if (cacheConfiguration.name.equalsIgnoreCase(net.sf.ehcache.Cache.DEFAULT_CACHE_NAME)) {
109             throw new ObjectExistsException("The Default Cache has already been configured");
110         }
111 
112         cacheConfigurations.put(cacheConfiguration.name, cacheConfiguration);
113     }
114 
115     /**
116      * Gets a Map of cacheConfigurations.
117      */
118     public final Set getCacheConfigurationsKeySet() {
119         return cacheConfigurations.keySet();
120     }
121 
122     /**
123      * @return the configuration's default cache configuration
124      */
125     public final CacheConfiguration getDefaultCacheConfiguration() {
126         return defaultCacheConfiguration;
127     }
128 
129     /**
130      *
131      * @param defaultCacheConfiguration
132      */
133     public final void setDefaultCacheConfiguration(CacheConfiguration defaultCacheConfiguration) {
134         this.defaultCacheConfiguration = defaultCacheConfiguration;
135     }
136 
137 
138     /**
139      * Gets the disk store configuration.
140      */
141     public final DiskStoreConfiguration getDiskStoreConfiguration() {
142         return diskStoreConfiguration;
143     }
144 
145     /**
146      * Gets the CacheManagerPeerProvider factory configuration.
147      */
148     public final FactoryConfiguration getCacheManagerPeerProviderFactoryConfiguration() {
149         return cacheManagerPeerProviderFactoryConfiguration;
150     }
151 
152     /**
153      * Gets the CacheManagerPeerListener factory configuration.
154      */
155     public final FactoryConfiguration getCacheManagerPeerListenerFactoryConfiguration() {
156         return cacheManagerPeerListenerFactoryConfiguration;
157     }
158 
159     /**
160      * Gets the CacheManagerEventListener factory configuration.
161      */
162     public final FactoryConfiguration getCacheManagerEventListenerFactoryConfiguration() {
163         return cacheManagerEventListenerFactoryConfiguration;
164     }
165 
166     /**
167      * Gets a Map of cache configurations, keyed by name.
168      */
169     public final Map getCacheConfigurations() {
170         return cacheConfigurations;
171     }
172 
173     /**
174      * Sets the configuration source.
175      * @param configurationSource  an informative description of the source, preferably
176      * including the resource name and location.
177      */
178     public final void setSource(String configurationSource) {
179         this.configurationSource = configurationSource;
180     }
181 
182     /**
183      * Gets a description of the source from which this configuration was created.
184      */
185     public final String getConfigurationSource() {
186         return configurationSource;
187     }
188 }