aboutsummaryrefslogtreecommitdiff
path: root/nailgun_launcher
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
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')
-rw-r--r--nailgun_launcher/NailgunLauncher.java29
-rw-r--r--nailgun_launcher/ProxySecurityManager.java102
-rw-r--r--nailgun_launcher/Stage0Lib.java33
-rw-r--r--nailgun_launcher/TrapSecurityManager.java83
4 files changed, 38 insertions, 209 deletions
diff --git a/nailgun_launcher/NailgunLauncher.java b/nailgun_launcher/NailgunLauncher.java
index fe9f27f..1c6f3b5 100644
--- a/nailgun_launcher/NailgunLauncher.java
+++ b/nailgun_launcher/NailgunLauncher.java
@@ -5,6 +5,7 @@ import java.net.*;
import java.security.*;
import java.util.*;
import static cbt.Stage0Lib.*;
+import cbt.reflect.TrapSystemExit;
import static java.io.File.pathSeparator;
import java.nio.file.*;
import static java.nio.file.Files.write;
@@ -66,7 +67,7 @@ public class NailgunLauncher{
return;
}
- System.setSecurityManager( new TrapSecurityManager() );
+ System.setSecurityManager( TrapSystemExit.createSecurityManager(initialSecurityManager) );
installProxySettings();
String[] diff = args[0].split("\\.");
long start = _start - (Long.parseLong(diff[0]) * 1000L) - Long.parseLong(diff[1]);
@@ -146,7 +147,7 @@ public class NailgunLauncher{
String nailgunTarget = nailgunSources + TARGET;
String stage1Sources = cbtHome + "/" + STAGE1;
String stage1Target = stage1Sources + TARGET;
- File compatibilitySources = new File(cbtHome + "/compatibility");
+ String compatibilitySources = cbtHome + "/compatibility";
String mavenCache = cache + "maven";
String mavenUrl = "https://repo1.maven.org/maven2";
File loopFile = new File(cwd + "/target/.cbt-loop.tmp");
@@ -171,9 +172,14 @@ public class NailgunLauncher{
compatibilityLastModified = new File( compatibilityTarget + "../classes.last-success" ).lastModified();
} else {
compatibilitySourceFiles = new ArrayList<File>();
- for( File f: compatibilitySources.listFiles() ){
- if( f.isFile() && f.toString().endsWith(".java") ){
- compatibilitySourceFiles.add(f);
+ for( String d: new String[]{
+ compatibilitySources,
+ cbtHome + "/libraries/interfaces"
+ } ){
+ for( File f: new File(d).listFiles() ){
+ if( f.isFile() && f.toString().endsWith(".java") ){
+ compatibilitySourceFiles.add(f);
+ }
}
}
@@ -206,9 +212,16 @@ public class NailgunLauncher{
String stage1Classpath = classpath( stage1ClasspathArray );
stage1SourceFiles = new ArrayList<File>();
- for( File f: new File(stage1Sources).listFiles() ){
- if( f.isFile() && f.toString().endsWith(".scala") ){
- stage1SourceFiles.add(f);
+ for( String d: new String[]{
+ stage1Sources,
+ cbtHome + "/libraries/reflect",
+ cbtHome + "/libraries/common-1",
+ cbtHome + "/libraries/file"
+ } ){
+ for( File f: new File(d).listFiles() ){
+ if( f.isFile() && (f.toString().endsWith(".scala") || f.toString().endsWith(".java")) ){
+ stage1SourceFiles.add(f);
+ }
}
}
diff --git a/nailgun_launcher/ProxySecurityManager.java b/nailgun_launcher/ProxySecurityManager.java
deleted file mode 100644
index 1a6e49c..0000000
--- a/nailgun_launcher/ProxySecurityManager.java
+++ /dev/null
@@ -1,102 +0,0 @@
-package cbt;
-
-import java.security.*;
-import java.io.FileDescriptor;
-import java.net.InetAddress;
-
-/*
-SecurityManager proxy that forwards all calls to the provided target if != null.
-Useful to replace a previously installed SecurityManager, overriding some methods
-but forwarding the rest.
-*/
-public class ProxySecurityManager extends SecurityManager{
- private SecurityManager target;
- public ProxySecurityManager(SecurityManager target){
- this.target = target;
- }
- public Object getSecurityContext() {
- if(target != null)
- return target.getSecurityContext();
- else return super.getSecurityContext();
- }
- public void checkPermission(Permission perm) {
- if(target != null) target.checkPermission(perm);
- }
- public void checkPermission(Permission perm, Object context) {
- if(target != null) target.checkPermission(perm, context);
- }
- public void checkCreateClassLoader() {
- if(target != null) target.checkCreateClassLoader();
- }
- public void checkAccess(Thread t) {
- if(target != null) target.checkAccess(t);
- }
- public void checkAccess(ThreadGroup g) {
- if(target != null) target.checkAccess(g);
- }
- public void checkExit(int status) {
- if(target != null) target.checkExit(status);
- }
- public void checkExec(String cmd) {
- if(target != null) target.checkExec(cmd);
- }
- public void checkLink(String lib) {
- if(target != null) target.checkLink(lib);
- }
- public void checkRead(FileDescriptor fd) {
- if(target != null) target.checkRead(fd);
- }
- public void checkRead(String file) {
- if(target != null) target.checkRead(file);
- }
- public void checkRead(String file, Object context) {
- if(target != null) target.checkRead(file, context);
- }
- public void checkWrite(FileDescriptor fd) {
- if(target != null) target.checkWrite(fd);
- }
- public void checkWrite(String file) {
- if(target != null) target.checkWrite(file);
- }
- public void checkDelete(String file) {
- if(target != null) target.checkDelete(file);
- }
- public void checkConnect(String host, int port) {
- if(target != null) target.checkConnect(host, port);
- }
- public void checkConnect(String host, int port, Object context) {
- if(target != null) target.checkConnect(host, port, context);
- }
- public void checkListen(int port) {
- if(target != null) target.checkListen(port);
- }
- public void checkAccept(String host, int port) {
- if(target != null) target.checkAccept(host, port);
- }
- public void checkMulticast(InetAddress maddr) {
- if(target != null) target.checkMulticast(maddr);
- }
- public void checkPropertiesAccess() {
- if(target != null) target.checkPropertiesAccess();
- }
- public void checkPropertyAccess(String key) {
- if(target != null) target.checkPropertyAccess(key);
- }
- public void checkPrintJobAccess() {
- if(target != null) target.checkPrintJobAccess();
- }
- public void checkPackageAccess(String pkg) {
- if(target != null) target.checkPackageAccess(pkg);
- }
- public void checkPackageDefinition(String pkg) {
- if(target != null) target.checkPackageDefinition(pkg);
- }
- public void checkSetFactory() {
- if(target != null) target.checkSetFactory();
- }
- public ThreadGroup getThreadGroup() {
- if(target != null)
- return target.getThreadGroup();
- else return super.getThreadGroup();
- }
-}
diff --git a/nailgun_launcher/Stage0Lib.java b/nailgun_launcher/Stage0Lib.java
index 8ab6150..34af7b0 100644
--- a/nailgun_launcher/Stage0Lib.java
+++ b/nailgun_launcher/Stage0Lib.java
@@ -12,6 +12,7 @@ import static cbt.NailgunLauncher.*;
import java.nio.file.*;
import java.nio.file.attribute.FileTime;
import static java.lang.Math.min;
+import cbt.reflect.TrapSystemExit;
public class Stage0Lib{
public static void _assert(boolean condition, Object msg){
@@ -21,22 +22,21 @@ public class Stage0Lib{
}
public static int runMain(String cls, String[] args, ClassLoader cl) throws Throwable{
- boolean trapExitCodeBefore = TrapSecurityManager.trapExitCode().get();
- try{
- TrapSecurityManager.trapExitCode().set(true);
- cl.loadClass(cls)
- .getMethod("main", String[].class)
- .invoke( null, (Object) args);
- return 0;
- }catch( InvocationTargetException exception ){
- Throwable cause = exception.getCause();
- if(TrapSecurityManager.isTrappedExit(cause)){
- return TrapSecurityManager.exitCode(cause);
+ return TrapSystemExit.run(
+ new TrapSystemExit<Integer>(){
+ @Override
+ public Integer run() throws Throwable{
+ cl.loadClass(cls)
+ .getMethod("main", String[].class)
+ .invoke( null, (Object) args);
+ return 0;
+ }
+ @Override
+ public Integer wrap(int exitCode){
+ return exitCode;
+ }
}
- throw exception;
- } finally {
- TrapSecurityManager.trapExitCode().set(trapExitCodeBefore);
- }
+ );
}
public static Object get(Object object, String method) throws Throwable{
@@ -111,7 +111,8 @@ public class Stage0Lib{
"-d", target,
"-S-deprecation",
"-S-feature",
- "-S-unchecked"
+ "-S-unchecked",
+ "-S-language:existentials"
}
)
);
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()) );
- }
-
-}