| <?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 :: All (aggregate jar)</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.config</a> > <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 |
| * "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. |
| */ |
| 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<T> extends AbstractFactory<T> { |
| |
| public static final String DEFAULT_INI_RESOURCE_PATH = "classpath:shiro.ini"; |
| |
| <span class="fc" id="L43"> private static transient final Logger log = LoggerFactory.getLogger(IniFactorySupport.class);</span> |
| |
| private Ini ini; |
| |
| private Map<String, ?> defaultBeans; |
| |
| <span class="fc" id="L49"> protected IniFactorySupport() {</span> |
| <span class="fc" id="L50"> }</span> |
| |
| <span class="fc" id="L52"> protected IniFactorySupport(Ini ini) {</span> |
| <span class="fc" id="L53"> this.ini = ini;</span> |
| <span class="fc" 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 <code>null</code>. |
| * @since 1.4 |
| */ |
| protected Map<String, ?> 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<String, ?> defaultBeans) { |
| <span class="fc" id="L81"> this.defaultBeans = defaultBeans;</span> |
| <span class="fc" 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("Found shiro.ini at the root of the classpath.");</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("shiro.ini found at the root of the classpath, but it did not contain any data.");</span> |
| } |
| } |
| <span class="fc" id="L101"> return ini;</span> |
| } |
| |
| /** |
| * Tries to resolve the Ini instance to use for configuration. This implementation functions as follows: |
| * <ol> |
| * <li>The {@code Ini} instance returned from {@link #getIni()} will be returned if it is not null or empty.</li> |
| * <li>If {@link #getIni()} is {@code null} or empty, this implementation will attempt to find and load the |
| * {@link #loadDefaultClassPathIni() default class path Ini}.</li> |
| * <li>If neither of the two attempts above returns an instance, {@code null} is returned</li> |
| * </ol> |
| * |
| * @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("Null or empty Ini instance. Falling back to the default {} file.", 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: |
| * <ol> |
| * <li>{@link #resolveIni() Resolve} the {@code Ini} source to use for configuration.</li> |
| * <li>If there was no resolved Ini source, create and return a simple default instance via the |
| * {@link #createDefaultInstance()} method.</li> |
| * </ol> |
| * |
| * @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("No populated Ini available. Creating a default instance.");</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() + " implementation did not return a default instance in " +</span> |
| "the event of a null/empty Ini configuration. This is required to support the " + |
| "Factory interface. Please check your implementation."; |
| <span class="nc" id="L146"> throw new IllegalStateException(msg);</span> |
| } |
| } else { |
| <span class="fc" id="L149"> log.debug("Creating instance from Ini [" + ini + "]");</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() + " implementation did not return a constructed instance from " +</span> |
| "the createInstance(Ini) method implementation."; |
| <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.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |