KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > Properties


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.debugger;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.lang.reflect.Array JavaDoc;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import org.openide.ErrorManager;
38
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileSystem;
41 import org.openide.filesystems.Repository;
42 import org.openide.filesystems.FileObject;
43 import org.openide.util.RequestProcessor;
44
45
46 /**
47  * Utility class hepls store properties.
48  *
49  * @author Jan Jancura
50  */

51 public abstract class Properties {
52
53     
54     private static Properties defaultProperties;
55
56     /**
57      * Returns shared instance of Properties class.
58      *
59      * @return shared instance of Properties class
60      */

61     public static synchronized Properties getDefault () {
62         if (defaultProperties == null)
63             defaultProperties = new PropertiesImpl ();
64         return defaultProperties;
65     }
66     
67     /**
68      * Reads String property from storage.
69      *
70      * @param propertyName a propertyName of property
71      * @param defaultValue a default value to be used if property is not set
72      * @return a current value of property
73      */

74     public abstract String JavaDoc getString (String JavaDoc propertyName, String JavaDoc defaultValue);
75     
76     /**
77      * Sets a new value of property with given propertyName.
78      *
79      * @param propertyName a propertyName of property
80      * @param value the new value of property
81      */

82     public abstract void setString (String JavaDoc propertyName, String JavaDoc value);
83     
84     /**
85      * Reads int property from storage.
86      *
87      * @param propertyName a propertyName of property
88      * @param defaultValue a default value to be used if property is not set
89      * @return a current value of property
90      */

91     public abstract int getInt (String JavaDoc propertyName, int defaultValue);
92     
93     /**
94      * Sets a new value of property with given propertyName.
95      *
96      * @param propertyName a propertyName of property
97      * @param value the new value of property
98      */

99     public abstract void setInt (String JavaDoc propertyName, int value);
100     
101     /**
102      * Reads char property from storage.
103      *
104      * @param propertyName a propertyName of property
105      * @param defaultValue a default value to be used if property is not set
106      * @return a current value of property
107      */

108     public abstract char getChar (String JavaDoc propertyName, char defaultValue);
109     
110     /**
111      * Sets a new value of property with given propertyName.
112      *
113      * @param propertyName a propertyName of property
114      * @param value the new value of property
115      */

116     public abstract void setChar (String JavaDoc propertyName, char value);
117     
118     /**
119      * Reads float property from storage.
120      *
121      * @param propertyName a propertyName of property
122      * @param defaultValue a default value to be used if property is not set
123      * @return a current value of property
124      */

125     public abstract float getFloat (String JavaDoc propertyName, float defaultValue);
126     
127     /**
128      * Sets a new value of property with given propertyName.
129      *
130      * @param propertyName a propertyName of property
131      * @param value the new value of property
132      */

133     public abstract void setFloat (String JavaDoc propertyName, float value);
134     
135     /**
136      * Reads long property from storage.
137      *
138      * @param propertyName a propertyName of property
139      * @param defaultValue a default value to be used if property is not set
140      * @return a current value of property
141      */

142     public abstract long getLong (String JavaDoc propertyName, long defaultValue);
143     
144     /**
145      * Sets a new value of property with given propertyName.
146      *
147      * @param propertyName a propertyName of property
148      * @param value the new value of property
149      */

150     public abstract void setLong (String JavaDoc propertyName, long value);
151     
152     /**
153      * Reads double property from storage.
154      *
155      * @param propertyName a propertyName of property
156      * @param defaultValue a default value to be used if property is not set
157      * @return a current value of property
158      */

159     public abstract double getDouble (String JavaDoc propertyName, double defaultValue);
160     
161     /**
162      * Sets a new value of property with given propertyName.
163      *
164      * @param propertyName a propertyName of property
165      * @param value the new value of property
166      */

167     public abstract void setDouble (String JavaDoc propertyName, double value);
168     
169     /**
170      * Reads boolean property from storage.
171      *
172      * @param propertyName a propertyName of property
173      * @param defaultValue a default value to be used if property is not set
174      * @return a current value of property
175      */

176     public abstract boolean getBoolean (String JavaDoc propertyName, boolean defaultValue);
177     
178     /**
179      * Sets a new value of property with given propertyName.
180      *
181      * @param propertyName a propertyName of property
182      * @param value the new value of property
183      */

184     public abstract void setBoolean (String JavaDoc propertyName, boolean value);
185     
186     /**
187      * Reads byte property from storage.
188      *
189      * @param propertyName a propertyName of property
190      * @param defaultValue a default value to be used if property is not set
191      * @return a current value of property
192      */

193     public abstract byte getByte (String JavaDoc propertyName, byte defaultValue);
194     
195     /**
196      * Sets a new value of property with given propertyName.
197      *
198      * @param propertyName a propertyName of property
199      * @param value the new value of property
200      */

201     public abstract void setByte (String JavaDoc propertyName, byte value);
202     
203     /**
204      * Reads short property from storage.
205      *
206      * @param propertyName a propertyName of property
207      * @param defaultValue a default value to be used if property is not set
208      * @return a current value of property
209      */

210     public abstract short getShort (String JavaDoc propertyName, short defaultValue);
211     
212     /**
213      * Sets a new value of property with given propertyName.
214      *
215      * @param propertyName a propertyName of property
216      * @param value the new value of property
217      */

218     public abstract void setShort (String JavaDoc propertyName, short value);
219     /**
220      * Reads Object property from storage.
221      *
222      * @param propertyName a propertyName of property
223      * @param defaultValue a default value to be used if property is not set
224      * @return a current value of property
225      */

226     
227     public abstract Object JavaDoc getObject (String JavaDoc propertyName, Object JavaDoc defaultValue);
228     
229     /**
230      * Sets a new value of property with given propertyName.
231      *
232      * @param propertyName a propertyName of property
233      * @param value the new value of property
234      */

235     public abstract void setObject (String JavaDoc propertyName, Object JavaDoc value);
236     
237     /**
238      * Reads array property from storage.
239      *
240      * @param propertyName a propertyName of property
241      * @param defaultValue a default value to be used if property is not set
242      * @return a current value of property
243      */

244     public abstract Object JavaDoc[] getArray (String JavaDoc propertyName, Object JavaDoc[] defaultValue);
245     
246     /**
247      * Sets a new value of property with given propertyName.
248      *
249      * @param propertyName a propertyName of property
250      * @param value the new value of property
251      */

252     public abstract void setArray (String JavaDoc propertyName, Object JavaDoc[] value);
253     
254     /**
255      * Reads Collection property from storage.
256      *
257      * @param propertyName a propertyName of property
258      * @param defaultValue a default value to be used if property is not set
259      * @return a current value of property
260      */

261     public abstract Collection JavaDoc getCollection (String JavaDoc propertyName, Collection JavaDoc defaultValue);
262     
263     /**
264      * Sets a new value of property with given propertyName.
265      *
266      * @param propertyName a propertyName of property
267      * @param value the new value of property
268      */

269     public abstract void setCollection (String JavaDoc propertyName, Collection JavaDoc value);
270     
271     /**
272      * Reads Map property from storage.
273      *
274      * @param propertyName a propertyName of property
275      * @param defaultValue a default value to be used if property is not set
276      * @return a current value of property
277      */

278     public abstract Map JavaDoc getMap (String JavaDoc propertyName, Map JavaDoc defaultValue);
279     
280     /**
281      * Sets a new value of property with given propertyName.
282      *
283      * @param propertyName a propertyName of property
284      * @param value the new value of property
285      */

286     public abstract void setMap (String JavaDoc propertyName, Map JavaDoc value);
287     
288     /**
289      * Returns Properties instance for some "subfolder" in properties file.
290      *
291      * @param propertyName a subfolder name
292      * @return a Properties instance for some "subfolder" in properties file
293      */

294     public abstract Properties getProperties (String JavaDoc propertyName);
295
296     
297     // innerclasses ............................................................
298

299     /**
300      * This class helps to store and read custom types using
301      * {@link Properties#setObject} and {@link Properties#getObject} methods.
302      * Implementations of this class should be stored in "META_INF\debugger"
303      * folder.
304      */

305     public interface Reader {
306         
307         /**
308          * Returns array of classNames supported by this reader.
309          *
310          * @return array of classNames supported by this reader
311          */

312         public String JavaDoc[] getSupportedClassNames ();
313         
314         /**
315          * Reads object with given className.
316          *
317          * @param className a name of class to be readed
318          * @param properties a properties subfloder containing properties
319          * for this object
320          * @return a new instance of className class
321          */

322         public Object JavaDoc read (String JavaDoc className, Properties properties);
323         
324         /**
325          * Writes given object to given properties subfolder.
326          *
327          * @param object a object to be saved
328          * @param properties a properties subfolder to be used
329          */

330         public void write (Object JavaDoc object, Properties properties);
331     }
332     
333     private final static class PrimitiveRegister {
334         
335         private HashMap JavaDoc properties = new HashMap JavaDoc ();
336         private boolean isInitialized = false;
337
338
339         public synchronized String JavaDoc getProperty (String JavaDoc propertyName, String JavaDoc defaultValue) {
340             if (!isInitialized) load ();
341             isInitialized = true;
342             String JavaDoc value = (String JavaDoc) properties.get (propertyName);
343             if (value != null) return value;
344             return defaultValue;
345         }
346
347         public synchronized void setProperty (String JavaDoc propertyName, String JavaDoc value) {
348             if (!isInitialized) load ();
349             isInitialized = true;
350             properties.put (propertyName, value);
351             save ();
352         }
353     
354         private void load () {
355             BufferedReader JavaDoc br = null;
356             try {
357                 FileObject fo = findSettings();
358                 InputStream JavaDoc is = fo.getInputStream ();
359                 br = new BufferedReader JavaDoc (new InputStreamReader JavaDoc (is));
360
361                 String JavaDoc l = br.readLine ();
362                 while (l != null) {
363                     int i = l.indexOf (':');
364                     if (i > 0)
365                         properties.put (l.substring (0, i), l.substring (i + 1));
366                     l = br.readLine ();
367                 }
368                 br.close ();
369             } catch (IOException JavaDoc ex) {
370                 ErrorManager.getDefault().notify(ex);
371             }
372         }
373         
374         // currently waiting / running refresh task
375
// there is at most one
376
private RequestProcessor.Task task;
377
378         private synchronized void save () {
379             if (task == null) {
380                 task = new RequestProcessor("Debugger Properties Save RP", 1).create(
381                         new Runnable JavaDoc() {
382                             public void run () {
383                                 saveIn ();
384                             }
385                         }
386                 );
387             }
388             task.schedule(4000);
389         }
390
391         private void saveIn () {
392             PrintWriter JavaDoc pw = null;
393             FileLock lock = null;
394             try {
395                 FileObject fo = findSettings();
396                 lock = fo.lock ();
397                 OutputStream JavaDoc os = fo.getOutputStream (lock);
398                 pw = new PrintWriter JavaDoc (os);
399
400                 Set JavaDoc s = properties.keySet ();
401                 ArrayList JavaDoc l = new ArrayList JavaDoc (s);
402                 Collections.sort (l);
403                 int i, k = l.size ();
404                 for (i = 0; i < k; i++) {
405                     String JavaDoc key = (String JavaDoc) l.get (i);
406                     Object JavaDoc value = properties.get (key);
407                     if (value != null) {
408                         // Do not write null values
409
pw.println ("" + key + ":" + value);
410                     }
411                 }
412                 pw.flush ();
413             } catch (IOException JavaDoc ex) {
414                 ErrorManager.getDefault().notify(
415                         ErrorManager.getDefault().annotate(ex,
416                         "Can not save debugger settings."));
417             } finally {
418                 if (pw != null) {
419                     pw.close ();
420                 }
421                 if (lock != null) {
422                     lock.releaseLock ();
423                 }
424             }
425         }
426         
427         private static FileObject findSettings() throws IOException JavaDoc {
428             FileSystem fs = Repository.getDefault().getDefaultFileSystem();
429             FileObject r = fs.findResource("Services"); // NOI18N
430
if (r == null) {
431                 r = fs.getRoot ().createFolder("Services"); // NOI18N
432
}
433             FileObject fo = r.getFileObject
434                 ("org-netbeans-modules-debugger-Settings", "properties"); // NOI18N
435
if (fo == null) {
436                 fo = r.createData
437                     ("org-netbeans-modules-debugger-Settings", "properties"); // NOI18N
438
}
439             return fo;
440         }
441     }
442
443     private static class PropertiesImpl extends Properties {
444
445         private static final Object JavaDoc BAD_OBJECT = new Object JavaDoc ();
446         private static final String JavaDoc BAD_STRING = "";
447         private static final Map JavaDoc BAD_MAP = new HashMap JavaDoc ();
448         private static final Collection JavaDoc BAD_COLLECTION = new ArrayList JavaDoc ();
449         private static final Object JavaDoc[] BAD_ARRAY = new Object JavaDoc [0];
450         
451         private HashMap JavaDoc register;
452         
453         
454         private PrimitiveRegister impl = new PrimitiveRegister ();
455
456         private void initReaders () {
457             register = new HashMap JavaDoc ();
458             Iterator JavaDoc i = DebuggerManager.getDebuggerManager().lookup(null, Reader JavaDoc.class).iterator ();
459             while (i.hasNext ()) {
460                 Reader JavaDoc r = (Reader JavaDoc) i.next ();
461                 String JavaDoc[] ns = r.getSupportedClassNames ();
462                 int j, jj = ns.length;
463                 for (j = 0; j < jj; j++)
464                     register.put (ns [j], r);
465             }
466         }
467         
468         private Reader JavaDoc findReader (String JavaDoc typeID) {
469             synchronized (this) {
470                 if (register == null) {
471                     initReaders ();
472                 }
473             }
474             
475             Reader JavaDoc r = (Reader JavaDoc) register.get (typeID);
476             if (r != null) return r;
477
478             Class JavaDoc c = null;
479             try {
480                 c = getClassLoader ().loadClass (typeID);
481             } catch (ClassNotFoundException JavaDoc e) {
482                 ErrorManager.getDefault().notify(e);
483                 return null;
484             }
485             while ((c != null) && (register.get (c.getName ()) == null)) {
486                 c = c.getSuperclass ();
487             }
488             if (c != null)
489                 r = (Reader JavaDoc) register.get (c.getName ());
490             return r;
491         }
492
493
494
495
496         // primitive properties ....................................................................................
497

498         public String JavaDoc getString (String JavaDoc propertyName, String JavaDoc defaultValue) {
499             String JavaDoc value = impl.getProperty (propertyName, null);
500             if (value == null) return defaultValue;
501             if (!value.startsWith ("\"")) {
502                 System.out.println("Can not read string " + value + ".");
503                 return defaultValue;
504             }
505             return value.substring (1, value.length () - 1);
506         }
507
508         public void setString (String JavaDoc propertyName, String JavaDoc value) {
509             if (value != null) {
510                 impl.setProperty (propertyName, "\"" + value + "\"");
511             } else {
512                 impl.setProperty (propertyName, value);
513             }
514         }
515
516         public int getInt (String JavaDoc propertyName, int defaultValue) {
517             String JavaDoc value = impl.getProperty (propertyName, null);
518             if (value == null) return defaultValue;
519             int val = Integer.parseInt (value);
520             return val;
521         }
522
523         public void setInt (String JavaDoc propertyName, int value) {
524             impl.setProperty (propertyName, Integer.toString (value));
525         }
526
527         public char getChar (String JavaDoc propertyName, char defaultValue) {
528             String JavaDoc value = impl.getProperty (propertyName, null);
529             if (value == null) return defaultValue;
530             char val = value.charAt (0);
531             return val;
532         }
533
534         public void setChar (String JavaDoc propertyName, char value) {
535             impl.setProperty (propertyName, "" + value);
536         }
537
538         public float getFloat (String JavaDoc propertyName, float defaultValue) {
539             String JavaDoc value = impl.getProperty (propertyName, null);
540             if (value == null) return defaultValue;
541             float val = Float.parseFloat (value);
542             return val;
543         }
544
545         public void setFloat (String JavaDoc propertyName, float value) {
546             impl.setProperty (propertyName, Float.toString (value));
547         }
548
549         public long getLong (String JavaDoc propertyName, long defaultValue) {
550             String JavaDoc value = impl.getProperty (propertyName, null);
551             if (value == null) return defaultValue;
552             long val = Long.parseLong (value);
553             return val;
554         }
555
556         public void setLong (String JavaDoc propertyName, long value) {
557             impl.setProperty (propertyName, Long.toString (value));
558         }
559
560         public double getDouble (String JavaDoc propertyName, double defaultValue) {
561             String JavaDoc value = impl.getProperty (propertyName, null);
562             if (value == null) return defaultValue;
563             double val = Double.parseDouble (value);
564             return val;
565         }
566
567         public void setDouble (String JavaDoc propertyName, double value) {
568             impl.setProperty (propertyName, Double.toString (value));
569         }
570
571         public boolean getBoolean (String JavaDoc propertyName, boolean defaultValue) {
572             String JavaDoc value = impl.getProperty (propertyName, null);
573             if (value == null) return defaultValue;
574             boolean val = value.equals ("true");
575             return val;
576         }
577
578         public void setBoolean (String JavaDoc propertyName, boolean value) {
579             impl.setProperty (propertyName, value ? "true" : "false");
580         }
581
582         public byte getByte (String JavaDoc propertyName, byte defaultValue) {
583             String JavaDoc value = impl.getProperty (propertyName, null);
584             if (value == null) return defaultValue;
585             byte val = Byte.parseByte (value);
586             return val;
587         }
588
589         public void setByte (String JavaDoc propertyName, byte value) {
590             impl.setProperty (propertyName, Byte.toString (value));
591         }
592
593         public short getShort (String JavaDoc propertyName, short defaultValue) {
594             String JavaDoc value = impl.getProperty (propertyName, null);
595             if (value == null) return defaultValue;
596             short val = Short.parseShort (value);
597             return val;
598         }
599
600         public void setShort (String JavaDoc propertyName, short value) {
601             impl.setProperty (propertyName, Short.toString (value));
602         }
603
604         public Object JavaDoc getObject (String JavaDoc propertyName, Object JavaDoc defaultValue) {
605             String JavaDoc typeID = impl.getProperty (propertyName, null);
606             if (typeID == null) return defaultValue;
607             if (typeID.equals ("# null"))
608                 return null;
609             if (!typeID.startsWith ("# ")) {
610                 if (typeID.startsWith ("\"")) {
611                     String JavaDoc s = getString (propertyName, BAD_STRING);
612                     if (s == BAD_STRING) return defaultValue;
613                     return s;
614                 }
615                 System.out.println("Can not read object " + typeID + ". No reader registered for type " + typeID + ".");
616                 return defaultValue;
617             }
618             typeID = typeID.substring (2);
619             Class JavaDoc c = null;
620             try {
621                 c = Class.forName (typeID);
622             } catch (ClassNotFoundException JavaDoc e) {
623             }
624             if (c != null) {
625                 if (Map JavaDoc.class.isAssignableFrom (c)) {
626                     Map JavaDoc m = getMap (propertyName, BAD_MAP);
627                     if (m == BAD_MAP) return defaultValue;
628                     return m;
629                 }
630                 if (Object JavaDoc [].class.isAssignableFrom (c)) {
631                     Object JavaDoc[] os = getArray (propertyName, BAD_ARRAY);
632                     if (os == BAD_ARRAY) return defaultValue;
633                     return os;
634                 }
635                 if (Collection JavaDoc.class.isAssignableFrom (c)) {
636                     Collection JavaDoc co = getCollection (propertyName, BAD_COLLECTION);
637                     if (co == BAD_COLLECTION) return defaultValue;
638                     return co;
639                 }
640             }
641             Reader JavaDoc r = findReader (typeID);
642             if (r == null) {
643                 System.out.println("Can not read object. No reader registered for type " + typeID + ".");
644                 return defaultValue;
645             }
646             return r.read (typeID, getProperties (propertyName));
647         }
648
649         public void setObject (String JavaDoc propertyName, Object JavaDoc value) {
650             if (value == null) {
651                 impl.setProperty (propertyName, "# null");
652                 return;
653             }
654             if (value instanceof String JavaDoc) {
655                 setString (propertyName, (String JavaDoc) value);
656                 return;
657             }
658             if (value instanceof Map JavaDoc) {
659                 setMap (propertyName, (Map JavaDoc) value);
660                 return;
661             }
662             if (value instanceof Collection JavaDoc) {
663                 setCollection (propertyName, (Collection JavaDoc) value);
664                 return;
665             }
666             if (value instanceof Object JavaDoc[]) {
667                 setArray (propertyName, (Object JavaDoc[]) value);
668                 return;
669             }
670
671             // find register
672
Reader JavaDoc r = findReader (value.getClass ().getName ());
673             if (r == null) {
674                 System.out.println ("Can not write object " + value);
675                 return;
676             }
677
678             // write
679
r.write (value, getProperties (propertyName));
680             impl.setProperty (propertyName, "# " + value.getClass ().getName ());
681         }
682
683         public Object JavaDoc[] getArray (String JavaDoc propertyName, Object JavaDoc[] defaultValue) {
684             String JavaDoc typeID = impl.getProperty (propertyName, null);
685             String JavaDoc arrayType = impl.getProperty (propertyName + ".array_type", null);
686             Properties p = getProperties (propertyName);
687             int l = p.getInt ("length", -1);
688             if (l < 0) return defaultValue;
689             Object JavaDoc[] os = null;
690             try {
691                 os = (Object JavaDoc[]) Array.newInstance (
692                     getClassLoader ().loadClass (arrayType),
693                     l
694                 );
695             } catch (ClassNotFoundException JavaDoc ex) {
696                 ErrorManager.getDefault().notify(ex);
697                 os = new Object JavaDoc [l];
698             }
699             for (int i = 0; i < l; i++) {
700                 Object JavaDoc o = p.getObject ("" + i, BAD_OBJECT);
701                 if (o == BAD_OBJECT) return defaultValue;
702                 os [i] = o;
703             }
704             return os;
705         }
706
707         public void setArray (String JavaDoc propertyName, Object JavaDoc[] value) {
708             impl.setProperty (propertyName, "# array");
709             impl.setProperty (propertyName + ".array_type", value.getClass ().getComponentType ().getName ());
710             Properties p = getProperties (propertyName);
711             int i, k = value.length;
712             p.setInt ("length", k);
713             for (i = 0; i < k; i++)
714                 p.setObject ("" + i, value [i]);
715         }
716
717         public Collection JavaDoc getCollection (String JavaDoc propertyName, Collection JavaDoc defaultValue) {
718             String JavaDoc typeID = impl.getProperty (propertyName, null);
719             if (typeID == null) return defaultValue;
720             if (!typeID.startsWith ("# ")) return defaultValue;
721             Collection JavaDoc c = null;
722             try {
723                 c = (Collection JavaDoc) Class.forName (typeID.substring (2)).newInstance ();
724             } catch (ClassNotFoundException JavaDoc ex) {
725                 return defaultValue;
726             } catch (InstantiationException JavaDoc ex) {
727                 return defaultValue;
728             } catch (IllegalAccessException JavaDoc ex) {
729                 return defaultValue;
730             }
731             Properties p = getProperties (propertyName);
732             int i, k = p.getInt ("length", 0);
733             for (i = 0; i < k; i++) {
734                 Object JavaDoc o = p.getObject ("" + i, BAD_OBJECT);
735                 if (o == BAD_OBJECT) return defaultValue;
736                 c.add (o);
737             }
738             return c;
739         }
740
741         public void setCollection (String JavaDoc propertyName, Collection JavaDoc value) {
742             if (value == null) return;
743             impl.setProperty (propertyName, "# " + value.getClass ().getName ());
744             Properties p = getProperties (propertyName);
745             Iterator JavaDoc it = value.iterator ();
746             int i = 0;
747             p.setInt ("length", value.size ());
748             while (it.hasNext ()) {
749                 p.setObject ("" + i, it.next ());
750                 i++;
751             }
752         }
753
754         public Map JavaDoc getMap (String JavaDoc propertyName, Map JavaDoc defaultValue) {
755             String JavaDoc typeID = impl.getProperty (propertyName, null);
756             if (typeID == null) return defaultValue;
757             if (!typeID.startsWith ("# ")) return defaultValue;
758             Map JavaDoc m = null;
759             try {
760                 m = (Map JavaDoc) Class.forName (typeID.substring (2)).newInstance ();
761             } catch (ClassNotFoundException JavaDoc ex) {
762                 return defaultValue;
763             } catch (InstantiationException JavaDoc ex) {
764                 return defaultValue;
765             } catch (IllegalAccessException JavaDoc ex) {
766                 return defaultValue;
767             }
768             Properties p = getProperties (propertyName);
769             int i, k = p.getInt ("length", 0);
770             for (i = 0; i < k; i++) {
771                 Object JavaDoc key = p.getObject ("" + i + "-key", BAD_OBJECT);
772                 if (key == BAD_OBJECT) return defaultValue;
773                 Object JavaDoc value = p.getObject ("" + i + "-value", BAD_OBJECT);
774                 if (value == BAD_OBJECT) return defaultValue;
775                 m.put (key, value);
776             }
777             return m;
778         }
779
780         public void setMap (String JavaDoc propertyName, Map JavaDoc value) {
781             if (value == null) return;
782             impl.setProperty (propertyName, "# " + value.getClass ().getName ());
783             Properties p = getProperties (propertyName);
784             Iterator JavaDoc it = value.keySet ().iterator ();
785             int i = 0;
786             p.setInt ("length", value.size ());
787             while (it.hasNext ()) {
788                 Object JavaDoc o = it.next ();
789                 p.setObject ("" + i + "-key", o);
790                 p.setObject ("" + i + "-value", value.get (o));
791                 i++;
792             }
793         }
794
795         public Properties getProperties (String JavaDoc propertyName) {
796             return new DelegatingProperties (this, propertyName);
797         }
798         
799         private static ClassLoader JavaDoc classLoader;
800         private static ClassLoader JavaDoc getClassLoader () {
801             if (classLoader == null)
802                 //Thread.currentThread ().getContextClassLoader ();
803
classLoader = (ClassLoader JavaDoc) org.openide.util.Lookup.
804                     getDefault ().lookup (ClassLoader JavaDoc.class);
805             return classLoader;
806         }
807     }
808     
809     private static class DelegatingProperties extends Properties {
810
811         private Properties delegatingProperties;
812         private String JavaDoc root;
813
814
815         DelegatingProperties (Properties properties, String JavaDoc root) {
816             delegatingProperties = properties;
817             this.root = root;
818         }
819
820         public String JavaDoc getString (String JavaDoc propertyName, String JavaDoc defaultValue) {
821             return delegatingProperties.getString (root + '.' + propertyName, defaultValue);
822         }
823
824         public void setString (String JavaDoc propertyName, String JavaDoc value) {
825             delegatingProperties.setString (root + '.' + propertyName, value);
826         }
827
828         public int getInt (String JavaDoc propertyName, int defaultValue) {
829             return delegatingProperties.getInt (root + '.' + propertyName, defaultValue);
830         }
831
832         public void setInt (String JavaDoc propertyName, int value) {
833             delegatingProperties.setInt (root + '.' + propertyName, value);
834         }
835
836         public byte getByte (String JavaDoc propertyName, byte defaultValue) {
837             return delegatingProperties.getByte (root + '.' + propertyName, defaultValue);
838         }
839
840         public void setByte (String JavaDoc propertyName, byte value) {
841             delegatingProperties.setByte (root + '.' + propertyName, value);
842         }
843
844         public char getChar (String JavaDoc propertyName, char defaultValue) {
845             return delegatingProperties.getChar (root + '.' + propertyName, defaultValue);
846         }
847
848         public void setChar (String JavaDoc propertyName, char value) {
849             delegatingProperties.setChar (root + '.' + propertyName, value);
850         }
851
852         public boolean getBoolean (String JavaDoc propertyName, boolean defaultValue) {
853             return delegatingProperties.getBoolean (root + '.' + propertyName, defaultValue);
854         }
855
856         public void setBoolean (String JavaDoc propertyName, boolean value) {
857             delegatingProperties.setBoolean (root + '.' + propertyName, value);
858         }
859
860         public short getShort (String JavaDoc propertyName, short defaultValue) {
861             return delegatingProperties.getShort (root + '.' + propertyName, defaultValue);
862         }
863
864         public void setShort (String JavaDoc propertyName, short value) {
865             delegatingProperties.setShort (root + '.' + propertyName, value);
866         }
867
868         public long getLong (String JavaDoc propertyName, long defaultValue) {
869             return delegatingProperties.getLong (root + '.' + propertyName, defaultValue);
870         }
871
872         public void setLong (String JavaDoc propertyName, long value) {
873             delegatingProperties.setLong (root + '.' + propertyName, value);
874         }
875
876         public double getDouble (String JavaDoc propertyName, double defaultValue) {
877             return delegatingProperties.getDouble (root + '.' + propertyName, defaultValue);
878         }
879
880         public void setDouble (String JavaDoc propertyName, double value) {
881             delegatingProperties.setDouble (root + '.' + propertyName, value);
882         }
883
884         public float getFloat (String JavaDoc propertyName, float defaultValue) {
885             return delegatingProperties.getFloat (root + '.' + propertyName, defaultValue);
886         }
887
888         public void setFloat (String JavaDoc propertyName, float value) {
889             delegatingProperties.setFloat (root + '.' + propertyName, value);
890         }
891
892         public Object JavaDoc getObject (String JavaDoc propertyName, Object JavaDoc defaultValue) {
893             return delegatingProperties.getObject (root + '.' + propertyName, defaultValue);
894         }
895
896         public void setObject (String JavaDoc propertyName, Object JavaDoc value) {
897             delegatingProperties.setObject (root + '.' + propertyName, value);
898         }
899
900         public Object JavaDoc[] getArray (String JavaDoc propertyName, Object JavaDoc[] defaultValue) {
901             return delegatingProperties.getArray (root + '.' + propertyName, defaultValue);
902         }
903
904         public void setArray (String JavaDoc propertyName, Object JavaDoc[] value) {
905             delegatingProperties.setArray (root + '.' + propertyName, value);
906         }
907
908         public Collection JavaDoc getCollection (String JavaDoc propertyName, Collection JavaDoc defaultValue) {
909             return delegatingProperties.getCollection (root + '.' + propertyName, defaultValue);
910         }
911
912         public void setCollection (String JavaDoc propertyName, Collection JavaDoc value) {
913             delegatingProperties.setCollection (root + '.' + propertyName, value);
914         }
915
916         public Map JavaDoc getMap (String JavaDoc propertyName, Map JavaDoc defaultValue) {
917             return delegatingProperties.getMap (root + '.' + propertyName, defaultValue);
918         }
919
920         public void setMap (String JavaDoc propertyName, Map JavaDoc value) {
921             delegatingProperties.setMap (root + '.' + propertyName, value);
922         }
923
924         public Properties getProperties (String JavaDoc propertyName) {
925             return new DelegatingProperties (delegatingProperties, root + '.' + propertyName);
926         }
927     }
928 }
929
Popular Tags