예제 #1
0
  protected boolean tryLoadingLibraryOrScript(Ruby runtime, SearchState state) {
    // attempt to load the found library
    RubyString loadNameRubyString = RubyString.newString(runtime, state.loadName);
    try {
      synchronized (loadedFeaturesInternal) {
        if (loadedFeaturesInternal.contains(loadNameRubyString)) {
          return false;
        } else {
          addLoadedFeature(loadNameRubyString);
        }
      }

      // otherwise load the library we've found
      state.library.load(runtime, false);
      return true;
    } catch (MainExitException mee) {
      // allow MainExitException to propagate out for exec and friends
      throw mee;
    } catch (Throwable e) {
      if (isJarfileLibrary(state, state.searchFile)) {
        return true;
      }

      removeLoadedFeature(loadNameRubyString);
      reraiseRaiseExceptions(e);

      if (runtime.getDebug().isTrue()) e.printStackTrace(runtime.getErr());

      RaiseException re = newLoadErrorFromThrowable(runtime, state.searchFile, e);
      re.initCause(e);
      throw re;
    }
  }
예제 #2
0
  public static IRScope loadScriptFromFile(
      Ruby runtime,
      InputStream inStream,
      File resourcePath,
      String resourceName,
      boolean isAbsolute) {
    String name = getFilenameFromPathAndName(resourcePath, resourceName, isAbsolute);
    try {
      Class clazz = loadCompiledScriptFromClass(runtime, inStream);

      try {
        Method method = clazz.getMethod("loadIR", Ruby.class, String.class);
        return (IRScope) method.invoke(null, runtime, name);
      } catch (Exception e) {
        if (runtime.getDebug().isTrue()) {
          e.printStackTrace();
        }
        throw runtime.newLoadError(
            name + " is not compiled Ruby; use java_import to load normal classes");
      }
    } catch (IOException e) {
      throw runtime.newIOErrorFromException(e);
    } catch (LinkageError le) {
      if (runtime.getDebug().isTrue()) {
        le.printStackTrace();
      }
      throw runtime.newLoadError(
          "Linkage error loading compiled script; you may need to recompile '" + name + "': " + le);
    } finally {
      try {
        inStream.close();
      } catch (IOException ioe) {
        throw runtime.newIOErrorFromException(ioe);
      }
    }
  }
예제 #3
0
  /**
   * Load the org.jruby.runtime.load.Library implementation specified by className. The purpose of
   * using this method is to avoid having static references to the given library class, thereby
   * avoiding the additional classloading when the library is not in use.
   *
   * @param runtime The runtime in which to load
   * @param libraryName The name of the library, to use for error messages
   * @param className The class of the library
   * @param classLoader The classloader to use to load it
   * @param wrap Whether to wrap top-level in an anonymous module
   */
  public static void reflectedLoad(
      Ruby runtime, String libraryName, String className, ClassLoader classLoader, boolean wrap) {
    try {
      if (classLoader == null && Ruby.isSecurityRestricted()) {
        classLoader = runtime.getInstanceConfig().getLoader();
      }

      Library library = (Library) classLoader.loadClass(className).newInstance();

      library.load(runtime, false);
    } catch (RaiseException re) {
      throw re;
    } catch (Throwable e) {
      if (runtime.getDebug().isTrue()) e.printStackTrace();
      throw runtime.newLoadError("library `" + libraryName + "' could not be loaded: " + e);
    }
  }
예제 #4
0
파일: Tempfile.java 프로젝트: rkh/jruby
 final void release() {
   if (!released) {
     released = true;
     if (openFile != null) {
       openFile.cleanup(runtime, false);
     }
     if (tmpFile.exists()) {
       boolean deleted = tmpFile.delete();
       if (runtime.getDebug().isTrue()) {
         String msg = "removing " + tmpFile.getPath() + " ... ";
         if (deleted) {
           runtime.getErr().println(msg + "done");
         } else {
           runtime.getErr().println(msg + "can't delete");
         }
       }
     }
   }
 }
예제 #5
0
  public void load(String file, boolean wrap) {
    if (!runtime.getProfile().allowLoad(file)) {
      throw runtime.newLoadError("No such file to load -- " + file);
    }

    SearchState state = new SearchState(file);
    state.prepareLoadSearch(file);

    Library library = findBuiltinLibrary(state, state.searchFile, state.suffixType);
    if (library == null) library = findLibraryWithoutCWD(state, state.searchFile, state.suffixType);

    if (library == null) {
      library = findLibraryWithClassloaders(state, state.searchFile, state.suffixType);
      if (library == null) {
        throw runtime.newLoadError("No such file to load -- " + file);
      }
    }
    try {
      library.load(runtime, wrap);
    } catch (IOException e) {
      if (runtime.getDebug().isTrue()) e.printStackTrace(runtime.getErr());
      throw newLoadErrorFromThrowable(runtime, file, e);
    }
  }