blob: 72f7c8dab5cc678b95a3434689fcbd8a06e45993 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>IniFactorySupport.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.config</a> &gt; <span class="el_source">IniFactorySupport.java</span></div><h1>IniFactorySupport.java</h1><pre class="source lang-java linenums">/*
* 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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.
*/
package org.apache.shiro.config;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.util.AbstractFactory;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Base support class for {@link Factory} implementations that generate their instance(s) based on
* {@link Ini} configuration.
*
* @since 1.0
* @deprecated use Shiro's {@code Environment} mechanisms instead.
*/
@Deprecated
public abstract class IniFactorySupport&lt;T&gt; extends AbstractFactory&lt;T&gt; {
public static final String DEFAULT_INI_RESOURCE_PATH = &quot;classpath:shiro.ini&quot;;
<span class="fc" id="L43"> private static transient final Logger log = LoggerFactory.getLogger(IniFactorySupport.class);</span>
private Ini ini;
private Map&lt;String, ?&gt; defaultBeans;
<span class="fc" id="L49"> protected IniFactorySupport() {</span>
<span class="fc" id="L50"> }</span>
<span class="nc" id="L52"> protected IniFactorySupport(Ini ini) {</span>
<span class="nc" id="L53"> this.ini = ini;</span>
<span class="nc" id="L54"> }</span>
public Ini getIni() {
<span class="fc" id="L57"> return ini;</span>
}
public void setIni(Ini ini) {
<span class="fc" id="L61"> this.ini = ini;</span>
<span class="fc" id="L62"> }</span>
/**
* Returns a mapping of String to bean representing the default set of object used by the factory.
* These beans can be used by this factory in conjunction with objects parsed from the INI configuration.
* @return A Map of default objects, or &lt;code&gt;null&lt;/code&gt;.
* @since 1.4
*/
protected Map&lt;String, ?&gt; getDefaults() {
<span class="fc" id="L71"> return defaultBeans;</span>
}
/**
* Sets the default objects used by this factory. These defaults may be used in conjunction with the INI
* configuration.
* @param defaultBeans String to object mapping used for default configuration in this factory.
* @since 1.4
*/
public void setDefaults(Map&lt;String, ?&gt; defaultBeans) {
<span class="nc" id="L81"> this.defaultBeans = defaultBeans;</span>
<span class="nc" id="L82"> }</span>
/**
* Returns a new Ini instance created from the default {@code classpath:shiro.ini} file, or {@code null} if
* the file does not exist.
*
* @return a new Ini instance created from the default {@code classpath:shiro.ini} file, or {@code null} if
* the file does not exist.
*/
public static Ini loadDefaultClassPathIni() {
<span class="fc" id="L92"> Ini ini = null;</span>
<span class="pc bpc" id="L93" title="1 of 2 branches missed."> if (ResourceUtils.resourceExists(DEFAULT_INI_RESOURCE_PATH)) {</span>
<span class="nc" id="L94"> log.debug(&quot;Found shiro.ini at the root of the classpath.&quot;);</span>
<span class="nc" id="L95"> ini = new Ini();</span>
<span class="nc" id="L96"> ini.loadFromPath(DEFAULT_INI_RESOURCE_PATH);</span>
<span class="nc bnc" id="L97" title="All 2 branches missed."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="nc" id="L98"> log.warn(&quot;shiro.ini found at the root of the classpath, but it did not contain any data.&quot;);</span>
}
}
<span class="fc" id="L101"> return ini;</span>
}
/**
* Tries to resolve the Ini instance to use for configuration. This implementation functions as follows:
* &lt;ol&gt;
* &lt;li&gt;The {@code Ini} instance returned from {@link #getIni()} will be returned if it is not null or empty.&lt;/li&gt;
* &lt;li&gt;If {@link #getIni()} is {@code null} or empty, this implementation will attempt to find and load the
* {@link #loadDefaultClassPathIni() default class path Ini}.&lt;/li&gt;
* &lt;li&gt;If neither of the two attempts above returns an instance, {@code null} is returned&lt;/li&gt;
* &lt;/ol&gt;
*
* @return the Ini instance to use for configuration.
*/
protected Ini resolveIni() {
<span class="fc" id="L116"> Ini ini = getIni();</span>
<span class="fc bfc" id="L117" title="All 2 branches covered."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="fc" id="L118"> log.debug(&quot;Null or empty Ini instance. Falling back to the default {} file.&quot;, DEFAULT_INI_RESOURCE_PATH);</span>
<span class="fc" id="L119"> ini = loadDefaultClassPathIni();</span>
}
<span class="fc" id="L121"> return ini;</span>
}
/**
* Creates a new object instance by using a configured INI source. This implementation functions as follows:
* &lt;ol&gt;
* &lt;li&gt;{@link #resolveIni() Resolve} the {@code Ini} source to use for configuration.&lt;/li&gt;
* &lt;li&gt;If there was no resolved Ini source, create and return a simple default instance via the
* {@link #createDefaultInstance()} method.&lt;/li&gt;
* &lt;/ol&gt;
*
* @return a new {@code SecurityManager} instance by using a configured INI source.
*/
public T createInstance() {
<span class="fc" id="L135"> Ini ini = resolveIni();</span>
T instance;
<span class="fc bfc" id="L139" title="All 2 branches covered."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="fc" id="L140"> log.debug(&quot;No populated Ini available. Creating a default instance.&quot;);</span>
<span class="fc" id="L141"> instance = createDefaultInstance();</span>
<span class="pc bpc" id="L142" title="1 of 2 branches missed."> if (instance == null) {</span>
<span class="nc" id="L143"> String msg = getClass().getName() + &quot; implementation did not return a default instance in &quot; +</span>
&quot;the event of a null/empty Ini configuration. This is required to support the &quot; +
&quot;Factory interface. Please check your implementation.&quot;;
<span class="nc" id="L146"> throw new IllegalStateException(msg);</span>
}
} else {
<span class="fc" id="L149"> log.debug(&quot;Creating instance from Ini [&quot; + ini + &quot;]&quot;);</span>
<span class="fc" id="L150"> instance = createInstance(ini);</span>
<span class="pc bpc" id="L151" title="1 of 2 branches missed."> if (instance == null) {</span>
<span class="nc" id="L152"> String msg = getClass().getName() + &quot; implementation did not return a constructed instance from &quot; +</span>
&quot;the createInstance(Ini) method implementation.&quot;;
<span class="nc" id="L154"> throw new IllegalStateException(msg);</span>
}
}
<span class="fc" id="L158"> return instance;</span>
}
protected abstract T createInstance(Ini ini);
protected abstract T createDefaultInstance();
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>