shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lhazlew...@apache.org
Subject svn commit: r1221994 - in /shiro/trunk/core/src: main/java/org/apache/shiro/config/ReflectionBuilder.java test/java/org/apache/shiro/config/ReflectionBuilderTest.java
Date Thu, 22 Dec 2011 02:11:48 GMT
Author: lhazlewood
Date: Thu Dec 22 02:11:47 2011
New Revision: 1221994

URL: http://svn.apache.org/viewvc?rev=1221994&view=rev
Log:
SHIRO-225: implementation complete w/ test cases.  Functionality was needed for the SHIRO-285
CAS module ('securityManager.rememberMeManager = null' support was desired for CAS environments)

Modified:
    shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
    shiro/trunk/core/src/test/java/org/apache/shiro/config/ReflectionBuilderTest.java

Modified: shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java?rev=1221994&r1=1221993&r2=1221994&view=diff
==============================================================================
--- shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java (original)
+++ shiro/trunk/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java Thu Dec
22 02:11:47 2011
@@ -34,7 +34,7 @@ import java.util.*;
  * Object builder that uses reflection and Apache Commons BeanUtils to build objects given
a
  * map of "property values".  Typically these come from the Shiro INI configuration and are
used
  * to construct or modify the SecurityManager, its dependencies, and web-based security filters.
