001: // You can redistribute this software and/or modify it under the terms of
002: // the Infozone Software License version 2 published by the Infozone Group
003: // (http://www.infozone-group.org).
004: //
005: // Copyright (C) @year@ by The Infozone Group. All rights reserved.
006: //
007: // $Id: EnhProperties.java,v 1.1 2002/05/10 08:59:11 per_nyfelt Exp $
008:
009: package org.infozone.tools;
010:
011: import java.io.*;
012: import java.util.*;
013:
014: /**
015: EnhProperties has methods to store/update the value of a property to handle
016: such dynamic properties.
017: <p>
018: In addition the Properties EnhProperties can hold not only String properties
019: but most of other primitive types and raw objects. Non-string properties are
020: internaly stored as Strings.
021: <p>
022: Setup extends java.util.Properties. So the system properties can be used as
023: defaults.
024:
025: @author <a href="http://www.softwarebuero.de/">SMB</a>
026: @version $Revision: 1.1 $Date: 2002/05/10 08:59:11 $
027: */
028: public class EnhProperties extends Properties {
029:
030: /**
031: An Observable object that is responsible for this properties. Directly
032: extending the class is not possible because it already extends Properties.
033: */
034: protected EnhObservable observable;
035:
036: public EnhProperties() {
037: observable = new EnhObservable();
038: }
039:
040: public EnhProperties(Properties _defaults) {
041: super (_defaults);
042: observable = new EnhObservable();
043: }
044:
045: public void addObserver(Observer _observer) {
046: observable.addObserver(_observer);
047: }
048:
049: public void removeObserver(Observer _observer) {
050: observable.deleteObserver(_observer);
051: }
052:
053: public void notifyObservers() {
054: observable.notifyObservers(this );
055: }
056:
057: public boolean hasChanged() {
058: return observable.hasChanged();
059: }
060:
061: /**
062: @param properties
063: @param keyPrefix
064: */
065: public void addProperties(Properties properties, String keyPrefix) {
066: observable.setChanged();
067: for (Enumeration e = properties.keys(); e.hasMoreElements();) {
068: String key = (String) e.nextElement();
069: if (key.startsWith(keyPrefix)) {
070: // System.out.println (key);
071: String val = properties.getProperty(key, "");
072: setStringProperty(key, val);
073: }
074: }
075: }
076:
077: /**
078: @param _val
079: @param _key
080: */
081: public synchronized void setStringProperty(String _key, String _val) {
082: observable.setChanged();
083: super .put(_key, _val);
084: }
085:
086: /**
087: @param _key
088: @param _default The default value to use if no property is found.
089: */
090: public String stringProperty(String _key, String _default) {
091: return super .getProperty(_key, _default);
092: }
093:
094: /**
095: @param _key
096: @param _default The default value to use if no property is found.
097: */
098: public String[] stringsProperty(String _key, String _default) {
099: String propList = stringProperty(_key, _default);
100: Vector v = new Vector();
101:
102: StringTokenizer st = new StringTokenizer(propList, " \t,",
103: false);
104: while (st.hasMoreTokens()) {
105: String token = st.nextToken();
106: // System.out.println ("'" + token + "'");
107: v.addElement(token);
108: }
109:
110: String[] result = new String[v.size()];
111: v.copyInto(result);
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: } catch (Exception e) {
153: throw new RuntimeException(e.getMessage());
154: }
155: }
156:
157: /**
158: @param _val
159: @param _key
160: */
161: public void setIntProperty(String _key, int _val) {
162: setStringProperty(_key, String.valueOf(_val));
163: }
164:
165: /**
166: @param _key
167: @param _default The default value to use if no property is found.
168: */
169: public int intProperty(String _key, int _default) {
170: String result = stringProperty(_key, String.valueOf(_default));
171: return Integer.parseInt(result);
172: }
173:
174: /**
175: @param _key
176: @param _default The default value to use if no property is found.
177: */
178: public long longProperty(String _key, long _default) {
179: String result = stringProperty(_key, String.valueOf(_default));
180: return Long.valueOf(result).longValue();
181: }
182:
183: /**
184: @param _key
185: @param _default The default value to use if no property is found.
186: */
187: public boolean booleanProperty(String _key, boolean _default) {
188: String result = stringProperty(_key, String.valueOf(_default));
189: return Boolean.valueOf(result).booleanValue();
190: }
191:
192: /**
193: @param _val
194: @param _key
195: */
196: public void setLongProperty(String _key, long _val) {
197: setStringProperty(_key, String.valueOf(_val));
198: }
199:
200: /**
201: @param _val
202: @param _key
203: */
204: public void setBooleanProperty(String _key, boolean _val) {
205: setStringProperty(_key, String.valueOf(_val));
206: }
207:
208: /**
209: @param
210: @param keyPrefix
211: @param printPrefix
212: */
213: // public void print (PrintStream out, String keyPrefix, String printPrefix) {
214: // DxTreeSet sortedProps = new DxTreeSet (new DxStringComparator());
215: // for (Enumeration e=propertyNames(); e.hasMoreElements();) {
216: // String key = (String)e.nextElement();
217: // String val = stringProperty (key, "(not set)");
218: // //avoid printing "object" properties
219: // //currently not supported!!!
220: // if (key.startsWith (keyPrefix) && !val.startsWith("[object]")) {
221: // sortedProps.add (printPrefix + key + " = " + val);
222: // }
223: // }
224: // for (DxIterator it=sortedProps.iterator(); it.next()!=null;)
225: // out.println (it.object().toString());
226: // }
227: }
228:
229: /**
230: @author <a href="http://www.softwarebuero.de/">SMB</a>
231: @version $Revision: 1.1 $Date: 2002/05/10 08:59:11 $
232: */
233: class EnhObservable extends Observable {
234:
235: public EnhObservable() {
236: super ();
237: }
238:
239: public void setChanged() {
240: super.setChanged();
241: }
242:
243: }
|