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 java.util.List;
20  
21  import net.sf.ehcache.config.CacheConfiguration;
22  import net.sf.ehcache.config.Configuration;
23  import net.sf.ehcache.config.DiskStoreConfiguration;
24  import net.sf.ehcache.config.FactoryConfiguration;
25  import net.sf.ehcache.config.TerracottaClientConfiguration;
26  import net.sf.ehcache.config.generator.model.SimpleNodeAttribute;
27  import net.sf.ehcache.config.generator.model.SimpleNodeElement;
28  
29  /***
30   * Element representing the {@link Configuration}. This element does not have a parent and is always null.
31   *
32   * @author Abhishek Sanoujam
33   *
34   */
35  public class ConfigurationElement extends SimpleNodeElement {
36  
37      private final Configuration configuration;
38  
39      /***
40       * Constructor accepting the {@link Configuration}. This element does not have a parent and is always null.
41       *
42       * @param configuration
43       */
44      public ConfigurationElement(Configuration configuration) {
45          super(null, "ehcache");
46          this.configuration = configuration;
47          init();
48      }
49  
50      private void init() {
51          if (configuration == null) {
52              return;
53          }
54          // add the attributes
55          addAttribute(new SimpleNodeAttribute("name", configuration.getName()).optional(true));
56          addAttribute(new SimpleNodeAttribute("updateCheck", configuration.getUpdateCheck()).optional(true).defaultValue(
57                  String.valueOf(Configuration.DEFAULT_UPDATE_CHECK)));
58          addAttribute(new SimpleNodeAttribute("monitoring", configuration.getMonitoring()).optional(true).defaultValue(
59                  Configuration.DEFAULT_MONITORING.name().toLowerCase()));
60          addAttribute(new SimpleNodeAttribute("dynamicConfig", configuration.getDynamicConfig()).optional(true).defaultValue(
61                  String.valueOf(Configuration.DEFAULT_DYNAMIC_CONFIG)));
62          addAttribute(new SimpleNodeAttribute("defaultTransactionTimeoutInSeconds", configuration.getDefaultTransactionTimeoutInSeconds())
63                  .optional(true).defaultValue(String.valueOf(Configuration.DEFAULT_TRANSACTION_TIMEOUT)));
64          addAttribute(new SimpleNodeAttribute("maxBytesLocalHeap", configuration.getMaxBytesLocalHeap())
65                  .optional(true).defaultValue(String.valueOf(Configuration.DEFAULT_MAX_BYTES_ON_HEAP)));
66          addAttribute(new SimpleNodeAttribute("maxBytesLocalOffHeap", configuration.getMaxBytesLocalOffHeap())
67                  .optional(true).defaultValue(String.valueOf(Configuration.DEFAULT_MAX_BYTES_OFF_HEAP)));
68          addAttribute(new SimpleNodeAttribute("maxBytesLocalDisk", configuration.getMaxBytesLocalDisk())
69                  .optional(true).defaultValue(String.valueOf(Configuration.DEFAULT_MAX_BYTES_ON_DISK)));
70  
71          // add the child elements
72          DiskStoreConfiguration diskStoreConfiguration = configuration.getDiskStoreConfiguration();
73          if (diskStoreConfiguration != null) {
74              addChildElement(new DiskStoreConfigurationElement(this, diskStoreConfiguration));
75          }
76          FactoryConfiguration transactionManagerLookupConfiguration = configuration.getTransactionManagerLookupConfiguration();
77          if (transactionManagerLookupConfiguration != null
78                  && !transactionManagerLookupConfiguration.equals(Configuration.DEFAULT_TRANSACTION_MANAGER_LOOKUP_CONFIG)) {
79              addChildElement(new FactoryConfigurationElement(this, "transactionManagerLookup", transactionManagerLookupConfiguration));
80          }
81          FactoryConfiguration cacheManagerEventListenerFactoryConfiguration = configuration
82                  .getCacheManagerEventListenerFactoryConfiguration();
83          if (cacheManagerEventListenerFactoryConfiguration != null) {
84              addChildElement(new FactoryConfigurationElement(this, "cacheManagerEventListenerFactory",
85                      cacheManagerEventListenerFactoryConfiguration));
86          }
87          List<FactoryConfiguration> cacheManagerPeerProviderFactoryConfiguration = configuration
88                  .getCacheManagerPeerProviderFactoryConfiguration();
89          if (cacheManagerPeerProviderFactoryConfiguration != null) {
90              addAllFactoryConfigsAsChildElements(this, "cacheManagerPeerProviderFactory", cacheManagerPeerProviderFactoryConfiguration);
91          }
92          List<FactoryConfiguration> cacheManagerPeerListenerFactoryConfigurations = configuration
93                  .getCacheManagerPeerListenerFactoryConfigurations();
94          if (cacheManagerPeerListenerFactoryConfigurations != null && !cacheManagerPeerListenerFactoryConfigurations.isEmpty()) {
95              addAllFactoryConfigsAsChildElements(this, "cacheManagerPeerListenerFactory", cacheManagerPeerListenerFactoryConfigurations);
96          }
97          addChildElement(new DefaultCacheConfigurationElement(this, configuration.getDefaultCacheConfiguration()));
98          for (CacheConfiguration cacheConfiguration : configuration.getCacheConfigurations().values()) {
99              addChildElement(new CacheConfigurationElement(this, cacheConfiguration));
100         }
101         TerracottaClientConfiguration terracottaConfiguration = configuration.getTerracottaConfiguration();
102         if (terracottaConfiguration != null) {
103             addChildElement(new TerracottaConfigConfigurationElement(this, terracottaConfiguration));
104         }
105     }
106 
107 }