- *
+ * <p/>
  * Recognizes {@link Factory} implementations and will call
  * {@link org.apache.shiro.util.Factory#getInstance() getInstance} to satisfy any reference
to this bean.
  *
@@ -51,6 +51,9 @@ public class ReflectionBuilder {
     private static final String GLOBAL_PROPERTY_PREFIX = "shiro";
     private static final char MAP_KEY_VALUE_DELIMITER = ':';
     private static final String HEX_BEGIN_TOKEN = "0x";
+    private static final String NULL_VALUE_TOKEN = "null";
+    private static final String EMPTY_STRING_VALUE_TOKEN = "\"\"";
+    private static final char STRING_VALUE_DELIMETER = '"';
 
     private Map<String, ?> objects;
 
@@ -229,8 +232,8 @@ public class ReflectionBuilder {
         String id = getId(reference);
         log.debug("Encountered object reference '{}'.  Looking up object with id '{}'", reference,
id);
         final Object referencedObject = getReferencedObject(id);
-        if(referencedObject instanceof Factory) {
-            return ((Factory)referencedObject).getInstance();
+        if (referencedObject instanceof Factory) {
+            return ((Factory) referencedObject).getInstance();
         }
         return referencedObject;
     }
@@ -343,12 +346,31 @@ public class ReflectionBuilder {
         return value;
     }
 
+    protected String checkForNullOrEmptyLiteral(String stringValue) {
+        if (stringValue == null) {
+            return null;
+        }
+        //check if the value is the actual literal string 'null' (expected to be wrapped
in quotes):
+        if (stringValue.equals("\"null\"")) {
+            return NULL_VALUE_TOKEN;
+        }
+        //or the actual literal string of two quotes '""' (expected to be wrapped in quotes):
+        else if (stringValue.equals("\"\"\"\"")) {
+            return EMPTY_STRING_VALUE_TOKEN;
+        } else {
+            return stringValue;
+        }
+    }
 
     protected void applyProperty(Object object, String propertyName, String stringValue)
{
 
         Object value;
 
-        if (isTypedProperty(object, propertyName, Set.class)) {
+        if (NULL_VALUE_TOKEN.equals(stringValue)) {
+            value = null;
+        } else if (EMPTY_STRING_VALUE_TOKEN.equals(stringValue)) {
+            value = StringUtils.EMPTY_STRING;
+        } else if (isTypedProperty(object, propertyName, Set.class)) {
             value = toSet(stringValue);
         } else if (isTypedProperty(object, propertyName, Map.class)) {
             value = toMap(stringValue);
@@ -361,7 +383,8 @@ public class ReflectionBuilder {
             byte[] bytes = toBytes(stringValue);
             value = ByteSource.Util.bytes(bytes);
         } else {
-            value = resolveValue(stringValue);
+            String checked = checkForNullOrEmptyLiteral(stringValue);
+            value = resolveValue(checked);
         }
 
         try {

Modified: shiro/trunk/core/src/test/java/org/apache/shiro/config/ReflectionBuilderTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/test/java/org/apache/shiro/config/ReflectionBuilderTest.java?rev=1221994&r1=1221993&r2=1221994&view=diff
==============================================================================
--- shiro/trunk/core/src/test/java/org/apache/shiro/config/ReflectionBuilderTest.java (original)
+++ shiro/trunk/core/src/test/java/org/apache/shiro/config/ReflectionBuilderTest.java Thu
Dec 22 02:11:47 2011
@@ -21,7 +21,6 @@ package org.apache.shiro.config;
 import org.apache.shiro.codec.Base64;
 import org.apache.shiro.codec.CodecSupport;
 import org.apache.shiro.codec.Hex;
-import org.apache.shiro.jndi.JndiObjectFactory;
 import org.apache.shiro.util.CollectionUtils;
 import org.junit.Test;
 
@@ -53,6 +52,84 @@ public class ReflectionBuilderTest {
     }
 
     @Test
+    public void testWithConfiguredNullValue() {
+        Map<String,Object> defaults = new LinkedHashMap<String,Object>();
+        CompositeBean cBean = new CompositeBean();
+        cBean.setSimpleBean(new SimpleBean());
+        defaults.put("compositeBean", cBean);
+        
+        Map<String, String> defs = new LinkedHashMap<String, String>();
+        defs.put("compositeBean.intProp", "42");
+        defs.put("compositeBean.booleanProp", "true");
+        defs.put("compositeBean.stringProp", "test");
+        defs.put("compositeBean.simpleBean", "null");
+
+        ReflectionBuilder builder = new ReflectionBuilder(defaults);
+        Map beans = builder.buildObjects(defs);
+
+        CompositeBean compositeBean = (CompositeBean) beans.get("compositeBean");
+        assertNotNull(compositeBean);
+        assertTrue(compositeBean.isBooleanProp());
+        assertEquals(compositeBean.getIntProp(), 42);
+        assertEquals("test", compositeBean.getStringProp());
+        assertNull(compositeBean.getSimpleBean());
+    }
+
+    @Test
+    public void testWithConfiguredNullLiteralValue() {
+        Map<String, String> defs = new LinkedHashMap<String, String>();
+        defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
+        defs.put("compositeBean.intProp", "42");
+        defs.put("compositeBean.booleanProp", "true");
+        defs.put("compositeBean.stringProp", "\"null\"");
+
+        ReflectionBuilder builder = new ReflectionBuilder();
+        Map beans = builder.buildObjects(defs);
+
+        CompositeBean compositeBean = (CompositeBean) beans.get("compositeBean");
+        assertNotNull(compositeBean);
+        assertTrue(compositeBean.isBooleanProp());
+        assertEquals(compositeBean.getIntProp(), 42);
+        assertEquals("null", compositeBean.getStringProp());
+    }
+
+    @Test
+    public void testWithConfiguredEmptyStringValue() {
+        Map<String, String> defs = new LinkedHashMap<String, String>();
+        defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
+        defs.put("compositeBean.intProp", "42");
+        defs.put("compositeBean.booleanProp", "true");
+        defs.put("compositeBean.stringProp", "\"\"");
+
+        ReflectionBuilder builder = new ReflectionBuilder();
+        Map beans = builder.buildObjects(defs);
+
+        CompositeBean compositeBean = (CompositeBean) beans.get("compositeBean");
+        assertNotNull(compositeBean);
+        assertTrue(compositeBean.isBooleanProp());
+        assertEquals(compositeBean.getIntProp(), 42);
+        assertEquals("", compositeBean.getStringProp());
+    }
+
+    @Test
+    public void testWithConfiguredEmptyStringLiteralValue() {
+        Map<String, String> defs = new LinkedHashMap<String, String>();
+        defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
+        defs.put("compositeBean.intProp", "42");
+        defs.put("compositeBean.booleanProp", "true");
+        defs.put("compositeBean.stringProp", "\"\"\"\"");
+
+        ReflectionBuilder builder = new ReflectionBuilder();
+        Map beans = builder.buildObjects(defs);
+
+        CompositeBean compositeBean = (CompositeBean) beans.get("compositeBean");
+        assertNotNull(compositeBean);
+        assertTrue(compositeBean.isBooleanProp());
+        assertEquals(compositeBean.getIntProp(), 42);
+        assertEquals("\"\"", compositeBean.getStringProp());
+    }
+
+    @Test
     public void testSimpleConfigWithDollarSignStringValue() {
         Map<String, String> defs = new LinkedHashMap<String, String>();
         defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");



Mime
View raw message