Esempio n. 1
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;
  }
Esempio n. 2
0
  public int run(String[] args) {
    try {
      config.processArguments(args);
      return run();
    } catch (MainExitException mee) {
      if (!mee.isAborted()) {
        config.getOutput().println(mee.getMessage());
        if (mee.isUsageError()) {
          printUsage();
        }
      }
      return mee.getStatus();
    } catch (OutOfMemoryError oome) {
      // produce a nicer error since Rubyists aren't used to seeing this
      System.gc();

      String memoryMax = SafePropertyAccessor.getProperty("jruby.memory.max");
      String message = "";
      if (memoryMax != null) {
        message = " of " + memoryMax;
      }
      config
          .getError()
          .println("Error: Your application used more memory than the safety cap" + message + ".");
      config.getError().println("Specify -J-Xmx####m to increase it (#### = cap size in MB).");

      if (config.getVerbose()) {
        config.getError().println("Exception trace follows:");
        oome.printStackTrace();
      } else {
        config.getError().println("Specify -w for full OutOfMemoryError stack trace");
      }
      return 1;
    } catch (StackOverflowError soe) {
      // produce a nicer error since Rubyists aren't used to seeing this
      System.gc();

      String stackMax = SafePropertyAccessor.getProperty("jruby.stack.max");
      String message = "";
      if (stackMax != null) {
        message = " of " + stackMax;
      }
      config
          .getError()
          .println(
              "Error: Your application used more stack memory than the safety cap" + message + ".");
      config.getError().println("Specify -J-Xss####k to increase it (#### = cap size in KB).");

      if (config.getVerbose()) {
        config.getError().println("Exception trace follows:");
        soe.printStackTrace();
      } else {
        config.getError().println("Specify -w for full StackOverflowError stack trace");
      }
      return 1;
    } catch (UnsupportedClassVersionError ucve) {
      config
          .getError()
          .println("Error: Some library (perhaps JRuby) was built with a later JVM version.");
      config
          .getError()
          .println(
              "Please use libraries built with the version you intend to use or an earlier one.");

      if (config.getVerbose()) {
        config.getError().println("Exception trace follows:");
        ucve.printStackTrace();
      } else {
        config.getError().println("Specify -w for full UnsupportedClassVersionError stack trace");
      }
      return 1;
    } catch (ThreadKill kill) {
      return 0;
    }
  }