001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.util;
019:
020: import java.io.*;
021: import java.net.URL;
022: import java.util.*;
023:
024: /**
025: * Util class that will read properties from the WEB-INF/classes/directory
026: * or by specifying a URL on the filesystem.
027: * Also has a helper method for creating a platform independent URL.
028: *
029: * @author Rudie Ekkelenkamp en Erik-Jan de Wit
030: * @created 07 aug 2002
031: * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
032: */
033: public class PropertyReader {
034:
035: /**
036: * Retrieve the properties specified by the fileName
037: * The property file should be in the WEB-INF/classess directory
038: * Suppose you need to get the properties in the
039: * web-inf/classes/config/application.properties ,
040: * you need to pass the propertyFile: config/application.properties
041: *
042: * @param propertyFile relative path to a properties file in the WEB-INF/classes directory
043: * @return a <code>Properties<code> object based on the input file
044: **/
045: public static Properties getProperties(String propertyFile) {
046: try {
047: URL url = getPropertiesURL(propertyFile);
048: return getProperties(url);
049: } catch (Exception e) {
050: System.out
051: .println("Error ocurred during properties retrieval");
052: System.out.println(e.getMessage());
053: return null;
054: }
055: }
056:
057: /**
058: * This method will return a platform independent URL to a file
059: * in the web-inf/classes direcotry.
060: *
061: * @param fileName relative path to a properties file in the WEB-INF/classes directory
062: * @return a platform independent URL to the xml file.
063: */
064: public static URL getPropertiesURL(String fileName) {
065: try {
066: System.out.println("Getting the properties URL");
067: URL url = null;
068: url = PropertyReader.class.getResource("/" + fileName);
069: String s = url.toString();
070: System.out.println("Filename of the properties file is: "
071: + s);
072: if (s.indexOf("file://") != -1) {
073: int indexOf = s.indexOf("file://") + 6;
074: String temp = s.substring(0, indexOf);
075: System.out.println("temp = " + temp
076: + " moet zijn file:/");
077: url = new URL(temp + "//" + s.substring(indexOf));
078: System.out.println("The url is now: " + url);
079: }
080: return url;
081: } catch (Exception e) {
082: System.out
083: .println("Error ocurred during properties retrieval");
084: System.out.println(e.getMessage());
085: return null;
086: }
087: }
088:
089: /**
090: * Retrieve the properties accesible through the specified URL
091: *
092: * @param url a reference to a properties file
093: * @return a properties file
094: **/
095: public static Properties getProperties(URL url) {
096: try {
097: Properties props = new Properties();
098: // Check for Solaris compatibility.
099: // A // in the file protocol won't be found in Solaris.
100: props.load(url.openStream());
101: System.out.println("Properties have been loaded: " + props);
102: return props;
103: } catch (Exception e) {
104: System.out
105: .println("Error ocurred during properties retrieval");
106: System.out.println(e.getMessage());
107: return null;
108: }
109: }
110: }
|