|
3 | 3 |
|
4 | 4 | package oracle.kubernetes.operator.helpers;
|
5 | 5 |
|
6 |
| -import java.io.ByteArrayOutputStream; |
7 | 6 | import java.io.IOException;
|
8 |
| -import java.io.InputStream; |
9 | 7 | import java.net.URI;
|
10 | 8 | import java.net.URISyntaxException;
|
| 9 | +import java.nio.charset.StandardCharsets; |
11 | 10 | import java.nio.file.FileSystem;
|
12 | 11 | import java.nio.file.FileSystems;
|
13 | 12 | import java.nio.file.Files;
|
14 | 13 | import java.nio.file.Path;
|
| 14 | +import java.nio.file.Paths; |
15 | 15 | import java.util.Collections;
|
16 | 16 | import java.util.HashMap;
|
17 |
| -import java.util.Iterator; |
18 | 17 | import java.util.List;
|
19 | 18 | import java.util.Map;
|
| 19 | +import java.util.stream.Collectors; |
20 | 20 | import java.util.stream.Stream;
|
21 | 21 |
|
22 | 22 | import io.kubernetes.client.ApiException;
|
|
25 | 25 | import oracle.kubernetes.operator.KubernetesConstants;
|
26 | 26 | import oracle.kubernetes.operator.LabelConstants;
|
27 | 27 | import oracle.kubernetes.operator.ProcessingConstants;
|
28 |
| -import oracle.kubernetes.operator.WebLogicConstants; |
29 | 28 | import oracle.kubernetes.operator.logging.LoggingFacade;
|
30 | 29 | import oracle.kubernetes.operator.logging.LoggingFactory;
|
31 | 30 | import oracle.kubernetes.operator.logging.MessageKeys;
|
|
37 | 36 | public class ConfigMapHelper {
|
38 | 37 | private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
|
39 | 38 |
|
40 |
| - private static final String SCRIPT_LOCATION = "/scripts"; |
| 39 | + private static final String SCRIPTS = "scripts"; |
| 40 | + private static final String SCRIPT_LOCATION = "/" + SCRIPTS; |
41 | 41 |
|
42 | 42 | private ConfigMapHelper() {}
|
43 | 43 |
|
@@ -163,38 +163,38 @@ private synchronized Map<String, String> loadScripts() {
|
163 | 163 | LOGGER.warning(MessageKeys.EXCEPTION, e);
|
164 | 164 | throw new RuntimeException(e);
|
165 | 165 | }
|
166 |
| - |
167 |
| - try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap())) { |
168 |
| - Stream<Path> walk = Files.walk(fileSystem.getPath(SCRIPT_LOCATION), 1); |
169 |
| - Map<String, String> data = new HashMap<>(); |
170 |
| - for (Iterator<Path> it = walk.iterator(); it.hasNext();) { |
171 |
| - Path script = it.next(); |
172 |
| - String scriptName = script.toString(); |
173 |
| - if (!SCRIPT_LOCATION.equals(scriptName)) { |
174 |
| - data.put(script.getFileName().toString(), readScript(scriptName)); |
| 166 | + |
| 167 | + try { |
| 168 | + if ("jar".equals(uri.getScheme())) { |
| 169 | + try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap())) { |
| 170 | + return walkScriptsPath(fileSystem.getPath(SCRIPTS)); |
175 | 171 | }
|
| 172 | + } else { |
| 173 | + return walkScriptsPath(Paths.get(uri)); |
176 | 174 | }
|
177 |
| - LOGGER.info(MessageKeys.SCRIPT_LOADED, domainNamespace); |
178 |
| - return data; |
179 | 175 | } catch (IOException e) {
|
180 | 176 | LOGGER.warning(MessageKeys.EXCEPTION, e);
|
181 | 177 | throw new RuntimeException(e);
|
182 | 178 | }
|
183 | 179 | }
|
184 |
| - |
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(); |
| 180 | + |
| 181 | + private Map<String, String> walkScriptsPath(Path scriptsDir) throws IOException { |
| 182 | + try (Stream<Path> walk = Files.walk(scriptsDir, 1)) { |
| 183 | + Map<String, String> data = walk.filter(i -> !Files.isDirectory(i)).collect(Collectors.toMap( |
| 184 | + i -> i.getFileName().toString(), |
| 185 | + i -> new String(read(i), StandardCharsets.UTF_8))); |
| 186 | + LOGGER.info(MessageKeys.SCRIPT_LOADED, domainNamespace); |
| 187 | + return data; |
196 | 188 | }
|
197 | 189 | }
|
| 190 | + |
| 191 | + private byte[] read(Path path) { |
| 192 | + try { |
| 193 | + return Files.readAllBytes(path); |
| 194 | + } catch (IOException io) { |
| 195 | + LOGGER.warning(MessageKeys.EXCEPTION, io); |
| 196 | + } |
| 197 | + return null; |
| 198 | + } |
198 | 199 | }
|
199 |
| - |
200 | 200 | }
|
0 commit comments