Top Description Inners Fields Constructors Methods
org.jruby.runtime.load

public Class LoadService

extends Object
Class Inheritance
Known Direct Subclasses
org.jruby.embed.osgi.internal.OSGiLoadService
Imports
java.io.File, .IOException, java.net.URL, java.util.EnumSet, .HashMap, .List, .Map, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.ReentrantLock, java.util.function.Function, java.util.jar.JarFile, java.util.regex.Matcher, .Pattern, org.jruby.Ruby, .RubyArray, .RubyFile, .RubyHash, .RubyInstanceConfig, .RubyModule, .RubyString, .RubySymbol, .RubyThread, org.jruby.anno.JRubyMethod, org.jruby.api.Convert, org.jruby.exceptions.CatchThrow, .JumpException, .MainExitException, .RaiseException, .Unrescuable, org.jruby.ext.rbconfig.RbConfigLibrary, org.jruby.platform.Platform, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.util.FileResource, .JRubyFile, org.jruby.util.collections.StringArraySet, org.jruby.util.log.Logger, .LoggerFactory

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:
  1. First, check if the name starts with 'jar:', then the path points to a jar-file resource which is returned.
  2. Second, try searching for the file in the current dir
  3. Then JRuby looks through the load path trying these variants:
    1. See if the current load path entry starts with 'jar:', if so check if this jar-file contains the name
    2. 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.
  4. 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
  5. 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:
    1. First it checks that we haven't already found a library. If we found a library of type JarredScript, the method continues.
    2. 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.
    3. If the previous library was of type JarredScript, we try to add the jar-file to the classpath
    4. 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.
  6. When all separate methods have been tried and there was no result, a LoadError will be raised.
  7. Otherwise, the name will be added to the loaded features, and the library loaded
Author
jpetersen

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class
pack-priv class
private static enum
public static enum
private static class

Field Summary

Modifier and TypeField and Description
protected static final Pattern
protected final Map<String, JarFile>
protected LibrarySearcher
protected StringArraySet
protected RubyArray<T extends IRubyObject>
private final LoadService.LoadTimer
pack-priv static final Logger
protected String
protected String
pack-priv final LoadService.RequireLocks
protected final Ruby
protected static final Pattern
private static final Pattern
private final ThreadLocal<RubyModule>

Constructor Summary

AccessConstructor and Description
public
LoadService(Ruby runtime)

Method Summary

Modifier and TypeMethod and Description
protected void
public void
addPaths(List<String>
a List of additional dirs to append to the load path
additionalDirectories
)

Add additional directories to the load path.

public void
addPaths(String...
an array of additional dirs to append to the load path
additionalDirectories
)

Add additional directories to the load path.

public boolean
protected void
public static String
classpathFilenameFromURL(String
the original filename requested
name
,
URL
the URL to the resource
loc
,
boolean
whether we're using the classpath: sceheme
isClasspathScheme
)

Given a URL to a classloader resource, build an appropriate load string.

protected Library
createLibrary(String baseName, String loadName, LoadServiceResource resource)

private static void
debugLoadException(final Ruby runtime, final Throwable ex)

protected final void
protected final void
protected final void
public boolean
public boolean
featureAlreadyLoaded(String feature, String[] loading)

protected LoadServiceResource

Returns:

the correct file
findFileInClasspath
(String
the file to find, this is a path name
name
)

this method uses the appropriate lookup strategy to find a file.

protected LibrarySearcher.FoundLibrary
public LoadServiceResource
public IRubyObject
public IRubyObject
protected String
public String
public String
public String
public RubyModule
public void
init(List<String> prependDirectories)

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, final String file)

protected static boolean
public void
load(String file, IRubyObject wrapWith)

public void
load(String file, boolean wrap)

public void
loadFromClassLoader(ClassLoader classLoader, String file, boolean wrap)

public void
public static void
reflectedLoad(Ruby
The runtime in which to load
runtime
,
String
The name of the library, to use for error messages
libraryName
,
String
The class of the library
className
,
ClassLoader
The classloader to use to load it
classLoader
,
boolean
Whether to wrap top-level in an anonymous module
wrap
)

