001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015
016import java.io.*;
017import java.util.*;
018
019/**
020 * Interface for finding classpath resources.
021 *
022 * <p>
023 * Essentially a wrapper around {@link Class#getResourceAsStream(String)}, but with support for looking up resources
024 * with localized names (e.g. <js>"myfile_ja_JP.txt"</js>).
025 *
026 * <p>
027 * The following predefined implementations are provided:
028 * <ul>
029 *    <li>{@link ClasspathResourceFinderSimple} - Simple searching of classpath.
030 *    <li>{@link ClasspathResourceFinderBasic} - Same as above, but looks in local JVM working directory if resource
031 *       can't be found on classpath.
032 *    <li>{@link ClasspathResourceFinderRecursive} - Same as above, except if the resource can't be found on the
033 *       classpath relative to the base class, recursively searches up the parent class hierarchy.
034 * </ul>
035 */
036public interface ClasspathResourceFinder {
037
038   /**
039    * Represents "no" classpath resource finder.
040    */
041   public static final class Null implements ClasspathResourceFinder {
042      @Override
043      public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException {
044         throw new NoSuchMethodError();
045      }
046   }
047
048   /**
049    * Returns the contents of the resource with the specified name.
050    *
051    * @param baseClass
052    *    The class to use to retrieve the resource.
053    * @param name The resource name.
054    *    See {@link Class#getResource(String)} for format.
055    * @param locale
056    *    The locale of the resource to retrieve.
057    *    <br>If <jk>null</jk>, won't look for localized file names.
058    * @return The resolved resource contents, or <jk>null</jk> if the resource was not found.
059    * @throws IOException Thrown by underlying stream.
060    */
061   InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException;
062}