/**
   * Gives the same basic functionality of File.exists but can be used to look for removable media
   * without showing a system dialog if the media is not present. Workaround pulled from the <A
   * HREF="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4089199"> bug report</A> on
   * bugs.sun.com. This bug was fixed in Java 6, and we can remove the workaround when we start
   * requiring Java 6.
   */
  protected static boolean fileExists(File file) {
    try {
      Process process =
          Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "dir", file.getAbsolutePath()});

      // We need to consume all available output or the process will block.
      boolean haveExitCode = false;
      int exitCode = -1;
      InputStream out = process.getInputStream();
      InputStream err = process.getErrorStream();

      while (!haveExitCode) {
        while (out.read() >= 0) {}
        while (err.read() >= 0) {}

        try {
          exitCode = process.exitValue();
          haveExitCode = true;
        } catch (IllegalThreadStateException e) {
          // Not yet complete.
          Thread.sleep(100);
        }
      }
      // int exitCode = process.waitFor();
      return exitCode == 0;

    } catch (IOException e) {
      System.out.println("Unable to check for file: " + file + " : " + e);
      return false;

    } catch (InterruptedException e) {
      System.out.println("Unable to check for file.  Interrupted: " + file + " : " + e);
      return false;
    }
  }
Example #2
0
 /** Redirect a VMs output and error streams to System.out and System.err */
 protected void redirectStreams(VirtualMachine vm) {
   MessageSiphon ms = new MessageSiphon(process.getErrorStream(), this);
   errThread = ms.getThread();
   outThread = new StreamRedirectThread("VM output reader", process.getInputStream(), System.out);
   errThread.start();
   outThread.start();
 }
Example #3
0
  /**
   * Generate the trace. Enable events, start thread to display events, start threads to forward
   * remote error and output streams, resume the remote VM, wait for the final event, and shutdown.
   */
  protected void generateTrace() {
    // vm.setDebugTraceMode(debugTraceMode);
    //    vm.setDebugTraceMode(VirtualMachine.TRACE_ALL);
    //    vm.setDebugTraceMode(VirtualMachine.TRACE_NONE);  // formerly, seems to have no effect

    // For internal debugging
    PrintWriter writer = null;

    // Calling this seems to set something internally to make the
    // Eclipse JDI wake up. Without it, an ObjectCollectedException
    // is thrown on excReq.enable(). No idea why this works,
    // but at least exception handling has returned. (Suspect that it may
    // block until all or at least some threads are available, meaning
    // that the app has launched and we have legit objects to talk to).
    vm.allThreads();
    // The bug may not have been noticed because the test suite waits for
    // a thread to be available, and queries it by calling allThreads().
    // See org.eclipse.debug.jdi.tests.AbstractJDITest for the example.

    EventRequestManager mgr = vm.eventRequestManager();
    // get only the uncaught exceptions
    ExceptionRequest excReq = mgr.createExceptionRequest(null, false, true);
    //    System.out.println(excReq);
    // this version reports all exceptions, caught or uncaught
    //  ExceptionRequest excReq = mgr.createExceptionRequest(null, true, true);
    // suspend so we can step
    excReq.setSuspendPolicy(EventRequest.SUSPEND_ALL);
    //    excReq.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
    //    excReq.setSuspendPolicy(EventRequest.SUSPEND_NONE);  // another option?
    excReq.enable();

    Thread eventThread =
        new Thread() {
          public void run() {
            try {
              boolean connected = true;
              while (connected) {
                EventQueue eventQueue = vm.eventQueue();
                // remove() blocks until event(s) available
                EventSet eventSet = eventQueue.remove();
                //            listener.vmEvent(eventSet);

                for (Event event : eventSet) {
                  //              System.out.println("EventThread.handleEvent -> " + event);
                  if (event instanceof VMStartEvent) {
                    vm.resume();
                  } else if (event instanceof ExceptionEvent) {
                    //                for (ThreadReference thread : vm.allThreads()) {
                    //                  System.out.println("thread : " + thread);
                    ////                  thread.suspend();
                    //                }
                    exceptionEvent((ExceptionEvent) event);
                  } else if (event instanceof VMDisconnectEvent) {
                    connected = false;
                  }
                }
              }
              //        } catch (VMDisconnectedException e) {
              //          Logger.getLogger(VMEventReader.class.getName()).log(Level.INFO,
              // "VMEventReader quit on VM disconnect");
            } catch (Exception e) {
              System.err.println("crashed in event thread due to " + e.getMessage());
              //          Logger.getLogger(VMEventReader.class.getName()).log(Level.SEVERE,
              // "VMEventReader quit", e);
              e.printStackTrace();
            }
          }
        };
    eventThread.start();

    errThread = new MessageSiphon(process.getErrorStream(), this).getThread();

    outThread = new StreamRedirectThread("JVM stdout Reader", process.getInputStream(), System.out);
    errThread.start();
    outThread.start();

    // Shutdown begins when event thread terminates
    try {
      if (eventThread != null) eventThread.join(); // is this the problem?

      //      System.out.println("in here");
      // Bug #852 tracked to this next line in the code.
      // http://dev.processing.org/bugs/show_bug.cgi?id=852
      errThread.join(); // Make sure output is forwarded
      //      System.out.println("and then");
      outThread.join(); // before we exit
      //      System.out.println("finished join for errThread and outThread");

      // At this point, disable the run button.
      // This happens when the sketch is exited by hitting ESC,
      // or the user manually closes the sketch window.
      // TODO this should be handled better, should it not?
      if (editor != null) {
        editor.deactivateRun();
      }
    } catch (InterruptedException exc) {
      // we don't interrupt
    }
    // System.out.println("and leaving");
    if (writer != null) writer.close();
  }
