SHIRO-445 - Expose get/set methods from IniWebEnvironment to ReflectionBuilder 
to allow configuring custom interpolation


Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/b2f5db9d
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/b2f5db9d
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/b2f5db9d

Branch: refs/heads/master
Commit: b2f5db9d19c736b6cc88a0a57520689820587724
Parents: 86d951e
Author: Brian Demers <bdem...@apache.org>
Authored: Tue Oct 4 14:38:51 2016 -0400
Committer: Brian Demers <bdem...@apache.org>
Committed: Tue Oct 18 13:24:41 2016 -0400

----------------------------------------------------------------------
 .../shiro/config/CommonsInterpolator.java       |  4 ++
 .../apache/shiro/config/ReflectionBuilder.java  | 20 +++++++--
 .../shiro/config/IniSecurityManagerFactory.java | 44 ++++++++++++++++----
 support/servlet-plugin/pom.xml                  | 23 +++++-----
 .../main/resources/META-INF/web-fragment.xml    | 22 ++++++----
 .../apache/shiro/web/env/IniWebEnvironment.java | 28 ++++++++++++-
 6 files changed, 108 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/config/ogdl/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
----------------------------------------------------------------------
diff --git 
a/config/ogdl/src/main/java/org/apache/shiro/config/CommonsInterpolator.java 
b/config/ogdl/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
index 0d1c7fc..03b472c 100644
--- a/config/ogdl/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
+++ b/config/ogdl/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
@@ -69,4 +69,8 @@ public class CommonsInterpolator implements Interpolator {
     public String interpolate(String value) {
         return (String) interpolator.interpolate(value);
     }
+
+    public ConfigurationInterpolator getConfigurationInterpolator() {
+        return interpolator;
+    }
 }

