1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.util.codingsupport;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.Properties;
27
28 import org.springframework.core.io.Resource;
29 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
30
31 import ch.elca.el4j.core.io.support.ListResourcePatternResolverDecorator;
32 import ch.elca.el4j.core.io.support.ManifestOrderedConfigLocationProvider;
33 import ch.elca.el4j.core.io.support.OrderedPathMatchingResourcePatternResolver;
34 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class PropertiesHelper {
49
50
51
52
53
54
55
56 public static Properties loadPropertiesFromResources(Resource[] resources) {
57 Properties properties = new Properties();
58 InputStream in = null;
59
60 for (Resource resource : resources) {
61 try {
62 in = resource.getInputStream();
63 properties.load(in);
64 } catch (IOException e) {
65
66 } finally {
67 if (in != null) {
68 try {
69 in.close();
70 } catch (IOException e) {
71 CoreNotificationHelper.notifyMisconfiguration(
72 "An IOException was thrown while loading properties from '"
73 + resource + "'.", e);
74 }
75 }
76 }
77 }
78
79 return properties;
80 }
81
82
83
84
85
86
87
88
89 public Properties loadProperties(String inputFileName) {
90
91 String resourceName = inputFileName;
92 if (new File(inputFileName).exists() && !inputFileName.startsWith("file:")) {
93 resourceName = "file:" + resourceName;
94 }
95
96 ListResourcePatternResolverDecorator resolver = new ListResourcePatternResolverDecorator(
97 new ManifestOrderedConfigLocationProvider(),
98 new OrderedPathMatchingResourcePatternResolver());
99
100 resolver.setMostSpecificResourceLast(true);
101 resolver.setMergeWithOuterResources(true);
102
103 try {
104 return loadPropertiesFromResources(resolver.getResources(resourceName));
105 } catch (IOException e) {
106 CoreNotificationHelper.notifyMisconfiguration(
107 "An IOException was thrown. The responsible file is '"
108 + inputFileName + "'.", e);
109 return null;
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122 public void storeProperties(Properties props, String outputFileName) {
123
124 PathMatchingResourcePatternResolver pmrpr
125 = new PathMatchingResourcePatternResolver();
126
127 String fileName = null;
128
129 Resource res = pmrpr.getResource(outputFileName);
130 OutputStream out = null;
131 try {
132 try {
133
134 fileName = res.getURL().getFile();
135 } catch (FileNotFoundException e) {
136
137 File file = new File(outputFileName);
138 fileName = file.getAbsolutePath();
139 }
140 out = new FileOutputStream(fileName);
141 props.store(out, "Title");
142 } catch (FileNotFoundException e) {
143 CoreNotificationHelper.notifyMisconfiguration(
144 "The file '" + outputFileName + "' could not be found.", e);
145 } catch (IOException e) {
146 CoreNotificationHelper.notifyMisconfiguration(
147 "An IOException was thrown. The responsible file is '"
148 + outputFileName + "'.", e);
149 } finally {
150 if (out != null) {
151 try {
152 out.close();
153 } catch (IOException e) {
154 CoreNotificationHelper.notifyMisconfiguration(
155 "The file '" + outputFileName
156 + "' could not be close.", e);
157 }
158 }
159 }
160
161 }
162
163 }