KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > util > EnhProperties


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9
10 package org.ozoneDB.util;
11
12
13 import org.ozoneDB.DxLib.*;
14 import org.ozoneDB.io.stream.ResolvingObjectInputStream;
15
16 import java.io.*;
17 import java.util.*;
18
19 /**
20  * EnhProperties has methods to store/update the value of a property to handle
21  * such dynamic properties.
22  * <p>
23  * In addition the Properties EnhProperties can hold not only String properties
24  * but most of other primitive types and raw objects. Non-string properties are
25  * internaly stored as Strings.
26  * <p>
27  * Setup extends java.util.Properties. So the system properties can be used as
28  * defaults.
29  *
30  *
31  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
32  * @version $Revision$Date$
33  */

34 public class EnhProperties extends Properties {
35
36     /**
37      * An Observable object that is responsible for this properties. Directly
38      * extending the class is not possible because it already extends Properties.
39      */

40     protected EnhObservable observable;
41
42     public EnhProperties() {
43         observable = new EnhObservable();
44     }
45
46     public EnhProperties( Properties _defaults ) {
47         super( _defaults );
48         observable = new EnhObservable();
49     }
50
51     public void addObserver( Observer _observer ) {
52         observable.addObserver( _observer );
53     }
54
55     public void removeObserver( Observer _observer ) {
56         observable.deleteObserver( _observer );
57     }
58
59     public void notifyObservers() {
60         observable.notifyObservers( this );
61     }
62
63     public boolean hasChanged() {
64         return observable.hasChanged();
65     }
66
67     /**
68      * @param properties
69      * @param keyPrefix
70      */

71     public void addProperties( Properties properties, String JavaDoc keyPrefix ) {
72         observable.setChanged();
73         for (Enumeration e = properties.keys(); e.hasMoreElements();) {
74             String JavaDoc key = (String JavaDoc)e.nextElement();
75             if (key.startsWith( keyPrefix )) {
76                 // System.out.println (key);
77
String JavaDoc val = properties.getProperty( key, "" );
78                 setStringProperty( key, val );
79             }
80         }
81     }
82
83
84     /**
85      * @param _val
86      * @param _key
87      */

88     public synchronized void setStringProperty( String JavaDoc _key, String JavaDoc _val ) {
89         observable.setChanged();
90         super.put( _key, _val );
91     }
92
93
94     /**
95      * @param _key
96      * @param _default The default value to use if no property is found.
97      */

98     public String JavaDoc stringProperty( String JavaDoc _key, String JavaDoc _default ) {
99         return super.getProperty( _key, _default );
100     }
101
102
103     /**
104      * @param _key
105      * @param _default The default value to use if no property is found.
106      */

107     public DxCollection stringsProperty( String JavaDoc _key, String JavaDoc _default ) {
108         String JavaDoc propList = stringProperty( _key, _default );
109         DxArrayBag result = new DxArrayBag();
110
111         StringTokenizer st = new StringTokenizer( propList, " \t,", false );
112         while (st.hasMoreTokens()) {
113             String JavaDoc token = st.nextToken();
114             // System.out.println ("'" + token + "'");
115
result.add( token );
116         }
117         return result;
118     }
119
120     /**
121      * @param _val
122      * @param _key
123      */

124     public void setProperty( String JavaDoc _key, Object JavaDoc _val ) {
125         observable.setChanged();
126         try {
127             ByteArrayOutputStream buf = new ByteArrayOutputStream( 1024 );
128             ObjectOutputStream out = new ObjectOutputStream( buf );
129             out.writeObject( _val );
130             out.close();
131
132             MimeBase64Encoder encoder = new MimeBase64Encoder();
133             encoder.translate( buf.toByteArray() );
134
135             setStringProperty( _key, new String JavaDoc( encoder.getCharArray() ) );
136         } catch (Exception JavaDoc e) {
137             throw new RuntimeException JavaDoc( e.getMessage() );
138         }
139     }
140
141
142     /**
143      * @param _key
144      * @param _default The default value to use if no property is found.
145      */

146     public Object JavaDoc property( String JavaDoc _key, Object JavaDoc _default ) {
147         try {
148             String JavaDoc result = stringProperty( _key, (String JavaDoc)_default );
149             if (result != null) {
150                 MimeBase64Decoder decoder = new MimeBase64Decoder();
151                 decoder.translate( result.toCharArray() );
152
153                 ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( decoder.getByteArray() ) );
154                 return in.readObject();
155             } else {
156                 return null;
157             }
158         } catch (Exception JavaDoc e) {
159             throw new RuntimeException JavaDoc( e.getMessage() );
160         }
161     }
162
163
164     /**
165      * @param _val
166      * @param _key
167      */