http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
----------------------------------------------------------------------
diff --git 
a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java 
b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
index af4fa06..b2da6a8 100644
--- a/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
+++ b/config/ogdl/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
@@ -83,9 +83,14 @@ public class ReflectionBuilder {
 
     private static final String EVENT_BUS_NAME = "eventBus";
 
-    private final Interpolator interpolator;
-
     private final Map<String, Object> objects;
+
+    /**
+     * Interpolation allows for ${key} substitution of values.
+     * @since 1.4
+     */
+    private Interpolator interpolator;
+
     /**
      * @since 1.3
      */
@@ -252,8 +257,7 @@ public class ReflectionBuilder {
 
             for (Map.Entry<String, String> entry : kvPairs.entrySet()) {
                 String lhs = entry.getKey();
-                String rhs = (String) 
interpolator.interpolate(entry.getValue());
-//                String rhs = entry.getValue();
+                String rhs = interpolator.interpolate(entry.getValue());
 
                 String beanId = parseBeanId(lhs);
                 if (beanId != null) { //a beanId could be parsed, so the line 
is a bean instance definition
@@ -734,6 +738,14 @@ public class ReflectionBuilder {
         return new DefaultInterpolator();
     }
 
+    /**
+     * Sets the {@link Interpolator} used when evaluating the right side of 
the expressions.
+     * @since 1.4
+     */
+    public void setInterpolator(Interpolator interpolator) {
+        this.interpolator = interpolator;
+    }
+
     private class BeanConfigurationProcessor {
 
         private final List<Statement> statements = new ArrayList<Statement>();

http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java 
b/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
index 628f0b5..b5d4431 100644
--- a/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
+++ b/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
@@ -61,9 +61,11 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
      * source will be resolved to use to build the instance.
      */
     public IniSecurityManagerFactory() {
+        this.builder = new ReflectionBuilder();
     }
 
     public IniSecurityManagerFactory(Ini config) {
+        this();
         setIni(config);
     }
 
@@ -76,13 +78,13 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
     }
 
     public void destroy() {
-        if(this.builder != null) {
-            builder.destroy();
+        if(getReflectionBuilder() != null) {
+            getReflectionBuilder().destroy();
         }
     }
 
     private SecurityManager getSecurityManagerBean() {
-        return builder.getBean(SECURITY_MANAGER_NAME, SecurityManager.class);
+        return getReflectionBuilder().getBean(SECURITY_MANAGER_NAME, 
SecurityManager.class);
     }
 
     protected SecurityManager createDefaultInstance() {
@@ -102,12 +104,17 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
     }
 
     private SecurityManager createSecurityManager(Ini ini) {
+        return createSecurityManager(ini, getConfigSection(ini));
+    }
+
+    private Ini.Section getConfigSection(Ini ini) {
+
         Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME);
         if (CollectionUtils.isEmpty(mainSection)) {
             //try the default:
             mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME);
         }
-        return createSecurityManager(ini, mainSection);
+        return mainSection;
     }
 
     protected boolean isAutoApplyRealms(SecurityManager securityManager) {
@@ -128,8 +135,8 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
     @SuppressWarnings({"unchecked"})
     private SecurityManager createSecurityManager(Ini ini, Ini.Section 
mainSection) {
 
-        Map<String, ?> defaults = createDefaults(ini, mainSection);
-        Map<String, ?> objects = buildInstances(mainSection, defaults);
+        getReflectionBuilder().setObjects(createDefaults(ini, mainSection));
+        Map<String, ?> objects = buildInstances(mainSection);
 
         SecurityManager securityManager = getSecurityManagerBean();
 
@@ -170,9 +177,8 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
         return defaults;
     }
 
-    private Map<String, ?> buildInstances(Ini.Section section, Map<String, ?> 
defaults) {
-        this.builder = new ReflectionBuilder(defaults);
-        return this.builder.buildObjects(section);
+    private Map<String, ?> buildInstances(Ini.Section section) {
+        return getReflectionBuilder().buildObjects(section);
     }
 
     private void addToRealms(Collection<Realm> realms, RealmFactory factory) {
@@ -263,4 +269,24 @@ public class IniSecurityManagerFactory extends 
IniFactorySupport<SecurityManager
         realm.setIni(ini); //added for SHIRO-322
         return realm;
     }
+
+    /**
+     * Returns the ReflectionBuilder instance used to create SecurityManagers 
object graph.
+     * @return ReflectionBuilder instance used to create SecurityManagers 
object graph.
+     * @since 1.4
+     */
+    public ReflectionBuilder getReflectionBuilder() {
+        return builder;
+    }
+
+    /**
+     * Sets the ReflectionBuilder that will be used to create the 
SecurityManager based on the contents of
+     * the Ini configuration.
+     * @param builder The ReflectionBuilder used to parse the Ini 
configuration.
+     * @since 1.4
+     */
+    @SuppressWarnings("unused")
+    public void setReflectionBuilder(ReflectionBuilder builder) {
+        this.builder = builder;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/support/servlet-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/support/servlet-plugin/pom.xml b/support/servlet-plugin/pom.xml
index 4087cce..4950cea 100644
--- a/support/servlet-plugin/pom.xml
+++ b/support/servlet-plugin/pom.xml
@@ -1,18 +1,21 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-  ~ Copyright 2012 Stormpath, Inc.
-  ~
-  ~ Licensed under the Apache License, Version 2.0 (the "License");
-  ~ you may not use this file except in compliance with the License.
-  ~ You may obtain a copy of the License at
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
   ~
   ~     http://www.apache.org/licenses/LICENSE-2.0
   ~
-  ~ Unless required by applicable law or agreed to in writing, software
-  ~ distributed under the License is distributed on an "AS IS" BASIS,
-  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  ~ See the License for the specific language governing permissions and
-  ~ limitations under the License.
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
   -->
 <!--suppress osmorcNonOsgiMavenDependency -->
 <project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>

http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/support/servlet-plugin/src/main/resources/META-INF/web-fragment.xml
----------------------------------------------------------------------
diff --git 
a/support/servlet-plugin/src/main/resources/META-INF/web-fragment.xml 
b/support/servlet-plugin/src/main/resources/META-INF/web-fragment.xml
index dd478c3..10c9673 100644
--- a/support/servlet-plugin/src/main/resources/META-INF/web-fragment.xml
+++ b/support/servlet-plugin/src/main/resources/META-INF/web-fragment.xml
@@ -1,17 +1,21 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~
-  ~ Licensed under the Apache License, Version 2.0 (the "License");
-  ~ you may not use this file except in compliance with the License.
-  ~ You may obtain a copy of the License at
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
   ~
   ~     http://www.apache.org/licenses/LICENSE-2.0
   ~
-  ~ Unless required by applicable law or agreed to in writing, software
-  ~ distributed under the License is distributed on an "AS IS" BASIS,
-  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  ~ See the License for the specific language governing permissions and
-  ~ limitations under the License.
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
   -->
 <web-fragment metadata-complete="true"
               xmlns="http://xmlns.jcp.org/xml/ns/javaee";

http://git-wip-us.apache.org/repos/asf/shiro/blob/b2f5db9d/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
----------------------------------------------------------------------
diff --git a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java 
b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
index bff933a..6bef199 100644
--- a/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
+++ b/web/src/main/java/org/apache/shiro/web/env/IniWebEnvironment.java
@@ -54,6 +54,12 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
      */
     private Ini ini;
 
+    private WebIniSecurityManagerFactory factory;
+
+    public IniWebEnvironment() {
+        factory = new WebIniSecurityManagerFactory();
+    }
+
     /**
      * Initializes this instance by resolving any potential (explicit or 
resource-configured) {@link Ini}
      * configuration and calling {@link #configure() configure} for actual 
instance configuration.
@@ -273,7 +279,6 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
     }
 
     protected WebSecurityManager createWebSecurityManager() {
-        WebIniSecurityManagerFactory factory = new 
WebIniSecurityManagerFactory();
 
         Ini ini = getIni();
         if (!CollectionUtils.isEmpty(ini)) {
@@ -397,4 +402,25 @@ public class IniWebEnvironment extends 
ResourceBasedWebEnvironment implements In
         defaults.put(FILTER_CHAIN_RESOLVER_NAME, new 
IniFilterChainResolverFactory());
         return defaults;
     }
+
+    /**
+     * Returns the SecurityManager factory used by this WebEnvironment.
+     *
+     * @return the SecurityManager factory used by this WebEnvironment.
+     * @since 1.4
+     */
+    @SuppressWarnings("unused")
+    protected WebIniSecurityManagerFactory getSecurityManagerFactory() {
+        return factory;
+    }
+
+    /**
+     * Allows for setting the SecurityManager factory which will be used to 
create the SecurityManager.
+     *
+     * @param factory the SecurityManager factory to used.
+     * @since 1.4
+     */
+    protected void setSecurityManagerFactory(WebIniSecurityManagerFactory 
factory) {
+        this.factory = factory;
+    }
 }

Reply via email to