Skip to content

Commit 9eecfcb

Browse files
committed
Use PyRealBoolean to read and write JSON and YAML files
1 parent 4aa42b9 commit 9eecfcb

File tree

19 files changed

+135
-89
lines changed

19 files changed

+135
-89
lines changed

core/src/main/java/oracle/weblogic/deploy/json/AbstractJsonTranslator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
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.json;
@@ -12,6 +12,7 @@
1212
import oracle.weblogic.deploy.exception.ExceptionHelper;
1313
import oracle.weblogic.deploy.logging.PlatformLogger;
1414
import oracle.weblogic.deploy.util.PyOrderedDict;
15+
import oracle.weblogic.deploy.util.PyRealBoolean;
1516
import oracle.weblogic.deploy.util.StringUtils;
1617

1718
import org.antlr.v4.runtime.CharStream;
@@ -240,7 +241,7 @@ public void exitJsonNumber(JSONParser.JsonNumberContext ctx) {
240241
*/
241242
@Override
242243
public void enterJsonTrue(JSONParser.JsonTrueContext ctx) {
243-
currentScalarValue = new PyString("true");
244+
currentScalarValue = new PyRealBoolean(true);
244245
currentValueType.push(ValueType.SCALAR);
245246
}
246247

@@ -257,7 +258,7 @@ public void exitJsonTrue(JSONParser.JsonTrueContext ctx) {
257258
*/
258259
@Override
259260
public void enterJsonFalse(JSONParser.JsonFalseContext ctx) {
260-
currentScalarValue = new PyString("false");
261+
currentScalarValue = new PyRealBoolean(false);
261262
currentValueType.push(ValueType.SCALAR);
262263
}
263264

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
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;
@@ -521,7 +521,8 @@ private static PyObject doDeepCopy(PyObject orig, PyObject memo) {
521521
case "NoneType":
522522
case "str":
523523
case "unicode":
524-
case "BooleanValue":
524+
case "PyRealBoolean":
525+
case "oracle.weblogic.deploy.util.PyRealBoolean":
525526
result = orig;
526527
break;
527528

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.util;
6+
7+
import org.python.core.Py;
8+
import org.python.core.PyObject;
9+
import org.python.core.PyString;
10+
11+
/**
12+
* A boolean value that can be passed to and from parsers, and converted to other types.
13+
* The PyBoolean type doesn't exist before Jython 2.7, so this allows
14+
* boolean values to be distinguished from other types.
15+
*/
16+
public class PyRealBoolean extends PyObject {
17+
private final boolean isTrue;
18+
19+
public PyRealBoolean(boolean isTrue) {
20+
this.isTrue = isTrue;
21+
}
22+
23+
public boolean getValue() {
24+
return isTrue;
25+
}
26+
27+
// support Python copy.deepcopy()
28+
public PyRealBoolean __deepcopy__(PyObject memo) {
29+
return new PyRealBoolean(isTrue);
30+
}
31+
32+
@Override
33+
// for Python equality checks like abc == xyz
34+
public PyObject __eq__(PyObject ob_other) {
35+
return equals(ob_other) ? Py.One : Py.Zero;
36+
}
37+
38+
@Override
39+
// for Python inequality checks like abc != xyz
40+
public PyObject __ne__(PyObject ob_other) {
41+
return equals(ob_other) ? Py.Zero : Py.One;
42+
}
43+
44+
@Override
45+
// for Python string conversion, like str(abc)
46+
public PyString __str__() {
47+
return new PyString(toString());
48+
}
49+
50+
@Override
51+
public boolean equals(Object other) {
52+
if(other instanceof PyRealBoolean) {
53+
return isTrue == ((PyRealBoolean) other).isTrue;
54+
}
55+
return false;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return String.valueOf(isTrue);
61+
}
62+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
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.yaml;
@@ -15,6 +15,7 @@
1515
import oracle.weblogic.deploy.logging.PlatformLogger;
1616
import oracle.weblogic.deploy.util.PyOrderedDict;
1717

18+
import oracle.weblogic.deploy.util.PyRealBoolean;
1819
import org.python.core.Py;
1920
import org.python.core.PyDictionary;
2021
import org.python.core.PyFloat;
@@ -233,12 +234,7 @@ private PyObject convertScalarToPythonObject(Object object) throws YamlException
233234
break;
234235

235236
case "java.lang.Boolean":
236-
boolean booleanValue = (Boolean) object;
237-
if (booleanValue) {
238-
result = new PyString("true");
239-
} else {
240-
result = new PyString("false");
241-
}
237+
result = new PyRealBoolean((Boolean) object);
242238
break;
243239

244240
case "java.lang.Integer":

core/src/main/python/wlsdeploy/aliases/alias_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
from java.util import Properties
1919
from javax.management import ObjectName
2020

21+
from oracle.weblogic.deploy.util import PyRealBoolean
2122
from oracle.weblogic.deploy.aliases import TypeUtils
2223
from oracle.weblogic.deploy.aliases import VersionException
2324
from oracle.weblogic.deploy.aliases import VersionUtils
2425

26+
from wlsdeploy.aliases.alias_constants import BOOLEAN
2527
from wlsdeploy.aliases.alias_constants import ChildFoldersTypes
2628
from wlsdeploy.aliases.model_constants import MODEL_LIST_DELIMITER
2729
from wlsdeploy.exception import exception_helper
@@ -52,7 +54,6 @@
5254
from wlsdeploy.aliases.alias_constants import WLST_TYPE
5355
from wlsdeploy.aliases.alias_constants import WLST_SUBFOLDERS_PATH
5456
from wlsdeploy.util import model_helper
55-
from wlsdeploy.util.boolean_value import BooleanValue
5657

5758
_class_name = 'alias_utils'
5859
_logger = PlatformLogger('wlsdeploy.aliases')
@@ -534,8 +535,8 @@ def convert_boolean(value):
534535
result = True
535536
elif value.lower() == 'false':
536537
result = False
537-
elif isinstance(value, BooleanValue):
538-
result = value.get_value()
538+
elif isinstance(value, PyRealBoolean):
539+
result = value.getValue()
539540
return result
540541

541542

@@ -848,6 +849,8 @@ def _convert_value_to_model_type(data_type, value, delimiter):
848849
try:
849850
if data_type == JAVA_LANG_BOOLEAN:
850851
converted = Boolean(converted)
852+
elif data_type == BOOLEAN:
853+
converted = PyRealBoolean('true' == converted)
851854
elif data_type == JARRAY:
852855
converted = _create_array(converted, delimiter)
853856
# elif data_type == PROPERTIES:

core/src/main/python/wlsdeploy/aliases/aliases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
from java.lang import String
@@ -768,8 +768,8 @@ def get_model_restart_required_attribute_names(self, location):
768768

769769
for key, value in module_folder[ATTRIBUTES].iteritems():
770770
if RESTART_REQUIRED in value:
771-
restart_required_value = value[RESTART_REQUIRED]
772-
if "true" == restart_required_value.lower():
771+
restart_required = alias_utils.convert_boolean(value[RESTART_REQUIRED])
772+
if restart_required:
773773
restart_attribute_names.append(key)
774774

775775
return restart_attribute_names

core/src/main/python/wlsdeploy/json/json_translator.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
55
This model provider translation classes that convert between JSON and Python Dictionaries.
@@ -18,7 +18,6 @@
1818
import oracle.weblogic.deploy.json.JsonTranslator as JJsonTranslator
1919

2020
from wlsdeploy.logging.platform_logger import PlatformLogger
21-
from wlsdeploy.util.boolean_value import BooleanValue
2221
import wlsdeploy.exception.exception_helper as exception_helper
2322

2423
# Unlike with yaml files, JSON files do not allow comments. remove from file
@@ -240,8 +239,6 @@ def _format_json_value(value):
240239
builder = StringBuilder()
241240
if type(value) == bool:
242241
builder.append(JBoolean.toString(value))
243-
elif isinstance(value, BooleanValue):
244-
builder.append(value.get_string_value())
245242
elif isinstance(value, types.StringTypes):
246243
builder.append('"').append(_escape_text(value.strip())).append('"')
247244
else:

core/src/main/python/wlsdeploy/tool/discover/topology_discoverer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
from java.io import File
@@ -12,6 +12,7 @@
1212
from oracle.weblogic.deploy.util import StringUtils
1313
from oracle.weblogic.deploy.util import WLSDeployArchiveIOException
1414

15+
from wlsdeploy.aliases import alias_utils
1516
from wlsdeploy.aliases import model_constants
1617
from wlsdeploy.aliases.location_context import LocationContext
1718
from wlsdeploy.aliases.model_constants import MODEL_LIST_DELIMITER
@@ -559,8 +560,9 @@ def _massage_security_credential(self, result, location):
559560
short_name = self._credential_injector.get_folder_short_name(location)
560561
if model_constants.SECURITY_CONFIGURATION_PASSWORD in result:
561562
# default is false
562-
if model_constants.SECURITY_CONFIGURATION_CD_ENABLED not in result or \
563-
Boolean.valueOf(result[model_constants.SECURITY_CONFIGURATION_CD_ENABLED]) == Boolean.FALSE:
563+
cd_enabled_raw = dictionary_utils.get_element(result, model_constants.SECURITY_CONFIGURATION_CD_ENABLED)
564+
cd_enabled = alias_utils.convert_boolean(cd_enabled_raw)
565+
if not cd_enabled:
564566
# Hard code it here or hard code it later. The target code will bypass tokenize of variable
565567
cache_name = short_name + VARIABLE_SEP + model_constants.SECURITY_CONFIGURATION_PASSWORD
566568
if cache_name in pass_cache:

core/src/main/python/wlsdeploy/tool/extract/domain_resource_extractor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from java.io import File
88
from oracle.weblogic.deploy.util import PyOrderedDict
9+
from oracle.weblogic.deploy.util import PyRealBoolean
910

1011
from wlsdeploy.aliases import alias_utils
1112
from wlsdeploy.aliases.alias_constants import PASSWORD_TOKEN
@@ -18,7 +19,6 @@
1819
from wlsdeploy.tool.extract import wko_schema_helper
1920
from wlsdeploy.tool.util import k8s_helper
2021
from wlsdeploy.util import dictionary_utils
21-
from wlsdeploy.util.boolean_value import BooleanValue
2222
from wlsdeploy.util.model_translator import PythonToFile
2323

2424
API_VERSION = 'apiVersion'
@@ -299,7 +299,7 @@ def _get_target_value(model_value, type_name):
299299
if type_name == 'boolean':
300300
# the model values can be true, false, 1, 0, etc.
301301
# target boolean values must be 'true' or 'false'
302-
return BooleanValue(model_value)
302+
return PyRealBoolean(alias_utils.convert_boolean(model_value))
303303

304304
if type_name == 'array':
305305
# the model values can be 'abc,123'.

core/src/main/python/wlsdeploy/tool/util/filters/wko_filter.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Copyright (c) 2021, 2022, Oracle Corporation and/or its affiliates.
1+
# Copyright (c) 2021, 2022, Oracle and/or its affiliates.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
#
44
# ------------
55
# Description:
66
# ------------
77
# WDT filters to prepare a model for use with WKO, using the createDomain or prepareModel tools.
88
# These operations can be invoked as a single call, or independently of each other.
9-
9+
from oracle.weblogic.deploy.util import PyRealBoolean
1010
from wlsdeploy.aliases import alias_utils
1111
from wlsdeploy.aliases.model_constants import CALCULATED_LISTEN_PORTS
1212
from wlsdeploy.aliases.model_constants import CLUSTER
@@ -20,7 +20,6 @@
2020
from wlsdeploy.logging.platform_logger import PlatformLogger
2121
from wlsdeploy.tool.util.filters.model_traverse import ModelTraverse
2222
from wlsdeploy.util import dictionary_utils
23-
from wlsdeploy.util.boolean_value import BooleanValue
2423

2524
_class_name = 'wko_filter'
2625
_logger = PlatformLogger('wlsdeploy.tool.util')
@@ -69,7 +68,7 @@ def check_clustered_server_ports(model, _model_context):
6968
if (calculated_listen_ports is None) or alias_utils.convert_boolean(calculated_listen_ports):
7069
_logger.info('WLSDPLY-20202', CALCULATED_LISTEN_PORTS, CLUSTER, cluster_name, class_name=_class_name,
7170
method_name=_method_name)
72-
dynamic_folder[CALCULATED_LISTEN_PORTS] = BooleanValue(False)
71+
dynamic_folder[CALCULATED_LISTEN_PORTS] = PyRealBoolean(False)
7372

7473
# be sure every server assigned to a cluster has the same listen port
7574

0 commit comments

Comments
 (0)