Skip to content

Commit 7a25ea7

Browse files
Snakeyaml 1.33 and exit handling refactor (#1208)
* intermediate checkin waiting on bug fix * moving to snakeyaml 1.33 to pick up bug fix * refactoring exit handling
1 parent 8cbfa80 commit 7a25ea7

29 files changed

+491
-717
lines changed

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

Lines changed: 7 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright (c) 2022, Oracle Corporation and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
5-
65
package oracle.weblogic.deploy.util;
76

87
import java.beans.BeanDescriptor;
@@ -20,7 +19,6 @@
2019
*
2120
* Includes an undocumented main intended for ad-hoc printing of a particular bean or bean property help.
2221
*/
23-
2422
public class WLSBeanHelp {
2523
private static final String EOL = System.getProperty("line.separator");
2624

@@ -42,12 +40,7 @@ private WLSBeanHelp() {}
4240
// and margin set to "width"
4341
// - if propDefault not null, inline it as "default=" along with
4442
// any of the discovered prop limits...
45-
public static String get(
46-
String beanName,
47-
String propName,
48-
int width,
49-
String propDefault
50-
) {
43+
public static String get(String beanName, String propName, int width, String propDefault) {
5144
String ds;
5245

5346
if (propName == null)
@@ -67,76 +60,17 @@ public static String get(
6760
return limits + prettyHTML(ds, width);
6861
}
6962

70-
public static String get(
71-
String beanName,
72-
int width
73-
) {
63+
public static String get(String beanName, int width) {
7464
return get(beanName, null, width, null);
7565
}
7666

77-
// undocumented main to ad hoc retrieve help for a particular bean or bean prop
78-
public static void main(String [] argv) {
79-
String beanName = "BeanNotSpecified";
80-
String propName = null;
81-
int margin = 60;
82-
83-
if (argv.length == 0) {
84-
mainHelp();
85-
System.exit(1);
86-
}
87-
88-
int i = 0;
89-
while (i < argv.length) {
90-
String arg = argv[i];
91-
try {
92-
if (arg.equals("-bean")) {
93-
beanName = argv[i+1]; i++;
94-
}
95-
else if (arg.equals("-prop")) {
96-
propName = argv[i+1]; i++;
97-
}
98-
else if (arg.equals("-margin")) {
99-
margin = Integer.parseInt(argv[i+1]); i++;
100-
}
101-
else {
102-
println("Error: Unrecognized parameter '" + arg +"'.");
103-
mainHelp();
104-
System.exit(1);
105-
}
106-
} catch (ArrayIndexOutOfBoundsException a) {
107-
println("Error: Expected argument after parameter: " + arg);
108-
mainHelp();
109-
System.exit(1);
110-
}
111-
i++;
112-
}
113-
114-
if (getBeanInfo(beanName) == null) {
115-
println("Error: Bean '" + beanName + "' not found.");
116-
}
117-
118-
if (propName == null) {
119-
println("*** Full bean help for bean '" + beanName + "':");
120-
println(get(beanName, null, margin, null));
121-
println("***");
122-
} else {
123-
println("*** Full property help for property '" + beanName + "/" + propName + "':");
124-
println(get(beanName, propName, margin, null));
125-
println("*** Raw property help for property '" + beanName + "/" + propName + "':");
126-
printAttributeHelp(beanName, propName, margin);
127-
println("***");
128-
}
129-
}
130-
13167
// convert basic javadoc HTML to plain text
13268
// (package visible to enable unit testing)
13369
static String prettyHTML(String html, int margin) {
13470
return new PrettyHTML(html, margin).toString();
13571
}
13672

137-
private static String getFeatureDescription(
138-
FeatureDescriptor fd
139-
) {
73+
private static String getFeatureDescription(FeatureDescriptor fd) {
14074
if (fd == null) return "";
14175
Object d = fd.getValue(PD_ATT_DESCRIPTION);
14276
if (d == null) return ""; // should pretty much never happen
@@ -146,11 +80,7 @@ private static String getFeatureDescription(
14680
// gets pretty printed default for a given bean prop, legal values, min, or max
14781
// default is passed in from outside
14882
// returns "" if not applicable or not found
149-
private static String getPropertyLimits(
150-
String beanName,
151-
String propName,
152-
String propDefault
153-
) {
83+
private static String getPropertyLimits(String beanName, String propName, String propDefault) {
15484
StringBuilder ret = new StringBuilder();
15585
if (propDefault != null) {
15686
// we report the passed in default from the alias DB instead of using the
@@ -180,10 +110,7 @@ private static String getPropertyLimits(
180110
}
181111

182112
// can return null if not found
183-
private static PropertyDescriptor getPropertyDescriptor(
184-
String beanName,
185-
String propName
186-
) {
113+
private static PropertyDescriptor getPropertyDescriptor(String beanName, String propName) {
187114
try {
188115
BeanInfo info = getBeanInfo(beanName);
189116
if (info == null) return null;
@@ -203,9 +130,7 @@ private static PropertyDescriptor getPropertyDescriptor(
203130
}
204131

205132
// can return null if not found
206-
private static BeanDescriptor getBeanDescriptor(
207-
String beanName
208-
) {
133+
private static BeanDescriptor getBeanDescriptor(String beanName) {
209134
try {
210135
BeanInfo info = getBeanInfo(beanName);
211136
if (info == null) return null;
@@ -252,15 +177,11 @@ private static BeanInfo getBeanInfo(String beanName)
252177
// use reflection to implement the equivalent of:
253178
// weblogic management provider ManagementServiceClient getBeanInfoAccess()
254179
// getBeanInfoForInterface(beanName, false, null)
255-
180+
//
256181
Class<?> mscClass = Class.forName("weblogic.management.provider.ManagementServiceClient");
257-
258182
Method getBeanInfoAccessMethod = mscClass.getMethod("getBeanInfoAccess");
259-
260183
Object mscObject = mscClass.getDeclaredConstructor().newInstance();
261-
262184
Object biaObject = getBeanInfoAccessMethod.invoke(mscObject);
263-
264185
Class<?> biaClass = Class.forName("weblogic.management.provider.beaninfo.BeanInfoAccess");
265186

266187
Method getBeanInfoForInterfaceMethod =
@@ -283,75 +204,6 @@ private static BeanInfo getBeanInfo(String beanName)
283204
return null;
284205
}
285206

286-
// called solely by the main in this class
287-
private static void mainHelp() {
288-
println("Usage:");
289-
println(" Ensure weblogic.jar is in CLASSPATH.");
290-
println(" java -cp \"$CLASSPATH:./core/target/classes\" oracle.weblogic.deploy.util.WLSBeanHelp -bean weblogic.j2ee.descriptor.wl.UniformDistributedTopicBean -prop ForwardingPolicy -margin 60");
291-
println(" java -cp \"$CLASSPATH:./core/target/classes\" oracle.weblogic.deploy.util.WLSBeanHelp -bean weblogic.j2ee.descriptor.wl.UniformDistributedTopicBean -margin 60");
292-
}
293-
294-
// called solely by the main in this class
295-
private static boolean printAttributeHelp(String beanName, String propName, int margin) {
296-
try {
297-
BeanInfo info = getBeanInfo(beanName);
298-
299-
if (info == null) {
300-
println("Error: Bean '" + beanName + "' not found.");
301-
return false;
302-
}
303-
304-
for (PropertyDescriptor pd:info.getPropertyDescriptors()) {
305-
if (propName.equals(pd.getName())) {
306-
println("Bean = " + beanName);
307-
println("");
308-
printPropertyDescriptor(pd, margin);
309-
return true;
310-
}
311-
}
312-
313-
println("Error: Prop '" + propName + "' not found in bean '" + beanName + "'.");
314-
} catch (Exception th) {
315-
println("Exception: " + th.getMessage());
316-
}
317-
return false;
318-
}
319-
320-
// called solely by the main in this class
321-
private static void println(String s) {
322-
// ignore sonar complaint - this is used for output from main
323-
System.out.println(s);
324-
}
325-
326-
// called solely by the main in this class
327-
private static void printPropertyDescriptor(PropertyDescriptor o, int margin) {
328-
println("\nPROPERTY\n");
329-
println(" name=" + o.getName());
330-
331-
if (!o.getName().equals(o.getDisplayName()))
332-
println(" display name=" + o.getDisplayName());
333-
334-
if (!o.getName().equals(o.getShortDescription()))
335-
println(" short description=" + o.getShortDescription());
336-
337-
println(" property type=" + o.getPropertyType());
338-
println(" hidden=" + o.isHidden());
339-
340-
for (Enumeration<String> en = o.attributeNames();
341-
en.hasMoreElements();) {
342-
String s = en.nextElement();
343-
Object v = o.getValue(s);
344-
if (s.equals(PD_ATT_DESCRIPTION)) continue;
345-
if (s.equals(PD_ATT_LEGALVALUES)) v = legalValuesAsString(v);
346-
if (s.equals(PD_ATT_SEE)) v = legalValuesAsString(v);
347-
println(" " + s + "=" + v);
348-
}
349-
350-
println(" " + PD_ATT_DESCRIPTION + "=");
351-
println(prettyHTML(o.getValue(PD_ATT_DESCRIPTION).toString(), margin));
352-
println("");
353-
}
354-
355207
// helper class for converting mbean javadoc HTML to plain text
356208
private static class PrettyHTML {
357209
private static final String PGS = "<p>";
@@ -498,5 +350,4 @@ public String toString() {
498350
return sb.toString();
499351
}
500352
}
501-
502353
}

core/src/main/java/oracle/weblogic/deploy/yaml/AbstractYamlTranslator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public abstract class AbstractYamlTranslator {
3636

3737
private final boolean useOrderedDict;
3838
private final String fileName;
39+
private final int codePointsLimit;
3940

4041
protected abstract PlatformLogger getLogger();
4142
protected abstract String getClassName();
@@ -46,9 +47,10 @@ public abstract class AbstractYamlTranslator {
4647
// override to write a list of documents as Python dictionaries to the YAML
4748
public abstract void dumpDocuments(List<?> documents) throws YamlException;
4849

49-
protected AbstractYamlTranslator(String fileName, boolean useOrderedDict) {
50+
protected AbstractYamlTranslator(String fileName, boolean useOrderedDict, int codePointsLimit) {
5051
this.fileName = fileName;
5152
this.useOrderedDict = useOrderedDict;
53+
this.codePointsLimit = codePointsLimit;
5254
}
5355

5456
/**
@@ -120,7 +122,7 @@ protected void dumpInternal(List<?> data, Writer outputWriter) throws YamlExcept
120122

121123
if (outputWriter != null) {
122124
DumperOptions dumperOptions = getDefaultDumperOptions();
123-
YamlRepresenter representer = new YamlRepresenter();
125+
YamlRepresenter representer = new YamlRepresenter(dumperOptions);
124126
Yaml yaml = new Yaml(representer, dumperOptions);
125127

126128
try {
@@ -139,6 +141,11 @@ private LoaderOptions getDefaultLoaderOptions() {
139141
// Turning on setProcessComments seems to trigger a parsing bug when dealing with
140142
// tags with no value so leave it off...
141143
//
144+
if (this.codePointsLimit > 0) {
145+
result.setCodePointLimit(this.codePointsLimit);
146+
} else if (this.codePointsLimit < 0 ){
147+
getLogger().fine("WLSDPLY-18111", this.codePointsLimit);
148+
}
142149
return result;
143150
}
144151

core/src/main/java/oracle/weblogic/deploy/yaml/YamlRepresenter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
*/
2828
public class YamlRepresenter extends Representer {
2929

30+
public YamlRepresenter(DumperOptions dumperOptions) {
31+
super(dumperOptions);
32+
}
33+
3034
@Override
3135
protected Node representMapping(Tag tag, Map<?, ?> mapping, DumperOptions.FlowStyle flowStyle) {
3236
MappingNode node = (MappingNode) super.representMapping(tag, mapping, flowStyle);

core/src/main/java/oracle/weblogic/deploy/yaml/YamlStreamTranslator.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,22 @@ public YamlStreamTranslator(String streamFileName, InputStream yamlStream) {
4343
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
4444
*/
4545
public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boolean useOrderedDict) {
46-
super(streamFileName, useOrderedDict);
46+
super(streamFileName, useOrderedDict, 0);
47+
this.streamFileName = streamFileName;
48+
this.yamlStream = yamlStream;
49+
this.yamlOutputWriter = null;
50+
}
51+
52+
/**
53+
* The constructor that allows control of ordering.
54+
*
55+
* @param streamFileName the name of the file used to create the InputStream (used only for logging purposes)
56+
* @param yamlStream the input stream
57+
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
58+
* @param maxCodePoints the maximum number of characters that the parser will accept
59+
*/
60+
public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boolean useOrderedDict, int maxCodePoints) {
61+
super(streamFileName, useOrderedDict, maxCodePoints);
4762
this.streamFileName = streamFileName;
4863
this.yamlStream = yamlStream;
4964
this.yamlOutputWriter = null;
@@ -56,7 +71,7 @@ public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boole
5671
* @param yamlOutputWriter the Writer to use for writing the YAML output
5772
*/
5873
public YamlStreamTranslator(String streamFileName, Writer yamlOutputWriter) {
59-
super(streamFileName, true);
74+
super(streamFileName, true, 0);
6075
this.streamFileName = streamFileName;
6176
this.yamlStream = null;
6277
this.yamlOutputWriter = yamlOutputWriter;

core/src/main/java/oracle/weblogic/deploy/yaml/YamlTranslator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class YamlTranslator extends AbstractYamlTranslator {
3232
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
3333
*/
3434
public YamlTranslator(String fileName) {
35-
this(fileName, false);
35+
this(fileName, false, 0);
3636
}
3737

3838
/**
@@ -43,7 +43,21 @@ public YamlTranslator(String fileName) {
4343
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
4444
*/
4545
public YamlTranslator(String fileName, boolean useOrderedDict) {
46-
super(fileName, useOrderedDict);
46+
super(fileName, useOrderedDict, 0);
47+
this.yamlFile = FileUtils.validateExistingFile(fileName);
48+
}
49+
50+
/**
51+
* Constructor for parsing YAML file into a Python dictionary, controlling ordering, and
52+
* controlling the maximum file size.
53+
*
54+
* @param fileName the name of the existing YAML file to parse
55+
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
56+
* @param maxCodePoints the maximum number of code points for the input file, or zero to accept the default
57+
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
58+
*/
59+
public YamlTranslator(String fileName, boolean useOrderedDict, int maxCodePoints) {
60+
super(fileName, useOrderedDict, maxCodePoints);
4761
this.yamlFile = FileUtils.validateExistingFile(fileName);
4862
}
4963

0 commit comments

Comments
 (0)