blob: 5ac73440b00f9b1467d0d7248013a2adfe759db7 [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>SubjectAwareExecutorService.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.concurrent</a> &gt; <span class="el_source">SubjectAwareExecutorService.java</span></div><h1>SubjectAwareExecutorService.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.concurrent;
import org.apache.shiro.subject.Subject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
/**
* {@code ExecutorService} implementation that will automatically first associate any argument
* {@link Runnable} or {@link Callable} instances with the {@link #getSubject currently available subject} and then
* dispatch the Subject-enabled runnable or callable to an underlying delegate
* {@link java.util.concurrent.ExecutorService ExecutorService} instance. The principle is the same as the
* parent {@link SubjectAwareExecutor} class, but enables the richer {@link ExecutorService} API.
* &lt;p/&gt;
* This is a simplification for applications that want to execute code as the currently
* executing {@code Subject} on another thread, but don't want or need to call the
* {@link Subject#associateWith(Runnable)} or {@link Subject#associateWith(Callable)} methods and dispatch them to a
* Thread manually. This simplifies code and reduces Shiro dependencies across application source code.
* &lt;p/&gt;
* Consider this code that could be repeated in many places across an application:
* &lt;pre&gt;
* {@link Callable Callable} applicationWork = //instantiate or acquire Callable from somewhere
* {@link Subject Subject} subject = {@link org.apache.shiro.SecurityUtils SecurityUtils}.{@link org.apache.shiro.SecurityUtils#getSubject() getSubject()};
* {@link Callable Callable} work = subject.{@link Subject#associateWith(Callable) associateWith(applicationWork)};
* {@link ExecutorService anExecutorService}.{@link ExecutorService#submit(Callable) submit(work)};
* &lt;/pre&gt;
* Instead, if the {@code ExecutorService} instance used at runtime is an instance of this class
* (which delegates to the target ExecutorService that you want), all places in code like the above reduce to this:
* &lt;pre&gt;
* {@link Callable Callable} applicationWork = //instantiate or acquire Callable from somewhere
* {@link ExecutorService anExecutorService}.{@link ExecutorService#submit(Callable) submit(work)};
* &lt;/pre&gt;
* Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across
* your codebase.
*
* @since 1.0
*/
public class SubjectAwareExecutorService extends SubjectAwareExecutor implements ExecutorService {
private ExecutorService targetExecutorService;
<span class="nc" id="L62"> public SubjectAwareExecutorService() {</span>
<span class="nc" id="L63"> }</span>
<span class="fc" id="L65"> public SubjectAwareExecutorService(ExecutorService target) {</span>
<span class="fc" id="L66"> setTargetExecutorService(target);</span>
<span class="fc" id="L67"> }</span>
public ExecutorService getTargetExecutorService() {
<span class="nc" id="L70"> return targetExecutorService;</span>
}
public void setTargetExecutorService(ExecutorService targetExecutorService) {
<span class="fc" id="L74"> super.setTargetExecutor(targetExecutorService);</span>
<span class="fc" id="L75"> this.targetExecutorService = targetExecutorService;</span>
<span class="fc" id="L76"> }</span>
@Override
public void setTargetExecutor(Executor targetExecutor) {
<span class="nc bnc" id="L80" title="All 2 branches missed."> if (!(targetExecutor instanceof ExecutorService)) {</span>
<span class="nc" id="L81"> String msg = &quot;The &quot; + getClass().getName() + &quot; implementation only accepts &quot; +</span>
<span class="nc" id="L82"> ExecutorService.class.getName() + &quot; target instances.&quot;;</span>
<span class="nc" id="L83"> throw new IllegalArgumentException(msg);</span>
}
<span class="nc" id="L85"> super.setTargetExecutor(targetExecutor);</span>
<span class="nc" id="L86"> }</span>
public void shutdown() {
<span class="nc" id="L89"> this.targetExecutorService.shutdown();</span>
<span class="nc" id="L90"> }</span>
public List&lt;Runnable&gt; shutdownNow() {
<span class="nc" id="L93"> return this.targetExecutorService.shutdownNow();</span>
}
public boolean isShutdown() {
<span class="nc" id="L97"> return this.targetExecutorService.isShutdown();</span>
}
public boolean isTerminated() {
<span class="nc" id="L101"> return this.targetExecutorService.isTerminated();</span>
}
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
<span class="nc" id="L105"> return this.targetExecutorService.awaitTermination(timeout, unit);</span>
}
protected &lt;T&gt; Callable&lt;T&gt; associateWithSubject(Callable&lt;T&gt; task) {
<span class="nc" id="L109"> Subject subject = getSubject();</span>
<span class="nc" id="L110"> return subject.associateWith(task);</span>
}
public &lt;T&gt; Future&lt;T&gt; submit(Callable&lt;T&gt; task) {
<span class="nc" id="L114"> Callable&lt;T&gt; work = associateWithSubject(task);</span>
<span class="nc" id="L115"> return this.targetExecutorService.submit(work);</span>
}
public &lt;T&gt; Future&lt;T&gt; submit(Runnable task, T result) {
<span class="nc" id="L119"> Runnable work = associateWithSubject(task);</span>
<span class="nc" id="L120"> return this.targetExecutorService.submit(work, result);</span>
}
public Future&lt;?&gt; submit(Runnable task) {
<span class="fc" id="L124"> Runnable work = associateWithSubject(task);</span>
<span class="fc" id="L125"> return this.targetExecutorService.submit(work);</span>
}
protected &lt;T&gt; Collection&lt;Callable&lt;T&gt;&gt; associateWithSubject(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks) {
<span class="nc" id="L129"> Collection&lt;Callable&lt;T&gt;&gt; workItems = new ArrayList&lt;Callable&lt;T&gt;&gt;(tasks.size());</span>
<span class="nc bnc" id="L130" title="All 2 branches missed."> for (Callable&lt;T&gt; task : tasks) {</span>
<span class="nc" id="L131"> Callable&lt;T&gt; work = associateWithSubject(task);</span>
<span class="nc" id="L132"> workItems.add(work);</span>
<span class="nc" id="L133"> }</span>
<span class="nc" id="L134"> return workItems;</span>
}
public &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks) throws InterruptedException {
<span class="nc" id="L138"> Collection&lt;Callable&lt;T&gt;&gt; workItems = associateWithSubject(tasks);</span>
<span class="nc" id="L139"> return this.targetExecutorService.invokeAll(workItems);</span>
}
public &lt;T&gt; List&lt;Future&lt;T&gt;&gt; invokeAll(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks, long timeout, TimeUnit unit)
throws InterruptedException {
<span class="nc" id="L144"> Collection&lt;Callable&lt;T&gt;&gt; workItems = associateWithSubject(tasks);</span>
<span class="nc" id="L145"> return this.targetExecutorService.invokeAll(workItems, timeout, unit);</span>
}
public &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks) throws InterruptedException, ExecutionException {
<span class="nc" id="L149"> Collection&lt;Callable&lt;T&gt;&gt; workItems = associateWithSubject(tasks);</span>
<span class="nc" id="L150"> return this.targetExecutorService.invokeAny(workItems);</span>
}
public &lt;T&gt; T invokeAny(Collection&lt;? extends Callable&lt;T&gt;&gt; tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
<span class="nc" id="L155"> Collection&lt;Callable&lt;T&gt;&gt; workItems = associateWithSubject(tasks);</span>
<span class="nc" id="L156"> return this.targetExecutorService.invokeAny(workItems, timeout, unit);</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>