KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > Properties


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.io.*;
27
28 import org.myoodb.util.*;
29
30 public final class Properties extends java.util.Properties JavaDoc
31 {
32     protected Observable m_observable;
33
34     public Properties()
35     {
36         m_observable = new Observable();
37     }
38
39     public Properties(Properties defaults)
40     {
41         super(defaults);
42
43         m_observable = new Observable();
44     }
45
46     public void addObserver(java.util.Observer JavaDoc observer)
47     {
48         m_observable.addObserver(observer);
49     }
50
51     public void removeObserver(java.util.Observer JavaDoc observer)
52     {
53         m_observable.deleteObserver(observer);
54     }
55
56     public void notifyObservers()
57     {
58         m_observable.notifyObservers(this);
59     }
60
61     public boolean hasChanged()
62     {
63         return m_observable.hasChanged();
64     }
65
66     public void setProperty(String JavaDoc key, Object JavaDoc val)
67     {
68         m_observable.setChanged();
69         try
70         {
71             ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
72             ObjectOutputStream out = new ObjectOutputStream(buf);
73             out.writeObject(val);
74             out.close();
75
76             MimeBase64 mimeBase64 = new MimeBase64();
77             mimeBase64.translate(buf.toByteArray());
78
79             super.setProperty(key, new String JavaDoc(mimeBase64.getCharArray()));
80         }
81         catch (Exception JavaDoc e)
82         {
83             throw new org.myoodb.exception.InternalException("Caught during set property: " + e, e);
84         }
85     }
86
87     public Object JavaDoc getProperty(String JavaDoc key, Object JavaDoc defaultVar)
88     {
89         try
90         {
91             String JavaDoc result = super.getProperty(key, (String JavaDoc)defaultVar);
92             if (result != null)
93             {
94                 MimeBase64 mimeBase64 = new MimeBase64();
95                 mimeBase64.translate(result.toCharArray());
96
97                 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(mimeBase64.getByteArray()));
98                 return in.readObject();
99             }
100             else
101             {
102                 return null;
103             }
104         }
105         catch (Exception JavaDoc e)
106         {
107             throw new org.myoodb.exception.InternalException("Caught during get property: " + e, e);
108         }
109     }
110
111     public void setLongProperty(String JavaDoc key, long val)
112     {
113         setProperty(key, String.valueOf(val));
114     }
115
116     public long getLongProperty(String JavaDoc key, long defaultVar)
117     {
118         String JavaDoc result = getProperty(key, String.valueOf(defaultVar));
119         return Long.valueOf(result).longValue();
120     }
121 }
122
123 class Observable extends java.util.Observable JavaDoc
124 {
125     public Observable()
126     {
127     }
128
129     public void setChanged()
130     {
131         super.setChanged();
132     }
133 }
134
Popular Tags