Skip to content

Commit 015e9a3

Browse files
authored
Null bundle (#208)
* prevent NPE when resource bundle fails to load * fix NPE for PatchNotFoundException * remove ANSI color markers from log messages sent to a file * improve usability when version of OPatch is provided by user
1 parent e663e9f commit 015e9a3

File tree

9 files changed

+61
-12
lines changed

9 files changed

+61
-12
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ void verifyResponse(Document response) throws AruException, XPathExpressionExcep
359359
String errorId = XPathUtil.string(response, "/results/error/id");
360360
AruException error;
361361
if ("10-016".equals(errorId)) {
362-
error = new NoPatchesFoundException();
362+
error = new NoPatchesFoundException(errorMessage);
363363
} else {
364364
error = new AruException(errorMessage);
365365
}
@@ -394,7 +394,11 @@ private List<AruPatch> getPatches(String bugNumber, String userId, String passwo
394394
String url = String.format(BUG_SEARCH_URL, bugNumber);
395395
logger.info("IMG-0063", bugNumber);
396396
Document response = HttpUtil.getXMLContent(url, userId, password);
397-
verifyResponse(response);
397+
try {
398+
verifyResponse(response);
399+
} catch (NoPatchesFoundException patchEx) {
400+
throw new NoPatchesFoundException(Utils.getMessage("IMG-0086", bugNumber), patchEx);
401+
}
398402
return AruPatch.getPatches(response, patchSelector);
399403
}
400404

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/NoPatchesFoundException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
package com.oracle.weblogic.imagetool.aru;
55

66
public class NoPatchesFoundException extends AruException {
7-
public NoPatchesFoundException() {
8-
super();
7+
public NoPatchesFoundException(String msg) {
8+
super(msg);
9+
}
10+
11+
public NoPatchesFoundException(String msg, Throwable throwable) {
12+
super(msg, throwable);
913
}
1014
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/OPatchFile.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,23 @@ public OPatchFile(String patchId, String userId, String password, CacheStore cac
3636

3737
// when offline, use the local cache to determine newest version available for OPatch
3838
if (userId == null && password == null) {
39-
setVersion(getLatestCachedVersion(cache, getDefaultBugNum(patchId)));
39+
if (patchId != null && patchId.contains(CacheStore.CACHE_KEY_SEPARATOR)) {
40+
setVersion(patchId.substring(patchId.indexOf(CacheStore.CACHE_KEY_SEPARATOR) + 1));
41+
} else {
42+
setVersion(getLatestCachedVersion(cache, getDefaultBugNum(patchId)));
43+
}
4044
}
4145
}
4246

4347
private static String getDefaultBugNum(String patchId) {
4448
if (patchId == null) {
4549
return DEFAULT_BUG_NUM;
4650
} else {
47-
return patchId;
51+
if (patchId.contains(CacheStore.CACHE_KEY_SEPARATOR)) {
52+
return patchId.substring(0, patchId.indexOf(CacheStore.CACHE_KEY_SEPARATOR));
53+
} else {
54+
return patchId;
55+
}
4856
}
4957
}
5058

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/ImageTool.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ public static void main(String[] args) {
5858

5959
if (response != null) {
6060
if (response.getStatus() != 0) {
61-
String message = String.format("Response code: %d, message: %s", response.getStatus(),
62-
response.getMessage());
63-
logger.severe(message);
61+
logger.severe(response.getMessage());
6462
} else if (!response.getMessage().isEmpty()) {
6563
logger.info(response.getMessage());
6664
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/logging/ConsoleFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public class ConsoleFormatter extends Formatter {
2020
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
21-
private static final Pattern colorPattern = Pattern.compile("\\[\\[([a-z]+): (.+?)]]");
21+
public static final Pattern colorPattern = Pattern.compile("\\[\\[([a-z]+): (.+?)]]");
2222

2323
static {
2424
// if user is redirecting output, do not print color codes

imagetool/src/main/java/com/oracle/weblogic/imagetool/logging/FileFormatter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Date;
1010
import java.util.logging.Formatter;
1111
import java.util.logging.LogRecord;
12+
import java.util.regex.Matcher;
1213
import java.util.regex.Pattern;
1314

1415
/**
@@ -34,6 +35,31 @@ public FileFormatter() {
3435
this.formatter = new MessageFormat(DATE_FORMAT_STRING);
3536
}
3637

38+
/**
39+
* Remove ANSI color tokens from messages to be logged to a file.
40+
* @param text message to be logged
41+
* @return message with color tokens removed
42+
*/
43+
private String replaceColorTokens(String text) {
44+
Matcher matcher = ConsoleFormatter.colorPattern.matcher(text);
45+
46+
StringBuilder builder = new StringBuilder();
47+
int i = 0;
48+
while (matcher.find()) {
49+
AnsiColor replacement = AnsiColor.fromValue(matcher.group(1));
50+
builder.append(text, i, matcher.start());
51+
if (replacement == null) {
52+
builder.append(matcher.group(0));
53+
} else {
54+
// remove color flags from messages logged to file
55+
builder.append(matcher.group(2));
56+
}
57+
i = matcher.end();
58+
}
59+
builder.append(text.substring(i));
60+
return builder.toString();
61+
}
62+
3763
/**
3864
* Formats the log record.
3965
*
@@ -74,7 +100,7 @@ public synchronized String format(LogRecord record) {
74100
sb.append(">");
75101

76102
String messageKey = record.getMessage();
77-
String message = formatMessage(record);
103+
String message = replaceColorTokens(formatMessage(record));
78104

79105
if (messageKey != null) {
80106
sb.append(" <");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ private static boolean validFile(Path file) throws IOException {
726726
*/
727727
public static String getMessage(@PropertyKey(resourceBundle = "ImageTool") String key, Object... params) {
728728
String message = key;
729-
if (bundle.containsKey(key)) {
729+
if (bundle != null && bundle.containsKey(key)) {
730730
message = bundle.getString(key);
731731
}
732732
if (params == null || params.length == 0) {

imagetool/src/main/resources/ImageTool.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@ IMG-0082=Failed to determine release number for product {0}, version {1}
8484
IMG-0083=Version {0} for patch ID {1} was not found in the available versions: {2}
8585
IMG-0084=No recommended patches found for {0}
8686
IMG-0085=The recommended ADR patch was skipped for the WLS installer, use --patch {0} to apply this patch
87+
IMG-0086=Failed to find patch number [[brightred: {0}]] on Oracle servers, verify the patch number is correct using My Oracle Support.

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CachedFileTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@ void latestOpatchVersion() throws IOException, AruException, XPathExpressionExce
107107
assertEquals(DEFAULT_BUG_NUM + "_13.9.4.0.0", patchFile.getKey(),
108108
"failed to get latest Opatch version from the cache");
109109
}
110+
111+
@Test
112+
void specificOpatchVersion() throws IOException, AruException, XPathExpressionException {
113+
// OPatch file should default to the default OPatch bug number and the latest version found in cache
114+
OPatchFile patchFile = new OPatchFile(DEFAULT_BUG_NUM + "_13.9.2.2.2", null, null, cacheStore);
115+
assertEquals(DEFAULT_BUG_NUM + "_13.9.2.2.2", patchFile.getKey(),
116+
"failed to get specific Opatch version from the cache");
117+
}
110118
}

0 commit comments

Comments
 (0)