Skip to content

Commit fb98ac2

Browse files
fixing PSU parsing to properly handle 12.2.1.3 PSUs (#1310)
* fixing PSU parsing to properly handle 12.2.1.3 PSUs * adding support for old 12.1.x-style PSUs * cleaning up i18n messages
1 parent a82449d commit fb98ac2

File tree

3 files changed

+162
-33
lines changed

3 files changed

+162
-33
lines changed

core/src/main/java/oracle/weblogic/deploy/util/XPathUtil.java

Lines changed: 126 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.util;
6-
import org.w3c.dom.Document;
7-
import org.w3c.dom.Node;
8-
import org.xml.sax.SAXException;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.file.DirectoryStream;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
920

1021
import javax.xml.XMLConstants;
1122
import javax.xml.parsers.DocumentBuilder;
@@ -15,22 +26,26 @@
1526
import javax.xml.xpath.XPathConstants;
1627
import javax.xml.xpath.XPathExpressionException;
1728
import javax.xml.xpath.XPathFactory;
18-
import java.io.File;
19-
import java.io.IOException;
20-
import java.nio.file.DirectoryStream;
21-
import java.nio.file.Files;
22-
import java.nio.file.Path;
23-
import java.nio.file.Paths;
24-
import java.util.*;
2529

2630
import oracle.weblogic.deploy.logging.PlatformLogger;
2731
import oracle.weblogic.deploy.logging.WLSDeployLogFactory;
2832

33+
import org.w3c.dom.Document;
34+
import org.w3c.dom.Node;
35+
import org.xml.sax.SAXException;
36+
2937
/*
3038
* Parse the xml file at the designated location for the PSU value
3139
*/
3240
public class XPathUtil {
3341
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy.util");
42+
private static final String PSU_DESCRIPTION_REGEX_OLD =
43+
"^WebLogic Server (\\d+(\\.\\d+){3,5}) PSU Patch.*$";
44+
private static final String PSU_DESCRIPTION_REGEX_NEW =
45+
"^WLS PATCH SET UPDATE (\\d+(\\.\\d+){3,5})(\\(ID:(\\d+)\\.\\d+\\))?$";
46+
private static final Pattern PSU_DESCRIPTION_PATTERN_OLD = Pattern.compile(PSU_DESCRIPTION_REGEX_OLD);
47+
private static final Pattern PSU_DESCRIPTION_PATTERN_NEW = Pattern.compile(PSU_DESCRIPTION_REGEX_NEW);
48+
3449
String oracleHome;
3550
String patchesHome;
3651
public XPathUtil(String oracleHome){
@@ -52,8 +67,18 @@ private static synchronized XPathFactory factory() {
5267
}
5368

5469
/*
55-
* Get the PSU if one exists at the inventory/patches files. Look at the description
56-
* for the PSU wording.
70+
* Get the PSU if one exists at the inventory/patches files (only works for WLS installations using OPatch).
71+
* Look at the description for the PSU wording. There are three known formats:
72+
*
73+
* - WLS PATCH SET UPDATE <version>.<PSU>
74+
* - WLS PATCH SET UPDATE <version>.0(ID:<PSU>.<number>)
75+
* - WebLogic Server <version>.<PSU> PSU Patch for BUG<number> <full date and time>
76+
*
77+
* The second format contains the PSU number in the first part of the ID. In at least one
78+
* case, the PSU number has a 4 digit year so the extractPsu() method is using this methodology
79+
* to compute the PSU in this case.
80+
*
81+
* The third format was used for early 12.1.2/12.1.3 PSUs.
5782
*/
5883
public String getPSU() {
5984
// find the names in the directory first
@@ -63,29 +88,105 @@ public String getPSU() {
6388
}
6489
List<String> patchFiles = findPatchFiles();
6590
List<String> list = new ArrayList<>();
66-
for (String patch_file : patchFiles){
67-
Document doc = readXmlFile(patch_file);
91+
for (String patchFile : patchFiles){
92+
Document doc = readXmlFile(patchFile);
6893
String descrip = description(doc, "//@description");
6994
LOGGER.fine("Description {0}", descrip);
70-
if (descrip != null && descrip.startsWith("WLS PATCH SET UPDATE")) {
71-
String psu = extractPsu(descrip);
72-
list.add(psu);
73-
Collections.sort(list);
74-
return list.get(list.size() -1);
95+
if (!StringUtils.isEmpty(descrip)) {
96+
String psu = null;
97+
Matcher matcher = PSU_DESCRIPTION_PATTERN_NEW.matcher(descrip);
98+
if (matcher.matches()) {
99+
psu = extractNewPsu(matcher);
100+
} else {
101+
matcher = PSU_DESCRIPTION_PATTERN_OLD.matcher(descrip);
102+
if (matcher.matches()) {
103+
psu = extractOldPsu(matcher);
104+
}
105+
}
106+
107+
if (psu != null) {
108+
list.add(psu);
109+
}
75110
}
76111
}
112+
if (!list.isEmpty()) {
113+
Collections.sort(list);
114+
return list.get(list.size() - 1);
115+
}
77116
return null;
78117
}
79118

80-
public String extractPsu(String descrip) {
81-
int idx = descrip.lastIndexOf('.') + 1;
82-
int endIdx = descrip.length() - 1;
83-
if (descrip.charAt(endIdx) == ')') {
84-
endIdx--;
119+
// Only for unit testing
120+
String extractNewPsu(String description) {
121+
String psu = null;
122+
if (description != null) {
123+
Matcher matcher = PSU_DESCRIPTION_PATTERN_NEW.matcher(description);
124+
if (matcher.matches()) {
125+
psu = extractNewPsu(matcher);
126+
}
127+
}
128+
return psu;
129+
}
130+
131+
private String extractNewPsu(Matcher matcher) {
132+
LOGGER.entering(matcher.group(0));
133+
134+
String psu = null;
135+
int groupCount = matcher.groupCount();
136+
if (groupCount == 4) {
137+
String idGroup = matcher.group(4);
138+
if (idGroup == null) {
139+
psu = matcher.group(2).substring(1);
140+
} else {
141+
switch (idGroup.length()) {
142+
case 6:
143+
psu = idGroup;
144+
break;
145+
146+
// PSU 12.2.1.3.0.190522 has the ID 20190522 so parse off the extra digits...
147+
case 8:
148+
psu = idGroup.substring(2);
149+
break;
150+
151+
default:
152+
LOGGER.warning("WLSDPLY-01052", idGroup, idGroup.length(), matcher.group(0));
153+
break;
154+
}
155+
}
156+
} else {
157+
LOGGER.warning("WLSDPLY-01051", groupCount, matcher.group(0));
158+
}
159+
160+
LOGGER.exiting(psu);
161+
return psu;
162+
}
163+
164+
// Only for unit testing
165+
String extractOldPsu(String description) {
166+
String psu = null;
167+
if (description != null) {
168+
Matcher matcher = PSU_DESCRIPTION_PATTERN_OLD.matcher(description);
169+
if (matcher.matches()) {
170+
psu = extractOldPsu(matcher);
171+
}
85172
}
86-
return descrip.substring(idx, endIdx+1);
173+
return psu;
87174
}
88175

176+
private String extractOldPsu(Matcher matcher) {
177+
LOGGER.entering(matcher.group(0));
178+
179+
String psu = null;
180+
int groupCount = matcher.groupCount();
181+
if (groupCount == 2) {
182+
psu = matcher.group(2).substring(1);
183+
} else {
184+
LOGGER.warning("WLSDPLY-01051", groupCount, matcher.group(0));
185+
}
186+
187+
LOGGER.exiting(psu);
188+
return psu;
189+
}
89190

90191
/**
91192
* Locate the patch files in the Oracle home

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ WLSDPLY-01000=Password was truncated to {0} characters
122122

123123
# oracle.weblogic.deploy.util.XPathUtils.java
124124
WLSDPLY-01050=WebLogic version for aliases is {0}
125+
WLSDPLY-01051=WebLogic PSU description has an unexpected number of matching groups {0}: {1}
126+
WLSDPLY-01052=WebLogic PSU description is an exception but the PSU number {0} length {1} was unexpected: {2}
125127

126128
# oracle.weblogic.deploy.util.FileUtils.java
127129
WLSDPLY-01100=Failed to get the canonical file for {0} so falling back to absolute file instead: {1}

core/src/test/java/oracle/weblogic/deploy/util/XPathUtilTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,46 @@
1313
public class XPathUtilTest {
1414

1515
@Test
16-
void testPSUWithParen() {
16+
void testNewPSUExceptionWith8Digits() {
1717
XPathUtil util = new XPathUtil();
18-
String tester = new String(".2145)");
19-
String expected = new String("2145");
20-
String actual = util.extractPsu(tester);
18+
String tester = new String("WLS PATCH SET UPDATE 12.2.1.3.0(ID:20190522.070630)");
19+
String expected = new String("190522");
20+
String actual = util.extractNewPsu(tester);
2121
assertEquals(expected, actual);
2222
}
2323

2424
@Test
25-
void testPSU() {
25+
void testNewPSUExceptionWith6Digits() {
2626
XPathUtil util = new XPathUtil();
27-
String tester = new String(".2145");
28-
String expected = new String("2145");
29-
String actual = util.extractPsu(tester);
27+
String tester = new String("WLS PATCH SET UPDATE 12.2.1.3.0(ID:191217.1425)");
28+
String expected = new String("191217");
29+
String actual = util.extractNewPsu(tester);
30+
assertEquals(expected, actual);
31+
}
32+
33+
void testNewPSUExceptionWithUnknownNumberOfDigits() {
34+
XPathUtil util = new XPathUtil();
35+
String tester = new String("WLS PATCH SET UPDATE 12.2.1.3.0(ID:12345.6789)");
36+
String expected = null;
37+
String actual = util.extractNewPsu(tester);
38+
assertEquals(expected, actual);
39+
}
40+
41+
@Test
42+
void testNewPSU() {
43+
XPathUtil util = new XPathUtil();
44+
String tester = new String("WLS PATCH SET UPDATE 12.2.1.4.220329");
45+
String expected = new String("220329");
46+
String actual = util.extractNewPsu(tester);
47+
assertEquals(expected, actual);
48+
}
49+
50+
@Test
51+
void testOldPSU() {
52+
XPathUtil util = new XPathUtil();
53+
String tester = new String("WebLogic Server 12.1.3.0.2 PSU Patch for BUG19637454 THU NOV 27 10:54:42 IST 2014");
54+
String expected = new String("2");
55+
String actual = util.extractOldPsu(tester);
3056
assertEquals(expected, actual);
3157
}
3258
}

0 commit comments

Comments
 (0)