168     public void setIntProperty( String JavaDoc _key, int _val ) {
169         setStringProperty( _key, String.valueOf( _val ) );
170     }
171
172
173     /**
174      * @param _key
175      * @param _default The default value to use if no property is found.
176      */

177     public int intProperty( String JavaDoc _key, int _default ) {
178         String JavaDoc result = stringProperty( _key, String.valueOf( _default ) );
179         return Integer.parseInt( result );
180     }
181
182
183     /**
184      * @param _key
185      * @param _default The default value to use if no property is found.
186      */

187     public long longProperty( String JavaDoc _key, long _default ) {
188         String JavaDoc result = stringProperty( _key, String.valueOf( _default ) );
189         return Long.valueOf( result ).longValue();
190     }
191
192
193     /**
194      * @param _key
195      * @param _default The default value to use if no property is found.
196      */

197     public boolean booleanProperty( String JavaDoc _key, boolean _default ) {
198         String JavaDoc result = stringProperty( _key, String.valueOf( _default ) );
199         return Boolean.valueOf( result ).booleanValue();
200     }
201
202
203     /**
204      * @param _val
205      * @param _key
206      */

207     public void setLongProperty( String JavaDoc _key, long _val ) {
208         setStringProperty( _key, String.valueOf( _val ) );
209     }
210
211     /**
212      * @param _val
213      * @param _key
214      */

215     public void setBooleanProperty( String JavaDoc _key, boolean _val ) {
216
217         setStringProperty( _key, String.valueOf( _val ) );
218
219     }
220
221
222
223
224
225     /**
226
227      * @param _val
228
229      * @param _key
230
231      */

232
233     public void setIntArrayProperty( String JavaDoc _key, int[] _val ) {
234         StringBuffer JavaDoc strVal = new StringBuffer JavaDoc();
235         for (int i = 0; i < _val.length; i++) {
236             strVal.append( "," + _val[i] );
237         }
238         setStringProperty( _key, strVal.substring(1) );
239     }
240
241
242
243
244
245     /**
246
247      * @param _key
248
249      * @param _default The default value to use if no property is found.
250
251      */

252
253     public int[] intArrayProperty( String JavaDoc _key, int[] _default ) {
254         String JavaDoc strVal = super.getProperty( _key );
255         if (strVal == null) {
256             return _default;
257         } else {
258             StringTokenizer st = new StringTokenizer( strVal, "," );
259             int[] result = new int[st.countTokens()];
260             for (int i = 0; i < result.length; i++) {
261                 result[i] = Integer.parseInt( st.nextToken() );
262             }
263             return result;
264         }
265     }
266
267
268
269
270
271     /**
272
273      * @param out
274
275      * @param keyPrefix
276
277      * @param printPrefix
278
279      */

280
281     public void print( PrintStream out, String JavaDoc keyPrefix, String JavaDoc printPrefix ) {
282
283         DxTreeSet sortedProps = new DxTreeSet( new DxStringComparator() );
284
285         for (Enumeration e = propertyNames(); e.hasMoreElements();) {
286
287             String JavaDoc key = (String JavaDoc)e.nextElement();
288
289             String JavaDoc val = stringProperty( key, "(not set)" );
290
291             //avoid printing "object" properties
292

293             //currently not supported!!!
294

295             if (key.startsWith( keyPrefix ) && !val.startsWith( "[object]" )) {
296
297                 sortedProps.add( printPrefix + key + " = " + val );
298
299             }
300
301         }
302
303         for (DxIterator it = sortedProps.iterator(); it.next() != null;) {
304
305             out.println( it.object().toString() );
306
307         }
308
309     }
310
311
312
313 }
314
315
316
317
318
319 /**
320
321  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
322
323  * @version $Revision$Date$
324
325  */

326
327 class EnhObservable extends Observable {
328
329
330
331
332
333     public EnhObservable() {
334
335         super();
336
337     }
338
339
340
341
342
343     public void setChanged() {
344
345         super.setChanged();
346
347     }
348
349
350
351 }
352
353
Popular Tags