Skip to content

Commit c3049b6

Browse files
committed
Extract FileGroupReader
1 parent 28891d2 commit c3049b6

File tree

6 files changed

+221
-74
lines changed

6 files changed

+221
-74
lines changed

operator/pom.xml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<version>1.0</version>
1111
</parent>
1212

13-
<groupId>oracle.kubernetes</groupId>
1413
<artifactId>weblogic-kubernetes-operator</artifactId>
15-
<version>1.0</version>
1614

1715
<description>Oracle Weblogic Server Kubernetes Operator</description>
1816
<name>operator-runtime</name>
@@ -131,6 +129,19 @@
131129
</compilerArgs>
132130
</configuration>
133131
</plugin>
132+
<plugin>
133+
<groupId>org.apache.maven.plugins</groupId>
134+
<artifactId>maven-failsafe-plugin</artifactId>
135+
<version>2.22.0</version>
136+
<executions>
137+
<execution>
138+
<goals>
139+
<goal>integration-test</goal>
140+
<goal>verify</goal>
141+
</goals>
142+
</execution>
143+
</executions>
144+
</plugin>
134145

135146
<plugin>
136147
<groupId>com.coveo</groupId>
@@ -332,17 +343,4 @@
332343

333344
</dependencies>
334345

335-
<profiles>
336-
<profile>
337-
<id>default</id>
338-
<activation>
339-
<activeByDefault>true</activeByDefault>
340-
</activation>
341-
<properties>
342-
<surefireArgLine></surefireArgLine>
343-
<failsafeArgLine></failsafeArgLine>
344-
</properties>
345-
</profile>
346-
</profiles>
347-
348346
</project>

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

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@
77
import io.kubernetes.client.ApiException;
88
import io.kubernetes.client.models.V1ConfigMap;
99
import io.kubernetes.client.models.V1ObjectMeta;
10-
import java.io.IOException;
11-
import java.net.URI;
12-
import java.net.URISyntaxException;
13-
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.FileSystem;
15-
import java.nio.file.FileSystems;
16-
import java.nio.file.Files;
17-
import java.nio.file.Path;
18-
import java.nio.file.Paths;
19-
import java.util.Collections;
2010
import java.util.HashMap;
2111
import java.util.List;
2212
import java.util.Map;
23-
import java.util.stream.Collectors;
24-
import java.util.stream.Stream;
2513
import oracle.kubernetes.operator.KubernetesConstants;
2614
import oracle.kubernetes.operator.LabelConstants;
2715
import oracle.kubernetes.operator.ProcessingConstants;
@@ -36,10 +24,11 @@
3624
public class ConfigMapHelper {
3725
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
3826

39-
private static final String SCRIPTS = "scripts";
40-
private static final String SCRIPT_LOCATION = "/" + SCRIPTS;
27+
private static final String SCRIPT_LOCATION = "/scripts";
4128
private static final ConfigMapComparator COMPARATOR = new ConfigMapComparatorImpl();
4229

30+
private static FileGroupReader scriptReader = new FileGroupReader(SCRIPT_LOCATION);
31+
4332
private ConfigMapHelper() {}
4433

4534
/**
@@ -198,56 +187,20 @@ protected V1ConfigMap computeDomainConfigMap() {
198187
metadata.setLabels(labels);
199188

200189
cm.setMetadata(metadata);
201-
cm.setData(loadScripts(domainNamespace));
190+
cm.setData(loadScriptsFromClasspath());
202191

203192
return cm;
204193
}
205194

206-
private static synchronized Map<String, String> loadScripts(String domainNamespace) {
207-
URI uri;
208-
try {
209-
uri = ScriptConfigMapStep.class.getResource(SCRIPT_LOCATION).toURI();
210-
} catch (URISyntaxException e) {
211-
LOGGER.warning(MessageKeys.EXCEPTION, e);
212-
throw new RuntimeException(e);
213-
}
214-
215-
try {
216-
if ("jar".equals(uri.getScheme())) {
217-
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
218-
return walkScriptsPath(fileSystem.getPath(SCRIPTS), domainNamespace);
219-
}
220-
} else {
221-
return walkScriptsPath(Paths.get(uri), domainNamespace);
222-
}
223-
} catch (IOException e) {
224-
LOGGER.warning(MessageKeys.EXCEPTION, e);
225-
throw new RuntimeException(e);
226-
}
227-
}
228-
229-
private static Map<String, String> walkScriptsPath(Path scriptsDir, String domainNamespace)
230-
throws IOException {
231-
try (Stream<Path> walk = Files.walk(scriptsDir, 1)) {
232-
Map<String, String> data =
233-
walk.filter(i -> !Files.isDirectory(i))
234-
.collect(
235-
Collectors.toMap(
236-
i -> i.getFileName().toString(),
237-
i -> new String(read(i), StandardCharsets.UTF_8)));
238-
LOGGER.info(MessageKeys.SCRIPT_LOADED, domainNamespace);
239-
return data;
240-
}
195+
private synchronized Map<String, String> loadScriptsFromClasspath() {
196+
Map<String, String> scripts = scriptReader.loadFilesFromClasspath();
197+
LOGGER.info(MessageKeys.SCRIPT_LOADED, domainNamespace);
198+
return scripts;
241199
}
200+
}
242201

243-
private static byte[] read(Path path) {
244-
try {
245-
return Files.readAllBytes(path);
246-
} catch (IOException io) {
247-
LOGGER.warning(MessageKeys.EXCEPTION, io);
248-
}
249-
return null;
250-
}
202+
static FileGroupReader getScriptReader() {
203+
return scriptReader;
251204
}
252205

253206
interface ConfigMapComparator {
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.helpers;
6+
7+
import java.io.IOException;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.FileSystem;
12+
import java.nio.file.FileSystems;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.Collections;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
20+
import oracle.kubernetes.operator.logging.LoggingFacade;
21+
import oracle.kubernetes.operator.logging.LoggingFactory;
22+
import oracle.kubernetes.operator.logging.MessageKeys;
23+
24+
/**
25+
* This class can load a group of files under a specified classpath directory into a map. It handles
26+
* both files on the file system and in a JAR.
27+
*/
28+
class FileGroupReader {
29+
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
30+
31+
private String pathToGroup;
32+
33+
/**
34+
* Creates a reader for a specific file location.
35+
*
36+
* @param pathToGroup the top-level directory containing the files, relative to the classpath.
37+
*/
38+
FileGroupReader(String pathToGroup) {
39+
this.pathToGroup = pathToGroup;
40+
}
41+
42+
/**
43+
* Loads the files at the defined location within the classpath into a map.
44+
*
45+
* @return a map of file paths to string contents.
46+
*/
47+
Map<String, String> loadFilesFromClasspath() {
48+
try {
49+
try (ScriptPath scriptPath = getScriptPath()) {
50+
return loadContents(scriptPath.getScriptsDir());
51+
}
52+
} catch (Exception e) {
53+
LOGGER.warning(MessageKeys.EXCEPTION, e);
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
58+
private ScriptPath getScriptPath() throws URISyntaxException, IOException {
59+
URI uri = getClass().getResource(pathToGroup).toURI();
60+
return isJar(uri) ? new JarScriptPath(uri) : new FileScriptPath(uri);
61+
}
62+
63+
private boolean isJar(URI uri) {
64+
return "jar".equals(uri.getScheme());
65+
}
66+
67+
/**
68+
* Given a file path, loads the contents of the files into a map.
69+
*
70+
* @param rootDir the path to the top-level directory
71+
* @return a map of file paths to string contents.
72+
* @throws IOException if an error occurs during the read
73+
*/
74+
static Map<String, String> loadContents(Path rootDir) throws IOException {
75+
try (Stream<Path> walk = Files.walk(rootDir, 1)) {
76+
return walk.filter(path -> !Files.isDirectory(path))
77+
.collect(Collectors.toMap(FileGroupReader::asString, FileGroupReader::readContents));
78+
}
79+
}
80+
81+
private static String asString(Path path) {
82+
return path.getFileName().toString();
83+
}
84+
85+
private static String readContents(Path path) {
86+
try {
87+
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
88+
} catch (IOException io) {
89+
LOGGER.warning(MessageKeys.EXCEPTION, io);
90+
return "";
91+
}
92+
}
93+
94+
interface ScriptPath extends AutoCloseable {
95+
Path getScriptsDir();
96+
}
97+
98+
class FileScriptPath implements ScriptPath {
99+
private URI uri;
100+
101+
FileScriptPath(URI uri) {
102+
this.uri = uri;
103+
}
104+
105+
@Override
106+
public Path getScriptsDir() {
107+
return Paths.get(uri);
108+
}
109+
110+
@Override
111+
public void close() {}
112+
}
113+
114+
class JarScriptPath implements ScriptPath {
115+
private FileSystem fileSystem;
116+
117+
JarScriptPath(URI uri) throws IOException {
118+
System.out.println("loading from " + uri);
119+
fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
120+
}
121+
122+
@Override
123+
public Path getScriptsDir() {
124+
return fileSystem.getPath(pathToGroup);
125+
}
126+
127+
@Override
128+
public void close() throws Exception {
129+
fileSystem.close();
130+
}
131+
}
132+
}

operator/src/test/java/oracle/kubernetes/operator/helpers/ConfigMapHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class ConfigMapHelperTest {
4444
private static final String DOMAIN_NS = "namespace";
4545
private static final String OPERATOR_NS = "operator";
46-
private static final String[] SCRIPT_NAMES = {
46+
static final String[] SCRIPT_NAMES = {
4747
"livenessProbe.sh",
4848
"readinessProbe.sh",
4949
"readState.sh",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.helpers;
6+
7+
import static oracle.kubernetes.operator.helpers.ConfigMapHelperTest.SCRIPT_NAMES;
8+
import static org.hamcrest.Matchers.containsInAnyOrder;
9+
import static org.hamcrest.junit.MatcherAssert.assertThat;
10+
11+
import java.util.Map;
12+
import org.junit.Test;
13+
14+
public class FileGroupReaderIT {
15+
16+
private final FileGroupReader scriptReader = ConfigMapHelper.getScriptReader();
17+
18+
@Test
19+
public void afterLoadScriptsFromClasspath_haveScriptNamesAsKeys() {
20+
Map<String, String> scripts = scriptReader.loadFilesFromClasspath();
21+
assertThat(scripts.keySet(), containsInAnyOrder(SCRIPT_NAMES));
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.helpers;
6+
7+
import static oracle.kubernetes.operator.helpers.ConfigMapHelperTest.SCRIPT_NAMES;
8+
import static org.hamcrest.Matchers.containsInAnyOrder;
9+
import static org.hamcrest.Matchers.hasEntry;
10+
import static org.hamcrest.junit.MatcherAssert.assertThat;
11+
12+
import java.io.IOException;
13+
import java.nio.file.FileSystem;
14+
import java.nio.file.Path;
15+
import java.util.Map;
16+
import oracle.kubernetes.operator.utils.InMemoryFileSystem;
17+
import org.junit.Test;
18+
19+
public class FileGroupReaderTest {
20+
21+
private final FileGroupReader scriptReader = ConfigMapHelper.getScriptReader();
22+
private static FileSystem fileSystem = InMemoryFileSystem.getInstance();
23+
24+
@Test
25+
public void afterLoadScriptsFromClasspath_haveScriptNamesAsKeys() {
26+
Map<String, String> scripts = scriptReader.loadFilesFromClasspath();
27+
assertThat(scripts.keySet(), containsInAnyOrder(SCRIPT_NAMES));
28+
}
29+
30+
@Test
31+
public void loadFilesFromMemory() throws IOException {
32+
InMemoryFileSystem.defineFile("group/a.b", "1234");
33+
InMemoryFileSystem.defineFile("group/x/c.d", "5678");
34+
35+
Path p = fileSystem.getPath("group");
36+
Map<String, String> map = FileGroupReader.loadContents(p);
37+
38+
assertThat(map, hasEntry("group/a.b", "1234"));
39+
assertThat(map, hasEntry("group/x/c.d", "5678"));
40+
}
41+
}

0 commit comments

Comments
 (0)