blob: 52c2d6d19d7664babdb4b2670f01fc16176422bd [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>JndiTemplate.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.jndi</a> &gt; <span class="el_source">JndiTemplate.java</span></div><h1>JndiTemplate.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.jndi;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Helper class that simplifies JNDI operations. It provides methods to lookup and
* bind objects, and allows implementations of the {@link JndiCallback} interface
* to perform any operation they like with a JNDI naming context provided.
* &lt;p/&gt;
* &lt;p&gt;Note that this implementation is an almost exact copy of the Spring Framework's identically named class from
* their 2.5.4 distribution - we didn't want to re-invent the wheel, but not require a full dependency on the
* Spring framework, nor does Spring make available only its JNDI classes in a small jar, or we would have used that.
* Since Shiro is also Apache 2.0 licensed, all regular licenses and conditions and authors have remained in tact.
*
* @see JndiCallback
* @see #execute
*/
public class JndiTemplate {
<span class="fc" id="L47"> private static final Logger log = LoggerFactory.getLogger(JndiTemplate.class);</span>
private Properties environment;
/** Create a new JndiTemplate instance. */
<span class="fc" id="L52"> public JndiTemplate() {</span>
<span class="fc" id="L53"> }</span>
/**
* Create a new JndiTemplate instance, using the given environment.
*
* @param environment the Properties to initialize with
*/
<span class="nc" id="L60"> public JndiTemplate(Properties environment) {</span>
<span class="nc" id="L61"> this.environment = environment;</span>
<span class="nc" id="L62"> }</span>
/**
* Set the environment for the JNDI InitialContext.
*
* @param environment the Properties to initialize with
*/
public void setEnvironment(Properties environment) {
<span class="nc" id="L70"> this.environment = environment;</span>
<span class="nc" id="L71"> }</span>
/**
* Return the environment for the JNDI InitialContext, or &lt;code&gt;null&lt;/code&gt; if none should be used.
*
* @return the environment for the JNDI InitialContext, or &lt;code&gt;null&lt;/code&gt; if none should be used.
*/
public Properties getEnvironment() {
<span class="nc" id="L79"> return this.environment;</span>
}
/**
* Execute the given JNDI context callback implementation.
*
* @param contextCallback JndiCallback implementation
* @return a result object returned by the callback, or &lt;code&gt;null&lt;/code&gt;
* @throws NamingException thrown by the callback implementation
* @see #createInitialContext
*/
public Object execute(JndiCallback contextCallback) throws NamingException {
<span class="nc" id="L91"> Context ctx = createInitialContext();</span>
try {
<span class="nc" id="L93"> return contextCallback.doInContext(ctx);</span>
}
finally {
<span class="nc" id="L96"> try {</span>
<span class="nc" id="L97"> ctx.close();</span>
<span class="nc" id="L98"> } catch (NamingException ex) {</span>
<span class="nc" id="L99"> log.debug(&quot;Could not close JNDI InitialContext&quot;, ex);</span>
<span class="nc" id="L100"> }</span>
}
}
/**
* Create a new JNDI initial context. Invoked by {@link #execute}.
* &lt;p&gt;The default implementation use this template's environment settings.
* Can be subclassed for custom contexts, e.g. for testing.
*
* @return the initial Context instance
* @throws NamingException in case of initialization errors
*/
@SuppressWarnings({&quot;unchecked&quot;})
protected Context createInitialContext() throws NamingException {
<span class="nc" id="L114"> Properties env = getEnvironment();</span>
<span class="nc" id="L115"> Hashtable icEnv = null;</span>
<span class="nc bnc" id="L116" title="All 2 branches missed."> if (env != null) {</span>
<span class="nc" id="L117"> icEnv = new Hashtable(env.size());</span>
<span class="nc bnc" id="L118" title="All 2 branches missed."> for (Enumeration en = env.propertyNames(); en.hasMoreElements();) {</span>
<span class="nc" id="L119"> String key = (String) en.nextElement();</span>
<span class="nc" id="L120"> icEnv.put(key, env.getProperty(key));</span>
<span class="nc" id="L121"> }</span>
}
<span class="nc" id="L123"> return new InitialContext(icEnv);</span>
}
/**
* Look up the object with the given name in the current JNDI context.
*
* @param name the JNDI name of the object
* @return object found (cannot be &lt;code&gt;null&lt;/code&gt;; if a not so well-behaved
* JNDI implementations returns null, a NamingException gets thrown)
* @throws NamingException if there is no object with the given
* name bound to JNDI
*/
public Object lookup(final String name) throws NamingException {
<span class="nc" id="L136"> log.debug(&quot;Looking up JNDI object with name '{}'&quot;, name);</span>
<span class="nc" id="L137"> return execute(new JndiCallback() {</span>
public Object doInContext(Context ctx) throws NamingException {
<span class="nc" id="L139"> Object located = ctx.lookup(name);</span>
<span class="nc bnc" id="L140" title="All 2 branches missed."> if (located == null) {</span>
<span class="nc" id="L141"> throw new NameNotFoundException(</span>
&quot;JNDI object with [&quot; + name + &quot;] not found: JNDI implementation returned null&quot;);
}
<span class="nc" id="L144"> return located;</span>
}
});
}
/**
* Look up the object with the given name in the current JNDI context.
*
* @param name the JNDI name of the object
* @param requiredType type the JNDI object must match. Can be an interface or
* superclass of the actual class, or &lt;code&gt;null&lt;/code&gt; for any match. For example,
* if the value is &lt;code&gt;Object.class&lt;/code&gt;, this method will succeed whatever
* the class of the returned instance.
* @return object found (cannot be &lt;code&gt;null&lt;/code&gt;; if a not so well-behaved
* JNDI implementations returns null, a NamingException gets thrown)
* @throws NamingException if there is no object with the given
* name bound to JNDI
*/
public Object lookup(String name, Class requiredType) throws NamingException {
<span class="nc" id="L163"> Object jndiObject = lookup(name);</span>
<span class="nc bnc" id="L164" title="All 4 branches missed."> if (requiredType != null &amp;&amp; !requiredType.isInstance(jndiObject)) {</span>
<span class="nc" id="L165"> String msg = &quot;Jndi object acquired under name '&quot; + name + &quot;' is of type [&quot; +</span>
<span class="nc" id="L166"> jndiObject.getClass().getName() + &quot;] and not assignable to the required type [&quot; +</span>
<span class="nc" id="L167"> requiredType.getName() + &quot;].&quot;;</span>
<span class="nc" id="L168"> throw new NamingException(msg);</span>
}
<span class="nc" id="L170"> return jndiObject;</span>
}
/**
* Bind the given object to the current JNDI context, using the given name.
*
* @param name the JNDI name of the object
* @param object the object to bind
* @throws NamingException thrown by JNDI, mostly name already bound
*/
public void bind(final String name, final Object object) throws NamingException {
<span class="nc" id="L181"> log.debug(&quot;Binding JNDI object with name '{}'&quot;, name);</span>
<span class="nc" id="L182"> execute(new JndiCallback() {</span>
public Object doInContext(Context ctx) throws NamingException {
<span class="nc" id="L184"> ctx.bind(name, object);</span>
<span class="nc" id="L185"> return null;</span>
}
});
<span class="nc" id="L188"> }</span>
/**
* Rebind the given object to the current JNDI context, using the given name.
* Overwrites any existing binding.
*
* @param name the JNDI name of the object
* @param object the object to rebind
* @throws NamingException thrown by JNDI
*/
public void rebind(final String name, final Object object) throws NamingException {
<span class="nc" id="L199"> log.debug(&quot;Rebinding JNDI object with name '{}'&quot;, name);</span>
<span class="nc" id="L200"> execute(new JndiCallback() {</span>
public Object doInContext(Context ctx) throws NamingException {
<span class="nc" id="L202"> ctx.rebind(name, object);</span>
<span class="nc" id="L203"> return null;</span>
}
});
<span class="nc" id="L206"> }</span>
/**
* Remove the binding for the given name from the current JNDI context.
*
* @param name the JNDI name of the object
* @throws NamingException thrown by JNDI, mostly name not found
*/
public void unbind(final String name) throws NamingException {
<span class="nc" id="L215"> log.debug(&quot;Unbinding JNDI object with name '{}'&quot;, name);</span>
<span class="nc" id="L216"> execute(new JndiCallback() {</span>
public Object doInContext(Context ctx) throws NamingException {
<span class="nc" id="L218"> ctx.unbind(name);</span>
<span class="nc" id="L219"> return null;</span>
}
});
<span class="nc" id="L222"> }</span>
}
</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>