001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.util;
018
019import java.io.InputStream;
020import java.net.URL;
021
022/**
023 * ResourceUtils.  Provide a set of convenience methods for working with
024 * Resources.
025 *
026 * @see org.apache.log4j.lf5.util.Resource
027 *
028 * @author Michael J. Sikorsky
029 * @author Robert Shaw
030 */
031
032// Contributed by ThoughtWorks Inc.
033
034public class ResourceUtils {
035  //--------------------------------------------------------------------------
036  //   Constants:
037  //--------------------------------------------------------------------------
038
039  //--------------------------------------------------------------------------
040  //   Protected Variables:
041  //--------------------------------------------------------------------------
042
043  //--------------------------------------------------------------------------
044  //   Private Variables:
045  //--------------------------------------------------------------------------
046
047  //--------------------------------------------------------------------------
048  //   Constructors:
049  //--------------------------------------------------------------------------
050
051  //--------------------------------------------------------------------------
052  //   Public Methods:
053  //--------------------------------------------------------------------------
054
055  /**
056   * Get the InputStream for this resource.  Note: to convert an InputStream
057   * into an InputReader, use: new InputStreamReader(InputStream).
058   *
059   * @param object   The object to grab the Classloader from.
060   *                 This parameter is quite important from a
061   *                 visibility of resources standpoint as the
062   *                 hierarchy of Classloaders plays a role.
063   *
064   * @param resource The resource to load.
065   *
066   * @return If the Resource was found, the InputStream, otherwise null.
067   *
068   * @see Resource
069   * @see #getResourceAsURL(Object,Resource)
070   * @see InputStream
071   */
072  public static InputStream getResourceAsStream(Object object, Resource resource) {
073    ClassLoader loader = object.getClass().getClassLoader();
074
075    InputStream in = null;
076
077    if (loader != null) {
078      in = loader.getResourceAsStream(resource.getName());
079    } else {
080      in = ClassLoader.getSystemResourceAsStream(resource.getName());
081    }
082
083    return in;
084  }
085
086  /**
087   * Get the URL for this resource.
088   *
089   * @param object   The object to grab the Classloader from.
090   *                 This parameter is quite important from a
091   *                 visibility of resources standpoint as the
092   *                 hierarchy of Classloaders plays a role.
093   *
094   * @param resource The resource to load.
095   *
096   * @return If the Resource was found, the URL, otherwise null.
097   *
098   * @see Resource
099   * @see #getResourceAsStream(Object,Resource)
100   */
101  public static URL getResourceAsURL(Object object, Resource resource) {
102    ClassLoader loader = object.getClass().getClassLoader();
103
104    URL url = null;
105
106    if (loader != null) {
107      url = loader.getResource(resource.getName());
108    } else {
109      url = ClassLoader.getSystemResource(resource.getName());
110    }
111
112    return (url);
113  }
114
115  //--------------------------------------------------------------------------
116  //   Protected Methods:
117  //--------------------------------------------------------------------------
118
119  //--------------------------------------------------------------------------
120  //   Private Methods:
121  //--------------------------------------------------------------------------
122
123  //--------------------------------------------------------------------------
124  //   Nested Top-Level Classes or Interfaces:
125  //--------------------------------------------------------------------------
126
127}
128
129
130
131
132
133