@Override
 public Object apply(List<Object> args, Context context) throws ParseException {
   File outFile = null;
   String editor = getEditor();
   try {
     outFile = File.createTempFile("stellar_shell", "out");
     if (args.size() > 0) {
       String arg = (String) args.get(0);
       try (PrintWriter pw = new PrintWriter(outFile)) {
         IOUtils.write(arg, pw);
       }
     }
   } catch (IOException e) {
     String message = "Unable to create temp file: " + e.getMessage();
     LOG.error(message, e);
     throw new IllegalStateException(message, e);
   }
   Optional<Object> console = context.getCapability(CONSOLE, false);
   try {
     PausableInput.INSTANCE.pause();
     // shut down the IO for the console
     ProcessBuilder processBuilder = new ProcessBuilder(editor, outFile.getAbsolutePath());
     processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
     processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
     processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
     try {
       Process p = processBuilder.start();
       // wait for termination.
       p.waitFor();
       try (BufferedReader br = new BufferedReader(new FileReader(outFile))) {
         String ret = IOUtils.toString(br).trim();
         return ret;
       }
     } catch (Exception e) {
       String message = "Unable to read output: " + e.getMessage();
       LOG.error(message, e);
       return null;
     }
   } finally {
     try {
       PausableInput.INSTANCE.unpause();
       if (console.isPresent()) {
         ((Console) console.get()).pushToInputStream("\b\n");
       }
     } catch (IOException e) {
       LOG.error("Unable to unpause: " + e.getMessage(), e);
     }
     if (outFile.exists()) {
       outFile.delete();
     }
   }
 }
Beispiel #2
0
  public int compileJava() {
    try {
      //    create new bin directory
      boolean createBin = new File(classPath).mkdir();

      //    create new javac ProcessBuilder
      //      ProcessBuilder pb =
      //      new ProcessBuilder("javac", "-d", classPath, "./" + studentPath + "/*.java");

      ProcessBuilder pbDir = new ProcessBuilder("dir");
      //    Determine current working directory
      File srcAbsPath = new File(sourcePath);
      srcAbsPathName = srcAbsPath.getAbsolutePath();
      System.out.println("Compiler.java line 69 source path: " + sourcePath);
      System.out.println("Compiler.java line 69 source absolute path: " + srcAbsPathName);

      File cwd = pbDir.directory();
      //    debug code - to confirm correct directory
      //    TestTools.dir(cwd);
      //    NB - ProcessBuilder default is to return a null
      //    pointer for the abstract path to indicate that it
      //    is using System.Properties "user.dir", i.e., the
      //    current system working directory; hence the
      //    critical need to handle a NullPointerException.
      //    Also returns a null pointer if the directory
      //    doesn't exist.

      // all this is doing is changing the dir, can we approach this in a different way using a
      // value in our Data Object? -mh
      File nwd = TestTools.cd(cwd, studentPath);

      System.out.println("(Compiler.java line 88)new working directory: " + nwd.toString());
      String studentPathName = nwd.getAbsolutePath();
      File nwdPath = new File(studentPath);
      System.out.println("(Compiler.java line 91)new working directory path: " + studentPathName);
      //    debug code to test new working directory
      //    TestTools.dir(nwd);

      FileFilter filter = new FileFilter() {};

      String[] javaFileList = nwdPath.list(filter);
      //    set up output file
      File outputFile = new File(outputFileName);
      //    System.out.println(outputFileName);
      outputFile.delete();

      for (int k = 0; k < javaFileList.length; k++) {
        try {
          if (filter.accept(nwdPath, javaFileList[k]) == true) {
            System.out.println("COMPILER.JAVA (line 111)  Compiling: " + javaFileList[k]);

            String compilePath =
                "javac" + "-d" + classPath + ".\\" + studentPath + "\\" + javaFileList[k];
            System.out.println("Compiler.java 117 compile path: " + compilePath);
            ProcessBuilder pb =
                // new ProcessBuilder("javac ", "-d", classPath, ".\\" + studentPath + "\\" +
                // javaFileList[k]);
                new ProcessBuilder(compilePath);

            // System.out.println(pb.environment().toString());  <-- THIS IS VERY INTERESTING

            //          Create environment map and set environmental variables
            Map<String, String> env = pb.environment();
            env.clear();
            env.put("PATH", path);
            env.put("CLASSPATH", classPath);
            //          env.put("SOURCEPATH", sourcePath);
            //          env.remove("OTHERVAR");

            pb.redirectErrorStream(true);
            pb.redirectOutput(Redirect.appendTo(outputFile));

            //          start javac process
            Process p = pb.start();

            //          need other processes to wait for compilation to finish
            //          basically joins the thread to the javac process to force sequential
            //          execution - need to be careful - if any process hangs, whole run hangs
            success =
                p
                    .waitFor(); // Returns the exit value of the process. By convention, 0 indicates
                                // normal termination.
                                // http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29

            assert pb.redirectInput() == Redirect.PIPE;
            assert pb.redirectOutput().file() == outputFile;
            assert p.getInputStream().read() == -1;
            System.out.println("COMPILER.JAVA (line 138)  end of loop, success = " + success);
          }
        } catch (Exception e) {
          System.out.println(" Compiler.java FOR LOOP Compile Exception: " + javaFileList[k]);
        }
      }
    } catch (Exception e) {
      System.out.println("Compile Exception, PROBABLY DUE TO FILE PATH");
      System.out.println("source absolute path: " + srcAbsPathName);
    }

    return success;
  }