You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by jb...@apache.org on 2011/09/19 15:36:30 UTC

svn commit: r1172623 - in /shiro/trunk/core/src: main/java/org/apache/shiro/config/ main/java/org/apache/shiro/jndi/ test/java/org/apache/shiro/config/

Author: jbunting
Date: Mon Sep 19 13:36:29 2011
New Revision: 1172623

URL: http://svn.apache.org/viewvc?rev=1172623&view=rev
Log:
SHIRO-217:

- ReflectionBuilder now recognizes Factory implementations and will call the factory method when resolving references
- Added JndiObjectFactory to allow for injection of arbitrary jndi values via ini.

Added:
    shiro/trunk/core/src/main/java/org/apache/shiro/jndi/JndiObjectFactory.java
    shiro/trunk/core/src/test/java/org/apache/shiro/config/SimpleBeanFactory.java
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=1172623&r1=1172622&r2=1172623&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 Mon Sep 19 13:36:29 2011
@@ -24,6 +24,7 @@ import org.apache.shiro.codec.Base64;
 import org.apache.shiro.codec.Hex;
 import org.apache.shiro.util.ClassUtils;
 import org.apache.shiro.util.CollectionUtils;
+import org.apache.shiro.util.Factory;
 import org.apache.shiro.util.Nameable;
 import org.apache.shiro.util.StringUtils;
 import org.slf4j.Logger;
@@ -38,6 +39,9 @@ import java.util.*;
  * 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.
  *
+ * Recognizes {@link Factory} implementations and will call
+ * {@link org.apache.shiro.util.Factory#getInstance() getInstance} to satisfy any reference to this bean.
+ *
  * @since 0.9
  */
 public class ReflectionBuilder {
@@ -228,7 +232,11 @@ public class ReflectionBuilder {
     protected Object resolveReference(String reference) {
         String id = getId(reference);
         log.debug("Encountered object reference '{}'.  Looking up object with id '{}'", reference, id);
-        return getReferencedObject(id);
+        final Object referencedObject = getReferencedObject(id);
+        if(referencedObject instanceof Factory) {
+            return ((Factory)referencedObject).getInstance();
+        }
+        return referencedObject;
     }
 
     protected boolean isTypedProperty(Object object, String propertyName, Class clazz) {

Added: shiro/trunk/core/src/main/java/org/apache/shiro/jndi/JndiObjectFactory.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/jndi/JndiObjectFactory.java?rev=1172623&view=auto
==============================================================================
--- shiro/trunk/core/src/main/java/org/apache/shiro/jndi/JndiObjectFactory.java (added)
+++ shiro/trunk/core/src/main/java/org/apache/shiro/jndi/JndiObjectFactory.java Mon Sep 19 13:36:29 2011
@@ -0,0 +1,45 @@
+package org.apache.shiro.jndi;
+
+import org.apache.shiro.util.Factory;
+
+import javax.naming.NamingException;
+
+/**
+ * A factory implementation intended to be used to look up objects in jndi.
+ * @param <T>
+ * @since 1.2
+ */
+public class JndiObjectFactory<T> extends JndiLocator implements Factory<T> {
+
+    private String resourceName;
+    private Class<? extends T> requiredType;
+
+    public T getInstance() {
+        try {
+            if(requiredType != null) {
+                return requiredType.cast(this.lookup(resourceName, requiredType));
+            } else {
+                return (T) this.lookup(resourceName);
+            }
+        } catch (NamingException e) {
+            final String typeName = requiredType != null ? requiredType.getName() : "object";
+            throw new IllegalStateException("Unable to look up " + typeName + " with jndi name '" + resourceName + "'.", e);
+        }
+    }
+
+    public String getResourceName() {
+        return resourceName;
+    }
+
+    public void setResourceName(String resourceName) {
+        this.resourceName = resourceName;
+    }
+
+    public Class<? extends T> getRequiredType() {
+        return requiredType;
+    }
+
+    public void setRequiredType(Class<? extends T> requiredType) {
+        this.requiredType = requiredType;
+    }
+}

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=1172623&r1=1172622&r2=1172623&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 Mon Sep 19 13:36:29 2011
@@ -21,6 +21,7 @@ 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;
 
@@ -240,4 +241,22 @@ public class ReflectionBuilderTest {
         assertEquals(2, children.size());
     }
 
+    @Test
+    public void testFactoryInstantiation() {
+        Map<String, String> defs = new LinkedHashMap<String, String>();
+        defs.put("simpleBeanFactory", "org.apache.shiro.config.SimpleBeanFactory");
+        defs.put("simpleBeanFactory.factoryInt", "5");
+        defs.put("simpleBeanFactory.factoryString", "someString");
+        defs.put("compositeBean", "org.apache.shiro.config.CompositeBean");
+        defs.put("compositeBean.simpleBean", "$simpleBeanFactory");
+
+        ReflectionBuilder builder = new ReflectionBuilder();
+        Map objects = builder.buildObjects(defs);
+        assertFalse(CollectionUtils.isEmpty(objects));
+        CompositeBean compositeBean = (CompositeBean) objects.get("compositeBean");
+        SimpleBean bean = compositeBean.getSimpleBean();
+        assertNotNull(bean);
+        assertEquals(5, bean.getIntProp());
+        assertEquals("someString", bean.getStringProp());
+    }
 }

Added: shiro/trunk/core/src/test/java/org/apache/shiro/config/SimpleBeanFactory.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/test/java/org/apache/shiro/config/SimpleBeanFactory.java?rev=1172623&view=auto
==============================================================================
--- shiro/trunk/core/src/test/java/org/apache/shiro/config/SimpleBeanFactory.java (added)
+++ shiro/trunk/core/src/test/java/org/apache/shiro/config/SimpleBeanFactory.java Mon Sep 19 13:36:29 2011
@@ -0,0 +1,31 @@
+package org.apache.shiro.config;
+
+import org.apache.shiro.util.Factory;
+
+public class SimpleBeanFactory implements Factory<SimpleBean> {
+    private int factoryInt;
+    private String factoryString;
+
+    public SimpleBean getInstance() {
+        final SimpleBean simpleBean = new SimpleBean();
+        simpleBean.setIntProp(factoryInt);
+        simpleBean.setStringProp(factoryString);
+        return simpleBean;
+    }
+
+    public int getFactoryInt() {
+        return factoryInt;
+    }
+
+    public void setFactoryInt(int factoryInt) {
+        this.factoryInt = factoryInt;
+    }
+
+    public String getFactoryString() {
+        return factoryString;
+    }
+
+    public void setFactoryString(String factoryString) {
+        this.factoryString = factoryString;
+    }
+}