This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/struts.git
commit faf1ac96b51a8a538e2aa9917a49d1bf705bc0f0 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Tue May 29 12:58:44 2018 +0200 Makes OgnlUtil more immutable --- .../com/opensymphony/xwork2/ognl/OgnlUtil.java | 51 +++++++++++++++++----- .../com/opensymphony/xwork2/ognl/OgnlUtilTest.java | 12 ++--- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java index 808efef..bb0d4e8 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlUtil.java @@ -61,14 +61,36 @@ public class OgnlUtil { private boolean enableExpressionCache = true; private boolean enableEvalExpression; - private Set<Class<?>> excludedClasses = Collections.emptySet(); - private Set<Pattern> excludedPackageNamePatterns = Collections.emptySet(); - private Set<String> excludedPackageNames = Collections.emptySet(); + private Set<Class<?>> excludedClasses; + private Set<Pattern> excludedPackageNamePatterns; + private Set<String> excludedPackageNames; private Container container; private boolean allowStaticMethodAccess; private boolean disallowProxyMemberAccess; + public OgnlUtil( + @Inject(value = XWorkConstants.OGNL_EXCLUDED_CLASSES, required = false) + String commaDelimitedClasses, + @Inject(value = XWorkConstants.OGNL_EXCLUDED_PACKAGE_NAME_PATTERNS, required = false) + String commaDelimitedPackagePatterns, + @Inject(value = XWorkConstants.OGNL_EXCLUDED_PACKAGE_NAMES, required = false) + String commaDelimitedPackageNames + ) { + excludedClasses = Collections.unmodifiableSet(parseExcludedClasses(commaDelimitedClasses)); + excludedPackageNamePatterns = Collections.unmodifiableSet(parseExcludedPackageNamePatterns(commaDelimitedPackagePatterns)); + excludedPackageNames = Collections.unmodifiableSet(parseExcludedPackageNames(commaDelimitedPackageNames)); + } + + /** + * Constructor used by internal DI + */ + public OgnlUtil() { + excludedClasses = Collections.emptySet(); + excludedPackageNamePatterns = Collections.emptySet(); + excludedPackageNames = Collections.emptySet(); + } + @Inject public void setXWorkConverter(XWorkConverter conv) { this.defaultConverter = new OgnlTypeConverterWrapper(conv); @@ -93,8 +115,7 @@ public class OgnlUtil { } } - @Inject(value = StrutsConstants.STRUTS_EXCLUDED_CLASSES, required = false) - public void setExcludedClasses(String commaDelimitedClasses) { + private Set<Class<?>> parseExcludedClasses(String commaDelimitedClasses) { Set<String> classNames = TextParseUtil.commaDelimitedStringToSet(commaDelimitedClasses); Set<Class<?>> classes = new HashSet<>(); @@ -106,11 +127,11 @@ public class OgnlUtil { } } - excludedClasses = Collections.unmodifiableSet(classes); + return classes; } - @Inject(value = StrutsConstants.STRUTS_EXCLUDED_PACKAGE_NAME_PATTERNS, required = false) - public void setExcludedPackageNamePatterns(String commaDelimitedPackagePatterns) { + + private Set<Pattern> parseExcludedPackageNamePatterns(String commaDelimitedPackagePatterns) { Set<String> packagePatterns = TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackagePatterns); Set<Pattern> packageNamePatterns = new HashSet<>(); @@ -118,12 +139,11 @@ public class OgnlUtil { packageNamePatterns.add(Pattern.compile(pattern)); } - excludedPackageNamePatterns = Collections.unmodifiableSet(packageNamePatterns); + return packageNamePatterns; } - @Inject(value = StrutsConstants.STRUTS_EXCLUDED_PACKAGE_NAMES, required = false) - public void setExcludedPackageNames(String commaDelimitedPackageNames) { - excludedPackageNames = Collections.unmodifiableSet(TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackageNames)); + private Set<String> parseExcludedPackageNames(String commaDelimitedPackageNames) { + return TextParseUtil.commaDelimitedStringToSet(commaDelimitedPackageNames); } public Set<Class<?>> getExcludedClasses() { @@ -679,6 +699,13 @@ public class OgnlUtil { return Ognl.createDefaultContext(root, memberAccess, resolver, defaultConverter); } + protected void addExcludedClasses(String commaDelimitedClasses) { + Set<Class<?>> existingClasses = new HashSet<>(excludedClasses); + existingClasses.addAll(parseExcludedClasses(commaDelimitedClasses)); + + excludedClasses = Collections.unmodifiableSet(existingClasses); + } + private interface OgnlTask<T> { T execute(Object tree) throws OgnlException; } diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java index 3b3c8ae..02f0f70 100644 --- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java @@ -41,7 +41,7 @@ public class OgnlUtilTest extends XWorkTestCase { ognlUtil = container.getInstance(OgnlUtil.class); } - public void testCanSetADependentObject() throws Exception { + public void testCanSetADependentObject() { String dogName = "fido"; OgnlRuntime.setNullHandler(Owner.class, new NullHandler() { @@ -653,7 +653,7 @@ public class OgnlUtilTest extends XWorkTestCase { Exception expected = null; try { - ognlUtil.setExcludedClasses(Object.class.getName()); + ognlUtil.addExcludedClasses(Object.class.getName()); ognlUtil.setValue("class.classLoader.defaultAssertionStatus", ognlUtil.createDefaultContext(foo), foo, true); fail(); } catch (OgnlException e) { @@ -669,7 +669,7 @@ public class OgnlUtilTest extends XWorkTestCase { Exception expected = null; try { - ognlUtil.setExcludedClasses(Object.class.getName()); + ognlUtil.addExcludedClasses(Object.class.getName()); ognlUtil.setValue("Class.ClassLoader.DefaultAssertionStatus", ognlUtil.createDefaultContext(foo), foo, true); fail(); } catch (OgnlException e) { @@ -685,7 +685,7 @@ public class OgnlUtilTest extends XWorkTestCase { Exception expected = null; try { - ognlUtil.setExcludedClasses(Object.class.getName()); + ognlUtil.addExcludedClasses(Object.class.getName()); ognlUtil.setValue("class['classLoader']['defaultAssertionStatus']", ognlUtil.createDefaultContext(foo), foo, true); fail(); } catch (OgnlException e) { @@ -716,7 +716,7 @@ public class OgnlUtilTest extends XWorkTestCase { Exception expected = null; try { - ognlUtil.setExcludedClasses(Object.class.getName()); + ognlUtil.addExcludedClasses(Object.class.getName()); ognlUtil.setValue("class[\"classLoader\"]['defaultAssertionStatus']", ognlUtil.createDefaultContext(foo), foo, true); fail(); } catch (OgnlException e) { @@ -762,7 +762,7 @@ public class OgnlUtilTest extends XWorkTestCase { Exception expected = null; try { - ognlUtil.setExcludedClasses(Runtime.class.getName()); + ognlUtil.addExcludedClasses(Runtime.class.getName()); ognlUtil.setValue("@java.lang.Runtime@getRuntime().exec('mate')", ognlUtil.createDefaultContext(foo), foo, true); fail(); } catch (OgnlException e) {