View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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 org.settings4j.settings;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.lang3.Validate;
27  import org.settings4j.Connector;
28  import org.settings4j.ConnectorPosition;
29  import org.settings4j.ConnectorPositions;
30  import org.settings4j.Settings4jInstance;
31  
32  /**
33   * The default Settings Object.
34   * 
35   * @author Harald.Brabenetz
36   */
37  public class DefaultSettings implements Settings4jInstance {
38  
39      /** General Logger for this Class. */
40      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultSettings.class);
41  
42      private final List<Connector> connectors = Collections.synchronizedList(new ArrayList<Connector>());
43      private final Map<String, Connector> connectorMap = Collections.synchronizedMap(new HashMap<String, Connector>());
44      private Map mapping;
45  
46      /** {@inheritDoc} */
47      public List<Connector> getConnectors() {
48          return Collections.unmodifiableList(connectors);
49      }
50  
51      /** {@inheritDoc} */
52      public Connector getConnector(final String connectorName) {
53          return connectorMap.get(connectorName);
54      }
55  
56      /** {@inheritDoc} */
57      public void addConnector(final Connector connector) {
58          addConnector(connector, ConnectorPositions.atLast());
59      }
60  
61      /** {@inheritDoc} */
62      public void addConnector(final Connector connector, final ConnectorPosition position) {
63          final int pos = position.getPosition(connectors);
64          Validate.isTrue(pos != ConnectorPosition.UNKNOWN_POSITION,
65              "No valid Position found to add the given connector.");
66          Validate.isTrue(connectorMap.get(connector.getName()) == null, //
67              "A connector with the given name '%s' already exists!", connector.getName());
68          connectors.add(pos, connector);
69          connectorMap.put(connector.getName(), connector);
70  
71      }
72  
73      /** {@inheritDoc} */
74      public void removeAllConnectors() {
75          connectors.clear();
76          connectorMap.clear();
77      }
78  
79      /** {@inheritDoc} */
80      public byte[] getContent(final String key) {
81          final String mappedKey = mappedKey(key);
82          byte[] result = null;
83          for (final Connector connector : connectors) {
84              result = connector.getContent(mappedKey);
85              if (result != null) {
86                  logDebugFoundValueForKey("Content", key, connector);
87                  return result;
88              }
89          }
90          return result;
91      }
92  
93      /** {@inheritDoc} */
94      public Object getObject(final String key) {
95          final String mappedKey = mappedKey(key);
96          Object result = null;
97          for (final Connector connector : connectors) {
98              result = connector.getObject(mappedKey);
99              if (result != null) {
100                 logDebugFoundValueForKey("Object", key, connector);
101                 return result;
102             }
103         }
104         return result;
105     }
106 
107     /** {@inheritDoc} */
108     public String getString(final String key) {
109         final String mappedKey = mappedKey(key);
110         String result = null;
111         for (final Connector connector : connectors) {
112             result = connector.getString(mappedKey);
113             if (result != null) {
114                 logDebugFoundValueForKey("String", key, connector);
115                 return result;
116             }
117         }
118         return result;
119     }
120 
121     /**
122      * Get the mapped Key.
123      * <p>
124      * if some Sub-Modules of your App defines separated Keys for the DataSource, you can refer it the the same Key:
125      * 
126      * <pre>
127      * Example:
128      * &lt;mapping name="defaultMapping"&gt;
129      *     &lt;entry key="com/mycompany/moduleX/datasource" ref-key="global/datasource"/&gt;
130      *     &lt;entry key="com/mycompany/moduleY/datasource" ref-key="global/datasource"/&gt;
131      * &lt;/mapping&gt;
132      * </pre>
133      * 
134      * Now you need only configure only one dataSource for your App.
135      * 
136      * @param key the Key to map.
137      * @return The key which must be configured for the given Key.
138      */
139     private String mappedKey(final String key) {
140         final String mappedKey = (String) this.getMapping().get(key);
141         if (StringUtils.isEmpty(mappedKey)) {
142             return key;
143         }
144         // else
145         return mappedKey;
146 
147     }
148 
149     /** {@inheritDoc} */
150     public Map getMapping() {
151         if (mapping == null) {
152             mapping = new HashMap();
153         }
154         return mapping;
155     }
156 
157     /** {@inheritDoc} */
158     public void setMapping(final Map mapping) {
159         this.mapping = mapping;
160     }
161 
162     private void logDebugFoundValueForKey(final String type, final String key, final Connector connector) {
163         LOG.debug("Found {} for Key '{}' in connector '{}' ({})", //
164             type, key, connector.getName(), connector.getClass().getName());
165     }
166 }