Resources.java

  1. /*
  2.  *    Copyright 2010-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.migration.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 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.   /**
  38.    * Returns the default classloader (may be null).
  39.    *
  40.    * @return The default classloader
  41.    */
  42.   public static ClassLoader getDefaultClassLoader() {
  43.     return classLoaderWrapper.defaultClassLoader;
  44.   }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  301.   public static Charset getCharset() {
  302.     return charset;
  303.   }

  304.   public static void setCharset(Charset charset) {
  305.     Resources.charset = charset;
  306.   }

  307. }