001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.framework;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.util.Properties;
027: import java.util.StringTokenizer;
028:
029: /**
030: *
031: * @author Herkules
032: */
033: public class PropertiesHandler {
034: private final static String DBPROPSDIR = ".bdbench";
035: private final String mFilename;
036: private Properties mProperties;
037:
038: /**
039: * Creates a new instance of BenchmarkSettings.
040: */
041: public PropertiesHandler(String propertiesname) {
042: mFilename = propertiesname;
043: load();
044: }
045:
046: private final String getSettingsDirectoryName() {
047: String home = System.getProperty("user.home", ".");
048: return home + File.separator + DBPROPSDIR;
049: }
050:
051: private final String getSettingsFilename() {
052:
053: String fileName = mFilename;
054:
055: File file = new File(fileName);
056: if (file.exists()) {
057: String path = file.getAbsolutePath();
058: reportSettingsFile(path);
059: return path;
060: }
061: fileName = getSettingsDirectoryName() + File.separator
062: + fileName;
063: reportSettingsFile(fileName);
064: return fileName;
065: }
066:
067: private void reportSettingsFile(String path) {
068: System.out.println("\nUsing settings file:");
069: System.out.println(path + "\n");
070: }
071:
072: /**
073: * Load default and custom settings.
074: */
075: public boolean load() {
076: try {
077: mProperties = new Properties();
078: File file = new File(mFilename);
079: if (file.exists()) {
080: mProperties.load(new FileInputStream(file));
081: } else {
082: mProperties.load(PropertiesHandler.class
083: .getClassLoader()
084: .getResourceAsStream(mFilename));
085: }
086: } catch (IOException ioex) {
087: Log.logger.warning("Cannot load default properties.");
088: return false;
089: }
090:
091: try {
092: FileInputStream in = new FileInputStream(
093: getSettingsFilename());
094: mProperties.load(in);
095: } catch (IOException ioex) {
096: // no custom file present .... thats ok.
097: Log.logger
098: .info("No custom properties found. Using defaults.");
099:
100: // create the missing file
101: save();
102: }
103:
104: return true;
105: }
106:
107: /**
108: * Persist the custom settings.
109: */
110: public boolean save() {
111: try {
112: File dir = new File(getSettingsDirectoryName());
113: dir.mkdir();
114: FileOutputStream out = new FileOutputStream(
115: getSettingsFilename());
116: mProperties.store(out, "DB benchmark settings");
117: } catch (IOException ioex) {
118: Log.logger.warning("Cannot save custom settings.");
119: return false;
120: }
121: return true;
122: }
123:
124: /**
125: * same as <code>Properties#getProperty()</code>
126: */
127: public String get(String key) {
128: return mProperties.getProperty(key);
129: }
130:
131: /**
132: * same as <code>Properties#getProperty()</code>
133: */
134: public String get(String key, String defaultValue) {
135: return mProperties.getProperty(key, defaultValue);
136: }
137:
138: /**
139: * same as <code>Properties#put()</code>
140: */
141: public void put(String key, String value) {
142: mProperties.put(key, value);
143: }
144:
145: /**
146: * retrieve an array that might be formatted like 1,2,3 or [1,2,3] or 1 2 3 or 1;2;3 ...
147: */
148: public String[] getArray(String key) {
149:
150: try {
151: String s = get(key);
152:
153: StringTokenizer tokenizer = new StringTokenizer(s,
154: "[ \t,;]");
155: int len = tokenizer.countTokens();
156: String[] res = new String[len];
157: for (int i = 0; i < len; i++) {
158: res[i] = tokenizer.nextToken();
159: }
160:
161: return res;
162: } catch (Exception e) {
163: System.out.println("Key not available in " + mFilename
164: + ":\n" + key + "\n");
165: }
166: return null;
167: }
168:
169: /**
170: * retrieve an array that might be formatted like 1,2,3 or [1,2,3] or 1 2 3 or 1;2;3 ...
171: */
172: public int[] getIntArray(String key) {
173: String s = get(key);
174:
175: StringTokenizer tokenizer = new StringTokenizer(s, "[ \t,;]");
176: int len = tokenizer.countTokens();
177: int[] res = new int[len];
178: for (int i = 0; i < len; i++) {
179: res[i] = Integer.parseInt(tokenizer.nextToken());
180: }
181:
182: return res;
183: }
184:
185: /**
186: * retrieve a flag
187: */
188: public boolean getBoolean(String key) {
189: String val = mProperties.getProperty(key);
190: return Boolean.valueOf(val).booleanValue();
191: }
192:
193: /**
194: * Retrieve the properties object handled here.
195: */
196: public Properties getProperties() {
197: return mProperties;
198: }
199: }
|