Package org.jruby.runtime.load
Class LoadService
java.lang.Object
org.jruby.runtime.load.LoadService
- Direct Known Subclasses:
OSGiLoadService
How require works in JRuby
When requiring a name from Ruby, JRuby will first remove any file extension it knows about, thereby making it possible to use this string to see if JRuby has already loaded the name in question. If a .rb extension is specified, JRuby will only try those extensions when searching. If a .so, .o, .dll, or .jar extension is specified, JRuby will only try .so or .jar when searching. Otherwise, JRuby goes through the known suffixes (.rb, .rb.ast.ser, .so, and .jar) and tries to find a library with this name. The process for finding a library follows this order for all searchable extensions:- First, check if the name starts with 'jar:', then the path points to a jar-file resource which is returned.
- Second, try searching for the file in the current dir
- Then JRuby looks through the load path trying these variants:
- See if the current load path entry starts with 'jar:', if so check if this jar-file contains the name
- Otherwise JRuby tries to construct a path by combining the entry and the current working directy, and then see if a file with the correct name can be reached from this point.
- If all these fail, try to load the name as a resource from classloader resources, using the bare name as well as the load path entries
- When we get to this state, the normal JRuby loading has failed. At
this stage JRuby tries to load Java native extensions, by following this
process:
- First it checks that we haven't already found a library. If we found a library of type JarredScript, the method continues.
- The first step is translating the name given into a valid Java Extension class name. First it splits the string into each path segment, and then makes all but the last downcased. After this it takes the last entry, removes all underscores and capitalizes each part separated by underscores. It then joins everything together and tacks on a 'Service' at the end. Lastly, it removes all leading dots, to make it a valid Java FWCN.
- If the previous library was of type JarredScript, we try to add the jar-file to the classpath
- Now JRuby tries to instantiate the class with the name constructed. If this works, we return a ClassExtensionLibrary. Otherwise, the old library is put back in place, if there was one.
- When all separate methods have been tried and there was no result, a LoadError will be raised.
- Otherwise, the name will be added to the loaded features, and the library loaded
- Author:
- jpetersen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Pattern
protected LibrarySearcher
protected StringArraySet
protected RubyArray
protected String
protected String
protected final Ruby
protected static final Pattern
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
void
Add additional directories to the load path.void
Add additional directories to the load path.boolean
autoloadRequire
(RubyString requireName) protected void
checkEmptyLoad
(String file) static String
classpathFilenameFromURL
(String name, URL loc, boolean isClasspathScheme) Given a URL to a classloader resource, build an appropriate load string.protected Library
createLibrary
(String baseName, String loadName, LoadServiceResource resource) protected final void
debugLogFound
(String what, String msg) protected final void
debugLogFound
(LoadServiceResource resource) protected final void
debugLogTry
(String what, String msg) boolean
featureAlreadyLoaded
(String feature) boolean
featureAlreadyLoaded
(String feature, String[] loading) protected LoadServiceResource
findFileInClasspath
(String name) this method uses the appropriate lookup strategy to find a file.protected LibrarySearcher.FoundLibrary
findLibraryWithClassloaders
(String baseName, LoadService.SuffixType suffixType) getClassPathResource
(ClassLoader classLoader, String name) protected String
getLoadPathEntry
(IRubyObject entry) getPathForLocation
(String filename) void
Called to initialize the load path with a set of optional prepended directories and then the standard set of dirs.protected boolean
isJarfileLibrary
(Library library, String file) protected static boolean
isRequireable
(URL loc) void
void
load
(String file, IRubyObject wrapWith) void
loadFromClassLoader
(ClassLoader classLoader, String file, boolean wrap) void
static void
reflectedLoad
(Ruby runtime, String libraryName, String className, ClassLoader classLoader, boolean wrap) Load the org.jruby.runtime.load.Library implementation specified by className.boolean
protected String
resolveLoadName
(LoadServiceResource foundResource, String previousPath) protected LibrarySearcher.FoundLibrary
searchForLoad
(String file) Replaces findLibraryBySearchState but split off for load.char
searchForRequire
(String file, LibrarySearcher.FoundLibrary[] path) Replaces findLibraryBySearchState but split off for require.void
setMainScript
(String filename, String cwd) void
tearDown()
protected boolean
tryLoadingLibraryOrScript
(Ruby runtime, Library library, String searchFile) protected void
warnCircularRequire
(String requireName)
-
Field Details
-
sourcePattern
-
extensionPattern
-
loadPath
-
loadedFeatures
-
jarFiles
-
runtime
-
librarySearcher
-
mainScript
-
mainScriptPath
-
-
Constructor Details
-
LoadService
-
-
Method Details
-
init
Called to initialize the load path with a set of optional prepended directories and then the standard set of dirs. This should only be called once, at load time, since it wipes out loaded features.- Parameters:
prependDirectories
-
-
addPaths
Add additional directories to the load path.- Parameters:
additionalDirectories
- a List of additional dirs to append to the load path
-
addPaths
Add additional directories to the load path.- Parameters:
additionalDirectories
- an array of additional dirs to append to the load path
-
provide
-
addPath
-
getWrapperSelf
-
load
-
load
-
loadFromClassLoader
-
require
-
autoloadRequire
-
warnCircularRequire
-
reflectedLoad
public static void reflectedLoad(Ruby runtime, String libraryName, String className, ClassLoader classLoader, boolean wrap) 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.- Parameters:
runtime
- The runtime in which to loadlibraryName
- The name of the library, to use for error messagesclassName
- The class of the libraryclassLoader
- The classloader to use to load itwrap
- Whether to wrap top-level in an anonymous module
-
getLoadPath
-
getLoadedFeatures
-
isJarfileLibrary
-
tryLoadingLibraryOrScript
-
checkEmptyLoad
- Throws:
RaiseException
-
debugLogTry
-
debugLogFound
-
debugLogFound
-
searchForRequire
Replaces findLibraryBySearchState but split off for require. Needed for OSGiLoadService to override. -
searchForLoad
Replaces findLibraryBySearchState but split off for load. Needed for OSGiLoadService to override. -
featureAlreadyLoaded
-
featureAlreadyLoaded
-
findLibraryWithClassloaders
protected LibrarySearcher.FoundLibrary findLibraryWithClassloaders(String baseName, LoadService.SuffixType suffixType) -
createLibrary
-
getLoadPathEntry
-
findFileInClasspath
this method uses the appropriate lookup strategy to find a file. It is used by Kernel#require.MRI: rb_find_file
- Parameters:
name
- the file to find, this is a path name- Returns:
- the correct file
-
isRequireable
-
getClassPathResource
-
classpathFilenameFromURL
Given a URL to a classloader resource, build an appropriate load string.- Parameters:
name
- the original filename requestedloc
- the URL to the resourceisClasspathScheme
- whether we're using the classpath: sceheme- Returns:
-
resolveLoadName
-
getMainScript
-
getMainScriptPath
-
setMainScript
-
getPathForLocation
-
tearDown
public void tearDown()
-