Load the org.jruby.runtime.load.Library implementation specified by className.

public boolean
require(String requireName)

protected String
resolveLoadName(LoadServiceResource foundResource, String previousPath)

protected LibrarySearcher.FoundLibrary
searchForLoad(String file)

Replaces findLibraryBySearchState but split off for load.

public char
searchForRequire(String file, LibrarySearcher.FoundLibrary[] path)

Replaces findLibraryBySearchState but split off for require.

public void
setMainScript(String filename, String cwd)

private LoadService.RequireState
smartLoadInternal(String file, boolean circularRequireWarning)

public void
protected boolean
tryLoadingLibraryOrScript(Ruby runtime, Library library, String searchFile)

protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

extensionPatternback to summary
protected static final Pattern extensionPattern
jarFilesback to summary
protected final Map<String, JarFile> jarFiles
librarySearcherback to summary
protected LibrarySearcher librarySearcher
loadedFeaturesback to summary
protected StringArraySet loadedFeatures
loadPathback to summary
protected RubyArray<T extends IRubyObject> loadPath
loadTimerback to summary
private final LoadService.LoadTimer loadTimer
LOGback to summary
pack-priv static final Logger LOG
mainScriptback to summary
protected String mainScript
mainScriptPathback to summary
protected String mainScriptPath
requireLocksback to summary
pack-priv final LoadService.RequireLocks requireLocks
runtimeback to summary
protected final Ruby runtime
sourcePatternback to summary
protected static final Pattern sourcePattern
URI_PATTERNback to summary
private static final Pattern URI_PATTERN
wrapperSelfback to summary
private final ThreadLocal<RubyModule> wrapperSelf

Constructor Detail

LoadServiceback to summary
public LoadService(Ruby runtime)

Method Detail

addPathback to summary
protected void addPath(String path)
addPathsback to summary
public void addPaths(List<String> additionalDirectories)

Add additional directories to the load path.

Parameters
additionalDirectories:List<String>

a List of additional dirs to append to the load path

addPathsback to summary
public void addPaths(String... additionalDirectories)

Add additional directories to the load path.

Parameters
additionalDirectories:String[]

an array of additional dirs to append to the load path

autoloadRequireback to summary
public boolean autoloadRequire(RubyString requireName)
checkEmptyLoadback to summary
protected void checkEmptyLoad(String file) throws RaiseException
classpathFilenameFromURLback to summary
public static String classpathFilenameFromURL(String name, URL loc, boolean isClasspathScheme)

Given a URL to a classloader resource, build an appropriate load string.

Parameters
name:String

the original filename requested

loc:URL

the URL to the resource

isClasspathScheme:boolean

whether we're using the classpath: sceheme

createLibraryback to summary
protected Library createLibrary(String baseName, String loadName, LoadServiceResource resource)
debugLoadExceptionback to summary
private static void debugLoadException(final Ruby runtime, final Throwable ex)
debugLogFoundback to summary
protected final void debugLogFound(LoadServiceResource resource)
debugLogFoundback to summary
protected final void debugLogFound(String what, String msg)
debugLogTryback to summary
protected final void debugLogTry(String what, String msg)
featureAlreadyLoadedback to summary
public boolean featureAlreadyLoaded(String feature)
featureAlreadyLoadedback to summary
public boolean featureAlreadyLoaded(String feature, String[] loading)
findFileInClasspathback to summary
protected LoadServiceResource findFileInClasspath(String name)

this method uses the appropriate lookup strategy to find a file. It is used by Kernel#require.

MRI: rb_find_file

Parameters
name:String

the file to find, this is a path name

Returns:LoadServiceResource

the correct file

