KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > util > PropertyReader


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.util;
19
20 import java.io.*;
21 import java.net.URL JavaDoc;
22 import java.util.*;
23
24 /**
25  * Util class that will read properties from the WEB-INF/classes/directory
26  * or by specifying a URL on the filesystem.
27  * Also has a helper method for creating a platform independent URL.
28  *
29  * @author Rudie Ekkelenkamp en Erik-Jan de Wit
30  * @created 07 aug 2002
31  * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
32  */

33 public class PropertyReader {
34
35    /**
36     * Retrieve the properties specified by the fileName
37     * The property file should be in the WEB-INF/classess directory
38     * Suppose you need to get the properties in the
39     * web-inf/classes/config/application.properties ,
40     * you need to pass the propertyFile: config/application.properties
41     *
42     * @param propertyFile relative path to a properties file in the WEB-INF/classes directory
43     * @return a <code>Properties<code> object based on the input file
44     **/

45    public static Properties getProperties(String JavaDoc propertyFile) {
46       try {
47          URL JavaDoc url = getPropertiesURL(propertyFile);
48          return getProperties(url);
49       }
50       catch (Exception JavaDoc e) {
51          System.out.println("Error ocurred during properties retrieval");
52          System.out.println(e.getMessage());
53          return null;
54       }
55    }
56
57    /**
58     * This method will return a platform independent URL to a file
59     * in the web-inf/classes direcotry.
60     *
61     * @param fileName relative path to a properties file in the WEB-INF/classes directory
62     * @return a platform independent URL to the xml file.
63     */

64    public static URL JavaDoc getPropertiesURL(String JavaDoc fileName) {
65       try {
66          System.out.println("Getting the properties URL");
67          URL JavaDoc url = null;
68          url = PropertyReader.class.getResource("/" + fileName);
69          String JavaDoc s = url.toString();
70          System.out.println("Filename of the properties file is: " + s);
71          if (s.indexOf("file://") != -1) {
72             int indexOf = s.indexOf("file://") + 6;
73             String JavaDoc temp = s.substring(0, indexOf);
74             System.out.println("temp = " + temp + " moet zijn file:/");
75             url = new URL JavaDoc(temp + "//" + s.substring(indexOf));
76             System.out.println("The url is now: " + url);
77          }
78          return url;
79       }
80       catch (Exception JavaDoc e) {
81          System.out.println("Error ocurred during properties retrieval");
82          System.out.println(e.getMessage());
83          return null;
84       }
85    }
86
87    /**
88     * Retrieve the properties accesible through the specified URL
89     *
90     * @param url a reference to a properties file
91     * @return a properties file
92     **/

93    public static Properties getProperties(URL JavaDoc url) {
94       try {
95          Properties props = new Properties();
96          // Check for Solaris compatibility.
97
// A // in the file protocol won't be found in Solaris.
98
props.load(url.openStream());
99          System.out.println("Properties have been loaded: " + props);
100          return props;
101       }
102       catch (Exception JavaDoc e) {
103          System.out.println("Error ocurred during properties retrieval");
104          System.out.println(e.getMessage());
105          return null;
106       }
107    }
108 }
109
Popular Tags