예제 #1
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;
  }
예제 #2
0
  protected LoadServiceResource tryResourceFromLoadPath(String namePlusSuffix, String loadPathEntry)
      throws RaiseException {
    LoadServiceResource foundResource = null;

    try {
      if (!Ruby.isSecurityRestricted()) {
        String reportedPath = loadPathEntry + "/" + namePlusSuffix;
        JRubyFile actualPath;
        boolean absolute = false;
        // we check length == 0 for 'load', which does not use load path
        if (new File(reportedPath).isAbsolute()) {
          absolute = true;
          // it's an absolute path, use it as-is
          actualPath =
              JRubyFile.create(
                  loadPathEntry,
                  RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
        } else {
          absolute = false;
          // prepend ./ if . is not already there, since we're loading based on CWD
          if (reportedPath.charAt(0) != '.') {
            reportedPath = "./" + reportedPath;
          }
          actualPath =
              JRubyFile.create(
                  JRubyFile.create(runtime.getCurrentDirectory(), loadPathEntry).getAbsolutePath(),
                  RubyFile.expandUserPath(runtime.getCurrentContext(), namePlusSuffix));
        }
        debugLogTry(
            "resourceFromLoadPath",
            "'" + actualPath.toString() + "' " + actualPath.isFile() + " " + actualPath.canRead());
        if (actualPath.isFile() && actualPath.canRead()) {
          foundResource = new LoadServiceResource(actualPath, reportedPath, absolute);
          debugLogFound(foundResource);
        }
      }
    } catch (SecurityException secEx) {
    }

    return foundResource;
  }
예제 #3
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;
  }