3
3
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4
4
*/
5
5
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 ;
9
20
10
21
import javax .xml .XMLConstants ;
11
22
import javax .xml .parsers .DocumentBuilder ;
15
26
import javax .xml .xpath .XPathConstants ;
16
27
import javax .xml .xpath .XPathExpressionException ;
17
28
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 .*;
25
29
26
30
import oracle .weblogic .deploy .logging .PlatformLogger ;
27
31
import oracle .weblogic .deploy .logging .WLSDeployLogFactory ;
28
32
33
+ import org .w3c .dom .Document ;
34
+ import org .w3c .dom .Node ;
35
+ import org .xml .sax .SAXException ;
36
+
29
37
/*
30
38
* Parse the xml file at the designated location for the PSU value
31
39
*/
32
40
public class XPathUtil {
33
41
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
+
34
49
String oracleHome ;
35
50
String patchesHome ;
36
51
public XPathUtil (String oracleHome ){
@@ -52,8 +67,18 @@ private static synchronized XPathFactory factory() {
52
67
}
53
68
54
69
/*
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.
57
82
*/
58
83
public String getPSU () {
59
84
// find the names in the directory first
@@ -63,29 +88,105 @@ public String getPSU() {
63
88
}
64
89
List <String > patchFiles = findPatchFiles ();
65
90
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 );
68
93
String descrip = description (doc , "//@description" );
69
94
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
+ }
75
110
}
76
111
}
112
+ if (!list .isEmpty ()) {
113
+ Collections .sort (list );
114
+ return list .get (list .size () - 1 );
115
+ }
77
116
return null ;
78
117
}
79
118
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
+ }
85
172
}
86
- return descrip . substring ( idx , endIdx + 1 ) ;
173
+ return psu ;
87
174
}
88
175
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
+ }
89
190
90
191
/**
91
192
* Locate the patch files in the Oracle home
0 commit comments