findLibraryWithClassloadersback to summary
protected LibrarySearcher.FoundLibrary findLibraryWithClassloaders(String baseName, LoadService.SuffixType suffixType)
getClassPathResourceback to summary
public LoadServiceResource getClassPathResource(ClassLoader classLoader, String name)
getLoadedFeaturesback to summary
public IRubyObject getLoadedFeatures()
getLoadPathback to summary
public IRubyObject getLoadPath()
getLoadPathEntryback to summary
protected String getLoadPathEntry(IRubyObject entry)
getMainScriptback to summary
public String getMainScript()
getMainScriptPathback to summary
public String getMainScriptPath()
getPathForLocationback to summary
public String getPathForLocation(String filename)
getWrapperSelfback to summary
public RubyModule getWrapperSelf()
initback to summary
public void init(List<String> prependDirectories)

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.

isJarfileLibraryback to summary
protected boolean isJarfileLibrary(Library library, final String file)
isRequireableback to summary
protected static boolean isRequireable(URL loc)
loadback to summary
public void load(String file, IRubyObject wrapWith)
loadback to summary
public void load(String file, boolean wrap)
loadFromClassLoaderback to summary
public void loadFromClassLoader(ClassLoader classLoader, String file, boolean wrap)
provideback to summary
public void provide(String name)
reflectedLoadback to summary
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:Ruby

The runtime in which to load

libraryName:String

The name of the library, to use for error messages

className:String

The class of the library

classLoader:ClassLoader

The classloader to use to load it

wrap:boolean

Whether to wrap top-level in an anonymous module

requireback to summary
public boolean require(String requireName)
resolveLoadNameback to summary
protected String resolveLoadName(LoadServiceResource foundResource, String previousPath)
searchForLoadback to summary
protected LibrarySearcher.FoundLibrary searchForLoad(String file)

Replaces findLibraryBySearchState but split off for load. Needed for OSGiLoadService to override.

searchForRequireback to summary
public char searchForRequire(String file, LibrarySearcher.FoundLibrary[] path)

Replaces findLibraryBySearchState but split off for require. Needed for OSGiLoadService to override.

setMainScriptback to summary
public void setMainScript(String filename, String cwd)
smartLoadInternalback to summary
private LoadService.RequireState smartLoadInternal(String file, boolean circularRequireWarning)
tearDownback to summary
public void tearDown()
tryLoadingLibraryOrScriptback to summary
protected boolean tryLoadingLibraryOrScript(Ruby runtime, Library library, String searchFile)
warnCircularRequireback to summary
protected void warnCircularRequire(String requireName)
org.jruby.runtime.load back to summary

public Class LoadService.LoadPathMethods

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static IRubyObject
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

LoadPathMethodsback to summary
public LoadPathMethods()

Method Detail

resolve_feature_pathback to summary
public static IRubyObject resolve_feature_path(ThreadContext context, IRubyObject self, IRubyObject pathArg)
Annotations
@JRubyMethod
org.jruby.runtime.load back to summary

private Class LoadService.LoadTimer

extends Object
Class Inheritance
Known Direct Subclasses
org.jruby.runtime.load.LoadService.TracingLoadTimer

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
endLoad(String file, long startTime)

public long
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

LoadTimerback to summary
private LoadTimer()

Method Detail

endLoadback to summary
public void endLoad(String file, long startTime)
startLoadback to summary
public long startLoad(String file)
org.jruby.runtime.load back to summary

pack-priv final Class LoadService.RequireLocks

extends Object
Class Inheritance

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class

Field Summary

Modifier and TypeField and Description
pack-priv final ConcurrentHashMap<String, LoadService.RequireLocks.RequireLock>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private LoadService.RequireState
private LoadService.RequireState

Returns:

If the sync object already locked by current thread, it just returns false without getting a lock. Otherwise true.
lock
(String
just a name for the lock.
path
,
boolean circularRequireWarning, LoadService.RequireState defaultResult, Function<String, LoadService.RequireState> ifLocked)

Get exclusive lock for the specified requireName.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

poolback to summary
pack-priv final ConcurrentHashMap<String, LoadService.RequireLocks.RequireLock> pool

Constructor Detail

RequireLocksback to summary
private RequireLocks()

Method Detail

executeAndClearLockback to summary
private LoadService.RequireState executeAndClearLock(String path, Function<String, LoadService.RequireState> ifLocked, RubyThread thread, LoadService.RequireLocks.RequireLock lock)
lockback to summary
private LoadService.RequireState lock(String path, boolean circularRequireWarning, LoadService.RequireState defaultResult, Function<String, LoadService.RequireState> ifLocked)

