KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > EnhProperties


1 // You can redistribute this software and/or modify it under the terms of
2
// the Infozone Software License version 2 published by the Infozone Group
3
// (http://www.infozone-group.org).
4
//
5
// Copyright (C) @year@ by The Infozone Group. All rights reserved.
6
//
7
// $Id: EnhProperties.java,v 1.2 2003/03/12 12:30:26 per_nyfelt Exp $
8

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

29 public class EnhProperties extends Properties {
30
31     /**
32     An Observable object that is responsible for this properties. Directly
33     extending the class is not possible because it already extends Properties.
34     */

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

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

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

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

109     public String JavaDoc[] stringsProperty (String JavaDoc _key, String JavaDoc _default) {
110         String JavaDoc propList = stringProperty( _key, _default );
111         Vector v = new Vector();
112
113         StringTokenizer st = new StringTokenizer( propList, " \t,", false );
114         while (st.hasMoreTokens()) {
115             String JavaDoc token = st.nextToken();
116            // System.out.println ("'" + token + "'");
117
v.addElement( token );
118         }
119
120         String JavaDoc[] result = new String JavaDoc[v.size()];
121         v.copyInto( result );
122         return result;
123         }
124
125
126     /**
127     @param _val
128     @param _key
129     */

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

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

176     public void setIntProperty (String JavaDoc _key, int _val) {
177         setStringProperty (_key, String.valueOf (_val));
178         }
179
180
181     /**
182     @param _key
183     @param _default The default value to use if no property is found.
184     */

185     public int intProperty (String JavaDoc _key, int _default) {
186         String JavaDoc result = stringProperty (_key, String.valueOf (_default));
187         return Integer.parseInt (result);
188         }
189
190
191     /**
192     @param _key
193     @param _default The default value to use if no property is found.
194     */

195     public long longProperty (String JavaDoc _key, long _default) {
196         String JavaDoc result = stringProperty (_key, String.valueOf (_default));
197         return Long.valueOf (result).longValue();
198         }
199
200
201     /**
202     @param _key
203     @param _default The default value to use if no property is found.
204     */

205     public boolean booleanProperty (String JavaDoc _key, boolean _default) {
206         String JavaDoc result = stringProperty (_key, String.valueOf (_default));
207         return Boolean.valueOf (result).booleanValue();
208         }
209
210
211     /**
212     @param _val
213     @param _key
214     */

215     public void setLongProperty (String JavaDoc _key, long _val) {
216         setStringProperty (_key, String.valueOf (_val));
217         }
218
219
220     /**
221     @param _val
222     @param _key
223     */

224     public void setBooleanProperty (String JavaDoc _key, boolean _val) {
225         setStringProperty (_key, String.valueOf (_val));
226         }
227
228
229     /**
230     @param
231     @param keyPrefix
232     @param printPrefix
233     */

234 // public void print (PrintStream out, String keyPrefix, String printPrefix) {
235
// DxTreeSet sortedProps = new DxTreeSet (new DxStringComparator());
236
// for (Enumeration e=propertyNames(); e.hasMoreElements();) {
237
// String key = (String)e.nextElement();
238
// String val = stringProperty (key, "(not set)");
239
// //avoid printing "object" properties
240
// //currently not supported!!!
241
// if (key.startsWith (keyPrefix) && !val.startsWith("[object]")) {
242
// sortedProps.add (printPrefix + key + " = " + val);
243
// }
244
// }
245
// for (DxIterator it=sortedProps.iterator(); it.next()!=null;)
246
// out.println (it.object().toString());
247
// }
248

249     }
250
251
252 /**
253 @author <a HREF="http://www.softwarebuero.de/">SMB</a>
254 @version $Revision: 1.2 $Date: 2003/03/12 12:30:26 $
255 */

256 class EnhObservable extends Observable {
257
258    public EnhObservable() {
259        super();
260        }
261
262
263    public void setChanged() {
264        super.setChanged();
265        }
266
267    }
268
269
Popular Tags