blob: 172149491e3db566908326145c0edc22b7a81b92 [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 :: Jar Bundle</a> &gt; <a href="../index.html" class="el_bundle">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;
/**
* Base support class for {@link Factory} implementations that generate their instance(s) based on
* {@link Ini} configuration.
*
* @since 1.0
*/
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="L38"> private static transient final Logger log = LoggerFactory.getLogger(IniFactorySupport.class);</span>
private Ini ini;
<span class="fc" id="L42"> protected IniFactorySupport() {</span>
<span class="fc" id="L43"> }</span>
<span class="fc" id="L45"> protected IniFactorySupport(Ini ini) {</span>
<span class="fc" id="L46"> this.ini = ini;</span>
<span class="fc" id="L47"> }</span>
public Ini getIni() {
<span class="fc" id="L50"> return ini;</span>
}
public void setIni(Ini ini) {
<span class="fc" id="L54"> this.ini = ini;</span>
<span class="fc" id="L55"> }</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="L65"> Ini ini = null;</span>
<span class="pc bpc" id="L66" title="1 of 2 branches missed."> if (ResourceUtils.resourceExists(DEFAULT_INI_RESOURCE_PATH)) {</span>
<span class="nc" id="L67"> log.debug(&quot;Found shiro.ini at the root of the classpath.&quot;);</span>
<span class="nc" id="L68"> ini = new Ini();</span>
<span class="nc" id="L69"> ini.loadFromPath(DEFAULT_INI_RESOURCE_PATH);</span>
<span class="nc bnc" id="L70" title="All 2 branches missed."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="nc" id="L71"> 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="L74"> 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="L89"> Ini ini = getIni();</span>
<span class="fc bfc" id="L90" title="All 2 branches covered."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="fc" id="L91"> log.debug(&quot;Null or empty Ini instance. Falling back to the default {} file.&quot;, DEFAULT_INI_RESOURCE_PATH);</span>
<span class="fc" id="L92"> ini = loadDefaultClassPathIni();</span>
}
<span class="fc" id="L94"> 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="L108"> Ini ini = resolveIni();</span>
T instance;
<span class="fc bfc" id="L112" title="All 2 branches covered."> if (CollectionUtils.isEmpty(ini)) {</span>
<span class="fc" id="L113"> log.debug(&quot;No populated Ini available. Creating a default instance.&quot;);</span>
<span class="fc" id="L114"> instance = createDefaultInstance();</span>
<span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (instance == null) {</span>
<span class="nc" id="L116"> 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="L119"> throw new IllegalStateException(msg);</span>
}
} else {
<span class="fc" id="L122"> log.debug(&quot;Creating instance from Ini [&quot; + ini + &quot;]&quot;);</span>
<span class="fc" id="L123"> instance = createInstance(ini);</span>
<span class="pc bpc" id="L124" title="1 of 2 branches missed."> if (instance == null) {</span>
<span class="nc" id="L125"> 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="L127"> throw new IllegalStateException(msg);</span>
}
}
<span class="fc" id="L131"> 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.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>