001: /**
002: * com.mckoi.database.control.DefaultDBConfig 29 Mar 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.control;
024:
025: import java.io.File;
026: import java.io.InputStream;
027: import java.io.BufferedInputStream;
028: import java.io.IOException;
029: import java.io.FileInputStream;
030: import java.net.URL;
031: import java.net.URLConnection;
032: import java.util.Hashtable;
033: import java.util.Properties;
034: import java.util.Enumeration;
035:
036: /**
037: * Implements a default database configuration that is useful for setting up
038: * a database. This configuration object is mutable. Configuration properties
039: * can be set by calling the 'setxxx' methods.
040: *
041: * @author Tobias Downer
042: */
043:
044: public class DefaultDBConfig extends AbstractDBConfig {
045:
046: /**
047: * Constructs the configuration.
048: *
049: * @param the current path of the configuration in the file system. This is
050: * useful if the configuration is based on a file with relative paths set
051: * in it.
052: */
053: public DefaultDBConfig(File current_path) {
054: super (current_path);
055: }
056:
057: /**
058: * Constructs the configuration with the current system path as the
059: * configuration path.
060: */
061: public DefaultDBConfig() {
062: this (new File("."));
063: }
064:
065: /**
066: * Gets the default value for the given property value.
067: */
068: protected String getDefaultValue(String property_key) {
069: ConfigProperty property = (ConfigProperty) CONFIG_DEFAULTS
070: .get(property_key);
071: if (property == null) {
072: return null;
073: } else {
074: return property.getDefaultValue();
075: }
076: }
077:
078: /**
079: * Overwrites the configuration key with the given value.
080: */
081: public void setValue(String property_key, String value) {
082: super .setValue(property_key, value);
083: }
084:
085: /**
086: * Loads all the configuration values from the given InputStream. The
087: * input stream must be formatted in a standard properties format.
088: */
089: public void loadFromStream(InputStream input) throws IOException {
090: Properties config = new Properties();
091: config.load(new BufferedInputStream(input));
092: // For each property in the file
093: Enumeration enum = config.propertyNames();
094: while (enum.hasMoreElements()) {
095: // Set the property value in this configuration.
096: String property_key = (String) enum.nextElement();
097: setValue(property_key, config.getProperty(property_key));
098: }
099: }
100:
101: /**
102: * Loads all the configuration settings from a configuration file. Useful if
103: * you want to load a default configuration from a 'db.conf' file. The
104: * file must be formatted in a standard properties format.
105: */
106: public void loadFromFile(File configuration_file)
107: throws IOException {
108: FileInputStream file_in = new FileInputStream(
109: configuration_file);
110: loadFromStream(file_in);
111: file_in.close();
112: }
113:
114: /**
115: * Loads all the configuration values from the given URL. The file must be
116: * formatted in a standard properties format.
117: */
118: public void loadFromURL(URL configuration_url) throws IOException {
119: InputStream url_in = configuration_url.openConnection()
120: .getInputStream();
121: loadFromStream(url_in);
122: url_in.close();
123: }
124:
125: // ---------- Variable helper setters ----------
126:
127: /**
128: * Sets the path of the database.
129: */
130: public void setDatabasePath(String path) {
131: setValue("database_path", path);
132: }
133:
134: /**
135: * Sets the path of the log.
136: */
137: public void setLogPath(String path) {
138: setValue("log_path", path);
139: }
140:
141: /**
142: * Sets that the engine ignores case for identifiers.
143: */
144: public void setIgnoreIdentifierCase(boolean status) {
145: setValue("ignore_case_for_identifiers", status ? "enabled"
146: : "disabled");
147: }
148:
149: /**
150: * Sets that the database is read only.
151: */
152: public void setReadOnly(boolean status) {
153: setValue("read_only", status ? "enabled" : "disabled");
154: }
155:
156: /**
157: * Sets the minimum debug level for output to the debug log file.
158: */
159: public void setMinimumDebugLevel(int debug_level) {
160: setValue("debug_level", "" + debug_level);
161: }
162:
163: // ---------- Statics ----------
164:
165: /**
166: * A Hashtable of default configuration values. This maps from property_key
167: * to ConfigProperty object that describes the property.
168: */
169: private static Hashtable CONFIG_DEFAULTS = new Hashtable();
170:
171: /**
172: * Adds a default property to the CONFIG_DEFAULTS map.
173: */
174: private static void addDefProperty(ConfigProperty property) {
175: CONFIG_DEFAULTS.put(property.getKey(), property);
176: }
177:
178: /**
179: * Sets up the CONFIG_DEFAULTS map with default configuration values.
180: */
181: static {
182: addDefProperty(new ConfigProperty("database_path", "./data",
183: "PATH"));
184:
185: // addDefProperty(new ConfigProperty("log_path", "./log", "PATH"));
186:
187: addDefProperty(new ConfigProperty("root_path", "jvm", "STRING"));
188:
189: addDefProperty(new ConfigProperty("jdbc_server_port", "9157",
190: "STRING"));
191:
192: addDefProperty(new ConfigProperty(
193: "ignore_case_for_identifiers", "disabled", "BOOLEAN"));
194:
195: addDefProperty(new ConfigProperty("regex_library",
196: "gnu.regexp", "STRING"));
197:
198: addDefProperty(new ConfigProperty("data_cache_size", "4194304",
199: "INT"));
200:
201: addDefProperty(new ConfigProperty("max_cache_entry_size",
202: "8192", "INT"));
203:
204: addDefProperty(new ConfigProperty("lookup_comparison_list",
205: "enabled", "BOOLEAN"));
206:
207: addDefProperty(new ConfigProperty("maximum_worker_threads",
208: "4", "INT"));
209:
210: addDefProperty(new ConfigProperty("dont_synch_filesystem",
211: "disabled", "BOOLEAN"));
212:
213: addDefProperty(new ConfigProperty(
214: "transaction_error_on_dirty_select", "enabled",
215: "BOOLEAN"));
216:
217: addDefProperty(new ConfigProperty("read_only", "disabled",
218: "BOOLEAN"));
219:
220: addDefProperty(new ConfigProperty("debug_log_file",
221: "debug.log", "FILE"));
222:
223: addDefProperty(new ConfigProperty("debug_level", "20", "INT"));
224:
225: addDefProperty(new ConfigProperty("table_lock_check",
226: "enabled", "BOOLEAN"));
227:
228: }
229:
230: // ---------- Inner classes ----------
231:
232: /**
233: * An object the describes a single configuration property and the default
234: * value for it.
235: */
236: private static class ConfigProperty {
237:
238: private String key;
239: private String default_value;
240: private String type;
241: private String comment;
242:
243: ConfigProperty(String key, String default_value, String type,
244: String comment) {
245: this .key = key;
246: this .default_value = default_value;
247: this .type = type;
248: this .comment = comment;
249: }
250:
251: ConfigProperty(String key, String default_value, String type) {
252: this (key, default_value, type, null);
253: }
254:
255: String getKey() {
256: return key;
257: }
258:
259: String getDefaultValue() {
260: return default_value;
261: }
262:
263: String getType() {
264: return type;
265: }
266:
267: String getComment() {
268: return comment;
269: }
270:
271: }
272:
273: }
|