1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.settings4j.objectresolver;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.settings4j.ContentResolver;
28 import org.settings4j.ObjectResolver;
29
30
31
32
33
34
35
36
37 public abstract class AbstractObjectResolver implements ObjectResolver {
38
39
40 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(AbstractObjectResolver.class);
41
42
43 public static final String PROP_OBJECT_RESOLVER_KEY = "objectResolverKey";
44
45 public static final String PROP_CACHED = "cached";
46
47 private String propertySuffix = ".properties";
48
49 private final Map<String, Object> cachedObjects = new HashMap<String, Object>();
50
51 private boolean cached = false;
52
53
54 public void addObjectResolver(final ObjectResolver objectResolver) {
55 throw new UnsupportedOperationException(this.getClass().getName() + " cannot add other ObjectResolvers");
56 }
57
58
59 public Object getObject(final String key, final ContentResolver contentResolver) {
60
61 Object result = this.cachedObjects.get(key);
62 if (result != null) {
63 return result;
64 }
65
66
67 final byte[] content = contentResolver.getContent(key);
68 if (content != null) {
69 final Properties properties = getObjectProperties(key, contentResolver);
70 if (properties != null) {
71 final String propObjectResolverKey = properties.getProperty(PROP_OBJECT_RESOLVER_KEY);
72 final String propCached = properties.getProperty(PROP_CACHED);
73 if (StringUtils.isEmpty(propObjectResolverKey)) {
74 LOG.warn("The property-File for Key '{}' doesn't have the required Property '{}'",
75 key, PROP_OBJECT_RESOLVER_KEY);
76 return null;
77 }
78
79 if (getObjectResolverKey().equals(propObjectResolverKey)) {
80 result = contentToObject(key, properties, content, contentResolver);
81 if (result != null) {
82 if ("true".equalsIgnoreCase(propCached) || (propCached == null && isCached())) {
83 this.cachedObjects.put(key, result);
84 }
85 return result;
86 }
87 }
88 }
89 }
90 return null;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 protected Properties getObjectProperties(final String key, final ContentResolver contentResolver) {
104
105 final byte[] propertyContent = contentResolver.getContent(key + this.propertySuffix);
106 if (propertyContent == null) {
107 return null;
108 }
109
110 final Properties properties = new Properties();
111 try {
112 properties.load(new ByteArrayInputStream(propertyContent));
113 } catch (final IOException e) {
114 LOG.error(e.getMessage(), e);
115 return null;
116 }
117 return properties;
118
119 }
120
121 public String getPropertySuffix() {
122 return this.propertySuffix;
123 }
124
125 public void setPropertySuffix(final String propertySuffix) {
126 this.propertySuffix = propertySuffix;
127 }
128
129 protected String getObjectResolverKey() {
130 return this.getClass().getName();
131 }
132
133
134
135
136
137
138
139
140
141
142
143 protected abstract Object contentToObject(String key, Properties properties, byte[] content,
144 ContentResolver contentResolver);
145
146 public boolean isCached() {
147 return this.cached;
148 }
149
150 public void setCached(final boolean cached) {
151 this.cached = cached;
152 }
153 }