Class ClassLocator

java.lang.Object
java.lang.ClassLoader
ortus.boxlang.runtime.loader.ClassLocator

public class ClassLocator extends ClassLoader
This is a Class Loader is in charge of locating Box classes in the lookup algorithm and also Java classes in the classloader paths. The resolution is done once per class path and stored for quicker lookups. Example: apppath.models.User - Verify if the class is in the cache - If not, verify if the class is in the declared class mappings path - If not, verify if the class is in the application path - If not, verify if the class is a Java class If the class is found, it will be cached for future lookups.
  • Field Details

  • Method Details

    • getInstance

      public static ClassLocator getInstance()
      Get the singleton instance
      Returns:
      ClassLocator
    • getInstance

      public static ClassLocator getInstance(BoxRuntime runtime)
      Get the singleton instance
      Parameters:
      runtime - The current runtime
      Returns:
      ClassLocator
    • getRuntime

      public BoxRuntime getRuntime()
      Get the runtime associated with this locator
    • getJavaResolver

      public JavaResolver getJavaResolver()
      Shortcut to get the Java Resolver
    • getBoxResolver

      public BoxResolver getBoxResolver()
      Shortcut to get the Box Resolver
    • getResolverCache

      public ConcurrentMap<String,ClassLocation> getResolverCache()
      Get the cache of resolved classes
      Returns:
      The cache of resolved classes
    • registerResolver

      public void registerResolver(IClassResolver resolver)
      Register a class resolver
      Parameters:
      resolver - The class resolver to register
    • getResolvedPrefixes

      public List<String> getResolvedPrefixes()
      Get the registered resolver prefixes
      Returns:
      The registered resolver names
    • getResolver

      public IClassResolver getResolver(String prefix)
      Get a registered resolver by prefix
      Parameters:
      prefix - The prefix of the resolver
      Returns:
      The registered resolver or null if not found
    • hasResolver

      public Boolean hasResolver(String prefix)
      Verify if a resolver is registered
      Parameters:
      prefix - The prefix of the resolver
      Returns:
      True if the resolver is registered, false otherwise
    • removeResolver

      public Boolean removeResolver(String prefix)
      Remove a resolver by prefix
      Parameters:
      prefix - The prefix of the resolver
      Returns:
      True if the resolver was removed, false otherwise
    • isEmpty

      public Boolean isEmpty()
      Verifies if the class resolver is empty or not
      Returns:
      True if the resolver cache is empty, false otherwise
    • size

      public int size()
      Verify the size of the resolver cache
      Returns:
      The size of the resolver cache
    • clear

      public ClassLocator clear()
      Clear the resolver cache
      Returns:
      The class locator instance
    • clear

      public Boolean clear(String name)
      Clear a specific key from the resolver cache
      Parameters:
      name - The fully qualified path of the class to remove
      Returns:
      True, if it was removed, else if it didn't exist
    • hasClass

      public boolean hasClass(String name)
      Verifies if the passed path key is in the resolver cache
      Parameters:
      name - The fully qualified path of the class to verify
      Returns:
      True if the key is in the resolver cache, false otherwise
    • classSet

      public Set<String> classSet()
      Get all the class paths keys in the resolver cache
      Returns:
      The keys in the resolver cache
    • getClassList

      public List<String> getClassList()
      Get a list of all the cached resolver classes
      Returns:
      The list of cached classes as a list of key names
    • load

      public DynamicObject load(IBoxContext context, String name)
      Load a class without a direct resolver. This will require a system resolution of the class location and will cache the result for future lookups. The lookup order is: 1. Bx Resolver 2. Java Resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      Returns:
      The invokable representation of the class
    • load

      public DynamicObject load(IBoxContext context, String name, List<ImportDefinition> imports)
      Load a class without a direct resolver or with a resovler prefix. If there is not a resolver prefix-- Ex: java.lang.String app.models.User This will require a system resolution of the class location and will cache the result for future lookups. The lookup order is: 1. Bx Resolver 2. Java Resolver If there is a resolver prefix, then it will be used directly. Ex: java:java.lang.String bx:app.models.User
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      imports - The list of imports to use when resolving the class
      Returns:
      The invokable representation of the class
    • load

      public DynamicObject load(IBoxContext context, String name, String resolverPrefix)
      Load a class from a specific resolver This is a convenience method that will throw an exception if the class is not found
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      Returns:
      The invokable representation of the class
    • load

      public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException)
      Load a class from a specific resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      throwException - If true, it will throw an exception if the class is not found, else it will return null
      Returns:
      The invokable representation of the class
    • load

      public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports)
      Load a class from a specific resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      throwException - If true, it will throw an exception if the class is not found, else it will return null
      imports - The list of imports to use when resolving the class
      Returns:
      The invokable representation of the class
    • load

      public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports, Boolean useCaching)
      Load a class from a specific resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      throwException - If true, it will throw an exception if the class is not found, else it will return null
      imports - The list of imports to use when resolving the class
      useCaching - If true, it will cache the resolved class if allowed, else just does discovery and passthrough
      Returns:
      The invokable representation of the class
    • loadFromClassPaths

      public DynamicObject loadFromClassPaths(IBoxContext context, String name, Array classPaths, Boolean throwException, List<ImportDefinition> imports)
      Load a class from a specific array of class paths
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      classPaths - The array of class paths to use when resolving the class
      throwException - If true, it will throw an exception if the class is not found, else it will return null
      imports - The list of imports to use when resolving the class
      Returns:
      The invokable representation of the class
    • safeLoad

      public Optional<DynamicObject> safeLoad(IBoxContext context, String name)
      Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      Returns:
      The invokable representation of the class or an empty optional if not found
    • safeLoad

      public Optional<DynamicObject> safeLoad(IBoxContext context, String name, List<ImportDefinition> imports)
      Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      imports - The list of imports to use when resolving the class
      Returns:
      The invokable representation of the class or an empty optional if not found
    • safeLoad

      public Optional<DynamicObject> safeLoad(IBoxContext context, String name, String resolverPrefix)
      Load a class from a specific resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      Returns:
      The invokable representation of the class
    • safeLoad

      public Optional<DynamicObject> safeLoad(IBoxContext context, String name, String resolverPrefix, List<ImportDefinition> imports)
      Load a class from a specific resolver
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      resolverPrefix - The prefix of the resolver to use
      imports - The list of imports to use when resolving the class
      Returns:
      The invokable representation of the class
    • findClass

      public Class<?> findClass(IBoxContext context, String name, List<ImportDefinition> imports)
      This method ONLY returns the class representation, it does not cache it. It is here to support the Java ClassLoader interface.
      Parameters:
      context - The current context of execution
      name - The fully qualified path/name of the class to load
      imports - The list of imports to use when resolving the class
      Returns:
      The class requested to be loaded represented by the incoming name
    • getClassLoaders

      public Map<String,DynamicClassLoader> getClassLoaders()
      Get all the class loaders registered
      Returns:
      The class loader map
    • hasClassLoader

      public boolean hasClassLoader(String loaderKey)
      Verify if the class loader exists by cache key
      Parameters:
      loaderKey - The key of the class loader
    • getClassLoader

      public DynamicClassLoader getClassLoader(String loaderKey)
      Get a class loader by cache key
      Parameters:
      loaderKey - The key of the class loader
      Returns:
      The class loader
    • getClassLoaderCount

      public long getClassLoaderCount()
      Count how many class loaders we have loaded
    • clearClassLoaders

      public void clearClassLoaders()
      Clear all the class loaders