KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > util > PropertiesFileHandler


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.util;
20
21 import java.io.*;
22 import java.util.Properties JavaDoc;
23
24 import cowsultants.itracker.ejb.client.util.Logger;
25
26 public class PropertiesFileHandler {
27     private Properties JavaDoc props;
28
29     public PropertiesFileHandler() {
30         props = new Properties JavaDoc();
31     }
32
33     public PropertiesFileHandler(String JavaDoc resource) {
34         this();
35         addProperties(resource);
36     }
37
38     public void addProperties(String JavaDoc resource) {
39         if(resource == null || resource.equals("") || ! resource.endsWith(".properties")) {
40             return;
41         }
42
43         try {
44             InputStream is = getClass().getResourceAsStream(resource);
45             if(is != null) {
46                 props.load(is);
47             } else {
48                 Logger.logDebug("No properties resource, " + resource + " was found.");
49             }
50         } catch(IOException ioe) {
51             Logger.logWarn("Could not load properties resource: " + resource, ioe);
52         }
53     }
54
55     public Properties JavaDoc getProperties() {
56         return (Properties JavaDoc) props.clone();
57     }
58
59     public String JavaDoc getProperty(String JavaDoc name) {
60         return props.getProperty(name);
61     }
62
63     public boolean hasProperty(String JavaDoc name) {
64         return props.containsKey(name);
65     }
66
67     public boolean hasProperties() {
68         return (props.size() > 0 ? true : false);
69     }
70 }
Popular Tags