1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.log4j.lf5.util; 18 19 import java.io.InputStream; 20 import java.io.InputStreamReader; 21 import java.net.URL; 22 23 /** 24 * Resource encapsulates access to Resources via the Classloader. 25 * 26 * @author Michael J. Sikorsky 27 * @author Robert Shaw 28 */ 29 30 // Contributed by ThoughtWorks Inc. 31 32 public class Resource { 33 //-------------------------------------------------------------------------- 34 // Constants: 35 //-------------------------------------------------------------------------- 36 37 //-------------------------------------------------------------------------- 38 // Protected Variables: 39 //-------------------------------------------------------------------------- 40 protected String _name; 41 42 //-------------------------------------------------------------------------- 43 // Private Variables: 44 //-------------------------------------------------------------------------- 45 46 //-------------------------------------------------------------------------- 47 // Constructors: 48 //-------------------------------------------------------------------------- 49 50 /** 51 * Default, no argument constructor. 52 */ 53 public Resource() { 54 super(); 55 } 56 57 /** 58 * Construct a Resource given a name. 59 * 60 * @see #setName(String) 61 */ 62 public Resource(String name) { 63 _name = name; 64 } 65 66 //-------------------------------------------------------------------------- 67 // Public Methods: 68 //-------------------------------------------------------------------------- 69 70 /** 71 * Set the name of the resource. 72 * <p> 73 * A resource is some data (images, audio, text, etc) that can be accessed 74 * by class code in a way that is independent of the location of the code. 75 * </p> 76 * <p> 77 * The name of a resource is a "/"-separated path name that identifies 78 * the resource. 79 * </p> 80 * 81 * @see #getName() 82 */ 83 public void setName(String name) { 84 _name = name; 85 } 86 87 /** 88 * Get the name of the resource. Set setName() for a description of 89 * a resource. 90 * 91 * @see #setName 92 */ 93 public String getName() { 94 return (_name); 95 } 96 97 /** 98 * Get the InputStream for this Resource. Uses the classloader 99 * from this Resource. 100 * 101 * @see #getInputStreamReader 102 * @see ResourceUtils 103 */ 104 public InputStream getInputStream() { 105 InputStream in = ResourceUtils.getResourceAsStream(this, this); 106 107 return (in); 108 } 109 110 /** 111 * Get the InputStreamReader for this Resource. Uses the classloader from 112 * this Resource. 113 * 114 * @see #getInputStream 115 * @see ResourceUtils 116 */ 117 public InputStreamReader getInputStreamReader() { 118 InputStream in = ResourceUtils.getResourceAsStream(this, this); 119 120 if (in == null) { 121 return null; 122 } 123 124 InputStreamReader reader = new InputStreamReader(in); 125 126 return reader; 127 } 128 129 /** 130 * Get the URL of the Resource. Uses the classloader from this Resource. 131 * 132 * @see ResourceUtils 133 */ 134 public URL getURL() { 135 return (ResourceUtils.getResourceAsURL(this, this)); 136 } 137 138 //-------------------------------------------------------------------------- 139 // Protected Methods: 140 //-------------------------------------------------------------------------- 141 142 //-------------------------------------------------------------------------- 143 // Private Methods: 144 //-------------------------------------------------------------------------- 145 146 //-------------------------------------------------------------------------- 147 // Nested Top-Level Classes or Interfaces: 148 //-------------------------------------------------------------------------- 149 150 } 151 152 153 154 155 156