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 this(targetFolder, PropertiesFormat.NATIVE);
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public FileSystemPropertySink(final File targetFolder,
85 final PropertiesFormat outputFormat) throws NullPointerException
86 {
87 this.targetFolder = Arg.checkNotNull("targetFolder", targetFolder);
88 this.outputFormat = Arg.checkNotNull("outputFormat", outputFormat);
89 }
90
91
92
93
94
95
96
97
98
99
100
101 @Override
102 public void clear() throws TransferException
103 {
104 try
105 {
106 FileUtils.cleanDirectory(targetFolder);
107 }
108 catch (final IOException e)
109 {
110 final ConfigurationKey<?> key =
111 ConfigurationKeyContextManager.INSTANCE.context()
112 .configurationKeyFactory().createDefaultKey();
113 throw new TransferException(new TransferMessageBean(
114 TransferFileCode.FAILED_TO_CLEAN, e, key, targetFolder));
115 }
116 }
117
118
119 @Override
120 public void write(final PropertyProvider provider) throws TransferException
121 {
122 final PropertyLocation location = provider.getSourceId();
123 final ConfigurationKey<?> key = provider.getConfigurationKey();
124
125 final Properties properties = createProperties(provider);
126
127 writeProperties(key, location, properties);
128 }
129
130 private Properties createProperties(final PropertyProvider provider)
131 {
132 final Properties properties = new Properties();
133 for (final Property property : provider.getProperties())
134 {
135 final String name = property.getName();
136 final String value = property.getValue();
137 properties.setProperty(name, value);
138 }
139 return properties;
140 }
141
142 private void writeProperties(final ConfigurationKey<?> key,
143 final PropertyLocation location, final Properties properties)
144 {
145
146 final String path = location.getPath();
147 final File propertiesFile = createFile(path);
148 OutputStream out = null;
149 try
150 {
151 final File parentDir = propertiesFile.getParentFile();
152 FileFunction.provideDirectory(parentDir);
153
154 out = new BufferedOutputStream(new FileOutputStream(propertiesFile));
155
156 final String comments = key.toString();
157 if (outputFormat == PropertiesFormat.XML)
158 {
159 properties.storeToXML(out, comments);
160 }
161 else
162 {
163 properties.store(out, comments);
164 }
165 }
166 catch (final IOException e)
167 {
168 throw new TransferException(new TransferMessageBean(
169 TransferFileCode.FAILED_TO_TRANSFER, e, key, targetFolder));
170 }
171 finally
172 {
173 IOUtils.closeQuietly(out);
174 }
175 }
176
177 private File createFile(final String path)
178 {
179 final String adjustedPath = outputFormat.adjustPath(path);
180 return new File(targetFolder, adjustedPath);
181 }
182
183
184 @Override
185 public void write(final Iterable<PropertyProvider> providers)
186 throws TransferException
187 {
188 for (final PropertyProvider provider : providers)
189 {
190 write(provider);
191 }
192 }
193
194
195
196
197
198
199
200 @Override
201 public void close() throws TransferException
202 {
203 }
204
205
206
207 }