Get exclusive lock for the specified requireName. Acquire sync object for the requireName from the pool, then try to lock it.

Note

This lock is not fair for now.

Parameters
path:String

just a name for the lock.

Returns:LoadService.RequireState

If the sync object already locked by current thread, it just returns false without getting a lock. Otherwise true.

org.jruby.runtime.load back to summary

pack-priv final Class LoadService.RequireLocks.RequireLock

extends ReentrantLock
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv volatile boolean

Constructor Summary

AccessConstructor and Description
pack-priv

Field Detail

destroyedback to summary
pack-priv volatile boolean destroyed

Constructor Detail

RequireLockback to summary
pack-priv RequireLock()
org.jruby.runtime.load back to summary

private final Enum LoadService.RequireState

extends Enum<LoadService.RequireState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final LoadService.RequireState
public static final LoadService.RequireState
public static final LoadService.RequireState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static LoadService.RequireState
public static LoadService.RequireState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ALREADY_LOADEDback to summary
public static final LoadService.RequireState ALREADY_LOADED
CIRCULARback to summary
public static final LoadService.RequireState CIRCULAR
LOADEDback to summary
public static final LoadService.RequireState LOADED

Constructor Detail

RequireStateback to summary
private RequireState()

Method Detail

valueOfback to summary
public static LoadService.RequireState valueOf(String name)
valuesback to summary
public static LoadService.RequireState[] values()
org.jruby.runtime.load back to summary

public final Enum LoadService.SuffixType

extends Enum<LoadService.SuffixType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final LoadService.SuffixType
public static final LoadService.SuffixType
public static final String[]
extensionSuffixes

Deprecated
public static final LoadService.SuffixType
public static final LoadService.SuffixType
public static final String[]
sourceSuffixes

Deprecated
public final EnumSet<LibrarySearcher.Suffix>

Constructor Summary

AccessConstructor and Description
private
SuffixType(EnumSet<E extends Enum<E>> suffixes)

Method Summary

Modifier and TypeMethod and Description
public String[]
getSuffixes()
Deprecated

public EnumSet<LibrarySearcher.Suffix>
public static LoadService.SuffixType
public static LoadService.SuffixType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

Bothback to summary
public static final LoadService.SuffixType Both
Extensionback to summary
public static final LoadService.SuffixType Extension
extensionSuffixesback to summary
public static final String[] extensionSuffixes

Deprecated

Annotations
@Deprecated
Neitherback to summary
public static final LoadService.SuffixType Neither
Sourceback to summary
public static final LoadService.SuffixType Source
sourceSuffixesback to summary
public static final String[] sourceSuffixes

Deprecated

Annotations
@Deprecated
suffixesback to summary
public final EnumSet<LibrarySearcher.Suffix> suffixes

Constructor Detail

SuffixTypeback to summary
private SuffixType(EnumSet<E extends Enum<E>> suffixes)

Method Detail

getSuffixesback to summary
public String[] getSuffixes()

Deprecated

Annotations
@Deprecated
getSuffixSetback to summary
public EnumSet<LibrarySearcher.Suffix> getSuffixSet()
valueOfback to summary
public static LoadService.SuffixType valueOf(String name)
valuesback to summary
public static LoadService.SuffixType[] values()
org.jruby.runtime.load back to summary

private final Class LoadService.TracingLoadTimer

extends LoadTimer
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final AtomicInteger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
private StringBuilder
public long

Field Detail

indentback to summary
private final AtomicInteger indent

Constructor Detail

TracingLoadTimerback to summary
private TracingLoadTimer()

Method Detail

endLoadback to summary
public void endLoad(String file, long startTime)

Overrides org.jruby.runtime.load.LoadService.LoadTimer.endLoad.

Annotations
@Override
getIndentStringback to summary
private StringBuilder getIndentString()
startLoadback to summary
public long startLoad(String file)

Overrides org.jruby.runtime.load.LoadService.LoadTimer.startLoad.

Annotations
@Override