Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.runtime.load;
  
  import java.io.File;
  import java.net.URL;
  import java.util.Map;
 
 import org.jruby.Ruby;
 
 class LibrarySearcher {
     static class Ruby18 extends LibrarySearcher {
         public Ruby18(LoadService loadService) {
             super(loadService);
         }
 
         @Override
         protected String resolveLoadName(FileResource unusedString ruby18Path) {
             return ruby18Path;
         }
 
         @Override
         protected String resolveScriptName(FileResource unusedString ruby18Path) {
             return ruby18Path;
         }
     }
 
     static class FoundLibrary implements Library {
         private final Library delegate;
         private final String loadName;
 
         public FoundLibrary(Library delegateString loadName) {
             this. = delegate;
             this. = loadName;
         }
 
         @Override
         public void load(Ruby runtimeboolean wrapthrows IOException {
             .load(runtimewrap);
         }
 
         public String getLoadName() {
             return ;
         }
     }
 
     private final LoadService loadService;
     private final Ruby runtime;
     private final Map<StringLibrarybuiltinLibraries;
 
     public LibrarySearcher(LoadService loadService) {
         this. = loadService;
         this. = loadService.runtime;
         this. = loadService.builtinLibraries;
     }
 
     // TODO(ratnikov): Kill this helper once we kill LoadService.SearchState
         FoundLibrary lib = findLibrary(state.searchFilestate.suffixType);
         if (lib != null) {
             state.library = lib;
             state.setLoadName(lib.getLoadName());
         }
         return lib;
     }
 
     public FoundLibrary findLibrary(String baseNameSuffixType suffixType) {
         boolean searchedForServiceLibrary = false;
 
         for (String suffix : suffixType.getSuffixes()) {
             FoundLibrary library = findBuiltinLibrary(baseNamesuffix);
             if (library == nulllibrary = findResourceLibrary(baseNamesuffix);
 
             // Since searching for a service library doesn't take the suffix into account, there's no need
             // to perform it more than once.
             if (library == null && !searchedForServiceLibrary) {
                 library = findServiceLibrary(baseNamesuffix);
                 searchedForServiceLibrary = true;
             }
 
             if (library != null) {
                 return library;
             }
         }
 
         return null;
     }
    private FoundLibrary findBuiltinLibrary(String nameString suffix) {
        String namePlusSuffix = name + suffix;
        ..logTry(namePlusSuffix);
        if (.containsKey(namePlusSuffix)) {
            ..logFound(namePlusSuffix);
            return new FoundLibrary(
                    .get(namePlusSuffix),
                    namePlusSuffix);
        }
        return null;
    }
    private FoundLibrary findServiceLibrary(String nameString ignored) {
        ..logTry(name);
        Library extensionLibrary = ClassExtensionLibrary.tryFind(name);
        if (extensionLibrary != null) {
            ..logFound(name);
            return new FoundLibrary(extensionLibraryname);
        } else {
            return null;
        }
    }
    private FoundLibrary findResourceLibrary(String baseNameString suffix) {
        if (baseName.startsWith("./")) {
            return findFileResource(baseNamesuffix);
        }
        if (baseName.startsWith("../")) {
            // Path should be canonicalized in the findFileResource
            return findFileResource(baseNamesuffix);
        }
        if (baseName.startsWith("~/")) {
            RubyHash env = (RubyHash.getObject().getConstant("ENV");
            RubyString env_home = .newString("HOME");
            if (env.has_key_p(env_home).isFalse()) {
                return null;
            }
            String home = env.op_aref(.getCurrentContext(), env_home).toString();
            String path = home + "/" + baseName.substring(2);
            return findFileResource(pathsuffix);
        }
        // If path is considered absolute, bypass loadPath iteration and load as-is
        if (isAbsolute(baseName)) {
          return findFileResource(baseNamesuffix);
        }
        try {
            for (IRubyObject loadPathEntry : ..toJavaArray()) {
                FoundLibrary library = findFileResourceWithLoadPath(baseNamesuffixgetPath(loadPathEntry));
                if (library != nullreturn library;
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return null;
    }
    // FIXME: to_path should not be called n times it should only be once and that means a cache which would
    // also reduce all this casting and/or string creates.
    private String getPath(IRubyObject loadPathEntry) {
        if (.is1_8()) return loadPathEntry.convertToString().asJavaString();
        return RubyFile.get_path(.getCurrentContext(), loadPathEntry).asJavaString();
    }
    private FoundLibrary findFileResource(String searchNameString suffix) {
        return findFileResourceWithLoadPath(searchNamesuffixnull);
    }
    private FoundLibrary findFileResourceWithLoadPath(String searchNameString suffixString loadPath) {
        String fullPath = loadPath != null ? loadPath + "/" + searchName : searchName;
        String pathWithSuffix = fullPath + suffix;
        ..logTry(pathWithSuffix);
        FileResource resource = JRubyFile.createResource(pathWithSuffix);
        if (resource.exists()) {
            ..logFound(pathWithSuffix);
            String scriptName = resolveScriptName(resourcepathWithSuffix);
            String loadName = resolveLoadName(resourcesearchName + suffix);
            return new FoundLibrary(
                    new ResourceLibrary(searchNamescriptNameresource),
                    loadName);
        }
        return null;
    }
    private static boolean isAbsolute(String path) {
        // jar: prefix doesn't mean anything anymore, but we might still encounter it
        if (path.startsWith("jar:")) {
            path = path.substring(4);
        }
        if (path.startsWith("file:")) {
            // We treat any paths with a file schema as absolute, because apparently some tests
            // explicitely depend on such behavior (test/test_load.rb). On other hand, maybe it's
            // not too bad, since otherwise joining LOAD_PATH logic would be more complicated if
            // it'd have to worry about schema.
            return true;
        }
        if (path.startsWith("uri:")) {
            // uri: are absolute
            return true;
        }
        return new File(path).isAbsolute();
    }
    protected String resolveLoadName(FileResource resourceString ruby18path) {
        return resource.absolutePath();
    }
    protected String resolveScriptName(FileResource resourceString ruby18Path) {
        return resource.absolutePath();
    }
    static class ResourceLibrary implements Library {
        private final String searchName;
        private final String scriptName;
        private final FileResource resource;
        private final String location;
        public ResourceLibrary(String searchNameString scriptNameFileResource resource) {
            this. = searchName;
            this. = scriptName;
            this. = resource.absolutePath();
            this. = resource;
        }
        @Override
        public void load(Ruby runtimeboolean wrap) {
            InputStream is;
            try {
                is = new BufferedInputStream(.inputStream(), 32768);
            } catch(IOException e) {
                throw runtime.newLoadError("no such file to load -- " + );
            }
            try {
                if (.endsWith(".jar")) {
                    loadJar(runtimewrap);
                } else if (.endsWith(".class")) {
                    loadClass(runtimeiswrap);
                } else {
                    loadScript(runtimeiswrap);
                }
            } finally {
                try {
                    is.close();
                } catch (IOException ioE) {
                    // At least we tried....
                }
            }
        }
        private void loadScript(Ruby runtimeInputStream isboolean wrap) {
            runtime.loadFile(iswrap);
        }
        private void loadClass(Ruby runtimeInputStream isboolean wrap) {
            Script script = CompiledScriptLoader.loadScriptFromFile(runtimeis);
            if (script == null) {
                // we're depending on the side effect of the load, which loads the class but does not turn it into a script
                // I don't like it, but until we restructure the code a bit more, we'll need to quietly let it by here.
                return;
            }
            script.setFilename();
            runtime.loadScript(scriptwrap);
        }
        private void loadJar(Ruby runtimeboolean wrap) {
            try {
                URL url;
                if (.startsWith(.)){
                    // get URL directly from the classloader with its StreamHandler set
                    // by the classloader itself
                    url = ClasspathResource.getResourceURL();
                }
                else if (.startsWith(.)){
                    url = null;
                    runtime.getJRubyClassLoader().addURLNoIndex(URLResource.getResourceURL(runtime));
                }
                else {
                    File f = new File();
                    if (f.exists() || .contains"!")){
                        url = f.toURI().toURL();
                        if ( .contains"!") ) {
                            url = new URL"jar:" + url );
                        }
                    }
                    else {
                        url = new URL();
                    }
                }
                if ( url != null ) {
                    runtime.getJRubyClassLoader().addURL(url);
                }
            } catch (MalformedURLException badUrl) {
                runtime.newIOErrorFromException(badUrl);
            }
            // If an associated Service library exists, load it as well
            ClassExtensionLibrary serviceExtension = ClassExtensionLibrary.tryFind(runtime);
            if (serviceExtension != null) {
                serviceExtension.load(runtimewrap);
            }
        }
    }
New to GrepCode? Check out our FAQ X