Example #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 = runtime.newLoadError("IO error -- " + state.searchFile);
      re.initCause(e);
      throw re;
    }
  }
Example #2
0
  public boolean require(String file) {
    if (!runtime.getProfile().allowRequire(file)) {
      throw runtime.newLoadError("No such file to load -- " + file);
    }

    long startTime = loadTimer.startLoad(file);
    try {
      return smartLoad(file);
    } finally {
      loadTimer.endLoad(file, startTime);
    }
  }
Example #3
0
 protected Library createLibrary(SearchState state, LoadServiceResource resource) {
   if (resource == null) {
     return null;
   }
   String file = state.loadName;
   if (file.endsWith(".so")) {
     throw runtime.newLoadError("JRuby does not support .so libraries from filesystem");
   } else if (file.endsWith(".jar")) {
     return new JarredScript(resource);
   } else if (file.endsWith(".class")) {
     return new JavaCompiledScript(resource);
   } else {
     return new ExternalScript(resource, file);
   }
 }
Example #4
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);
    }
  }
Example #5
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);
      }
    }
  }
Example #6
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);
    }
  }
Example #7
0
  protected LoadServiceResource tryResourceFromLoadPathOrURL(
      SearchState state, String baseName, SuffixType suffixType) {
    LoadServiceResource foundResource = null;

    // if it's a ./ baseName, use CWD logic
    if (baseName.startsWith("./")) {
      foundResource = tryResourceFromCWD(state, baseName, suffixType);

      if (foundResource != null) {
        state.loadName = resolveLoadName(foundResource, foundResource.getName());
        return foundResource;
      }
    }

    // if it's a ~/ baseName use HOME logic
    if (baseName.startsWith("~/")) {
      foundResource = tryResourceFromHome(state, baseName, suffixType);

      if (foundResource != null) {
        state.loadName = resolveLoadName(foundResource, foundResource.getName());
        return foundResource;
      }
    }

    // if given path is absolute, just try it as-is (with extensions) and no load path
    if (new File(baseName).isAbsolute() || baseName.startsWith("../")) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        foundResource = tryResourceAsIs(namePlusSuffix);

        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          return foundResource;
        }
      }

      return null;
    }

    Outer:
    for (int i = 0; i < loadPath.size(); i++) {
      // TODO this is really inefficient, and potentially a problem everytime anyone require's
      // something.
      // we should try to make LoadPath a special array object.
      RubyString entryString = loadPath.eltInternal(i).convertToString();
      String loadPathEntry = entryString.asJavaString();

      if (loadPathEntry.equals(".") || loadPathEntry.equals("")) {
        foundResource = tryResourceFromCWD(state, baseName, suffixType);

        if (foundResource != null) {
          String ss = foundResource.getName();
          if (ss.startsWith("./")) {
            ss = ss.substring(2);
          }
          state.loadName = resolveLoadName(foundResource, ss);
          break Outer;
        }
      } else {
        boolean looksLikeJarURL = loadPathLooksLikeJarURL(loadPathEntry);
        for (String suffix : suffixType.getSuffixes()) {
          String namePlusSuffix = baseName + suffix;

          if (looksLikeJarURL) {
            foundResource = tryResourceFromJarURLWithLoadPath(namePlusSuffix, loadPathEntry);
          } else if (namePlusSuffix.startsWith("./")) {
            throw runtime.newLoadError("");
          } else {
            foundResource = tryResourceFromLoadPath(namePlusSuffix, loadPathEntry);
          }

          if (foundResource != null) {
            String ss = namePlusSuffix;
            if (ss.startsWith("./")) {
              ss = ss.substring(2);
            }
            state.loadName = resolveLoadName(foundResource, ss);
            break Outer; // end suffix iteration
          }
        }
      }
    }

    return foundResource;
  }
Example #8
0
 protected void checkEmptyLoad(String file) throws RaiseException {
   if (file.equals("")) {
     throw runtime.newLoadError("No such file to load -- " + file);
   }
 }
Example #9
0
 private static RaiseException newLoadErrorFromThrowable(Ruby runtime, String file, Throwable t) {
   return runtime.newLoadError(
       String.format("load error: %s -- %s: %s", file, t.getClass().getName(), t.getMessage()));
 }
Example #10
0
 public boolean require(String file) {
   if (!runtime.getProfile().allowRequire(file)) {
     throw runtime.newLoadError("No such file to load -- " + file);
   }
   return smartLoad(file);
 }