001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027:
028: public final class Properties extends java.util.Properties {
029: protected Observable m_observable;
030:
031: public Properties() {
032: m_observable = new Observable();
033: }
034:
035: public Properties(Properties defaults) {
036: super (defaults);
037:
038: m_observable = new Observable();
039: }
040:
041: public void addObserver(java.util.Observer observer) {
042: m_observable.addObserver(observer);
043: }
044:
045: public void removeObserver(java.util.Observer observer) {
046: m_observable.deleteObserver(observer);
047: }
048:
049: public void notifyObservers() {
050: m_observable.notifyObservers(this );
051: }
052:
053: public boolean hasChanged() {
054: return m_observable.hasChanged();
055: }
056:
057: public void setProperty(String key, Object val) {
058: m_observable.setChanged();
059:
060: try {
061: ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
062: ObjectOutputStream out = new ObjectOutputStream(buf);
063: out.writeObject(val);
064: out.close();
065:
066: org.myoodb.util.MimeBase64 mimeBase64 = new org.myoodb.util.MimeBase64();
067: mimeBase64.translate(buf.toByteArray());
068:
069: super .setProperty(key,
070: new String(mimeBase64.getCharArray()));
071: } catch (Exception e) {
072: throw new org.myoodb.exception.InternalException(
073: "Caught during set property: " + e, e);
074: }
075: }
076:
077: public Object getProperty(String key, Object defaultVar) {
078: try {
079: String result = super .getProperty(key, (String) defaultVar);
080: if (result != null) {
081: org.myoodb.util.MimeBase64 mimeBase64 = new org.myoodb.util.MimeBase64();
082: mimeBase64.translate(result.toCharArray());
083:
084: ObjectInputStream in = new ObjectInputStream(
085: new ByteArrayInputStream(mimeBase64
086: .getByteArray()));
087: return in.readObject();
088: } else {
089: return null;
090: }
091: } catch (Exception e) {
092: throw new org.myoodb.exception.InternalException(
093: "Caught during get property: " + e, e);
094: }
095: }
096:
097: public void setLongProperty(String key, long val) {
098: setProperty(key, String.valueOf(val));
099: }
100:
101: public long getLongProperty(String key, long defaultVar) {
102: String result = getProperty(key, String.valueOf(defaultVar));
103: return Long.valueOf(result).longValue();
104: }
105:
106: static class Observable extends java.util.Observable {
107: public Observable() {
108: }
109:
110: public void setChanged() {
111: super.setChanged();
112: }
113: }
114: }
|