View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.config;
20  
21  import org.apache.shiro.io.ResourceUtils;
22  import org.apache.shiro.util.AbstractFactory;
23  import org.apache.shiro.util.CollectionUtils;
24  import org.apache.shiro.util.Factory;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * Base support class for {@link Factory} implementations that generate their instance(s) based on
30   * {@link Ini} configuration.
31   *
32   * @since 1.0
33   */
34  public abstract class IniFactorySupport<T> extends AbstractFactory<T> {
35  
36      public static final String DEFAULT_INI_RESOURCE_PATH = "classpath:shiro.ini";
37  
38      private static transient final Logger log = LoggerFactory.getLogger(IniFactorySupport.class);
39  
40      private Ini ini;
41  
42      protected IniFactorySupport() {
43      }
44  
45      protected IniFactorySupport(Ini ini) {
46          this.ini = ini;
47      }
48  
49      public Ini getIni() {
50          return ini;
51      }
52  
53      public void setIni(Ini ini) {
54          this.ini = ini;
55      }
56  
57      /**
58       * Returns a new Ini instance created from the default {@code classpath:shiro.ini} file, or {@code null} if
59       * the file does not exist.
60       *
61       * @return a new Ini instance created from the default {@code classpath:shiro.ini} file, or {@code null} if
62       *         the file does not exist.
63       */
64      public static Ini loadDefaultClassPathIni() {
65          Ini ini = null;
66          if (ResourceUtils.resourceExists(DEFAULT_INI_RESOURCE_PATH)) {
67              log.debug("Found shiro.ini at the root of the classpath.");
68              ini = new Ini();
69              ini.loadFromPath(DEFAULT_INI_RESOURCE_PATH);
70              if (CollectionUtils.isEmpty(ini)) {
71                  log.warn("shiro.ini found at the root of the classpath, but it did not contain any data.");
72              }
73          }
74          return ini;
75      }
76  
77      /**
78       * Tries to resolve the Ini instance to use for configuration.  This implementation functions as follows:
79       * <ol>
80       * <li>The {@code Ini} instance returned from {@link #getIni()} will be returned if it is not null or empty.</li>
81       * <li>If {@link #getIni()} is {@code null} or empty, this implementation will attempt to find and load the
82       * {@link #loadDefaultClassPathIni() default class path Ini}.</li>
83       * <li>If neither of the two attempts above returns an instance, {@code null} is returned</li>
84       * </ol>
85       *
86       * @return the Ini instance to use for configuration.
87       */
88      protected Ini resolveIni() {
89          Ini ini = getIni();
90          if (CollectionUtils.isEmpty(ini)) {
91              log.debug("Null or empty Ini instance.  Falling back to the default {} file.", DEFAULT_INI_RESOURCE_PATH);
92              ini = loadDefaultClassPathIni();
93          }
94          return ini;
95      }
96  
97      /**
98       * Creates a new object instance by using a configured INI source.  This implementation functions as follows:
99       * <ol>
100      * <li>{@link #resolveIni() Resolve} the {@code Ini} source to use for configuration.</li>
101      * <li>If there was no resolved Ini source, create and return a simple default instance via the
102      * {@link #createDefaultInstance()} method.</li>
103      * </ol>
104      *
105      * @return a new {@code SecurityManager} instance by using a configured INI source.
106      */
107     public T createInstance() {
108         Ini ini = resolveIni();
109 
110         T instance;
111 
112         if (CollectionUtils.isEmpty(ini)) {
113             log.debug("No populated Ini available.  Creating a default instance.");
114             instance = createDefaultInstance();
115             if (instance == null) {
116                 String msg = getClass().getName() + " implementation did not return a default instance in " +
117                         "the event of a null/empty Ini configuration.  This is required to support the " +
118                         "Factory interface.  Please check your implementation.";
119                 throw new IllegalStateException(msg);
120             }
121         } else {
122             log.debug("Creating instance from Ini [" + ini + "]");
123             instance = createInstance(ini);
124             if (instance == null) {
125                 String msg = getClass().getName() + " implementation did not return a constructed instance from " +
126                         "the createInstance(Ini) method implementation.";
127                 throw new IllegalStateException(msg);
128             }
129         }
130 
131         return instance;
132     }
133 
134     protected abstract T createInstance(Ini ini);
135 
136     protected abstract T createDefaultInstance();
137 }