예제 #1
0
파일: Main.java 프로젝트: rebx/jruby
  private Status internalRun() {
    doShowVersion();
    doShowCopyright();

    if (!config.shouldRunInterpreter()) {
      doPrintUsage(false);
      doPrintProperties();
      return new Status();
    }

    InputStream in = config.getScriptSource();
    String filename = config.displayedFileName();

    doProcessArguments(in);

    Ruby runtime = Ruby.newInstance(config);

    try {
      doSetContextClassLoader(runtime);

      if (in == null) {
        // no script to run, return success
        return new Status();
      } else if (config.isxFlag() && !config.hasShebangLine()) {
        // no shebang was found and x option is set
        throw new MainExitException(1, "jruby: no Ruby script found in input (LoadError)");
      } else if (config.isShouldCheckSyntax()) {
        // check syntax only and exit
        return doCheckSyntax(runtime, in, filename);
      } else {
        // proceed to run the script
        return doRunFromMain(runtime, in, filename);
      }
    } finally {
      runtime.tearDown();
    }
  }
예제 #2
0
  public int run() {
    if (config.isShowVersion()) {
      showVersion();
    }

    if (config.isShowCopyright()) {
      showCopyright();
    }

    if (!config.shouldRunInterpreter()) {
      if (config.shouldPrintUsage()) {
        printUsage();
      }
      if (config.shouldPrintProperties()) {
        printProperties();
      }
      return 0;
    }

    InputStream in = config.getScriptSource();
    String filename = config.displayedFileName();

    String[] args = parseShebangOptions(in);
    if (args.length > 0) {
      config.processArguments(args);
    }
    Ruby runtime = Ruby.newInstance(config);

    // set thread context JRuby classloader here, for the main thread
    try {
      Thread.currentThread().setContextClassLoader(runtime.getJRubyClassLoader());
    } catch (SecurityException se) {
      // can't set TC classloader
      if (runtime.getInstanceConfig().isVerbose()) {
        config
            .getError()
            .println(
                "WARNING: Security restrictions disallowed setting context classloader for main thread.");
      }
    }

    if (in == null) {
      // no script to run, return success below
    } else if (config.isShouldCheckSyntax()) {
      int status = 0;
      try {
        runtime.parseFromMain(in, filename);
        config.getOutput().println("Syntax OK for " + filename);
      } catch (RaiseException re) {
        status = -1;
        if (re.getException().getMetaClass().getBaseName().equals("SyntaxError")) {
          config
              .getOutput()
              .println("SyntaxError in " + re.getException().message(runtime.getCurrentContext()));
        } else {
          throw re;
        }
      }

      if (config.getArgv().length > 0) {
        for (String arg : config.getArgv()) {
          File argFile = new File(arg);
          if (argFile.exists()) {
            try {
              runtime.parseFromMain(new FileInputStream(argFile), arg);
              config.getOutput().println("Syntax OK for " + arg);
            } catch (FileNotFoundException fnfe) {
              status = -1;
              config.getOutput().println("File not found: " + arg);
            } catch (RaiseException re) {
              status = -1;
              if (re.getException().getMetaClass().getBaseName().equals("SyntaxError")) {
                config
                    .getOutput()
                    .println(
                        "SyntaxError in " + re.getException().message(runtime.getCurrentContext()));
              } else {
                throw re;
              }
            }
          } else {
            status = -1;
            config.getOutput().println("File not found: " + arg);
          }
        }
      }
      return status;
    } else {
      long now = -1;

      try {
        if (config.isBenchmarking()) {
          now = System.currentTimeMillis();
        }

        if (config.isSamplingEnabled()) {
          SimpleSampler.startSampleThread();
        }

        try {
          runtime.runFromMain(in, filename);
        } finally {
          runtime.tearDown();

          if (config.isBenchmarking()) {
            config.getOutput().println("Runtime: " + (System.currentTimeMillis() - now) + " ms");
          }

          if (config.isSamplingEnabled()) {
            org.jruby.util.SimpleSampler.report();
          }
        }
      } catch (RaiseException rj) {
        RubyException raisedException = rj.getException();
        if (runtime.getSystemExit().isInstance(raisedException)) {
          IRubyObject status = raisedException.callMethod(runtime.getCurrentContext(), "status");

          if (status != null && !status.isNil()) {
            return RubyNumeric.fix2int(status);
          }
        } else {
          runtime.printError(raisedException);
          return 1;
        }
      }
    }
    return 0;
  }
예제 #3
-1
파일: Main.java 프로젝트: ratnikov/jruby
  private Status internalRun() {
    doShowVersion();
    doShowCopyright();

    if (!config.getShouldRunInterpreter()) {
      doPrintUsage(false);
      doPrintProperties();
      return new Status();
    }

    InputStream in = config.getScriptSource();
    String filename = config.displayedFileName();

    doProcessArguments(in);

    Ruby _runtime;

    if (DripMain.DRIP_RUNTIME != null) {
      // use drip's runtime, reinitializing config
      _runtime = DripMain.DRIP_RUNTIME;
      _runtime.reinitialize(true);
    } else {
      _runtime = Ruby.newInstance(config);
    }

    final Ruby runtime = _runtime;
    final AtomicBoolean didTeardown = new AtomicBoolean();

    if (config.isHardExit()) {
      // we're the command-line JRuby, and should set a shutdown hook for
      // teardown.
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {
                public void run() {
                  if (didTeardown.compareAndSet(false, true)) {
                    runtime.tearDown();
                  }
                }
              });
    }

    try {
      doSetContextClassLoader(runtime);

      if (in == null) {
        // no script to run, return success
        return new Status();
      } else if (config.isXFlag() && !config.hasShebangLine()) {
        // no shebang was found and x option is set
        throw new MainExitException(1, "jruby: no Ruby script found in input (LoadError)");
      } else if (config.getShouldCheckSyntax()) {
        // check syntax only and exit
        return doCheckSyntax(runtime, in, filename);
      } else {
        // proceed to run the script
        return doRunFromMain(runtime, in, filename);
      }
    } finally {
      if (didTeardown.compareAndSet(false, true)) {
        runtime.tearDown();
      }
    }
  }