1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37 public class DefaultSettings implements Settings4jInstance {
38
39
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
47 public List<Connector> getConnectors() {
48 return Collections.unmodifiableList(connectors);
49 }
50
51
52 public Connector getConnector(final String connectorName) {
53 return connectorMap.get(connectorName);
54 }
55
56
57 public void addConnector(final Connector connector) {
58 addConnector(connector, ConnectorPositions.atLast());
59 }
60
61
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
74 public void removeAllConnectors() {
75 connectors.clear();
76 connectorMap.clear();
77 }
78
79
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
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
145 return mappedKey;
146
147 }
148
149
150 public Map getMapping() {
151 if (mapping == null) {
152 mapping = new HashMap();
153 }
154 return mapping;
155 }
156
157
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 }