1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package de.smartics.properties.config.transfer.filesystem; |
17 |
|
|
18 |
|
import java.io.BufferedOutputStream; |
19 |
|
import java.io.File; |
20 |
|
import java.io.FileOutputStream; |
21 |
|
import java.io.IOException; |
22 |
|
import java.io.OutputStream; |
23 |
|
import java.util.Properties; |
24 |
|
|
25 |
|
import org.apache.commons.io.FileUtils; |
26 |
|
import org.apache.commons.io.IOUtils; |
27 |
|
|
28 |
|
import de.smartics.properties.api.config.domain.Property; |
29 |
|
import de.smartics.properties.api.config.domain.PropertyLocation; |
30 |
|
import de.smartics.properties.api.config.domain.PropertyProvider; |
31 |
|
import de.smartics.properties.api.config.domain.key.ConfigurationKey; |
32 |
|
import de.smartics.properties.api.config.transfer.PropertySink; |
33 |
|
import de.smartics.properties.api.config.transfer.TransferException; |
34 |
|
import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager; |
35 |
|
import de.smartics.util.io.FileFunction; |
36 |
|
import de.smartics.util.lang.Arg; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public final class FileSystemPropertySink implements PropertySink |
42 |
|
{ |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private final File targetFolder; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private final PropertiesFormat outputFormat; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
public FileSystemPropertySink(final File targetFolder) |
70 |
|
{ |
71 |
0 |
this(targetFolder, PropertiesFormat.NATIVE); |
72 |
0 |
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public FileSystemPropertySink(final File targetFolder, |
85 |
|
final PropertiesFormat outputFormat) throws NullPointerException |
86 |
1 |
{ |
87 |
1 |
this.targetFolder = Arg.checkNotNull("targetFolder", targetFolder); |
88 |
1 |
this.outputFormat = Arg.checkNotNull("outputFormat", outputFormat); |
89 |
1 |
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@Override |
102 |
|
public void clear() throws TransferException |
103 |
|
{ |
104 |
|
try |
105 |
|
{ |
106 |
0 |
FileUtils.cleanDirectory(targetFolder); |
107 |
|
} |
108 |
0 |
catch (final IOException e) |
109 |
|
{ |
110 |
0 |
final ConfigurationKey<?> key = |
111 |
|
ConfigurationKeyContextManager.INSTANCE.context() |
112 |
|
.configurationKeyFactory().createDefaultKey(); |
113 |
0 |
throw new TransferException(new TransferMessageBean( |
114 |
|
TransferFileCode.FAILED_TO_CLEAN, e, key, targetFolder)); |
115 |
0 |
} |
116 |
0 |
} |
117 |
|
|
118 |
|
|
119 |
|
@Override |
120 |
|
public void write(final PropertyProvider provider) throws TransferException |
121 |
|
{ |
122 |
4 |
final PropertyLocation location = provider.getSourceId(); |
123 |
4 |
final ConfigurationKey<?> key = provider.getConfigurationKey(); |
124 |
|
|
125 |
4 |
final Properties properties = createProperties(provider); |
126 |
|
|
127 |
4 |
writeProperties(key, location, properties); |
128 |
4 |
} |
129 |
|
|
130 |
|
private Properties createProperties(final PropertyProvider provider) |
131 |
|
{ |
132 |
4 |
final Properties properties = new Properties(); |
133 |
4 |
for (final Property property : provider.getProperties()) |
134 |
|
{ |
135 |
12 |
final String name = property.getName(); |
136 |
12 |
final String value = property.getValue(); |
137 |
12 |
properties.setProperty(name, value); |
138 |
12 |
} |
139 |
4 |
return properties; |
140 |
|
} |
141 |
|
|
142 |
|
private void writeProperties(final ConfigurationKey<?> key, |
143 |
|
final PropertyLocation location, final Properties properties) |
144 |
|
{ |
145 |
|
|
146 |
4 |
final String path = location.getPath(); |
147 |
4 |
final File propertiesFile = createFile(path); |
148 |
4 |
OutputStream out = null; |
149 |
|
try |
150 |
|
{ |
151 |
4 |
final File parentDir = propertiesFile.getParentFile(); |
152 |
4 |
FileFunction.provideDirectory(parentDir); |
153 |
|
|
154 |
4 |
out = new BufferedOutputStream(new FileOutputStream(propertiesFile)); |
155 |
|
|
156 |
4 |
final String comments = key.toString(); |
157 |
4 |
if (outputFormat == PropertiesFormat.XML) |
158 |
|
{ |
159 |
4 |
properties.storeToXML(out, comments); |
160 |
|
} |
161 |
|
else |
162 |
|
{ |
163 |
0 |
properties.store(out, comments); |
164 |
|
} |
165 |
|
} |
166 |
0 |
catch (final IOException e) |
167 |
|
{ |
168 |
0 |
throw new TransferException(new TransferMessageBean( |
169 |
|
TransferFileCode.FAILED_TO_TRANSFER, e, key, targetFolder)); |
170 |
|
} |
171 |
|
finally |
172 |
|
{ |
173 |
4 |
IOUtils.closeQuietly(out); |
174 |
4 |
} |
175 |
4 |
} |
176 |
|
|
177 |
|
private File createFile(final String path) |
178 |
|
{ |
179 |
4 |
final String adjustedPath = outputFormat.adjustPath(path); |
180 |
4 |
return new File(targetFolder, adjustedPath); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
@Override |
185 |
|
public void write(final Iterable<PropertyProvider> providers) |
186 |
|
throws TransferException |
187 |
|
{ |
188 |
1 |
for (final PropertyProvider provider : providers) |
189 |
|
{ |
190 |
4 |
write(provider); |
191 |
|
} |
192 |
1 |
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
@Override |
201 |
|
public void close() throws TransferException |
202 |
|
{ |
203 |
0 |
} |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
} |