blob: 736c3b5ca74069d64af6dd827c70fb0a18a86949 [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>SubjectThreadState.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.subject.support</a> &gt; <span class="el_source">SubjectThreadState.java</span></div><h1>SubjectThreadState.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.subject.support;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.util.ThreadState;
import java.util.Map;
/**
* Manages thread-state for {@link Subject Subject} access (supporting
* {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls)
* during a thread's execution.
* &lt;p/&gt;
* The {@link #bind bind} method will bind a {@link Subject} and a
* {@link org.apache.shiro.mgt.SecurityManager SecurityManager} to the {@link ThreadContext} so they can be retrieved
* from the {@code ThreadContext} later by any
* {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur during
* the thread's execution.
*
* @since 1.0
*/
public class SubjectThreadState implements ThreadState {
private Map&lt;Object, Object&gt; originalResources;
private final Subject subject;
private transient SecurityManager securityManager;
/**
* Creates a new {@code SubjectThreadState} that will bind and unbind the specified {@code Subject} to the
* thread
*
* @param subject the {@code Subject} instance to bind and unbind from the {@link ThreadContext}.
*/
<span class="fc" id="L55"> public SubjectThreadState(Subject subject) {</span>
<span class="pc bpc" id="L56" title="1 of 2 branches missed."> if (subject == null) {</span>
<span class="nc" id="L57"> throw new IllegalArgumentException(&quot;Subject argument cannot be null.&quot;);</span>
}
<span class="fc" id="L59"> this.subject = subject;</span>
<span class="fc" id="L61"> SecurityManager securityManager = null;</span>
<span class="fc bfc" id="L62" title="All 2 branches covered."> if ( subject instanceof DelegatingSubject) {</span>
<span class="fc" id="L63"> securityManager = ((DelegatingSubject)subject).getSecurityManager();</span>
}
<span class="fc bfc" id="L65" title="All 2 branches covered."> if ( securityManager == null) {</span>
<span class="fc" id="L66"> securityManager = ThreadContext.getSecurityManager();</span>
}
<span class="fc" id="L68"> this.securityManager = securityManager;</span>
<span class="fc" id="L69"> }</span>
/**
* Returns the {@code Subject} instance managed by this {@code ThreadState} implementation.
*
* @return the {@code Subject} instance managed by this {@code ThreadState} implementation.
*/
protected Subject getSubject() {
<span class="nc" id="L77"> return this.subject;</span>
}
/**
* Binds a {@link Subject} and {@link org.apache.shiro.mgt.SecurityManager SecurityManager} to the
* {@link ThreadContext} so they can be retrieved later by any
* {@code SecurityUtils.}{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()} calls that might occur
* during the thread's execution.
* &lt;p/&gt;
* Prior to binding, the {@code ThreadContext}'s existing {@link ThreadContext#getResources() resources} are
* retained so they can be restored later via the {@link #restore restore} call.
*/
public void bind() {
<span class="fc" id="L90"> SecurityManager securityManager = this.securityManager;</span>
<span class="fc bfc" id="L91" title="All 2 branches covered."> if ( securityManager == null ) {</span>
//try just in case the constructor didn't find one at the time:
<span class="fc" id="L93"> securityManager = ThreadContext.getSecurityManager();</span>
}
<span class="fc" id="L95"> this.originalResources = ThreadContext.getResources();</span>
<span class="fc" id="L96"> ThreadContext.remove();</span>
<span class="fc" id="L98"> ThreadContext.bind(this.subject);</span>
<span class="fc bfc" id="L99" title="All 2 branches covered."> if (securityManager != null) {</span>
<span class="fc" id="L100"> ThreadContext.bind(securityManager);</span>
}
<span class="fc" id="L102"> }</span>
/**
* {@link ThreadContext#remove Remove}s all thread-state that was bound by this instance. If any previous
* thread-bound resources existed prior to the {@link #bind bind} call, they are restored back to the
* {@code ThreadContext} to ensure the thread state is exactly as it was before binding.
*/
public void restore() {
<span class="fc" id="L110"> ThreadContext.remove();</span>
<span class="pc bpc" id="L111" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(this.originalResources)) {</span>
<span class="nc" id="L112"> ThreadContext.setResources(this.originalResources);</span>
}
<span class="fc" id="L114"> }</span>
/**
* Completely {@link ThreadContext#remove removes} the {@code ThreadContext} state. Typically this method should
* only be called in special cases - it is more 'correct' to {@link #restore restore} a thread to its previous
* state than to clear it entirely.
*/
public void clear() {
<span class="fc" id="L122"> ThreadContext.remove();</span>
<span class="fc" id="L123"> }</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>