Resources.java

  1. /*
  2.  *    Copyright 2009-2023 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.io;

  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.net.URL;
  23. import java.net.URLConnection;
  24. import java.nio.charset.Charset;
  25. import java.util.Properties;

  26. /**
  27.  * A class to simplify access to resources through the classloader.
  28.  *
  29.  * @author Clinton Begin
  30.  */
  31. public class Resources {

  32.   private static final ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();

  33.   /**
  34.    * Charset to use when calling getResourceAsReader. null means use the system default.
  35.    */
  36.   private static Charset charset;

  37.   private Resources() {
  38.   }

  39.   /**
  40.    * Returns the default classloader (may be null).
  41.    *
  42.    * @return The default classloader
  43.    */
  44.   public static ClassLoader getDefaultClassLoader() {
  45.     return classLoaderWrapper.defaultClassLoader;
  46.   }

  47.   /**
  48.    * Sets the default classloader
  49.    *
  50.    * @param defaultClassLoader
  51.    *          - the new default ClassLoader
  52.    */
  53.   public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
  54.     classLoaderWrapper.defaultClassLoader = defaultClassLoader;
  55.   }

  56.   /**
  57.    * Returns the URL of the resource on the classpath
  58.    *
  59.    * @param resource
  60.    *          The resource to find
  61.    *
  62.    * @return The resource
  63.    *
  64.    * @throws java.io.IOException
  65.    *           If the resource cannot be found or read
  66.    */
  67.   public static URL getResourceURL(String resource) throws IOException {
  68.     // issue #625
  69.     return getResourceURL(null, resource);
  70.   }

  71.   /**
  72.    * Returns the URL of the resource on the classpath
  73.    *
  74.    * @param loader
  75.    *          The classloader used to fetch the resource
  76.    * @param resource
  77.    *          The resource to find
  78.    *
  79.    * @return The resource
  80.    *
  81.    * @throws java.io.IOException
  82.    *           If the resource cannot be found or read
  83.    */
  84.   public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
  85.     URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
  86.     if (url == null) {
  87.       throw new IOException("Could not find resource " + resource);
  88.     }
  89.     return url;
  90.   }

  91.   /**
  92.    * Returns a resource on the classpath as a Stream object
  93.    *
  94.    * @param resource
  95.    *          The resource to find
  96.    *
  97.    * @return The resource
  98.    *
  99.    * @throws java.io.IOException
  100.    *           If the resource cannot be found or read
  101.    */
  102.   public static InputStream getResourceAsStream(String resource) throws IOException {
  103.     return getResourceAsStream(null, resource);
  104.   }

  105.   /**
  106.    * Returns a resource on the classpath as a Stream object
  107.    *
  108.    * @param loader
  109.    *          The classloader used to fetch the resource
  110.    * @param resource
  111.    *          The resource to find
  112.    *
  113.    * @return The resource
  114.    *
  115.    * @throws java.io.IOException
  116.    *           If the resource cannot be found or read
  117.    */
  118.   public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
  119.     InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
  120.     if (in == null) {
  121.       throw new IOException("Could not find resource " + resource);
  122.     }
  123.     return in;
  124.   }

  125.   /**
  126.    * Returns a resource on the classpath as a Properties object
  127.    *
  128.    * @param resource
  129.    *          The resource to find
  130.    *
  131.    * @return The resource
  132.    *
  133.    * @throws java.io.IOException
  134.    *           If the resource cannot be found or read
  135.    */
  136.   public static Properties getResourceAsProperties(String resource) throws IOException {
  137.     Properties props = new Properties();
  138.     try (InputStream in = getResourceAsStream(resource)) {
  139.       props.load(in);
  140.     }
  141.     return props;
  142.   }

  143.   /**
  144.    * Returns a resource on the classpath as a Properties object
  145.    *
  146.    * @param loader
  147.    *          The classloader used to fetch the resource
  148.    * @param resource
  149.    *          The resource to find
  150.    *
  151.    * @return The resource
  152.    *
  153.    * @throws java.io.IOException
  154.    *           If the resource cannot be found or read
  155.    */
  156.   public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
  157.     Properties props = new Properties();
  158.     try (InputStream in = getResourceAsStream(loader, resource)) {
  159.       props.load(in);
  160.     }
  161.     return props;
  162.   }

  163.   /**
  164.    * Returns a resource on the classpath as a Reader object
  165.    *
  166.    * @param resource
  167.    *          The resource to find
  168.    *
  169.    * @return The resource
  170.    *
  171.    * @throws java.io.IOException
  172.    *           If the resource cannot be found or read
  173.    */
  174.   public static Reader getResourceAsReader(String resource) throws IOException {
  175.     Reader reader;
  176.     if (charset == null) {
  177.       reader = new InputStreamReader(getResourceAsStream(resource));
  178.     } else {
  179.       reader = new InputStreamReader(getResourceAsStream(resource), charset);
  180.     }
  181.     return reader;
  182.   }

  183.   /**
  184.    * Returns a resource on the classpath as a Reader object
  185.    *
  186.    * @param loader
  187.    *          The classloader used to fetch the resource
  188.    * @param resource
  189.    *          The resource to find
  190.    *
  191.    * @return The resource
  192.    *
  193.    * @throws java.io.IOException
  194.    *           If the resource cannot be found or read
  195.    */
  196.   public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
  197.     Reader reader;
  198.     if (charset == null) {
  199.       reader = new InputStreamReader(getResourceAsStream(loader, resource));
  200.     } else {
  201.       reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
  202.     }
  203.     return reader;
  204.   }

  205.   /**
  206.    * Returns a resource on the classpath as a File object
  207.    *
  208.    * @param resource
  209.    *          The resource to find
  210.    *
  211.    * @return The resource
  212.    *
  213.    * @throws java.io.IOException
  214.    *           If the resource cannot be found or read
  215.    */
  216.   public static File getResourceAsFile(String resource) throws IOException {
  217.     return new File(getResourceURL(resource).getFile());
  218.   }

  219.   /**
  220.    * Returns a resource on the classpath as a File object
  221.    *
  222.    * @param loader
  223.    *          - the classloader used to fetch the resource
  224.    * @param resource
  225.    *          - the resource to find
  226.    *
  227.    * @return The resource
  228.    *
  229.    * @throws java.io.IOException
  230.    *           If the resource cannot be found or read
  231.    */
  232.   public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
  233.     return new File(getResourceURL(loader, resource).getFile());
  234.   }

  235.   /**
  236.    * Gets a URL as an input stream
  237.    *
  238.    * @param urlString
  239.    *          - the URL to get
  240.    *
  241.    * @return An input stream with the data from the URL
  242.    *
  243.    * @throws java.io.IOException
  244.    *           If the resource cannot be found or read
  245.    */
  246.   public static InputStream getUrlAsStream(String urlString) throws IOException {
  247.     URL url = new URL(urlString);
  248.     URLConnection conn = url.openConnection();
  249.     return conn.getInputStream();
  250.   }

  251.   /**
  252.    * Gets a URL as a Reader
  253.    *
  254.    * @param urlString
  255.    *          - the URL to get
  256.    *
  257.    * @return A Reader with the data from the URL
  258.    *
  259.    * @throws java.io.IOException
  260.    *           If the resource cannot be found or read
  261.    */
  262.   public static Reader getUrlAsReader(String urlString) throws IOException {
  263.     Reader reader;
  264.     if (charset == null) {
  265.       reader = new InputStreamReader(getUrlAsStream(urlString));
  266.     } else {
  267.       reader = new InputStreamReader(getUrlAsStream(urlString), charset);
  268.     }
  269.     return reader;
  270.   }

  271.   /**
  272.    * Gets a URL as a Properties object
  273.    *
  274.    * @param urlString
  275.    *          - the URL to get
  276.    *
  277.    * @return A Properties object with the data from the URL
  278.    *
  279.    * @throws java.io.IOException
  280.    *           If the resource cannot be found or read
  281.    */
  282.   public static Properties getUrlAsProperties(String urlString) throws IOException {
  283.     Properties props = new Properties();
  284.     try (InputStream in = getUrlAsStream(urlString)) {
  285.       props.load(in);
  286.     }
  287.     return props;
  288.   }

  289.   /**
  290.    * Loads a class
  291.    *
  292.    * @param className
  293.    *          - the class to fetch
  294.    *
  295.    * @return The loaded class
  296.    *
  297.    * @throws ClassNotFoundException
  298.    *           If the class cannot be found (duh!)
  299.    */
  300.   public static Class<?> classForName(String className) throws ClassNotFoundException {
  301.     return classLoaderWrapper.classForName(className);
  302.   }

  303.   public static Charset getCharset() {
  304.     return charset;
  305.   }

  306.   public static void setCharset(Charset charset) {
  307.     Resources.charset = charset;
  308.   }

  309. }