Class TextResourceCache

java.lang.Object
net.solarnetwork.service.support.TextResourceCache

public class TextResourceCache extends Object
A simple text resource cache.

All loaded resources are cached in memory.

Since:
1.67
Version:
1.0
Author:
matt
  • Field Details

    • INSTANCE

      public static final TextResourceCache INSTANCE
      A global, thread-safe, shared instance.
  • Constructor Details

    • TextResourceCache

      public TextResourceCache()
      Constructor.

      This uses a HashMap for the cache, which is not thread safe.

    • TextResourceCache

      public TextResourceCache(Map<String,String> cache)
      Constructor.
      Parameters:
      cache - the cache to use; for thread safety use something like a ConcurrentMap
  • Method Details

    • getResourceAsString

      public String getResourceAsString(String resourceName, Class<?> clazz)
      Get a text resource as a string.
      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      Returns:
      the text
      Throws:
      RuntimeException - if the resource cannot be loaded
    • getResourceAsString

      public String getResourceAsString(String resourceName, Class<?> clazz, Map<String,?> templateVariables)
      Get a text resource as a string.
      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      templateVariables - optional template variables to substitute in the loaded resource
      Returns:
      the text
      Throws:
      RuntimeException - if the resource cannot be loaded
    • getResourceAsString

      public String getResourceAsString(String resourceName, Class<?> clazz, Pattern skip)
      Get a text resource as a string.
      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      skip - an optional pattern that will be used to match against lines; matches will be left out of the string used to match
      Returns:
      the text
      Throws:
      RuntimeException - if the resource cannot be loaded
    • getResourceAsString

      public String getResourceAsString(String resourceName, Class<?> clazz, Pattern skip, Map<String,?> templateVariables)
      Get a text resource as a string.

      Note that the final, template resolved text is cached in memory.

      Parameters:
      resourceName - the resource to load
      clazz - the Class to load the resource from
      skip - an optional pattern that will be used to match against lines; matches will be left out of the string used to match
      templateVariables - optional template variables to substitute in the loaded resource
      Returns:
      the text
      Throws:
      RuntimeException - if the resource cannot be loaded
      See Also: