Source code: org/hsqldb/HsqlProperties.java
1 /* Copyright (c) 2001-2002, The HSQL Development Group
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the HSQL Development Group nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 package org.hsqldb;
33
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.FileOutputStream;
37 import java.util.Enumeration;
38 import java.util.Properties;
39
40 /**
41 * Wrapper for java.util.Properties to limit values to String objects and
42 * allow saving and loading.
43 *
44 * @author fredt@users
45 * @verison 1.7.0
46 */
47 public class HsqlProperties {
48
49 protected String fileName;
50 protected Properties stringProps;;
51
52 public HsqlProperties() {
53 stringProps = new Properties();
54 fileName = null;
55 }
56
57 public HsqlProperties(String name) {
58 stringProps = new Properties();
59 fileName = name;
60 }
61
62 public HsqlProperties(Properties props) {
63 stringProps = props;
64 }
65
66 public void setFileName(String name) {
67 fileName = name;
68 }
69
70 public String setProperty(String key, int value) {
71 return (String) stringProps.put(key, Integer.toString(value));
72 }
73
74 public String setProperty(String key, boolean value) {
75 return (String) stringProps.put(key, String.valueOf(value));
76 }
77
78 public String setProperty(String key, String value) {
79 return (String) stringProps.put(key, value);
80 }
81
82 public String setPropertyIfNotExists(String key, String value) {
83
84 value = stringProps.getProperty(key, value);
85
86 return (String) stringProps.put(key, value);
87 }
88
89 public String getProperty(String key) {
90 return stringProps.getProperty(key);
91 }
92
93 public String getProperty(String key, String defaultValue) {
94 return stringProps.getProperty(key, defaultValue);
95 }
96
97 public int getIntegerProperty(String key, int defaultValue) {
98
99 String prop = getProperty(key);
100
101 try {
102 defaultValue = Integer.parseInt(prop);
103 } catch (NumberFormatException e) {}
104
105 return defaultValue;
106 }
107
108 public boolean isPropertyTrue(String key) {
109 return isPropertyTrue(key, false);
110 }
111
112 public boolean isPropertyTrue(String key, boolean defaultValue) {
113
114 String value = stringProps.getProperty(key, defaultValue ? "true"
115 : "false");
116
117 return Boolean.valueOf(value).booleanValue();
118 }
119
120 public void removeProperty(String key) {
121 stringProps.remove(key);
122 }
123
124 public static HsqlProperties argArrayToProps(String[] arg, String type) {
125
126 HsqlProperties props = new HsqlProperties();
127
128 for (int i = 0; i < arg.length - 1; i++) {
129 String p = arg[i];
130
131 if ((p.charAt(0) == '-') && (!p.startsWith("-?"))) {
132 props.setProperty(type + "." + p.substring(1), arg[i + 1]);
133
134 i++;
135 }
136 }
137
138 return props;
139 }
140
141 public void addProperties(HsqlProperties props) {
142
143 Enumeration keys = props.stringProps.propertyNames();
144
145 for (; keys.hasMoreElements(); ) {
146 Object key = keys.nextElement();
147
148 this.stringProps.put(key, props.stringProps.get(key));
149 }
150 }
151
152 public boolean checkFileExists() {
153
154 if (fileName == null || fileName.length() == 0) {
155 return false;
156 }
157
158 return new File(fileName + ".properties").exists();
159 }
160
161 public void load() throws Exception {
162
163 if (fileName == null || fileName.length() == 0) {
164 throw new java.io.FileNotFoundException(
165 "properties name is null or empty");
166 }
167
168 FileInputStream fis = null;
169
170 try {
171 File f = new File(fileName + ".properties");
172
173 fis = new FileInputStream(f);
174
175 stringProps.load(fis);
176 fis.close();
177 } finally {
178 if (fis != null) {
179 fis.close();
180 }
181 }
182 }
183
184 /**
185 * Method declaration
186 *
187 * @throws SQLException
188 */
189 public void save() throws Exception {
190
191 if (fileName == null || fileName.length() == 0) {
192 throw new java.io.FileNotFoundException(
193 "properties name is null or empty");
194 }
195
196 File f = new File(fileName + ".properties");
197
198 //#ifdef JAVA2
199 File parent = f.getParentFile();
200
201 if (parent != null) {
202 parent.mkdirs();
203 }
204
205 //#endif JAVA2
206 FileOutputStream fos = new FileOutputStream(f);
207
208 //#ifdef JAVA2
209 stringProps.store(fos, "HSQL database");
210
211 //#else
212 /*
213 stringProps.save(fos,"HSQL database");
214
215
216
217 */
218
219 //#endif JAVA2
220 fos.close();
221 }
222 }