Skip to content

Commit d2ed805

Browse files
committed
optimze imports and add error when wrong
1 parent 3afa0cf commit d2ed805

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
import com.oracle.weblogic.imagetool.util.Constants;
1212
import com.oracle.weblogic.imagetool.util.Utils;
1313
import com.oracle.weblogic.imagetool.util.ValidationResult;
14-
import picocli.CommandLine.Command;
15-
import picocli.CommandLine.Option;
1614

1715
import java.io.File;
16+
import java.io.IOException;
1817
import java.nio.file.Files;
1918
import java.nio.file.Path;
2019
import java.nio.file.Paths;
@@ -26,9 +25,11 @@
2625
import java.util.List;
2726
import java.util.Properties;
2827
import java.util.Set;
29-
import java.util.logging.FileHandler;
3028
import java.util.logging.Logger;
3129

30+
import picocli.CommandLine.Command;
31+
import picocli.CommandLine.Option;
32+
3233
@Command(
3334
name = "update",
3435
description = "Update WebLogic docker image with selected patches",
@@ -90,17 +91,27 @@ public CommandResponse call() throws Exception {
9091
String opatchVersion = baseImageProperties.getProperty("OPATCH_VERSION");
9192

9293
// We need to find out the actual version number of the opatchBugNumber - what if useCache=always ?
94+
String opatchBugNumberVersion = "";
9395

9496
if (useCache == CachePolicy.ALWAYS) {
95-
opatch_1394_required = (latestPSU || !patches.isEmpty()) &&
96-
(Utils.compareVersions(installerVersion, Constants.DEFAULT_WLS_VERSION) >= 0);
97+
String opatchFile = cacheStore.getValueFromCache(opatchBugNumber + "_opatch");
98+
if (opatchFile != null ) {
99+
opatchBugNumberVersion = Utils.getOpatchVersionFromZip(opatchFile);
100+
logger.info(String.format("OPatch patch number %s cached file %s version %s", opatchBugNumber,
101+
opatchFile, opatchBugNumberVersion ));
102+
} else {
103+
String msg = String.format("OPatch patch number --opatchBugNumber %s cannot be found in cache",
104+
opatchBugNumber);
105+
logger.severe(msg);
106+
throw new IOException(msg);
107+
}
97108
} else {
98-
String opatchBugNumberVersion = ARUUtil.getOPatchVersionByBugNumber(opatchBugNumber, userId, password);
99-
opatch_1394_required = (latestPSU || !patches.isEmpty()) &&
100-
(Utils.compareVersions(installerVersion, Constants.DEFAULT_WLS_VERSION) >= 0 &&
101-
Utils.compareVersions(opatchVersion, opatchBugNumberVersion) < 0);
109+
opatchBugNumberVersion = ARUUtil.getOPatchVersionByBugNumber(opatchBugNumber, userId, password);
102110
}
103111

112+
opatch_1394_required = (latestPSU || !patches.isEmpty()) &&
113+
(Utils.compareVersions(installerVersion, Constants.DEFAULT_WLS_VERSION) >= 0 &&
114+
Utils.compareVersions(opatchVersion, opatchBugNumberVersion) < 0);
104115

105116
//Do not update or install packages in offline only mode
106117
if (useCache != CachePolicy.ALWAYS) {

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.Comparator;
29+
import java.util.Enumeration;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
@@ -36,6 +37,8 @@
3637
import java.util.regex.Pattern;
3738
import java.util.stream.Collectors;
3839
import java.util.stream.Stream;
40+
import java.util.zip.ZipEntry;
41+
import java.util.zip.ZipFile;
3942

4043
public class Utils {
4144

@@ -566,4 +569,38 @@ public static String getBuildWorkingDir() throws IOException {
566569

567570
return workingDir;
568571
}
572+
573+
/**
574+
* Return the version number inside a opatch file
575+
* @param fileName full path to the opatch patch
576+
* @return version number of the patch
577+
*/
578+
579+
public static String getOpatchVersionFromZip(String fileName) {
580+
581+
try {
582+
ZipFile zipFile = new ZipFile(fileName);
583+
584+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
585+
586+
while (entries.hasMoreElements()) {
587+
ZipEntry entry = entries.nextElement();
588+
if (entry.getName().endsWith("/version.txt")) {
589+
InputStream stream = zipFile.getInputStream(entry);
590+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
591+
StringBuilder out = new StringBuilder();
592+
String line;
593+
while ((line = bufferedReader.readLine()) != null) {
594+
out.append(line);
595+
}
596+
return out.toString();
597+
}
598+
}
599+
} catch (IOException ioe) {
600+
ioe.printStackTrace();
601+
}
602+
return null;
603+
}
604+
605+
569606
}

0 commit comments

Comments
 (0)