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.io.InputStreamReader; 021import java.net.URL; 022 023/** 024 * Resource encapsulates access to Resources via the Classloader. 025 * 026 * @author Michael J. Sikorsky 027 * @author Robert Shaw 028 */ 029 030// Contributed by ThoughtWorks Inc. 031 032public class Resource { 033 //-------------------------------------------------------------------------- 034 // Constants: 035 //-------------------------------------------------------------------------- 036 037 //-------------------------------------------------------------------------- 038 // Protected Variables: 039 //-------------------------------------------------------------------------- 040 protected String _name; 041 042 //-------------------------------------------------------------------------- 043 // Private Variables: 044 //-------------------------------------------------------------------------- 045 046 //-------------------------------------------------------------------------- 047 // Constructors: 048 //-------------------------------------------------------------------------- 049 050 /** 051 * Default, no argument constructor. 052 */ 053 public Resource() { 054 super(); 055 } 056 057 /** 058 * Construct a Resource given a name. 059 * 060 * @see #setName(String) 061 */ 062 public Resource(String name) { 063 _name = name; 064 } 065 066 //-------------------------------------------------------------------------- 067 // Public Methods: 068 //-------------------------------------------------------------------------- 069 070 /** 071 * Set the name of the resource. 072 * <p> 073 * A resource is some data (images, audio, text, etc) that can be accessed 074 * by class code in a way that is independent of the location of the code. 075 * </p> 076 * <p> 077 * The name of a resource is a "/"-separated path name that identifies 078 * the resource. 079 * </p> 080 * 081 * @see #getName() 082 */ 083 public void setName(String name) { 084 _name = name; 085 } 086 087 /** 088 * Get the name of the resource. Set setName() for a description of 089 * a resource. 090 * 091 * @see #setName 092 */ 093 public String getName() { 094 return (_name); 095 } 096 097 /** 098 * Get the InputStream for this Resource. Uses the classloader 099 * 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