aboutsummaryrefslogtreecommitdiff
path: root/nailgun_launcher/TrapSecurityManager.java
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2017-03-20 22:09:38 -0400
committerChristopher Vogt <oss.nsp@cvogt.org>2017-03-27 19:56:13 -0400
commitbba2abe7ee38b8903822a07578c46466923d13ed (patch)
treea357fb8def6f58a9ea9a37411f3f5640dcb525fe /nailgun_launcher/TrapSecurityManager.java
parentd2f8cade709b7d55a93e18592b6e38247d648ca9 (diff)
downloadcbt-bba2abe7ee38b8903822a07578c46466923d13ed.tar.gz
cbt-bba2abe7ee38b8903822a07578c46466923d13ed.tar.bz2
cbt-bba2abe7ee38b8903822a07578c46466923d13ed.zip
start modularizing cbt into libraries
this extracts certain parts of cbt into stand-alone libraries, which can be published to maven and used outside of cbt. This also adds scalariform for these parts of the code. This slows down cbt’s own build a lot because of the number of projects involved! So we’ll follow this by a bunch of performance tweak commits.
Diffstat (limited to 'nailgun_launcher/TrapSecurityManager.java')
-rw-r--r--nailgun_launcher/TrapSecurityManager.java83
1 files changed, 0 insertions, 83 deletions
diff --git a/nailgun_launcher/TrapSecurityManager.java b/nailgun_launcher/TrapSecurityManager.java
deleted file mode 100644
index be59671..0000000
--- a/nailgun_launcher/TrapSecurityManager.java
+++ /dev/null
@@ -1,83 +0,0 @@
-package cbt;
-import java.security.*;
-/*
-When enabled, this SecurityManager turns System.exit(...) calls into exceptions that can be caught and handled.
-Installing a SecurityManager is a global side-effect and thus needs extra care in a persistent
-background process like CBT's. The current approach is install it once during JVM-startup.
-When disabled this delegates to the SecurityManager installed before if any, which
-would be Nailgun's if running on Nailgun. If we do not delegate to Nailgun, it seems we
-could in some cases kill the server process
-*/
-public class TrapSecurityManager extends ProxySecurityManager{
- public static ThreadLocal<Boolean> trapExitCode(){
- // storing the flag in the installed security manager
- // instead of e.g. a static member is necessary because
- // we run multiple versions of CBT with multiple TrapSecurityManager classes
- // but we need to affect the installed one
- SecurityManager sm = System.getSecurityManager();
- if(sm instanceof TrapSecurityManager){
- return ((TrapSecurityManager) sm)._trapExitCode;
- } else {
- try{
- @SuppressWarnings("unchecked")
- ThreadLocal<Boolean> res =
- (ThreadLocal<Boolean>) sm.getClass().getMethod("trapExitCode").invoke(null);
- return res;
- } catch(Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- private final ThreadLocal<Boolean> _trapExitCode =
- new ThreadLocal<Boolean>() {
- @Override protected Boolean initialValue() {
- return false;
- }
- };
-
- public TrapSecurityManager(){
- super(NailgunLauncher.initialSecurityManager);
- }
-
- public void checkPermission( Permission permission ){
- /*
- NOTE: is it actually ok, to just make these empty?
- Calling .super leads to ClassNotFound exteption for a lambda.
- Calling to the previous SecurityManager leads to a stack overflow
- */
- if(!TrapSecurityManager.trapExitCode().get()){
- super.checkPermission(permission);
- }
- }
- public void checkPermission( Permission permission, Object context ){
- /* Does this methods need to be overidden? */
- if(!TrapSecurityManager.trapExitCode().get()){
- super.checkPermission(permission, context);
- }
- }
-
- private static final String prefix = "[TrappedExit] ";
-
- @Override
- public void checkExit( int status ){
- if(TrapSecurityManager.trapExitCode().get()){
- // using a RuntimeException and a prefix here instead of a custom
- // exception type because this is thrown by the installed TrapSecurityManager
- // but other versions of cbt need to be able to catch it, that do not have access
- // to that version of the TrapSecurityManager class
- throw new RuntimeException(prefix+status);
- }
- super.checkExit(status);
- }
-
- public static boolean isTrappedExit( Throwable t ){
- return t instanceof RuntimeException && t.getMessage() != null && t.getMessage().startsWith(prefix);
- }
-
- public static int exitCode( Throwable t ){
- assert(isTrappedExit(t));
- return Integer.parseInt( t.getMessage().substring(prefix.length()) );
- }
-
-}