| <?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>JndiRealmFactory.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> > <a href="index.source.html" class="el_package">org.apache.shiro.realm.jndi</a> > <span class="el_source">JndiRealmFactory.java</span></div><h1>JndiRealmFactory.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.realm.jndi; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.apache.shiro.jndi.JndiLocator; |
| import org.apache.shiro.realm.Realm; |
| import org.apache.shiro.realm.RealmFactory; |
| import org.apache.shiro.util.StringUtils; |
| |
| |
| /** |
| * Looks up one or more Realm instances from JNDI using specified {@link #setJndiNames jndiNames}. |
| * |
| * <p>This is primarily provided to support Realm instances configured in JEE and EJB environments, but will |
| * work in any environment where {@link Realm Realm} instances are bound in JNDI instead of using |
| * programmatic or text-based configuration. |
| * |
| * @since 0.9 |
| */ |
| <span class="nc" id="L41">public class JndiRealmFactory extends JndiLocator implements RealmFactory {</span> |
| |
| <span class="nc" id="L43"> Collection<String> jndiNames = null;</span> |
| |
| /** |
| * Returns the JNDI names that will be used to look up Realm(s) from JNDI. |
| * |
| * @return the JNDI names that will be used to look up Realm(s) from JNDI. |
| * @see #setJndiNames(String) |
| * @see #setJndiNames(Collection) |
| */ |
| public Collection<String> getJndiNames() { |
| <span class="nc" id="L53"> return jndiNames;</span> |
| } |
| |
| /** |
| * Sets the JNDI names that will be used to look up Realm(s) from JNDI. |
| * <p/> |
| * The order of the collection determines the order that the Realms will be returned to the SecurityManager. |
| * <p/> |
| * If you find it easier to specify these names as a comma-delmited string, you may use the |
| * {@link #setJndiNames(String)} method instead. |
| * |
| * @param jndiNames the JNDI names that will be used to look up Realm(s) from JNDI. |
| * @see #setJndiNames(String) |
| */ |
| public void setJndiNames(Collection<String> jndiNames) { |
| <span class="nc" id="L68"> this.jndiNames = jndiNames;</span> |
| <span class="nc" id="L69"> }</span> |
| |
| /** |
| * Specifies a comma-delimited list of JNDI names to lookup, each one corresponding to a jndi-bound |
| * {@link Realm Realm}. The Realms will be made available to the SecurityManager in the order |
| * they are defined. |
| * |
| * @param commaDelimited a comma-delimited list of JNDI names, each representing the JNDI name used to |
| * look up a corresponding jndi-bound Realm. |
| * @throws IllegalStateException if the specified argument is null or the empty string. |
| */ |
| public void setJndiNames(String commaDelimited) throws IllegalStateException { |
| <span class="nc" id="L81"> String arg = StringUtils.clean(commaDelimited);</span> |
| <span class="nc bnc" id="L82" title="All 2 branches missed."> if (arg == null) {</span> |
| <span class="nc" id="L83"> String msg = "One or more comma-delimited jndi names must be specified for the " +</span> |
| <span class="nc" id="L84"> getClass().getName() + " to locate Realms.";</span> |
| <span class="nc" id="L85"> throw new IllegalStateException(msg);</span> |
| } |
| <span class="nc" id="L87"> String[] names = StringUtils.tokenizeToStringArray(arg, ",");</span> |
| <span class="nc" id="L88"> setJndiNames(Arrays.asList(names));</span> |
| <span class="nc" id="L89"> }</span> |
| |
| /** |
| * Performs the JNDI lookups for each specified {@link #getJndiNames() JNDI name} and returns all |
| * discovered Realms in an ordered collection. |
| * |
| * <p>The returned Collection is in the same order as the specified |
| * {@link #setJndiNames(java.util.Collection) jndiNames} |
| * |
| * @return an ordered collection of the {@link #setJndiNames(java.util.Collection) specified Realms} found in JNDI. |
| * @throws IllegalStateException if any of the JNDI names fails to successfully look up a Realm instance. |
| */ |
| public Collection<Realm> getRealms() throws IllegalStateException { |
| <span class="nc" id="L102"> Collection<String> jndiNames = getJndiNames();</span> |
| <span class="nc bnc" id="L103" title="All 4 branches missed."> if (jndiNames == null || jndiNames.isEmpty()) {</span> |
| <span class="nc" id="L104"> String msg = "One or more jndi names must be specified for the " +</span> |
| <span class="nc" id="L105"> getClass().getName() + " to locate Realms.";</span> |
| <span class="nc" id="L106"> throw new IllegalStateException(msg);</span> |
| } |
| <span class="nc" id="L108"> List<Realm> realms = new ArrayList<Realm>(jndiNames.size());</span> |
| <span class="nc bnc" id="L109" title="All 2 branches missed."> for (String name : jndiNames) {</span> |
| try { |
| <span class="nc" id="L111"> Realm realm = (Realm) lookup(name, Realm.class);</span> |
| <span class="nc" id="L112"> realms.add(realm);</span> |
| <span class="nc" id="L113"> } catch (Exception e) {</span> |
| <span class="nc" id="L114"> throw new IllegalStateException("Unable to look up realm with jndi name '" + name + "'.", e);</span> |
| <span class="nc" id="L115"> }</span> |
| <span class="nc" id="L116"> }</span> |
| <span class="nc bnc" id="L117" title="All 2 branches missed."> return realms.isEmpty() ? null : realms;</span> |
| } |
| } |
| </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> |