001 /* 002 * Copyright 2001,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.scaffold.util; 018 019 020 import java.util.Properties; 021 import javax.servlet.UnavailableException; 022 023 024 /** 025 * Property and Resource utilities. 026 * 027 * @author Ted Husted 028 * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $ 029 */ 030 public class ResourceUtils { 031 032 /** 033 * Message to log if resource is not found. 034 */ 035 private static final String RESOURCE_NOT_FOUND = 036 "Resources not found"; 037 038 039 /** 040 * Load a properties file. 041 * The path must include the package name(s), 042 * if any, in system directory format and 043 * include the properties extension. 044 * 045 * @param caller The object making the call. Needed to access ClassLoader. 046 * @param path The path to the properties file 047 * @return The initialized Properties object 048 * @throws UnavailableException if resource cannot be found 049 */ 050 public static final Properties loadProperties(Object caller, 051 String path) throws Exception { 052 053 // name = name.replace('.', '/'); 054 // if doesn't end in .properties 055 // name += ".properties"; 056 057 java.io.InputStream is = caller.getClass().getClassLoader().getResourceAsStream(path); 058 if (null==is) 059 throw new UnavailableException(RESOURCE_NOT_FOUND); 060 061 java.io.BufferedInputStream bis = new java.io.BufferedInputStream(is); 062 Properties p = null; 063 064 if (bis != null) { 065 066 try { 067 p = new Properties(); 068 p.load(bis); 069 bis.close(); 070 is.close(); 071 } 072 073 catch (java.io.IOException e) { 074 p = null; 075 } 076 077 finally { 078 is = null; 079 bis = null; 080 } 081 } 082 083 return p; 084 085 } 086 087 088 public static final Properties loadProperties (String path) throws Exception { 089 return loadProperties (new Object(),path); 090 } 091 092 093 /** 094 * Load a properties file. 095 * This is a simplified version of <code>loadProperties(Object, String)</code>. 096 * 097 * @param caller The object making the call. Needed to access ClassLoader. 098 * @param path The path to the properties file 099 * @return The initialized Properties object 100 */ 101 public static final Properties loadPropertiesFile(String path) { 102 103 Properties p = null; 104 105 try { 106 107 java.io.FileInputStream i = new java.io.FileInputStream(path); 108 p = new java.util.Properties(); 109 p.load(i); 110 111 } 112 catch (Exception e) { 113 114 p = null; 115 116 } 117 return p; 118 119 } 120 }