Class ResourceLoader

java.lang.Object
net.time4j.base.ResourceLoader

public abstract class ResourceLoader extends Object

Defines a general access point of loading any text resources and services.

Specification: All external subclasses must have a public no-arg constructor.

Since:
3.5/4.3
Author:
Meno Hochschild
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static String
    Name of system property responsible for getting an external instance as fully qualified class name.
    static String
    Name of system property controlling if the use of classloader should be enforced instead of trying an URI-construction first.
  • Method Summary

    Modifier and Type
    Method
    Description
    Applications should never use the constructor but this static method to achieve a general instance.
    load​(Class<?> moduleRef, String path, boolean noCache)
    Loads a resource as input stream based on the classloader of given module reference.
    abstract InputStream
    load​(URI uri, boolean noCache)
    Loads given URI-resource as input stream.
    abstract URI
    locate​(String moduleName, Class<?> moduleRef, String path)
    Constructs an URI for given module resource.
    <S> void
    registerService​(Class<S> serviceInterface, S implementation)
    Registers an external service provider.
    abstract <S> Iterable<S>
    services​(Class<S> serviceInterface)
    Finds a collection of service providers available for given service provider interface.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EXTERNAL_RESOURCE_LOADER

      public static final String EXTERNAL_RESOURCE_LOADER

      Name of system property responsible for getting an external instance as fully qualified class name.

      Time4J will throw an Error if the configuration entry is wrong.

      See Also:
      Constant Field Values
    • USE_OF_CLASSLOADER_ONLY

      public static final String USE_OF_CLASSLOADER_ONLY

      Name of system property controlling if the use of classloader should be enforced instead of trying an URI-construction first.

      The value is either "true" or "false" (default). This property will be ignored on Android platforms. Applications in an OSGi-environment should set the property value to true.

      Since:
      3.16/4.13
      See Also:
      Constant Field Values
  • Method Details

    • getInstance

      public static ResourceLoader getInstance()

      Applications should never use the constructor but this static method to achieve a general instance.

      Returns:
      ResourceLoader
      Since:
      3.5/4.3
    • locate

      public abstract URI locate(String moduleName, Class<?> moduleRef, String path)

      Constructs an URI for given module resource.

      Attention: Some implementations might yield an uri without verifying if the uri resource really exists.

      Parameters:
      moduleName - name of related time4j-module
      moduleRef - module-specific class reference
      path - path to text resource
      Returns:
      uri of resource or null if unable to locate the resource
      Since:
      3.5/4.3
    • load

      public abstract InputStream load(URI uri, boolean noCache)

      Loads given URI-resource as input stream.

      Callers are responsible for closing the result stream.

      Parameters:
      uri - uniform resource identifier as result of locate-method (optional)
      noCache - avoid caching?
      Returns:
      input stream or null if the resource could not be opened
      Since:
      3.5/4.3
      See Also:
      locate(String, Class, String)
    • load

      public final InputStream load(Class<?> moduleRef, String path, boolean noCache) throws IOException

      Loads a resource as input stream based on the classloader of given module reference.

      Callers are responsible for closing the result stream.

      Parameters:
      moduleRef - module-specific class reference
      path - path to text resource (must be understandable by class loaders)
      noCache - avoid caching?
      Returns:
      input stream
      Throws:
      IOException - if the stream cannot be opened or if this method is called on Android platforms
      Since:
      3.16/4.13
    • services

      public abstract <S> Iterable<S> services(Class<S> serviceInterface)

      Finds a collection of service providers available for given service provider interface.

      Type Parameters:
      S - generic service type
      Parameters:
      serviceInterface - service provider interface
      Returns:
      iterable collection of service providers
      Since:
      3.5/4.3
    • registerService

      public <S> void registerService(Class<S> serviceInterface, S implementation)

      Registers an external service provider.

      This method is mainly intended to register an extra time zone repository in the context of OSGi where the service loader mechanism does not work. Important: Calling this method must be done before executing any other Time4J-code. Example for registering the tzdata-module (v3.0 or later):

           ResourceLoader.getInstance().registerService(
             ZoneModelProvider.class,
             new net.time4j.tz.repo.TimezoneRepositoryProviderSPI());
       

      Note: The example can be shortened by the expression net.time4j.tz.repo.TZDATA.init() which also provides actual leap second support.

      Type Parameters:
      S - generic service type
      Parameters:
      serviceInterface - service interface
      implementation - service provider
      Throws:
      IllegalStateException - if already registered or called on a non-standard resource loader like on Android
      Since:
      5.0