Esempio n. 1
0
  protected LoadServiceResource tryResourceFromJarURL(
      SearchState state, String baseName, SuffixType suffixType) {
    // if a jar or file URL, return load service resource directly without further searching
    LoadServiceResource foundResource = null;
    if (baseName.startsWith("jar:")) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        try {
          URL url = new URL(namePlusSuffix);
          debugLogTry("resourceFromJarURL", url.toString());
          if (url.openStream() != null) {
            foundResource = new LoadServiceResource(url, namePlusSuffix);
            debugLogFound(foundResource);
          }
        } catch (FileNotFoundException e) {
        } catch (MalformedURLException e) {
          throw runtime.newIOErrorFromException(e);
        } catch (IOException e) {
          throw runtime.newIOErrorFromException(e);
        }
        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          break; // end suffix iteration
        }
      }
    } else if (baseName.startsWith("file:") && baseName.indexOf("!/") != -1) {
      for (String suffix : suffixType.getSuffixes()) {
        String namePlusSuffix = baseName + suffix;
        try {
          String jarFile = namePlusSuffix.substring(5, namePlusSuffix.indexOf("!/"));
          JarFile file = new JarFile(jarFile);
          String filename = namePlusSuffix.substring(namePlusSuffix.indexOf("!/") + 2);
          String canonicalFilename = canonicalizePath(filename);

          debugLogTry("resourceFromJarURL", canonicalFilename.toString());
          if (file.getJarEntry(canonicalFilename) != null) {
            foundResource =
                new LoadServiceResource(
                    new URL("jar:file:" + jarFile + "!/" + canonicalFilename), namePlusSuffix);
            debugLogFound(foundResource);
          }
        } catch (Exception e) {
        }
        if (foundResource != null) {
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          break; // end suffix iteration
        }
      }
    }

    return foundResource;
  }
Esempio n. 2
0
  protected LoadServiceResource tryResourceFromCWD(
      SearchState state, String baseName, SuffixType suffixType) throws RaiseException {
    LoadServiceResource foundResource = null;

    for (String suffix : suffixType.getSuffixes()) {
      String namePlusSuffix = baseName + suffix;
      // check current directory; if file exists, retrieve URL and return resource
      try {
        JRubyFile file =
            JRubyFile.create(
                runtime.getCurrentDirectory(),
                RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
        debugLogTry("resourceFromCWD", file.toString());
        if (file.isFile() && file.isAbsolute() && file.canRead()) {
          boolean absolute = true;
          String s = namePlusSuffix;
          if (!namePlusSuffix.startsWith("./")) {
            s = "./" + s;
          }
          foundResource = new LoadServiceResource(file, s, absolute);
          debugLogFound(foundResource);
          state.loadName = resolveLoadName(foundResource, namePlusSuffix);
          break;
        }
      } catch (IllegalArgumentException illArgEx) {
      } catch (SecurityException secEx) {
      }
    }

    return foundResource;
  }
Esempio n. 3
0
 protected Library findLibraryWithClassloaders(
     SearchState state, String baseName, SuffixType suffixType) {
   for (String suffix : suffixType.getSuffixes()) {
     String file = baseName + suffix;
     LoadServiceResource resource = findFileInClasspath(file);
     if (resource != null) {
       state.loadName = resolveLoadName(resource, file);
       return createLibrary(state, resource);
     }
   }
   return null;
 }
Esempio n. 4
0
 protected Library findBuiltinLibrary(SearchState state, String baseName, SuffixType suffixType) {
   for (String suffix : suffixType.getSuffixes()) {
     String namePlusSuffix = baseName + suffix;
     debugLogTry("builtinLib", namePlusSuffix);
     if (builtinLibraries.containsKey(namePlusSuffix)) {
       state.loadName = namePlusSuffix;
       Library lib = builtinLibraries.get(namePlusSuffix);
       debugLogFound("builtinLib", namePlusSuffix);
       return lib;
     }
   }
   return null;
 }
Esempio n. 5
0
  protected LoadServiceResource tryResourceFromHome(
      SearchState state, String baseName, SuffixType suffixType) throws RaiseException {
    LoadServiceResource foundResource = null;

    RubyHash env = (RubyHash) runtime.getObject().fastGetConstant("ENV");
    RubyString env_home = runtime.newString("HOME");
    if (env.has_key_p(env_home).isFalse()) {
      return null;
    }
    String home = env.op_aref(runtime.getCurrentContext(), env_home).toString();
    String path = baseName.substring(2);

    for (String suffix : suffixType.getSuffixes()) {
      String namePlusSuffix = path + suffix;
      // check home directory; if file exists, retrieve URL and return resource
      try {
        JRubyFile file =
            JRubyFile.create(
                home, RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
        debugLogTry("resourceFromHome", file.toString());
        if (file.isFile() && file.isAbsolute() && file.canRead()) {
          boolean absolute = true;
          String s = "~/" + namePlusSuffix;

          foundResource = new LoadServiceResource(file, s, absolute);
          debugLogFound(foundResource);
          state.loadName = resolveLoadName(foundResource, s);
          break;
        }
      } catch (IllegalArgumentException illArgEx) {
      } catch (SecurityException secEx) {
      }
    }

    return foundResource;
  }
Esempio n. 6
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;
  }
Esempio n. 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 = 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 = namePlusSuffix;
          return foundResource;
        }
      }

      return null;
    }

    Outer:
    for (Iterator pathIter = loadPath.getList().iterator(); pathIter.hasNext(); ) {
      // TODO this is really inefficient, and potentially a problem everytime anyone require's
      // something.
      // we should try to make LoadPath a special array object.
      String loadPathEntry = ((IRubyObject) pathIter.next()).toString();

      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 = ss;
          break Outer;
        }
      } else {
        for (String suffix : suffixType.getSuffixes()) {
          String namePlusSuffix = baseName + suffix;

          if (loadPathLooksLikeJarURL(loadPathEntry)) {
            foundResource = tryResourceFromJarURLWithLoadPath(namePlusSuffix, loadPathEntry);
          } else {
            foundResource = tryResourceFromLoadPath(namePlusSuffix, loadPathEntry);
          }

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

    return foundResource;
  }