Resources.java

  1. /*
  2.  *    Copyright 2009-2021 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.  *       http://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 ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();

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

  38.   Resources() {
  39.   }

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

  48.   /**
  49.    * Sets the default classloader
  50.    *
  51.    * @param defaultClassLoader - 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 The resource to find
  60.    * @return The resource
  61.    * @throws java.io.IOException If the resource cannot be found or read
  62.    */
  63.   public static URL getResourceURL(String resource) throws IOException {
  64.     // issue #625
  65.     return getResourceURL(null, resource);
  66.   }

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

  82.   /**
  83.    * Returns a resource on the classpath as a Stream object
  84.    *
  85.    * @param resource The resource to find
  86.    * @return The resource
  87.    * @throws java.io.IOException If the resource cannot be found or read
  88.    */
  89.   public static InputStream getResourceAsStream(String resource) throws IOException {
  90.     return getResourceAsStream(null, resource);
  91.   }

  92.   /**
  93.    * Returns a resource on the classpath as a Stream object
  94.    *
  95.    * @param loader   The classloader used to fetch the resource
  96.    * @param resource The resource to find
  97.    * @return The resource
  98.    * @throws java.io.IOException If the resource cannot be found or read
  99.    */
  100.   public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
  101.     InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
  102.     if (in == null) {
  103.       throw new IOException("Could not find resource " + resource);
  104.     }
  105.     return in;
  106.   }

  107.   /**
  108.    * Returns a resource on the classpath as a Properties object
  109.    *
  110.    * @param resource The resource to find
  111.    * @return The resource
  112.    * @throws java.io.IOException If the resource cannot be found or read
  113.    */
  114.   public static Properties getResourceAsProperties(String resource) throws IOException {
  115.     Properties props = new Properties();
  116.     try (InputStream in = getResourceAsStream(resource)) {
  117.       props.load(in);
  118.     }
  119.     return props;
  120.   }

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

  136.   /**
  137.    * Returns a resource on the classpath as a Reader object
  138.    *
  139.    * @param resource The resource to find
  140.    * @return The resource
  141.    * @throws java.io.IOException If the resource cannot be found or read
  142.    */
  143.   public static Reader getResourceAsReader(String resource) throws IOException {
  144.     Reader reader;
  145.     if (charset == null) {
  146.       reader = new InputStreamReader(getResourceAsStream(resource));
  147.     } else {
  148.       reader = new InputStreamReader(getResourceAsStream(resource), charset);
  149.     }
  150.     return reader;
  151.   }

  152.   /**
  153.    * Returns a resource on the classpath as a Reader object
  154.    *
  155.    * @param loader   The classloader used to fetch the resource
  156.    * @param resource The resource to find
  157.    * @return The resource
  158.    * @throws java.io.IOException If the resource cannot be found or read
  159.    */
  160.   public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
  161.     Reader reader;
  162.     if (charset == null) {
  163.       reader = new InputStreamReader(getResourceAsStream(loader, resource));
  164.     } else {
  165.       reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
  166.     }
  167.     return reader;
  168.   }

  169.   /**
  170.    * Returns a resource on the classpath as a File object
  171.    *
  172.    * @param resource The resource to find
  173.    * @return The resource
  174.    * @throws java.io.IOException If the resource cannot be found or read
  175.    */
  176.   public static File getResourceAsFile(String resource) throws IOException {
  177.     return new File(getResourceURL(resource).getFile());
  178.   }

  179.   /**
  180.    * Returns a resource on the classpath as a File object
  181.    *
  182.    * @param loader   - the classloader used to fetch the resource
  183.    * @param resource - the resource to find
  184.    * @return The resource
  185.    * @throws java.io.IOException If the resource cannot be found or read
  186.    */
  187.   public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
  188.     return new File(getResourceURL(loader, resource).getFile());
  189.   }

  190.   /**
  191.    * Gets a URL as an input stream
  192.    *
  193.    * @param urlString - the URL to get
  194.    * @return An input stream with the data from the URL
  195.    * @throws java.io.IOException If the resource cannot be found or read
  196.    */
  197.   public static InputStream getUrlAsStream(String urlString) throws IOException {
  198.     URL url = new URL(urlString);
  199.     URLConnection conn = url.openConnection();
  200.     return conn.getInputStream();
  201.   }

  202.   /**
  203.    * Gets a URL as a Reader
  204.    *
  205.    * @param urlString - the URL to get
  206.    * @return A Reader with the data from the URL
  207.    * @throws java.io.IOException If the resource cannot be found or read
  208.    */
  209.   public static Reader getUrlAsReader(String urlString) throws IOException {
  210.     Reader reader;
  211.     if (charset == null) {
  212.       reader = new InputStreamReader(getUrlAsStream(urlString));
  213.     } else {
  214.       reader = new InputStreamReader(getUrlAsStream(urlString), charset);
  215.     }
  216.     return reader;
  217.   }

  218.   /**
  219.    * Gets a URL as a Properties object
  220.    *
  221.    * @param urlString - the URL to get
  222.    * @return A Properties object with the data from the URL
  223.    * @throws java.io.IOException If the resource cannot be found or read
  224.    */
  225.   public static Properties getUrlAsProperties(String urlString) throws IOException {
  226.     Properties props = new Properties();
  227.     try (InputStream in = getUrlAsStream(urlString)) {
  228.       props.load(in);
  229.     }
  230.     return props;
  231.   }

  232.   /**
  233.    * Loads a class
  234.    *
  235.    * @param className - the class to fetch
  236.    * @return The loaded class
  237.    * @throws ClassNotFoundException If the class cannot be found (duh!)
  238.    */
  239.   public static Class<?> classForName(String className) throws ClassNotFoundException {
  240.     return classLoaderWrapper.classForName(className);
  241.   }

  242.   public static Charset getCharset() {
  243.     return charset;
  244.   }

  245.   public static void setCharset(Charset charset) {
  246.     Resources.charset = charset;
  247.   }

  248. }