Skip to content

Commit 85e1761

Browse files
authored
reduce duplicate code (#41)
* reduce duplicate code and reduce conversions between Path and String types
1 parent e5ac102 commit 85e1761

File tree

4 files changed

+74
-83
lines changed

4 files changed

+74
-83
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CreateImage.java

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
12-
import java.nio.file.attribute.PosixFilePermissions;
1312
import java.time.Duration;
1413
import java.time.Instant;
1514
import java.util.ArrayList;
@@ -57,8 +56,7 @@ public CommandResponse call() throws Exception {
5756
logger.finer("Entering CreateImage call ");
5857
Instant startTime = Instant.now();
5958

60-
Path tmpDir = null;
61-
Path tmpDir2 = null;
59+
String tmpDir = null;
6260

6361
try {
6462

@@ -68,13 +66,7 @@ public CommandResponse call() throws Exception {
6866
}
6967

7068
List<String> cmdBuilder = getInitialBuildCmd();
71-
tmpDir = Files.createTempDirectory(Paths.get(Utils.getBuildWorkingDir()),
72-
"wlsimgbuilder_temp",
73-
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x")));
74-
String tmpDirPath = tmpDir.toAbsolutePath().toString();
75-
logger.info("tmp directory used for build context: " + tmpDirPath);
76-
Path tmpPatchesDir = Files.createDirectory(Paths.get(tmpDirPath, "patches"));
77-
Files.createFile(Paths.get(tmpPatchesDir.toAbsolutePath().toString(), "dummy.txt"));
69+
tmpDir = getTempDirectory();
7870

7971
// this handles wls, jdk and wdt install files.
8072
cmdBuilder.addAll(handleInstallerFiles(tmpDir));
@@ -88,7 +80,7 @@ public CommandResponse call() throws Exception {
8880
dockerfileOptions.setBaseImage(fromImage);
8981

9082
Utils.copyResourceAsFile("/probe-env/test-create-env.sh",
91-
tmpDir.toAbsolutePath().toString() + File.separator + "test-env.sh", true);
83+
tmpDir + File.separator + "test-env.sh", true);
9284

9385
List<String> imageEnvCmd = Utils.getDockerRunCmd(tmpDir, fromImage, "test-env.sh");
9486
Properties baseImageProperties = Utils.runDockerCommand(imageEnvCmd);
@@ -111,20 +103,20 @@ public CommandResponse call() throws Exception {
111103
}
112104

113105
// build wdt args if user passes --wdtModelPath
114-
cmdBuilder.addAll(handleWDTArgsIfRequired(tmpDir));
106+
cmdBuilder.addAll(handleWdtArgsIfRequired(tmpDir));
115107

116108
// resolve required patches
117-
cmdBuilder.addAll(handlePatchFiles(tmpDir, tmpPatchesDir));
109+
cmdBuilder.addAll(handlePatchFiles(tmpDir, createPatchesTempDirectory(tmpDir)));
118110

119111
// Copy wls response file to tmpDir
120-
copyResponseFilesToDir(tmpDirPath);
112+
copyResponseFilesToDir(tmpDir);
121113

122114
// Create Dockerfile
123-
Utils.writeDockerfile(tmpDirPath + File.separator + "Dockerfile", "Create_Image.mustache",
115+
Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile", "Create_Image.mustache",
124116
dockerfileOptions);
125117

126118
// add directory to pass the context
127-
cmdBuilder.add(tmpDirPath);
119+
cmdBuilder.add(tmpDir);
128120
logger.info("docker cmd = " + String.join(" ", cmdBuilder));
129121
Utils.runDockerCommand(isCLIMode, cmdBuilder, dockerLog);
130122
} catch (Exception ex) {
@@ -146,19 +138,18 @@ public CommandResponse call() throws Exception {
146138
* @return list of strings
147139
* @throws Exception in case of error
148140
*/
149-
private List<String> handleInstallerFiles(Path tmpDir) throws Exception {
141+
private List<String> handleInstallerFiles(String tmpDir) throws Exception {
150142

151-
logger.finer("Entering CreateImage.handleInstallerFiles: " + tmpDir.toAbsolutePath().toString());
143+
logger.finer("Entering CreateImage.handleInstallerFiles: " + tmpDir);
152144
List<String> retVal = new LinkedList<>();
153-
String tmpDirPath = tmpDir.toAbsolutePath().toString();
154145
List<InstallerFile> requiredInstallers = gatherRequiredInstallers();
155-
for (InstallerFile eachInstaller : requiredInstallers) {
156-
String targetFilePath = eachInstaller.resolve(cacheStore);
157-
logger.finer("Entering CreateImage.handleInstallerFiles targetFilePath: " + targetFilePath);
158-
File targetFile = new File(targetFilePath);
146+
for (InstallerFile installerFile : requiredInstallers) {
147+
String targetFilePath = installerFile.resolve(cacheStore);
148+
logger.finer("CreateImage.handleInstallerFiles copying targetFilePath: " + targetFilePath);
149+
String filename = new File(targetFilePath).getName();
159150
try {
160-
Path targetLink = Files.copy(Paths.get(targetFilePath), Paths.get(tmpDirPath, targetFile.getName()));
161-
retVal.addAll(eachInstaller.getBuildArg(tmpDir.relativize(targetLink).toString()));
151+
Files.copy(Paths.get(targetFilePath), Paths.get(tmpDir, filename));
152+
retVal.addAll(installerFile.getBuildArg(filename));
162153
} catch (Exception ee) {
163154
ee.printStackTrace();
164155
}
@@ -168,8 +159,8 @@ private List<String> handleInstallerFiles(Path tmpDir) throws Exception {
168159
}
169160

170161
@Override
171-
List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception {
172-
logger.finer("Entering CreateImage.handlePatchFiles: " + tmpDir.toAbsolutePath().toString());
162+
List<String> handlePatchFiles(String tmpDir, Path tmpPatchesDir) throws Exception {
163+
logger.finer("Entering CreateImage.handlePatchFiles: " + tmpDir);
173164
if ((latestPSU || !patches.isEmpty()) && Utils.compareVersions(installerVersion,
174165
Constants.DEFAULT_WLS_VERSION) == 0) {
175166
addOPatch1394ToImage(tmpDir, opatchBugNumber);
@@ -213,10 +204,9 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
213204
* @return list of build args
214205
* @throws IOException in case of error
215206
*/
216-
private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
217-
logger.finer("Entering CreateImage.handleWDTArgsIfRequired: " + tmpDir.toAbsolutePath().toString());
207+
private List<String> handleWdtArgsIfRequired(String tmpDir) throws IOException {
208+
logger.finer("Entering CreateImage.handleWdtArgsIfRequired: " + tmpDir);
218209
List<String> retVal = new LinkedList<>();
219-
String tmpDirPath = tmpDir.toAbsolutePath().toString();
220210
if (wdtModelPath != null) {
221211
if (Files.isRegularFile(wdtModelPath)) {
222212
if (wdtDomainType != DomainType.WLS) {
@@ -231,17 +221,16 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
231221
}
232222
}
233223
dockerfileOptions.setWdtEnabled();
234-
Path targetLink = Files.copy(wdtModelPath, Paths.get(tmpDirPath, wdtModelPath.getFileName().toString())
235-
);
224+
String modelFilename = wdtModelPath.getFileName().toString();
225+
Files.copy(wdtModelPath, Paths.get(tmpDir, modelFilename));
236226
retVal.add(Constants.BUILD_ARG);
237-
retVal.add("WDT_MODEL=" + tmpDir.relativize(targetLink).toString());
227+
retVal.add("WDT_MODEL=" + modelFilename);
238228

239229
if (wdtArchivePath != null && Files.isRegularFile(wdtArchivePath)) {
240-
targetLink = Files.copy(wdtArchivePath, Paths.get(tmpDirPath,
241-
wdtArchivePath.getFileName().toString())
242-
);
230+
String archiveFilename = wdtArchivePath.getFileName().toString();
231+
Files.copy(wdtArchivePath, Paths.get(tmpDir, archiveFilename));
243232
retVal.add(Constants.BUILD_ARG);
244-
retVal.add("WDT_ARCHIVE=" + tmpDir.relativize(targetLink).toString());
233+
retVal.add("WDT_ARCHIVE=" + archiveFilename);
245234
}
246235

247236
if (wdtDomainHome != null) {
@@ -251,15 +240,14 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
251240

252241

253242
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
254-
targetLink = Files.copy(wdtVariablesPath, Paths.get(tmpDirPath,
255-
wdtVariablesPath.getFileName().toString())
256-
);
243+
String variableFileName = wdtVariablesPath.getFileName().toString();
244+
Files.copy(wdtVariablesPath, Paths.get(tmpDir, variableFileName));
257245
retVal.add(Constants.BUILD_ARG);
258-
retVal.add("WDT_VARIABLE=" + tmpDir.relativize(targetLink).toString());
259-
retVal.addAll(getWDTRequiredBuildArgs(wdtVariablesPath));
246+
retVal.add("WDT_VARIABLE=" + variableFileName);
247+
retVal.addAll(getWdtRequiredBuildArgs(wdtVariablesPath));
260248
}
261249

262-
Path tmpScriptsDir = Files.createDirectory(Paths.get(tmpDirPath, "scripts"));
250+
Path tmpScriptsDir = Files.createDirectory(Paths.get(tmpDir, "scripts"));
263251
String toScriptsPath = tmpScriptsDir.toAbsolutePath().toString();
264252
Utils.copyResourceAsFile("/container-scripts/startAdminServer.sh", toScriptsPath, true);
265253
Utils.copyResourceAsFile("/container-scripts/startManagedServer.sh", toScriptsPath, true);
@@ -268,7 +256,7 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
268256
throw new IOException("WDT model file " + wdtModelPath + " not found");
269257
}
270258
}
271-
logger.finer("Exiting CreateImage.handleWDTArgsIfRequired: ");
259+
logger.finer("Exiting CreateImage.handleWdtArgsIfRequired: ");
272260
return retVal;
273261
}
274262

@@ -279,8 +267,8 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
279267
* @return list of build args
280268
* @throws IOException in case of error
281269
*/
282-
private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOException {
283-
logger.finer("Entering CreateImage.getWDTRequiredBuildArgs: " + wdtVariablesPath.toAbsolutePath().toString());
270+
private List<String> getWdtRequiredBuildArgs(Path wdtVariablesPath) throws IOException {
271+
logger.finer("Entering CreateImage.getWdtRequiredBuildArgs: " + wdtVariablesPath.toAbsolutePath().toString());
284272
List<String> retVal = new LinkedList<>();
285273
Properties variableProps = new Properties();
286274
variableProps.load(new FileInputStream(wdtVariablesPath.toFile()));
@@ -292,7 +280,7 @@ private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
292280
retVal.add(Constants.BUILD_ARG);
293281
retVal.add(((String) x).toUpperCase() + "=" + variableProps.getProperty((String) x));
294282
});
295-
logger.finer("Exiting CreateImage.getWDTRequiredBuildArgs: ");
283+
logger.finer("Exiting CreateImage.getWdtRequiredBuildArgs: ");
296284
return retVal;
297285
}
298286

@@ -309,7 +297,7 @@ private List<InstallerFile> gatherRequiredInstallers() throws Exception {
309297
if (wdtModelPath != null && Files.isRegularFile(wdtModelPath)) {
310298
InstallerFile wdtInstaller = new InstallerFile(useCache, InstallerType.WDT, wdtVersion, null, null);
311299
retVal.add(wdtInstaller);
312-
addWDTURL(wdtInstaller.getKey());
300+
addWdtUrl(wdtInstaller.getKey());
313301
}
314302
retVal.add(new InstallerFile(useCache, InstallerType.fromValue(installerType.toString()), installerVersion,
315303
userId, password));
@@ -326,9 +314,9 @@ private List<InstallerFile> gatherRequiredInstallers() throws Exception {
326314
* @param wdtKey key in the format wdt_0.17
327315
* @throws Exception in case of error
328316
*/
329-
private void addWDTURL(String wdtKey) throws Exception {
317+
private void addWdtUrl(String wdtKey) throws Exception {
330318
logger.finer("Entering CreateImage.wdtKey: ");
331-
String wdtURLKey = wdtKey + "_url";
319+
String wdtUrlKey = wdtKey + "_url";
332320
if (cacheStore.getValueFromCache(wdtKey) == null) {
333321
if (userId == null || password == null) {
334322
throw new Exception("CachePolicy prohibits download. Add the required wdt installer to cache");
@@ -339,7 +327,7 @@ private void addWDTURL(String wdtKey) throws Exception {
339327
if (wdtTags.contains(tagToMatch)) {
340328
String downloadLink = String.format(Constants.WDT_URL_FORMAT, tagToMatch);
341329
logger.info("WDT Download link = " + downloadLink);
342-
cacheStore.addToCache(wdtURLKey, downloadLink);
330+
cacheStore.addToCache(wdtUrlKey, downloadLink);
343331
} else {
344332
throw new Exception("Couldn't find WDT download url for version:" + wdtVersion);
345333
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/ImageOperation.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
11+
import java.nio.file.attribute.PosixFilePermissions;
1112
import java.util.ArrayList;
1213
import java.util.LinkedList;
1314
import java.util.List;
@@ -89,7 +90,7 @@ private String handlePasswordOptions() throws IOException {
8990
* @return list of strings
9091
* @throws Exception in case of error
9192
*/
92-
List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception {
93+
List<String> handlePatchFiles(String tmpDir, Path tmpPatchesDir) throws Exception {
9394
logger.finer("Entering ImageOperation.handlePatchFiles");
9495
List<String> retVal = new LinkedList<>();
9596
List<String> patchLocations = new LinkedList<>();
@@ -130,7 +131,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
130131
}
131132
if (!patchLocations.isEmpty()) {
132133
retVal.add(Constants.BUILD_ARG);
133-
retVal.add("PATCHDIR=" + tmpDir.relativize(tmpPatchesDir).toString());
134+
retVal.add("PATCHDIR=" + Paths.get(tmpDir).relativize(tmpPatchesDir).toString());
134135
dockerfileOptions.setPatchingEnabled();
135136
}
136137
logger.finer("Exiting ImageOperation.handlePatchFiles");
@@ -173,14 +174,28 @@ List<String> getInitialBuildCmd() {
173174
return cmdBuilder;
174175
}
175176

177+
public String getTempDirectory() throws IOException {
178+
Path tmpDir = Files.createTempDirectory(Paths.get(Utils.getBuildWorkingDir()), "wlsimgbuilder_temp",
179+
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x")));
180+
String pathAsString = tmpDir.toAbsolutePath().toString();
181+
logger.info("tmp directory used for docker build context: " + pathAsString);
182+
return pathAsString;
183+
}
184+
185+
public Path createPatchesTempDirectory(String tmpDir) throws IOException {
186+
Path tmpPatchesDir = Files.createDirectory(Paths.get(tmpDir, "patches"));
187+
Files.createFile(Paths.get(tmpPatchesDir.toAbsolutePath().toString(), "dummy.txt"));
188+
return tmpPatchesDir;
189+
}
190+
176191
void handleProxyUrls() throws IOException {
177192
httpProxyUrl = Utils.findProxyUrl(httpProxyUrl, Constants.HTTP);
178193
httpsProxyUrl = Utils.findProxyUrl(httpsProxyUrl, Constants.HTTPS);
179194
nonProxyHosts = Utils.findProxyUrl(nonProxyHosts, "none");
180195
Utils.setProxyIfRequired(httpProxyUrl, httpsProxyUrl, nonProxyHosts);
181196
}
182197

183-
void addOPatch1394ToImage(Path tmpDir, String opatchBugNumber) throws Exception {
198+
void addOPatch1394ToImage(String tmpDir, String opatchBugNumber) throws Exception {
184199
// opatch patch now is in the format #####_opatch in the cache store
185200
// So the version passing to the constructor of PatchFile is also "opatch".
186201
// since opatch releases is on it's own and there is not really a patch to opatch
@@ -189,7 +204,7 @@ void addOPatch1394ToImage(Path tmpDir, String opatchBugNumber) throws Exception
189204
String filePath =
190205
new PatchFile(useCache, Constants.OPATCH_PATCH_TYPE, Constants.OPATCH_PATCH_TYPE, opatchBugNumber,
191206
userId, password).resolve(cacheStore);
192-
Files.copy(Paths.get(filePath), Paths.get(tmpDir.toAbsolutePath().toString(), new File(filePath).getName()));
207+
Files.copy(Paths.get(filePath), Paths.get(tmpDir, new File(filePath).getName()));
193208
dockerfileOptions.setOPatchPatchingEnabled();
194209
}
195210

0 commit comments

Comments
 (0)