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.cp;
014
015import java.io.*;
016import java.util.*;
017
018/**
019 * Interface for finding classpath resources.
020 *
021 * <p>
022 * Essentially a wrapper around {@link Class#getResourceAsStream(String)}, but with support for looking up resources
023 * with localized names (e.g. <js>"myfile_ja_JP.txt"</js>).
024 *
025 * <p>
026 * The following predefined implementations are provided:
027 * <ul>
028 *    <li>{@link SimpleResourceFinder} - Simple searching of classpath.
029 *    <li>{@link BasicResourceFinder} - Same as above, but looks in local JVM working directory if resource
030 *       can't be found on classpath.
031 *    <li>{@link RecursiveResourceFinder} - Same as above, except if the resource can't be found on the
032 *       classpath relative to the base class, recursively searches up the parent class hierarchy.
033 * </ul>
034 */
035public interface ResourceFinder {
036
037   /**
038    * Represents "no" classpath resource finder.
039    */
040   public static final class Null implements ResourceFinder {
041      @Override
042      public InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException {
043         return null;
044      }
045   }
046
047   /**
048    * Returns the contents of the resource with the specified name.
049    *
050    * @param baseClass
051    *    The class to use to retrieve the resource.
052    * @param name The resource name.
053    *    See {@link Class#getResource(String)} for format.
054    * @param locale
055    *    The locale of the resource to retrieve.
056    *    <br>If <jk>null</jk>, won't look for localized file names.
057    * @return The resolved resource contents, or <jk>null</jk> if the resource was not found.
058    * @throws IOException Thrown by underlying stream.
059    */
060   InputStream findResource(Class<?> baseClass, String name, Locale locale) throws IOException;
061}