Skip to content

Snakeyaml 1.33 and exit handling refactor #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 7 additions & 156 deletions core/src/main/java/oracle/weblogic/deploy/util/WLSBeanHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright (c) 2022, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/

package oracle.weblogic.deploy.util;

import java.beans.BeanDescriptor;
Expand All @@ -20,7 +19,6 @@
*
* Includes an undocumented main intended for ad-hoc printing of a particular bean or bean property help.
*/

public class WLSBeanHelp {
private static final String EOL = System.getProperty("line.separator");

Expand All @@ -42,12 +40,7 @@ private WLSBeanHelp() {}
// and margin set to "width"
// - if propDefault not null, inline it as "default=" along with
// any of the discovered prop limits...
public static String get(
String beanName,
String propName,
int width,
String propDefault
) {
public static String get(String beanName, String propName, int width, String propDefault) {
String ds;

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

public static String get(
String beanName,
int width
) {
public static String get(String beanName, int width) {
return get(beanName, null, width, null);
}

// undocumented main to ad hoc retrieve help for a particular bean or bean prop
public static void main(String [] argv) {
String beanName = "BeanNotSpecified";
String propName = null;
int margin = 60;

if (argv.length == 0) {
mainHelp();
System.exit(1);
}

int i = 0;
while (i < argv.length) {
String arg = argv[i];
try {
if (arg.equals("-bean")) {
beanName = argv[i+1]; i++;
}
else if (arg.equals("-prop")) {
propName = argv[i+1]; i++;
}
else if (arg.equals("-margin")) {
margin = Integer.parseInt(argv[i+1]); i++;
}
else {
println("Error: Unrecognized parameter '" + arg +"'.");
mainHelp();
System.exit(1);
}
} catch (ArrayIndexOutOfBoundsException a) {
println("Error: Expected argument after parameter: " + arg);
mainHelp();
System.exit(1);
}
i++;
}

if (getBeanInfo(beanName) == null) {
println("Error: Bean '" + beanName + "' not found.");
}

if (propName == null) {
println("*** Full bean help for bean '" + beanName + "':");
println(get(beanName, null, margin, null));
println("***");
} else {
println("*** Full property help for property '" + beanName + "/" + propName + "':");
println(get(beanName, propName, margin, null));
println("*** Raw property help for property '" + beanName + "/" + propName + "':");
printAttributeHelp(beanName, propName, margin);
println("***");
}
}

// convert basic javadoc HTML to plain text
// (package visible to enable unit testing)
static String prettyHTML(String html, int margin) {
return new PrettyHTML(html, margin).toString();
}

private static String getFeatureDescription(
FeatureDescriptor fd
) {
private static String getFeatureDescription(FeatureDescriptor fd) {
if (fd == null) return "";
Object d = fd.getValue(PD_ATT_DESCRIPTION);
if (d == null) return ""; // should pretty much never happen
Expand All @@ -146,11 +80,7 @@ private static String getFeatureDescription(
// gets pretty printed default for a given bean prop, legal values, min, or max
// default is passed in from outside
// returns "" if not applicable or not found
private static String getPropertyLimits(
String beanName,
String propName,
String propDefault
) {
private static String getPropertyLimits(String beanName, String propName, String propDefault) {
StringBuilder ret = new StringBuilder();
if (propDefault != null) {
// we report the passed in default from the alias DB instead of using the
Expand Down Expand Up @@ -180,10 +110,7 @@ private static String getPropertyLimits(
}

// can return null if not found
private static PropertyDescriptor getPropertyDescriptor(
String beanName,
String propName
) {
private static PropertyDescriptor getPropertyDescriptor(String beanName, String propName) {
try {
BeanInfo info = getBeanInfo(beanName);
if (info == null) return null;
Expand All @@ -203,9 +130,7 @@ private static PropertyDescriptor getPropertyDescriptor(
}

// can return null if not found
private static BeanDescriptor getBeanDescriptor(
String beanName
) {
private static BeanDescriptor getBeanDescriptor(String beanName) {
try {
BeanInfo info = getBeanInfo(beanName);
if (info == null) return null;
Expand Down Expand Up @@ -252,15 +177,11 @@ private static BeanInfo getBeanInfo(String beanName)
// use reflection to implement the equivalent of:
// weblogic management provider ManagementServiceClient getBeanInfoAccess()
// getBeanInfoForInterface(beanName, false, null)

//
Class<?> mscClass = Class.forName("weblogic.management.provider.ManagementServiceClient");

Method getBeanInfoAccessMethod = mscClass.getMethod("getBeanInfoAccess");

Object mscObject = mscClass.getDeclaredConstructor().newInstance();

Object biaObject = getBeanInfoAccessMethod.invoke(mscObject);

Class<?> biaClass = Class.forName("weblogic.management.provider.beaninfo.BeanInfoAccess");

Method getBeanInfoForInterfaceMethod =
Expand All @@ -283,75 +204,6 @@ private static BeanInfo getBeanInfo(String beanName)
return null;
}

// called solely by the main in this class
private static void mainHelp() {
println("Usage:");
println(" Ensure weblogic.jar is in CLASSPATH.");
println(" java -cp \"$CLASSPATH:./core/target/classes\" oracle.weblogic.deploy.util.WLSBeanHelp -bean weblogic.j2ee.descriptor.wl.UniformDistributedTopicBean -prop ForwardingPolicy -margin 60");
println(" java -cp \"$CLASSPATH:./core/target/classes\" oracle.weblogic.deploy.util.WLSBeanHelp -bean weblogic.j2ee.descriptor.wl.UniformDistributedTopicBean -margin 60");
}

// called solely by the main in this class
private static boolean printAttributeHelp(String beanName, String propName, int margin) {
try {
BeanInfo info = getBeanInfo(beanName);

if (info == null) {
println("Error: Bean '" + beanName + "' not found.");
return false;
}

for (PropertyDescriptor pd:info.getPropertyDescriptors()) {
if (propName.equals(pd.getName())) {
println("Bean = " + beanName);
println("");
printPropertyDescriptor(pd, margin);
return true;
}
}

println("Error: Prop '" + propName + "' not found in bean '" + beanName + "'.");
} catch (Exception th) {
println("Exception: " + th.getMessage());
}
return false;
}

// called solely by the main in this class
private static void println(String s) {
// ignore sonar complaint - this is used for output from main
System.out.println(s);
}

// called solely by the main in this class
private static void printPropertyDescriptor(PropertyDescriptor o, int margin) {
println("\nPROPERTY\n");
println(" name=" + o.getName());

if (!o.getName().equals(o.getDisplayName()))
println(" display name=" + o.getDisplayName());

if (!o.getName().equals(o.getShortDescription()))
println(" short description=" + o.getShortDescription());

println(" property type=" + o.getPropertyType());
println(" hidden=" + o.isHidden());

for (Enumeration<String> en = o.attributeNames();
en.hasMoreElements();) {
String s = en.nextElement();
Object v = o.getValue(s);
if (s.equals(PD_ATT_DESCRIPTION)) continue;
if (s.equals(PD_ATT_LEGALVALUES)) v = legalValuesAsString(v);
if (s.equals(PD_ATT_SEE)) v = legalValuesAsString(v);
println(" " + s + "=" + v);
}

println(" " + PD_ATT_DESCRIPTION + "=");
println(prettyHTML(o.getValue(PD_ATT_DESCRIPTION).toString(), margin));
println("");
}

// helper class for converting mbean javadoc HTML to plain text
private static class PrettyHTML {
private static final String PGS = "<p>";
Expand Down Expand Up @@ -498,5 +350,4 @@ public String toString() {
return sb.toString();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class AbstractYamlTranslator {

private final boolean useOrderedDict;
private final String fileName;
private final int codePointsLimit;

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

protected AbstractYamlTranslator(String fileName, boolean useOrderedDict) {
protected AbstractYamlTranslator(String fileName, boolean useOrderedDict, int codePointsLimit) {
this.fileName = fileName;
this.useOrderedDict = useOrderedDict;
this.codePointsLimit = codePointsLimit;
}

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

if (outputWriter != null) {
DumperOptions dumperOptions = getDefaultDumperOptions();
YamlRepresenter representer = new YamlRepresenter();
YamlRepresenter representer = new YamlRepresenter(dumperOptions);
Yaml yaml = new Yaml(representer, dumperOptions);

try {
Expand All @@ -139,6 +141,11 @@ private LoaderOptions getDefaultLoaderOptions() {
// Turning on setProcessComments seems to trigger a parsing bug when dealing with
// tags with no value so leave it off...
//
if (this.codePointsLimit > 0) {
result.setCodePointLimit(this.codePointsLimit);
} else if (this.codePointsLimit < 0 ){
getLogger().fine("WLSDPLY-18111", this.codePointsLimit);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*/
public class YamlRepresenter extends Representer {

public YamlRepresenter(DumperOptions dumperOptions) {
super(dumperOptions);
}

@Override
protected Node representMapping(Tag tag, Map<?, ?> mapping, DumperOptions.FlowStyle flowStyle) {
MappingNode node = (MappingNode) super.representMapping(tag, mapping, flowStyle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,22 @@ public YamlStreamTranslator(String streamFileName, InputStream yamlStream) {
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
*/
public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boolean useOrderedDict) {
super(streamFileName, useOrderedDict);
super(streamFileName, useOrderedDict, 0);
this.streamFileName = streamFileName;
this.yamlStream = yamlStream;
this.yamlOutputWriter = null;
}

/**
* The constructor that allows control of ordering.
*
* @param streamFileName the name of the file used to create the InputStream (used only for logging purposes)
* @param yamlStream the input stream
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
* @param maxCodePoints the maximum number of characters that the parser will accept
*/
public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boolean useOrderedDict, int maxCodePoints) {
super(streamFileName, useOrderedDict, maxCodePoints);
this.streamFileName = streamFileName;
this.yamlStream = yamlStream;
this.yamlOutputWriter = null;
Expand All @@ -56,7 +71,7 @@ public YamlStreamTranslator(String streamFileName, InputStream yamlStream, boole
* @param yamlOutputWriter the Writer to use for writing the YAML output
*/
public YamlStreamTranslator(String streamFileName, Writer yamlOutputWriter) {
super(streamFileName, true);
super(streamFileName, true, 0);
this.streamFileName = streamFileName;
this.yamlStream = null;
this.yamlOutputWriter = yamlOutputWriter;
Expand Down
18 changes: 16 additions & 2 deletions core/src/main/java/oracle/weblogic/deploy/yaml/YamlTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class YamlTranslator extends AbstractYamlTranslator {
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
*/
public YamlTranslator(String fileName) {
this(fileName, false);
this(fileName, false, 0);
}

/**
Expand All @@ -43,7 +43,21 @@ public YamlTranslator(String fileName) {
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
*/
public YamlTranslator(String fileName, boolean useOrderedDict) {
super(fileName, useOrderedDict);
super(fileName, useOrderedDict, 0);
this.yamlFile = FileUtils.validateExistingFile(fileName);
}

/**
* Constructor for parsing YAML file into a Python dictionary, controlling ordering, and
* controlling the maximum file size.
*
* @param fileName the name of the existing YAML file to parse
* @param useOrderedDict whether or not to use an ordered dictionary to maintain the order
* @param maxCodePoints the maximum number of code points for the input file, or zero to accept the default
* @throws IllegalArgumentException if the file name is null or does not point to a valid, existing file.
*/
public YamlTranslator(String fileName, boolean useOrderedDict, int maxCodePoints) {
super(fileName, useOrderedDict, maxCodePoints);
this.yamlFile = FileUtils.validateExistingFile(fileName);
}

Expand Down
Loading