001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: EnhProperties.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.util;
010:
011: import java.io.*;
012: import java.util.*;
013: import org.ozoneDB.DxLib.*;
014: import org.ozoneDB.core.Env;
015:
016: /**
017: * EnhProperties has methods to store/update the value of a property to handle
018: * such dynamic properties.
019: * <p>
020: * In addition the Properties EnhProperties can hold not only String properties
021: * but most of other primitive types and raw objects. Non-string properties are
022: * internaly stored as Strings.
023: * <p>
024: * Setup extends java.util.Properties. So the system properties can be used as
025: * defaults.
026: *
027: *
028: * @author <a href="http://www.softwarebuero.de/">SMB</a>
029: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $
030: */
031: public class EnhProperties extends Properties {
032:
033: /**
034: * An Observable object that is responsible for this properties. Directly
035: * extending the class is not possible because it already extends Properties.
036: */
037: protected EnhObservable observable;
038:
039: public EnhProperties() {
040: observable = new EnhObservable();
041: }
042:
043: public EnhProperties(Properties _defaults) {
044: super (_defaults);
045: observable = new EnhObservable();
046: }
047:
048: public void addObserver(Observer _observer) {
049: observable.addObserver(_observer);
050: }
051:
052: public void removeObserver(Observer _observer) {
053: observable.deleteObserver(_observer);
054: }
055:
056: public void notifyObservers() {
057: observable.notifyObservers(this );
058: }
059:
060: public boolean hasChanged() {
061: return observable.hasChanged();
062: }
063:
064: /**
065: * @param properties
066: * @param keyPrefix
067: */
068: public void addProperties(Properties properties, String keyPrefix) {
069: observable.setChanged();
070: for (Enumeration e = properties.keys(); e.hasMoreElements();) {
071: String key = (String) e.nextElement();
072: if (key.startsWith(keyPrefix)) {
073: // System.out.println (key);
074: String val = properties.getProperty(key, "");
075: setStringProperty(key, val);
076: }
077: }
078: }
079:
080: /**
081: * @param _val
082: * @param _key
083: */
084: public synchronized void setStringProperty(String _key, String _val) {
085: observable.setChanged();
086: super .put(_key, _val);
087: }
088:
089: /**
090: * @param _key
091: * @param _default The default value to use if no property is found.
092: */
093: public String stringProperty(String _key, String _default) {
094: return super .getProperty(_key, _default);
095: }
096:
097: /**
098: * @param _key
099: * @param _default The default value to use if no property is found.
100: */
101: public DxCollection stringsProperty(String _key, String _default) {
102: String propList = stringProperty(_key, _default);
103: DxArrayBag result = new DxArrayBag();
104:
105: StringTokenizer st = new StringTokenizer(propList, " \t,",
106: false);
107: while (st.hasMoreTokens()) {
108: String token = st.nextToken();
109: // System.out.println ("'" + token + "'");
110: result.add(token);
111: }
112: return result;
113: }
114:
115: /**
116: * @param _val
117: * @param _key
118: */
119: public void setProperty(String _key, Object _val) {
120: observable.setChanged();
121: try {
122: ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
123: ObjectOutputStream out = new ObjectOutputStream(buf);
124: out.writeObject(_val);
125: out.close();
126:
127: MimeBase64Encoder encoder = new MimeBase64Encoder();
128: encoder.translate(buf.toByteArray());
129:
130: setStringProperty(_key, new String(encoder.getCharArray()));
131: } catch (Exception e) {
132: throw new RuntimeException(e.getMessage());
133: }
134: }
135:
136: /**
137: * @param _key
138: * @param _default The default value to use if no property is found.
139: */
140: public Object property(String _key, Object _default) {
141: try {
142: String result = stringProperty(_key, (String) _default);
143: if (result != null) {
144: MimeBase64Decoder decoder = new MimeBase64Decoder();
145: decoder.translate(result.toCharArray());
146:
147: ObjectInputStream in = new ObjectInputStream(
148: new ByteArrayInputStream(decoder.getByteArray()));
149: return in.readObject();
150: } else {
151: return null;
152: }
153: } catch (Exception e) {
154: throw new RuntimeException(e.getMessage());
155: }
156: }
157:
158: /**
159: * @param _val
160: * @param _key
161: */
162: public void setIntProperty(String _key, int _val) {
163: setStringProperty(_key, String.valueOf(_val));
164: }
165:
166: /**
167: * @param _key
168: * @param _default The default value to use if no property is found.
169: */
170: public int intProperty(String _key, int _default) {
171: String result = stringProperty(_key, String.valueOf(_default));
172: return Integer.parseInt(result);
173: }
174:
175: /**
176: * @param _key
177: * @param _default The default value to use if no property is found.
178: */
179: public long longProperty(String _key, long _default) {
180: String result = stringProperty(_key, String.valueOf(_default));
181: return Long.valueOf(result).longValue();
182: }
183:
184: /**
185: * @param _key
186: * @param _default The default value to use if no property is found.
187: */
188: public boolean booleanProperty(String _key, boolean _default) {
189: String result = stringProperty(_key, String.valueOf(_default));
190: return Boolean.valueOf(result).booleanValue();
191: }
192:
193: /**
194: * @param _val
195: * @param _key
196: */
197: public void setLongProperty(String _key, long _val) {
198: setStringProperty(_key, String.valueOf(_val));
199: }
200:
201: /**
202: * @param _val
203: * @param _key
204: */
205: public void setBooleanProperty(String _key, boolean _val) {
206: setStringProperty(_key, String.valueOf(_val));
207: }
208:
209: /**
210: * @param
211: * @param keyPrefix
212: * @param printPrefix
213: */
214: public void print(PrintStream out, String keyPrefix,
215: String printPrefix) {
216: DxTreeSet sortedProps = new DxTreeSet(new DxStringComparator());
217: for (Enumeration e = propertyNames(); e.hasMoreElements();) {
218: String key = (String) e.nextElement();
219: String val = stringProperty(key, "(not set)");
220: //avoid printing "object" properties
221: //currently not supported!!!
222: if (key.startsWith(keyPrefix)
223: && !val.startsWith("[object]")) {
224: sortedProps.add(printPrefix + key + " = " + val);
225: }
226: }
227: for (DxIterator it = sortedProps.iterator(); it.next() != null;) {
228: out.println(it.object().toString());
229: }
230: }
231:
232: }
233:
234: /**
235: * @author <a href="http://www.softwarebuero.de/">SMB</a>
236: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $
237: */
238: class EnhObservable extends Observable {
239:
240: public EnhObservable() {
241: super ();
242: }
243:
244: public void setChanged() {
245: super.setChanged();
246: }
247:
248: }
|