View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.settings;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.commons.lang3.Validate;
30  import org.settings4j.Connector;
31  import org.settings4j.ConnectorPosition;
32  import org.settings4j.ConnectorPositions;
33  import org.settings4j.Settings4jInstance;
34  
35  /**
36   * The default Settings Object.
37   *
38   * @author Harald.Brabenetz
39   */
40  public class DefaultSettings implements Settings4jInstance {
41  
42      /** General Logger for this Class. */
43      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultSettings.class);
44  
45      private final List<Connector> connectors = Collections.synchronizedList(new ArrayList<Connector>());
46      private final Map<String, Connector> connectorMap = Collections.synchronizedMap(new HashMap<String, Connector>());
47      private Map<String, String> mapping;
48  
49      @Override
50      public List<Connector> getConnectors() {
51          return Collections.unmodifiableList(this.connectors);
52      }
53  
54      @Override
55      public Connector getConnector(final String connectorName) {
56          return this.connectorMap.get(connectorName);
57      }
58  
59      @Override
60      public void addConnector(final Connector connector) {
61          addConnector(connector, ConnectorPositions.atLast());
62      }
63  
64      @Override
65      public void addConnector(final Connector connector, final ConnectorPosition position) {
66          final int pos = position.getPosition(this.connectors);
67          Validate.isTrue(pos != ConnectorPosition.UNKNOWN_POSITION,
68              "No valid Position found to add the given connector.");
69          Validate.isTrue(this.connectorMap.get(connector.getName()) == null, //
70              "A connector with the given name '%s' already exists!", connector.getName());
71          this.connectors.add(pos, connector);
72          this.connectorMap.put(connector.getName(), connector);
73  
74      }
75  
76      @Override
77      public void removeAllConnectors() {
78          this.connectors.clear();
79          this.connectorMap.clear();
80      }
81  
82      @Override
83      public byte[] getContent(final String key) {
84          final String mappedKey = mappedKey(key);
85          byte[] result = null;
86          for (final Connector connector : this.connectors) {
87              result = connector.getContent(mappedKey);
88              if (result != null) {
89                  logDebugFoundValueForKey("Content", key, connector);
90                  return result;
91              }
92          }
93          return result;
94      }
95  
96      @Override
97      public Object getObject(final String key) {
98          final String mappedKey = mappedKey(key);
99          Object result = null;
100         for (final Connector connector : this.connectors) {
101             result = connector.getObject(mappedKey);
102             if (result != null) {
103                 logDebugFoundValueForKey("Object", key, connector);
104                 return result;
105             }
106         }
107         return result;
108     }
109 
110     @Override
111     public String getString(final String key) {
112         final String mappedKey = mappedKey(key);
113         String result = null;
114         for (final Connector connector : this.connectors) {
115             result = connector.getString(mappedKey);
116             if (result != null) {
117                 logDebugFoundValueForKey("String", key, connector);
118                 return result;
119             }
120         }
121         return result;
122     }
123 
124     /**
125      * Get the mapped Key.
126      * <p>
127      * if some Sub-Modules of your App defines separated Keys for the DataSource, you can refer it the the same Key:
128      * </p>
129      *
130      * <pre>
131      * Example:
132      * &lt;mapping name="defaultMapping"&gt;
133      *     &lt;entry key="com/mycompany/moduleX/datasource" ref-key="global/datasource"/&gt;
134      *     &lt;entry key="com/mycompany/moduleY/datasource" ref-key="global/datasource"/&gt;
135      * &lt;/mapping&gt;
136      * </pre>
137      * <p>
138      * Now you need only configure only one dataSource for your App.
139      * </p>
140      *
141      * @param key
142      *        the Key to map.
143      * @return The key which must be configured for the given Key.
144      */
145     private String mappedKey(final String key) {
146         final String mappedKey = this.getMapping().get(key);
147         if (StringUtils.isEmpty(mappedKey)) {
148             return key;
149         }
150         // else
151         return mappedKey;
152 
153     }
154 
155     @Override
156     public Map<String, String> getMapping() {
157         if (this.mapping == null) {
158             this.mapping = new HashMap<String, String>();
159         }
160         return this.mapping;
161     }
162 
163     @Override
164     public void setMapping(final Map<String, String> mapping) {
165         this.mapping = mapping;
166     }
167 
168     private void logDebugFoundValueForKey(final String type, final String key, final Connector connector) {
169         LOG.debug("Found {} for Key '{}' in connector '{}' ({})", //
170             type, key, connector.getName(), connector.getClass().getName());
171     }
172 }