Skip to content

Commit 98e0b5c

Browse files
simon-meng-cnrjeberhard
authored andcommitted
close file system after loading scripts
1 parent 189f6d0 commit 98e0b5c

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/ConfigMapHelper.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected V1ConfigMap computeDomainConfigMap() {
155155
return cm;
156156
}
157157

158-
private Map<String, String> loadScripts() {
158+
private synchronized Map<String, String> loadScripts() {
159159
URI uri = null;
160160
try {
161161
uri = getClass().getResource(SCRIPT_LOCATION).toURI();
@@ -164,15 +164,14 @@ private Map<String, String> loadScripts() {
164164
throw new RuntimeException(e);
165165
}
166166

167-
try {
168-
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
167+
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap())) {
169168
Stream<Path> walk = Files.walk(fileSystem.getPath(SCRIPT_LOCATION), 1);
170169
Map<String, String> data = new HashMap<>();
171170
for (Iterator<Path> it = walk.iterator(); it.hasNext();) {
172171
Path script = it.next();
173172
String scriptName = script.toString();
174173
if (!SCRIPT_LOCATION.equals(scriptName)) {
175-
data.put(script.getFileName().toString(), readScript(getClass().getResourceAsStream(scriptName)));
174+
data.put(script.getFileName().toString(), readScript(scriptName));
176175
}
177176
}
178177
LOGGER.info(MessageKeys.SCRIPT_LOADED, domainNamespace);
@@ -183,14 +182,18 @@ private Map<String, String> loadScripts() {
183182
}
184183
}
185184

186-
private String readScript(InputStream inputStream) throws IOException {
187-
ByteArrayOutputStream result = new ByteArrayOutputStream();
188-
byte[] buffer = new byte[1024];
189-
int length;
190-
while ((length = inputStream.read(buffer)) != -1) {
191-
result.write(buffer, 0, length);
185+
private String readScript(String scriptName) throws IOException {
186+
try (
187+
InputStream inputStream = getClass().getResourceAsStream(scriptName);
188+
ByteArrayOutputStream result = new ByteArrayOutputStream()
189+
) {
190+
byte[] buffer = new byte[1024];
191+
int length;
192+
while ((length = inputStream.read(buffer)) != -1) {
193+
result.write(buffer, 0, length);
194+
}
195+
return result.toString();
192196
}
193-
return result.toString();
194197
}
195198
}
196199

0 commit comments

Comments
 (0)