예제 #1
0
파일: Main.java 프로젝트: rebx/jruby
  private Status doRunFromMain(Ruby runtime, InputStream in, String filename) {
    long now = -1;
    try {
      if (config.isBenchmarking()) {
        now = System.currentTimeMillis();
      }
      if (config.isSamplingEnabled()) {
        SimpleSampler.startSampleThread();
      }

      doCheckSecurityManager();

      try {
        runtime.runFromMain(in, filename);
      } finally {
        if (config.isBenchmarking()) {
          config.getOutput().println("Runtime: " + (System.currentTimeMillis() - now) + " ms");
        }
        if (config.isSamplingEnabled()) {
          org.jruby.util.SimpleSampler.report();
        }
      }
    } catch (RaiseException rj) {
      return new Status(handleRaiseException(rj));
    }
    return new Status();
  }
예제 #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;
  }