|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ThreadTerminatingSecurityManager.java | 66.7% | 68.4% | 75% | 68.6% |
|
||||||||||||||
| 1 |
package abbot.util;
|
|
| 2 |
|
|
| 3 |
import java.security.Permission;
|
|
| 4 |
import java.util.*;
|
|
| 5 |
|
|
| 6 |
import abbot.NoExitSecurityManager;
|
|
| 7 |
|
|
| 8 |
/** Provides a method for terminating threads over which you otherwise have no
|
|
| 9 |
* control. Usually works.<p>
|
|
| 10 |
* NOTE: Still needs some work; if main script editor exits from the event
|
|
| 11 |
* dispatch thread, an exception is thrown and the exit aborted. Perhaps
|
|
| 12 |
* ignore event dispatch threads?
|
|
| 13 |
*/
|
|
| 14 |
|
|
| 15 |
public abstract class ThreadTerminatingSecurityManager |
|
| 16 |
extends NoExitSecurityManager {
|
|
| 17 |
|
|
| 18 |
public class ThreadTerminatedException extends SecurityException { |
|
| 19 |
} |
|
| 20 |
|
|
| 21 |
private Map terminatedGroups = new WeakHashMap(); |
|
| 22 |
|
|
| 23 | 14 |
private boolean isTerminated(Thread t) { |
| 24 | 14 |
Iterator iter = terminatedGroups.keySet().iterator(); |
| 25 | 14 |
while (iter.hasNext()) {
|
| 26 | 11 |
ThreadGroup group = (ThreadGroup)iter.next(); |
| 27 | 11 |
ThreadGroup thisGroup = t.getThreadGroup(); |
| 28 | 11 |
if (thisGroup != null |
| 29 |
&& group.parentOf(thisGroup)) {
|
|
| 30 | 1 |
return true; |
| 31 |
} |
|
| 32 |
} |
|
| 33 | 13 |
return false; |
| 34 |
} |
|
| 35 |
|
|
| 36 |
/** Ensure ThreadTermination exceptions are thrown for any thread in the
|
|
| 37 |
* given group when any such thread causes the security manager to be
|
|
| 38 |
* invoked.
|
|
| 39 |
*/
|
|
| 40 | 1 |
public void terminateThreads(ThreadGroup group) { |
| 41 | 1 |
if (group == null) |
| 42 | 0 |
throw new NullPointerException("Thread group must not be null"); |
| 43 | 1 |
terminatedGroups.put(group, Boolean.TRUE); |
| 44 |
// maybe do an interrupt/notify; but be careful b/c you might block
|
|
| 45 |
// trying to sycnhronize on the thread
|
|
| 46 | 1 |
if (group.activeCount() == 0) {
|
| 47 | 0 |
try {
|
| 48 | 0 |
group.destroy(); |
| 49 |
} |
|
| 50 |
catch(IllegalThreadStateException e) {
|
|
| 51 |
} |
|
| 52 |
} |
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
/** Throw ThreadTerminated for any thread marked for termination. */
|
|
| 56 | 0 |
public void checkPermission(Permission perm, Object context) { |
| 57 | 0 |
if (isTerminated(Thread.currentThread()))
|
| 58 | 0 |
throw new ThreadTerminatedException(); |
| 59 | 0 |
super.checkPermission(perm, context);
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/** Throw ThreadTerminated for any thread marked for termination. */
|
|
| 63 | 14 |
public void checkPermission(Permission perm) { |
| 64 | 14 |
if (isTerminated(Thread.currentThread()))
|
| 65 | 1 |
throw new ThreadTerminatedException(); |
| 66 | 13 |
super.checkPermission(perm);
|
| 67 |
} |
|
| 68 |
} |
|
| 69 |
|
|
||||||||||