Skip to content

Commit

Permalink
Refactor external process tools for JavapTask -> ext process migratio…
Browse files Browse the repository at this point in the history
…n (JDK9)
  • Loading branch information
chriswhocodes committed May 9, 2016
1 parent d4af158 commit bb6cc56
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 49 deletions.
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
package org.adoptopenjdk.jitwatch.sandbox;
package org.adoptopenjdk.jitwatch.process;

import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.C_SPACE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_EMPTY;
Expand All @@ -16,16 +16,23 @@
import java.util.List;
import java.util.Map;

import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.runtime.RuntimeJava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractProcess
public abstract class AbstractProcess implements IExternalProcess
{
protected static final Logger logger = LoggerFactory.getLogger(RuntimeJava.class);

public static final Path PATH_STD_ERR = new File(Sandbox.SANDBOX_DIR.toFile(), "sandbox.err").toPath();
public static final Path PATH_STD_OUT = new File(Sandbox.SANDBOX_DIR.toFile(), "sandbox.out").toPath();
private Path stdErr;
private Path stdOut;

public AbstractProcess(Path stdErr, Path stdOut)
{
this.stdErr = stdErr;
this.stdOut = stdOut;
}

public String getExecutableSuffix()
{
Expand All @@ -42,15 +49,16 @@ public boolean isWindows()
return System.getProperty("os.name", S_EMPTY).contains("Windows");
}

public static String getOutputStream()
@Override
public String getOutputStream()
{
String result = null;

if (PATH_STD_OUT.toFile().exists())
if (stdOut.toFile().exists())
{
try
{
result = new String(Files.readAllBytes(PATH_STD_OUT), StandardCharsets.UTF_8);
result = new String(Files.readAllBytes(stdOut), StandardCharsets.UTF_8);
}
catch (IOException ioe)
{
Expand All @@ -61,15 +69,16 @@ public static String getOutputStream()
return result;
}

public static String getErrorStream()
@Override
public String getErrorStream()
{
String result = null;

if (PATH_STD_ERR.toFile().exists())
if (stdErr.toFile().exists())
{
try
{
result = new String(Files.readAllBytes(PATH_STD_ERR), StandardCharsets.UTF_8);
result = new String(Files.readAllBytes(stdErr), StandardCharsets.UTF_8);
}
catch (IOException ioe)
{
Expand Down Expand Up @@ -135,8 +144,8 @@ protected boolean runCommands(List<String> commands, File workingDirectory, Map<
pb.directory(workingDirectory);
}

pb.redirectError(PATH_STD_ERR.toFile());
pb.redirectOutput(PATH_STD_OUT.toFile());
pb.redirectError(stdErr.toFile());
pb.redirectOutput(stdOut.toFile());

Process proc = pb.start();

Expand Down
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
package org.adoptopenjdk.jitwatch.process;

public interface IExternalProcess
{
String getOutputStream();
String getErrorStream();
}
24 changes: 21 additions & 3 deletions src/main/java/org/adoptopenjdk/jitwatch/sandbox/Sandbox.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -26,6 +26,7 @@
import org.adoptopenjdk.jitwatch.model.IMetaMember;
import org.adoptopenjdk.jitwatch.model.IReadOnlyJITDataModel;
import org.adoptopenjdk.jitwatch.model.MetaClass;
import org.adoptopenjdk.jitwatch.process.IExternalProcess;
import org.adoptopenjdk.jitwatch.sandbox.compiler.ICompiler;
import org.adoptopenjdk.jitwatch.sandbox.runtime.IRuntime;
import org.adoptopenjdk.jitwatch.ui.sandbox.ISandboxStage;
Expand All @@ -44,6 +45,9 @@ public class Sandbox
public static final Path SANDBOX_DIR;
public static final Path SANDBOX_SOURCE_DIR;
public static final Path SANDBOX_CLASS_DIR;

public static final Path PATH_STD_ERR;
public static final Path PATH_STD_OUT;

private static final String SANDBOX_LOGFILE = "sandbox.log";

Expand All @@ -52,6 +56,8 @@ public class Sandbox
private ILogParser logParser;

private LanguageManager languageManager;

private IExternalProcess lastProcess;

static
{
Expand All @@ -60,6 +66,9 @@ public class Sandbox
SANDBOX_DIR = Paths.get(userDir, "sandbox");
SANDBOX_SOURCE_DIR = Paths.get(userDir, "sandbox", "sources");
SANDBOX_CLASS_DIR = Paths.get(userDir, "sandbox", "classes");

PATH_STD_ERR = new File(Sandbox.SANDBOX_DIR.toFile(), "sandbox.err").toPath();
PATH_STD_OUT = new File(Sandbox.SANDBOX_DIR.toFile(), "sandbox.out").toPath();

initialise();
}
Expand Down Expand Up @@ -141,6 +150,8 @@ public void runSandbox(String language, List<File> compileList, File fileToRun)

logListener.log("Compiling: " + StringUtil.listToString(compileList));

lastProcess = compiler;

boolean compiledOK = compiler.compile(compileList, buildUniqueClasspath(logParser.getConfig()), SANDBOX_CLASS_DIR.toFile(),
logListener);

Expand All @@ -150,6 +161,8 @@ public void runSandbox(String language, List<File> compileList, File fileToRun)
{
String fqClassNameToRun = runtime.getClassToExecute(fileToRun);

lastProcess = runtime;

boolean executionSuccess = executeClass(fqClassNameToRun, runtime, logParser.getConfig().isSandboxIntelMode());

logListener.log("Execution success: " + executionSuccess);
Expand All @@ -167,14 +180,19 @@ public void runSandbox(String language, List<File> compileList, File fileToRun)
}
else
{
sandboxStage.showError(AbstractProcess.getErrorStream());
sandboxStage.showError(runtime.getErrorStream());
}
}
else
{
sandboxStage.showError(AbstractProcess.getErrorStream());
sandboxStage.showError(compiler.getErrorStream());
}
}

public IExternalProcess getLastProcess()
{
return lastProcess;
}

private List<String> buildUniqueClasspath(JITWatchConfig config)
{
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -14,8 +14,9 @@
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerGroovy extends AbstractProcess implements ICompiler
{
Expand All @@ -25,6 +26,8 @@ public class CompilerGroovy extends AbstractProcess implements ICompiler

public CompilerGroovy(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -17,8 +17,9 @@
import java.util.List;
import java.util.Set;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerJRuby extends AbstractProcess implements ICompiler
{
Expand All @@ -31,6 +32,8 @@ public class CompilerJRuby extends AbstractProcess implements ICompiler

public CompilerJRuby(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -14,8 +14,9 @@
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerJava extends AbstractProcess implements ICompiler
{
Expand All @@ -25,6 +26,8 @@ public class CompilerJava extends AbstractProcess implements ICompiler

public CompilerJava(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "..", "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -14,8 +14,9 @@
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerJavaScript extends AbstractProcess implements ICompiler
{
Expand All @@ -25,6 +26,8 @@ public class CompilerJavaScript extends AbstractProcess implements ICompiler

public CompilerJavaScript(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -14,8 +14,9 @@
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerKotlin extends AbstractProcess implements ICompiler
{
Expand All @@ -26,6 +27,8 @@ public class CompilerKotlin extends AbstractProcess implements ICompiler

public CompilerKotlin(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -14,8 +14,9 @@
import java.util.Arrays;
import java.util.List;

import org.adoptopenjdk.jitwatch.sandbox.AbstractProcess;
import org.adoptopenjdk.jitwatch.process.AbstractProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;
import org.adoptopenjdk.jitwatch.sandbox.Sandbox;

public class CompilerScala extends AbstractProcess implements ICompiler
{
Expand All @@ -25,6 +26,8 @@ public class CompilerScala extends AbstractProcess implements ICompiler

public CompilerScala(String languageHomeDir) throws FileNotFoundException
{
super(Sandbox.PATH_STD_ERR, Sandbox.PATH_STD_OUT);

compilerPath = Paths.get(languageHomeDir, "bin", COMPILER_NAME);

if (!compilerPath.toFile().exists())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -9,9 +9,10 @@
import java.io.IOException;
import java.util.List;

import org.adoptopenjdk.jitwatch.process.IExternalProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;

public interface ICompiler
public interface ICompiler extends IExternalProcess
{
public boolean compile(List<File> sourceFiles, List<String> classpathEntries, File outputDir, ISandboxLogListener logListener) throws IOException;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015 Chris Newland.
* Copyright (c) 2013-2016 Chris Newland.
* Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
* Instructions: https://github.com/AdoptOpenJDK/jitwatch/wiki
*/
Expand All @@ -8,9 +8,10 @@
import java.io.File;
import java.util.List;

import org.adoptopenjdk.jitwatch.process.IExternalProcess;
import org.adoptopenjdk.jitwatch.sandbox.ISandboxLogListener;

public interface IRuntime
public interface IRuntime extends IExternalProcess
{
public boolean execute(String className, List<String> classpathEntries, List<String> vmOptions, ISandboxLogListener logListener);

Expand Down

0 comments on commit bb6cc56

Please sign in to comment.