View Javadoc
1   package org.fax4j.common;
2   
3   import java.text.MessageFormat;
4   import java.util.Collections;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   /**
9    * This is a common basic implementation of the configuration holder.
10   *
11   * @author Sagie Gur-Ari
12   * @version 1.01
13   * @since 0.41.7
14   */
15  public final class ConfigurationHolderImpl implements ConfigurationHolder {
16      /** The property part */
17      private final String PROPERTY_PART;
18      /** The configuration */
19      private final Map<String, String> CONFIGURATION;
20      /** The logger */
21      private final Logger LOGGER;
22  
23      /**
24       * This is the class constructor.
25       *
26       * @param map
27       *            The configuration as map
28       */
29      public ConfigurationHolderImpl(Map<String, String> map) {
30          this(map, null);
31      }
32  
33      /**
34       * This is the class constructor.
35       *
36       * @param map
37       *            The configuration as map
38       * @param propertyPart
39       *            The property part
40       */
41      public ConfigurationHolderImpl(Map<String, String> map, String propertyPart) {
42          super();
43  
44          // get logger
45          LoggerManager loggerManager = LoggerManager.getInstance();
46          this.LOGGER = loggerManager.getLogger();
47  
48          // get configuration
49          Map<String, String> configuration = new HashMap<String, String>(map);
50          this.CONFIGURATION = Collections.unmodifiableMap(configuration);
51  
52          // get property part
53          this.PROPERTY_PART = propertyPart;
54      }
55  
56      /**
57       * Returns the property part.<br>
58       * Property parts enables to replace the input request key with the part defined to enable to reuse services with
59       * different configuration blocks.<br>
60       * Property parts are the values after the common prefix and before any specific configuration key, or in other
61       * words the top context of the configuration.<br>
62       * For example: org.fax4j.[partvalue].some.key<br>
63       * The org.fax4j is the common prefix, the some.key is the suffix and partvalue is the part to be replaced.
64       *
65       * @return The property part
66       */
67      public String getPropertyPart() {
68          return this.PROPERTY_PART;
69      }
70  
71      /**
72       * Returns the configuration.
73       *
74       * @return The configuration
75       */
76      public Map<String, String> getConfiguration() {
77          return this.CONFIGURATION;
78      }
79  
80      /**
81       * Returns the value from the component configuration based on the provided configuration key. The value will be
82       * trimmed.<br>
83       * If the trimmed configuration value is an empty string, null will be returned instead.
84       *
85       * @param key
86       *            The configuration key
87       * @return The value
88       */
89      public String getConfigurationValue(String key) {
90          // format property key
91          String propertyKey = key;
92          if (this.PROPERTY_PART != null) {
93              propertyKey = MessageFormat.format(key, new Object[] { this.PROPERTY_PART });
94          }
95  
96          // get value
97          String value = this.CONFIGURATION.get(propertyKey);
98  
99          // trim value
100         if (value != null) {
101             value = value.trim();
102 
103             // set as null if empty string
104             if (value.length() == 0) {
105                 value = null;
106             }
107         }
108 
109         this.LOGGER.logDebug(new Object[] { "Extracted configuration for key: ", propertyKey, " value: ", value },
110                 null);
111 
112         return value;
113     }
114 
115     /**
116      * Returns the value from the component configuration based on the provided configuration key. The value will be
117      * trimmed.<br>
118      * If the trimmed configuration value is an empty string, null will be returned instead.
119      *
120      * @param key
121      *            The configuration key (toString value will be used)
122      * @return The value
123      */
124     public String getConfigurationValue(Enum<?> key) {
125         // get string key
126         String keyStr = key.toString();
127 
128         // get value
129         String value = this.getConfigurationValue(keyStr);
130 
131         return value;
132     }
133 }