1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.settings4j.connector;
18
19 import java.io.UnsupportedEncodingException;
20
21 import org.settings4j.ContentResolver;
22 import org.settings4j.contentresolver.ClasspathContentResolver;
23 import org.settings4j.contentresolver.UnionContentResolver;
24
25
26
27
28
29
30
31
32
33
34 public class ClasspathConnector extends AbstractConnector {
35
36
37 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(ClasspathConnector.class);
38
39 private final ClasspathContentResolver classpathContentResolver = new ClasspathContentResolver();
40 private ContentResolver unionContentResolver = new UnionContentResolver(this.classpathContentResolver);
41 private String charset = "UTF-8";
42
43
44 public byte[] getContent(final String key) {
45 return this.classpathContentResolver.getContent(key);
46 }
47
48
49 public Object getObject(final String key) {
50 if (getObjectResolver() != null) {
51 return getObjectResolver().getObject(key, this.unionContentResolver);
52 }
53
54 return null;
55
56 }
57
58
59 public String getString(final String key) {
60 try {
61 final byte[] content = getContent(key);
62 if (content != null) {
63 return new String(this.classpathContentResolver.getContent(key), this.charset);
64 }
65
66 return null;
67
68 } catch (final UnsupportedEncodingException e) {
69
70 LOG.error("Charset not found: " + this.charset, e);
71 return null;
72 }
73 }
74
75 public String getCharset() {
76 return this.charset;
77 }
78
79 public void setCharset(final String charset) {
80 this.charset = charset;
81 }
82
83
84 @Override
85 public void setContentResolver(final ContentResolver contentResolver) {
86 this.unionContentResolver = new UnionContentResolver(this.classpathContentResolver);
87 this.unionContentResolver.addContentResolver(contentResolver);
88 }
89 }