View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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.generator.model.elements;
18  
19  import net.sf.ehcache.config.CacheConfiguration;
20  import net.sf.ehcache.config.CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration;
21  import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
22  import net.sf.ehcache.config.CacheWriterConfiguration;
23  import net.sf.ehcache.config.CopyStrategyConfiguration;
24  import net.sf.ehcache.config.ElementValueComparatorConfiguration;
25  import net.sf.ehcache.config.PinningConfiguration;
26  import net.sf.ehcache.config.TerracottaConfiguration;
27  import net.sf.ehcache.config.generator.model.NodeElement;
28  import net.sf.ehcache.config.generator.model.SimpleNodeAttribute;
29  import net.sf.ehcache.config.generator.model.SimpleNodeElement;
30  import net.sf.ehcache.store.MemoryStoreEvictionPolicy.MemoryStoreEvictionPolicyEnum;
31  
32  /***
33   * Element representing the {@link CacheConfiguration}
34   *
35   * @author Abhishek Sanoujam
36   *
37   */
38  public class CacheConfigurationElement extends SimpleNodeElement {
39  
40      private final CacheConfiguration cacheConfiguration;
41  
42      /***
43       * Constructor accepting the parent and the {@link CacheConfiguration}
44       *
45       * @param parent
46       * @param cacheConfiguration
47       */
48      public CacheConfigurationElement(NodeElement parent, CacheConfiguration cacheConfiguration) {
49          super(parent, "cache");
50          this.cacheConfiguration = cacheConfiguration;
51          init();
52      }
53  
54      private void init() {
55          if (cacheConfiguration == null) {
56              return;
57          }
58          addAttribute(new SimpleNodeAttribute("name", cacheConfiguration.getName()).optional(false));
59          addCommonAttributesWithDefaultCache(this, cacheConfiguration);
60          addAttribute(new SimpleNodeAttribute("logging", cacheConfiguration.getLogging()).optional(true).defaultValue(
61                  CacheConfiguration.DEFAULT_LOGGING));
62  
63          addCommonChildElementsWithDefaultCache(this, cacheConfiguration);
64          addAttribute(new SimpleNodeAttribute("maxBytesLocalHeap", cacheConfiguration.getMaxBytesLocalHeap())
65                  .optional(true).defaultValue(String.valueOf(CacheConfiguration.DEFAULT_MAX_BYTES_ON_HEAP)));
66          addAttribute(new SimpleNodeAttribute("maxBytesLocalOffHeap", cacheConfiguration.getMaxBytesLocalOffHeap())
67                  .optional(true).defaultValue(String.valueOf(CacheConfiguration.DEFAULT_MAX_BYTES_OFF_HEAP)));
68          addAttribute(new SimpleNodeAttribute("maxBytesLocalDisk", cacheConfiguration.getMaxBytesLocalDisk())
69              .optional(true).defaultValue(String.valueOf(CacheConfiguration.DEFAULT_MAX_BYTES_ON_DISK)));
70      }
71  
72      /***
73       * Adds all attributes which are common with the "defaultCache" element in ehcache.xsd
74       *
75       * @param element
76       * @param cacheConfiguration
77       */
78      public static void addCommonAttributesWithDefaultCache(NodeElement element, CacheConfiguration cacheConfiguration) {
79          element.addAttribute(new SimpleNodeAttribute("eternal", cacheConfiguration.isEternal()).optional(false));
80          element.addAttribute(new SimpleNodeAttribute("maxElementsInMemory", cacheConfiguration.getMaxElementsInMemory()).optional(false));
81          element.addAttribute(new SimpleNodeAttribute("maxEntriesLocalHeap", cacheConfiguration.getMaxEntriesLocalHeap()).optional(false));
82          element.addAttribute(new SimpleNodeAttribute("overflowToDisk", cacheConfiguration.isOverflowToDisk()).optional(false));
83          element.addAttribute(new SimpleNodeAttribute("clearOnFlush", cacheConfiguration.isClearOnFlush()).optional(true).defaultValue(
84                  String.valueOf(CacheConfiguration.DEFAULT_CLEAR_ON_FLUSH)));
85          element.addAttribute(new SimpleNodeAttribute("diskAccessStripes", cacheConfiguration.getDiskAccessStripes()).optional(true)
86                  .defaultValue(CacheConfiguration.DEFAULT_DISK_ACCESS_STRIPES));
87          element.addAttribute(new SimpleNodeAttribute("diskPersistent", cacheConfiguration.isDiskPersistent()).optional(true).defaultValue(
88                  CacheConfiguration.DEFAULT_DISK_PERSISTENT));
89          element.addAttribute(new SimpleNodeAttribute("diskSpoolBufferSizeMB", cacheConfiguration.getDiskSpoolBufferSizeMB()).optional(true)
90                  .defaultValue(CacheConfiguration.DEFAULT_SPOOL_BUFFER_SIZE));
91          element
92                  .addAttribute(new SimpleNodeAttribute("diskExpiryThreadIntervalSeconds", cacheConfiguration
93                          .getDiskExpiryThreadIntervalSeconds()).optional(true).defaultValue(
94                          CacheConfiguration.DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS));
95          element.addAttribute(new SimpleNodeAttribute("copyOnWrite", cacheConfiguration.isCopyOnWrite()).optional(true).defaultValue(
96                  CacheConfiguration.DEFAULT_COPY_ON_WRITE));
97          element.addAttribute(new SimpleNodeAttribute("copyOnRead", cacheConfiguration.isCopyOnRead()).optional(true).defaultValue(
98                  CacheConfiguration.DEFAULT_COPY_ON_READ));
99          element.addAttribute(new SimpleNodeAttribute("timeToIdleSeconds", cacheConfiguration.getTimeToIdleSeconds()).optional(true)
100                 .defaultValue(CacheConfiguration.DEFAULT_TTI));
101         element.addAttribute(new SimpleNodeAttribute("timeToLiveSeconds", cacheConfiguration.getTimeToLiveSeconds()).optional(true)
102                 .defaultValue(CacheConfiguration.DEFAULT_TTL));
103         element.addAttribute(new SimpleNodeAttribute("maxElementsOnDisk", cacheConfiguration.getMaxElementsOnDisk()).optional(true)
104                 .defaultValue(CacheConfiguration.DEFAULT_MAX_ELEMENTS_ON_DISK));
105         element.addAttribute(new SimpleNodeAttribute("maxEntriesLocalDisk", cacheConfiguration.getMaxEntriesLocalDisk()).optional(true)
106                 .defaultValue(CacheConfiguration.DEFAULT_MAX_ELEMENTS_ON_DISK));
107         element.addAttribute(new SimpleNodeAttribute("maxMemoryOffHeap", cacheConfiguration.getMaxMemoryOffHeap()).optional(true)
108                 .defaultValue((String) null));
109         element.addAttribute(new SimpleNodeAttribute("overflowToOffHeap", cacheConfiguration.isOverflowToOffHeap()).optional(true)
110                 .defaultValue(false));
111         element.addAttribute(new SimpleNodeAttribute("cacheLoaderTimeoutMillis", cacheConfiguration.getCacheLoaderTimeoutMillis())
112                 .optional(true).defaultValue(0L));
113         element.addAttribute(new SimpleNodeAttribute("transactionalMode", cacheConfiguration.getTransactionalMode()).optional(true)
114                 .defaultValue(CacheConfiguration.DEFAULT_TRANSACTIONAL_MODE));
115         element.addAttribute(new SimpleNodeAttribute("statistics", cacheConfiguration.getStatistics()).optional(true).defaultValue(
116                 CacheConfiguration.DEFAULT_STATISTICS));
117         element.addAttribute(new SimpleNodeAttribute("memoryStoreEvictionPolicy", MemoryStoreEvictionPolicyEnum.valueOf(cacheConfiguration
118                 .getMemoryStoreEvictionPolicy().toString())).optional(true).defaultValue(
119                 CacheConfiguration.DEFAULT_MEMORY_STORE_EVICTION_POLICY.toString().toLowerCase()));
120     }
121 
122     /***
123      * Adds all common child elements with the "defaultCache" element in ehcache.xsd
124      *
125      * @param element
126      * @param cacheConfiguration
127      */
128     public static void addCommonChildElementsWithDefaultCache(NodeElement element, CacheConfiguration cacheConfiguration) {
129         for (FactoryConfigurationElement child : getAllFactoryElements(element, "cacheEventListenerFactory", cacheConfiguration
130                 .getCacheEventListenerConfigurations())) {
131             CacheEventListenerFactoryConfiguration factoryConfiguration = (CacheEventListenerFactoryConfiguration) child
132                     .getFactoryConfiguration();
133             child.addAttribute(new SimpleNodeAttribute("listenFor", factoryConfiguration.getListenFor()));
134             element.addChildElement(child);
135         }
136         addAllFactoryConfigsAsChildElements(element, "cacheExtensionFactory", cacheConfiguration.getCacheExtensionConfigurations());
137         addAllFactoryConfigsAsChildElements(element, "cacheLoaderFactory", cacheConfiguration.getCacheLoaderConfigurations());
138         addBootstrapCacheLoaderFactoryConfigurationElement(element, cacheConfiguration);
139         addCacheExceptionHandlerFactoryConfigurationElement(element, cacheConfiguration);
140         addCopyStrategyConfigurationElement(element, cacheConfiguration);
141         addElementValueComparatorConfigurationElement(element, cacheConfiguration);
142         addCacheWriterConfigurationElement(element, cacheConfiguration);
143         addAllFactoryConfigsAsChildElements(element, "cacheDecoratorFactory", cacheConfiguration.getCacheDecoratorConfigurations());
144         addTerracottaConfigurationElement(element, cacheConfiguration);
145         addPinningElement(element, cacheConfiguration);
146         addSearchElement(element, cacheConfiguration);
147     }
148 
149     private static void addBootstrapCacheLoaderFactoryConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
150         BootstrapCacheLoaderFactoryConfiguration bootstrapCacheLoaderFactoryConfiguration = cacheConfiguration
151                 .getBootstrapCacheLoaderFactoryConfiguration();
152         if (bootstrapCacheLoaderFactoryConfiguration != null) {
153             element.addChildElement(new FactoryConfigurationElement(element, "bootstrapCacheLoaderFactory",
154                     bootstrapCacheLoaderFactoryConfiguration));
155         }
156     }
157 
158     private static void addCacheExceptionHandlerFactoryConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
159         CacheConfiguration.CacheExceptionHandlerFactoryConfiguration cacheExceptionHandlerFactoryConfiguration = cacheConfiguration
160                 .getCacheExceptionHandlerFactoryConfiguration();
161         if (cacheExceptionHandlerFactoryConfiguration != null) {
162             element.addChildElement(new FactoryConfigurationElement(element, "cacheExceptionHandlerFactory",
163                     cacheExceptionHandlerFactoryConfiguration));
164         }
165     }
166 
167     private static void addCopyStrategyConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
168         CopyStrategyConfiguration copyStrategyConfiguration = cacheConfiguration.getCopyStrategyConfiguration();
169         if (copyStrategyConfiguration != null &&
170                 !copyStrategyConfiguration.equals(CacheConfiguration.DEFAULT_COPY_STRATEGY_CONFIGURATION)) {
171             element.addChildElement(new CopyStrategyConfigurationElement(element, copyStrategyConfiguration));
172         }
173     }
174 
175     private static void addElementValueComparatorConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
176         ElementValueComparatorConfiguration elementValueComparatorConfiguration = cacheConfiguration
177                 .getElementValueComparatorConfiguration();
178         if (elementValueComparatorConfiguration != null
179                 && !elementValueComparatorConfiguration.equals(CacheConfiguration.DEFAULT_ELEMENT_VALUE_COMPARATOR_CONFIGURATION)) {
180             element.addChildElement(new ElementValueComparatorConfigurationElement(element, elementValueComparatorConfiguration));
181         }
182     }
183 
184     private static void addCacheWriterConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
185         CacheWriterConfiguration cacheWriterConfiguration = cacheConfiguration.getCacheWriterConfiguration();
186         if (cacheWriterConfiguration != null && !CacheConfiguration.DEFAULT_CACHE_WRITER_CONFIGURATION.equals(cacheWriterConfiguration)) {
187             element.addChildElement(new CacheWriterConfigurationElement(element, cacheWriterConfiguration));
188         }
189     }
190 
191     private static void addTerracottaConfigurationElement(NodeElement element, CacheConfiguration cacheConfiguration) {
192         TerracottaConfiguration terracottaConfiguration = cacheConfiguration.getTerracottaConfiguration();
193         if (terracottaConfiguration != null) {
194             element.addChildElement(new TerracottaConfigurationElement(element, terracottaConfiguration));
195         }
196     }
197 
198     private static void addSearchElement(NodeElement element, CacheConfiguration cacheConfiguration) {
199         if (cacheConfiguration.isSearchable()) {
200             element.addChildElement(new SearchableConfigurationElement(element, cacheConfiguration.getSearchable()));
201         }
202     }
203 
204     private static void addPinningElement(NodeElement element, CacheConfiguration cacheConfiguration) {
205         PinningConfiguration pinningConfiguration = cacheConfiguration.getPinningConfiguration();
206         if (pinningConfiguration != null) {
207             element.addChildElement(new PinningConfigurationElement(element, pinningConfiguration));
208         }
209     }
210 
211 }