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

[12/20] shiro git commit: SHIRO-445 - Expose get/set methods from IniWebEnvironment to ReflectionBuilder to allow configuring custom interpolation

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/a76b1ee9
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/a76b1ee9
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/a76b1ee9

Branch: refs/heads/1.4.x
Commit: a76b1ee9f3a45557d96cdd5e4abadd7a0e7640d0
Parents: 542a32e
Author: Brian Demers <bd...@apache.org>
Authored: Tue Oct 4 14:38:51 2016 -0400
Committer: Brian Demers <bd...@apache.org>
Committed: Fri Oct 14 15:15:51 2016 -0400

----------------------------------------------------------------------
 .../shiro/config/CommonsInterpolator.java       |  4 ++
 .../shiro/config/IniSecurityManagerFactory.java | 44 ++++++++++++++++----
 .../apache/shiro/config/ReflectionBuilder.java  | 20 +++++++--
 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/a76b1ee9/core/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/shiro/config/CommonsInterpolator.java b/core/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
index 0d1c7fc..03b472c 100644
--- a/core/src/main/java/org/apache/shiro/config/CommonsInterpolator.java
+++ b/core/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/a76b1ee9/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 6ff6e64..9a0b609 100644
--- a/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
+++ b/core/src/main/java/org/apache/shiro/config/IniSecurityManagerFactory.java
@@ -59,9 +59,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);
     }
 
@@ -74,13 +76,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() {
@@ -100,12 +102,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) {
@@ -126,8 +133,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();
 
@@ -168,9 +175,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) {
@@ -261,4 +267,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/a76b1ee9/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java b/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
index 9900459..63d6d57 100644
--- a/core/src/main/java/org/apache/shiro/config/ReflectionBuilder.java
+++ b/core/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
@@ -735,6 +739,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/a76b1ee9/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/a76b1ee9/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/a76b1ee9/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;
+    }
 }