Example #4
0
  /**
   * Either succeeds or throws a RunnerException fit for public consumption.
   
   * [ROBOTIS]Changed prototype to support ARM Cortex-M3 based CM-900 Pandora project
   * 2012-09-26 [email protected]
   * */ 
  protected void execAsynchronously(List<String> commandList)
    throws RunnerException {
    String[] command = new String[commandList.size()];
    commandList.toArray(command);
    
    //System.out.println("[ROBOTIS]execAsynchronously Start");
   /* for(String tmp:command)
    	System.out.print(tmp+"   ");
    System.out.println("");*/
    
    int result = 0;
    
    if (verbose || Preferences.getBoolean("build.verbose")) {
      for(int j = 0; j < command.length; j++) {
        System.out.print(command[j] + " ");
      }
      System.out.println();
    }
    
    firstErrorFound = false;  // haven't found any errors yet
    secondErrorFound = false;

    Process process;
    
    try {
      process = Runtime.getRuntime().exec(command); //excute process to run commnad
    } catch (IOException e) {
      RunnerException re = new RunnerException(e.getMessage());
      re.hideStackTrace();
      throw re;
    }
    
   // new InputStreamFromConsole( process.getInputStream()).start(); //[ROBOTIS] add to display console reply.
    
 //[ROBOTIS][CHANGE] dont use below codes
    MessageSiphon in = new MessageSiphon(process.getInputStream(), this);
    MessageSiphon err = new MessageSiphon(process.getErrorStream(), this);
    
    // wait for the process to finish.  if interrupted
    // before waitFor returns, continue waiting
    boolean compiling = true;
    
    while (compiling) {
      try {
      /* in.join();
        err.join();
        result = process.waitFor();
       System.out.println("result is " + result);
        compiling = false;*/
		  if (in.thread != null)
		      in.thread.join();
		  if (err.thread != null)
		      err.thread.join();
		  result = process.waitFor();
		  
		 // System.out.println("result is " + result);
		  compiling = false;
      } catch (InterruptedException ignored) { }
    }

    // an error was queued up by message(), barf this back to compile(),
    // which will barf it back to Editor. if you're having trouble
    // discerning the imagery, consider how cows regurgitate their food
    // to digest it, and the fact that they have five stomaches.
    //
    //System.out.println("throwing up " + exception);
    if (exception != null) { throw exception; }

    if (result > 1) {
      // a failure in the tool (e.g. unable to locate a sub-executable)
      System.err.println(
	  I18n.format(_("{0} returned {1}"), command[0], result));
    }

    if (result != 0) {
      RunnerException re = new RunnerException(_("Error compiling."));
      re.hideStackTrace();
      throw re;
    }
  }