001 // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org)
002 // The Molly framework is freely distributable under the terms of an
003 // MIT-style license. For details, see the molly pages web site at:
004 // http://www.mollypages.org/. Use, modify, have fun !
005
006 package fc.util;
007
008 import fc.util.*;
009 import fc.io.*;
010 import java.io.*;
011 import java.util.*;
012 import java.util.logging.*;
013
014 /**
015 Loads properties from a file. Trims whitespace from
016 property values, in contrast to ({@link java.util.Property} does not trim whitespace from
017 values, so "x = y " will return "y ", which is hard to
018 debug).
019 <p>
020 ThreadSafety: This class <b>is</b> thread safe and can be used by multiple
021 threads concurrently.
022
023 @author hursh jain
024 @version 1.1 5/31/2002
025 */
026 public class FilePropertyMgr extends PropertyMgr
027 {
028 String myname = getClass().getName();
029
030 Properties props;
031 File propertyFile;
032
033 public FilePropertyMgr(File propertyFile)
034 throws IOException
035 {
036 props = new Properties();
037 this.propertyFile = propertyFile;
038 try {
039 props.load(new BufferedInputStream(
040 new FileInputStream(propertyFile)));
041 log.info(myname, "; Loaded properties from file: ", propertyFile);
042 log.bug(IOUtil.propertiesToString(props));
043 }
044 catch (IOException e) {
045 log.error("Cannot open file: ", propertyFile.getAbsolutePath());
046 log.error("*** PropertyMgr will not be available ***", e);
047 throw e;
048 }
049 }
050
051 public String get(String name)
052 {
053 final String prop = props.getProperty(name);
054 if (prop != null) {
055 return prop.trim();
056 }
057 return prop;
058 }
059
060 public String set(String name, String value)
061 {
062 Object obj = props.setProperty(name, value);
063 return (String) obj;
064 }
065
066 public void save() throws IOException
067 {
068 String header = "Properties saved on: " + new Date();
069 props.store( new FileOutputStream(propertyFile), header );
070 }
071
072
073 /**
074 Returns a description but the exact details of said description
075 are unspecified and subject to change. However, the following
076 may be regarded as typical:
077 <tt>
078 [getClass().getName()] contains the following properties: <br>
079 [List of properties]
080 </tt>
081 **/
082 public String toString()
083 {
084 String temp = myname + "; contains the following properties: " + IOUtil.LINE_SEP;
085 temp += IOUtil.propertiesToString(props);
086 temp += "------------------------";
087 return temp;
088 }
089
090 public static void main(String[] args)
091 {
092 new Test();
093 }
094
095 static private class Test
096 {
097 Test()
098 {
099 final String filename = "./FilePropertyMgr_test.txt";
100 try {
101
102 class FooFilePropertyMgr extends FilePropertyMgr
103 {
104 public FooFilePropertyMgr(String filename) throws Exception {
105 super(new File(filename));
106 }
107
108 protected void handleError(String msg) {
109 System.out.println("Error: " + msg);
110 }
111 }
112
113 FilePropertyMgr fp = new FooFilePropertyMgr(filename);
114 System.out.println("got required property 'foo' = " + fp.getRequired("foo"));
115 System.out.println("got required property 'abc' = " + fp.getRequired("abc"));
116 System.out.println("got required property 'x.y' = " + fp.getRequired("x.y"));
117 System.out.println("got required property 'num' = " + fp.getRequiredInt("num"));
118 System.out.println("got required property 'bool' = " + fp.getRequiredBoolean("bool"));
119
120 System.out.println("got property 'num2' = " + fp.getInt("num2", 123));
121 System.out.println("got property 'bool2' = " + fp.getBoolean("bool2", false));
122
123 System.out.println("Getting bad format properties...");
124 System.out.println("got required property 'num3' = " + fp.getRequiredInt("num3"));
125 System.out.println("got required property 'bool3' = " + fp.getRequiredBoolean("bool3"));
126 System.out.println("got property 'num3' = " + fp.getInt("num3", 123));
127 System.out.println("got property 'bool3' = " + fp.getBoolean("bool3", false));
128
129 System.out.println("now calling the FilePropertyMgr.toString()");
130 System.out.println(fp.toString());
131
132 System.out.println("The following should exit the test");
133 fp = new FilePropertyMgr(new File(filename));
134 System.out.println("got property 'test.don'texist' = " + fp.getRequired("test.don'texist"));
135 }
136 catch (Exception e) {
137 e.printStackTrace();
138 }
139 }
140
141 } //~end Test
142
143
144 } //~class FilePropertyMgr