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.File;
20 import java.io.IOException;
21 import java.io.UnsupportedEncodingException;
22
23 import org.settings4j.ContentResolver;
24 import org.settings4j.contentresolver.FSContentResolver;
25 import org.settings4j.contentresolver.UnionContentResolver;
26
27
28
29
30
31
32
33 public class FSConnector extends AbstractConnector {
34
35
36 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(FSConnector.class);
37
38 private final FSContentResolver fsContentResolver = new FSContentResolver();
39 private ContentResolver unionContentResolver = new UnionContentResolver(fsContentResolver);
40 private String charset = "UTF-8";
41
42
43 public byte[] getContent(final String key) {
44 return fsContentResolver.getContent(key);
45 }
46
47
48 public Object getObject(final String key) {
49 if (getObjectResolver() != null) {
50 return getObjectResolver().getObject(key, unionContentResolver);
51 }
52
53 return null;
54 }
55
56
57 public String getString(final String key) {
58 try {
59 final byte[] content = getContent(key);
60 if (content != null) {
61 return new String(fsContentResolver.getContent(key), charset);
62 }
63
64 return null;
65
66 } catch (final UnsupportedEncodingException e) {
67
68 LOG.error("Charset not found: " + charset, e);
69 return null;
70 }
71 }
72
73
74
75
76
77
78 public void setContent(final String key, final byte[] value) throws IOException {
79 fsContentResolver.setContent(key, value);
80 }
81
82
83
84
85
86
87 public void setString(final String key, final String value) throws IOException {
88 try {
89 setContent(key, value.getBytes(charset));
90 } catch (final UnsupportedEncodingException e) {
91
92 LOG.error("Charset not found: " + charset, e);
93 }
94 }
95
96 public String getCharset() {
97 return charset;
98 }
99
100 public void setCharset(final String charset) {
101 this.charset = charset;
102 }
103
104
105
106
107
108
109 public void setRootFolderPath(final String rootFolderPath) {
110 fsContentResolver.setRootFolderPath(rootFolderPath);
111 }
112
113
114 @Override
115 public void setContentResolver(final ContentResolver contentResolver) {
116 unionContentResolver = new UnionContentResolver(fsContentResolver);
117 unionContentResolver.addContentResolver(contentResolver);
118 }
119
120
121
122
123
124
125
126
127 public File getRootFolder() {
128 return fsContentResolver.getRootFolder();
129 }
130 }