1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 module hunt.shiro.subject.support.SubjectCallable; 20 21 import hunt.shiro.subject.support.SubjectThreadState; 22 import hunt.shiro.subject.Subject; 23 import hunt.shiro.util.ThreadState; 24 25 import hunt.Exceptions; 26 import hunt.util.Common; 27 28 /** 29 * A {@code SubjectCallable} associates a {@link Subject Subject} with a target/delegate 30 * {@link Callable Callable} to ensure proper {@code Subject} thread-state management when the {@code Callable} executes. 31 * This ensures that any calls to {@code SecurityUtils.}{@link hunt.shiro.SecurityUtils#getSubject() getSubject()} 32 * during the target {@code Callable}'s execution still work correctly even if the {@code Callable} executes on a 33 * different thread than the one that created it. This allows {@code Subject} access during asynchronous operations. 34 * <p/> 35 * When instances of this class execute (typically via a {@link java.util.concurrent.ExecutorService ExecutorService}), 36 * the following occurs: 37 * <ol> 38 * <li>The specified Subject any of its associated thread state is first bound to the thread that executes the 39 * {@code Callable}.</li> 40 * <li>The delegate/target {@code Callable} is {@link java.util.concurrent.Callable#call() executed}</li> 41 * <li>The previous thread state that might have existed before the {@code Subject} was bound is fully restored</li> 42 * </ol> 43 * <p/> 44 * This behavior ensures that the thread that executes this {@code Callable}, which is often a different thread than 45 * the one that created the instance, retains a {@code Subject} to support {@code SecurityUtils.getSubject()} 46 * invocations. It also guarantees that the running thread remains 'clean' in any thread-pooled environments. 47 * 48 * <h3>Usage</h3> 49 * 50 * This is typically considered a support class and is not often directly referenced. Most people prefer to use 51 * the {@code Subject.}{@link Subject#associateWith(Callable) associateWith} method, which will automatically return 52 * an instance of this class. 53 * <p/> 54 * An even more convenient alternative is to use a 55 * {@link hunt.shiro.concurrent.SubjectAwareExecutorService SubjectAwareExecutorService}, which 56 * transparently uses instances of this class. 57 * 58 * @see Subject#associateWith(Callable) 59 * @see hunt.shiro.concurrent.SubjectAwareExecutorService SubjectAwareExecutorService 60 */ 61 class SubjectCallable(V) : Callable!(V) { 62 63 protected ThreadState threadState; 64 private Callable!(V) callable; 65 66 this(Subject subject, Callable!(V) dg) { 67 this(new SubjectThreadState(subject), dg); 68 } 69 70 protected this(ThreadState threadState, Callable!(V) dg) { 71 if (threadState is null) { 72 throw new IllegalArgumentException("ThreadState argument cannot be null."); 73 } 74 this.threadState = threadState; 75 if (dg is null) { 76 throw new IllegalArgumentException("Callable delegate instance cannot be null."); 77 } 78 this.callable = dg; 79 } 80 81 V call(){ 82 try { 83 threadState.bind(); 84 return doCall(this.callable); 85 } finally { 86 threadState.restore(); 87 } 88 } 89 90 protected V doCall(Callable!(V) target){ 91 return target.call(); 92 } 93 }