KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > OzoneObject


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: OzoneObject.java,v 1.15 2003/12/06 20:59:44 per_nyfelt Exp $
8

9 package org.ozoneDB;
10
11 import org.ozoneDB.core.ObjectContainer;
12 import org.ozoneDB.core.ObjectID;
13 import org.xml.sax.ContentHandler JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15
16
17 /**
18  * This class can be extended to build actual database objects. It provides a
19  * default implementation of the {@link OzoneCompatible} interface.
20  *
21  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
22  * @version $Revision: 1.15 $Date: 2003/12/06 20:59:44 $
23  */

24 public class OzoneObject implements OzoneCompatible {
25
26     final static long serialVersionUID = 3171995582505722338L;
27
28     transient ObjectContainer container = null;
29
30
31     public int hashCode() {
32         return container.id().hashCode();
33     }
34
35
36     public synchronized void setContainer(ObjectContainer _container) {
37         container = _container;
38     }
39
40
41     public OzoneProxy self() {
42         if (container == null) {
43             throw new RuntimeException JavaDoc("The object of class " + this.getClass().getName() + " is not (yet) associated to a database container.");
44         }
45         return container.ozoneProxy();
46     }
47
48
49     public ObjectContainer container() {
50         if (container == null) {
51             throw new RuntimeException JavaDoc("The object of class " + this.getClass().getName() + " is not (yet) associated to a database container.");
52         }
53         return container;
54     }
55
56     /**
57      * Retrieves a handle to a specific instance of an OzoneObject. A handle is
58      * the externalizeable equivalent of OzoneProxy. The purpose of handles is to enable
59      * detached systems (such as web interfaces) that are not able to use OzoneProxy
60      * objects directly a means to access specific objects where object naming is not
61      * practical or convenient. A handle is valid for the lifetime of
62      * the OzoneObject it references. Unlike OzoneProxy, retrieving a handle on an
63      * object does nothing to guarantee its existence. While the handle is valid
64      * for the lifetime of the object it refers to, that object's lifetime may
65      * be shorter than the handle's.
66      * <br><br>
67      * The resultant string representation will be composed
68      * entirely of alphanumeric characters [A-Z], [a-z], and [0-9]; as such,
69      * it can safely and reliably be used in URLs without need for escaping.
70      *
71      */

72     public String JavaDoc getHandle() {
73         if (container == null) {
74             throw new RuntimeException JavaDoc("The object of class " + this.getClass().getName() + " is not (yet) associated to a database container.");
75         }
76         return container.id().toString();
77     }
78
79     /**
80      * @deprecated use getHandle()
81      */

82     public String JavaDoc handle() {
83         return getHandle();
84     }
85     
86     public OzoneInterface database() {
87         if (container == null) {
88             return CurrentDatabase.get();
89         }
90         return container.database();
91     }
92
93
94     public String JavaDoc toString() {
95         return "OzoneObject, ID: " + (container != null ? container.id().toString() : "null");
96     }
97
98
99     /**
100      * This default implementation of the onCreate() method does nothing.
101      */

102     public void onCreate() {
103     }
104
105     /**
106      * This method will be automaticly called immediately after this object is
107      * loaded (deserialized) from storage.
108      * This default implementation of the onActivate() method does nothing.
109      */

110     public void onActivate() {
111         //System.out.println("[OzoneObject] - onActivate called for " + getClass().getName());
112
}
113
114     /**
115         Calling this tells the OzoneDatabase to upgrade the locking of this OzoneObject
116         (more precisely: of this {@link ObjectContainer}) to write-locking if it is not already
117         write locked. After calling this, you may safely change members if the Object.
118         <DIV>
119             Note that calling this method currently should only be done from {@link #onActivate},
120             though it may be possible to call it from any user method.
121         </DIV>
122         <DIV>
123             Please do not override this method. We reserve the right to make it final.
124         </DIV>
125         <DIV>
126             Some specifics on the implementation: Require write locking upgrades the locking of this object
127             to write-locking, if it was not already write-locked. If the upgrade is not possible, because other
128             transactions also read-locked this object, this method waits until this is complete. If a deadlock
129             happens, this method may throw a TransactionError due to that deadlock. In this case, the transaction
130             is retried, which means that calling {@link #onActivate()} is retried. So it is guaranteed that write-locking
131             is established after this call.
132         </DIV>
133     */

134     protected void requireWriteLocking() {
135         container.requireWriteLocking();
136     }
137
138     /**
139      * This method will be automaticly called immediately before this object is
140      * stored (serialized) to storage.
141      */

142     public void onPassivate() {
143         //System.out.println("[OzoneObject] - onPassivate called for " + getClass().getName());
144
}
145
146     /**
147      * This default implementation of the onDelete() method does nothing.
148      */

149     public void onDelete() {
150     }
151
152
153     // This Method is not needed anymore, because it is only used in ClassicStore
154
/**
155      * This default implementation of the size() method. It returns just -1 to
156      * signal that a default value should be used.
157      *
158     public int size() throws Exception {
159         return -1;
160     }
161     */

162
163
164     /**
165      * This default implementation of the toSAX() method. It returns just false
166      * to signal that a default value should be used.
167      */

168     public boolean toXML(ContentHandler JavaDoc ch) throws SAXException JavaDoc {
169         return false;
170     }
171
172
173     public void deleteRecursive() {
174         throw new RuntimeException JavaDoc("deleteRecursive() is not implemented yet.");
175     }
176
177     /**
178       Returns the ObjectID of the represented ozone object. ObjectIDs are equal for equal
179       ozone objects and different for different ozone objects. They are comparable, so that
180       ozone objects may use {@link org.ozoneDB.core.ObjectID#compareTo(Object)} in comparison functions.
181       <p>
182         Currently, ObjectID exposes other methods than {@link org.ozoneDB.core.ObjectID#equals(Object)} and
183         {@link org.ozoneDB.core.ObjectID#compareTo(Object)}. However, they should not be used, as ObjectIDs should
184         be, apart from this method, opaque.
185       </p>
186      @return the ObjectID of the represented ozone object
187     */

188     public ObjectID getObjectID() {
189         return container.id();
190     }
191     
192     /**
193      * Returns wether or not the passed object is equal to this object. Two ozone
194      * objects are considered equal when their object ids are equal.
195      * If you pass on OzoneObject, its <code>getObjectID()</code> will be called
196      * and compared with <code>this.getObjectID()</code>. The same goes for
197      * <code>OzoneProxy</code> instances.
198      *
199      * @param o the object to test for equality
200      */

201     public boolean equals(Object JavaDoc o) {
202         // as we normally only pass proxies and not the actual object itself
203
// the next 2 tests will probably never return true, but are included
204
// for completeness
205
if (o == this) {
206             return true;
207         } else if (o instanceof OzoneObject && getObjectID().equals(((OzoneObject) o).getObjectID())) {
208             return true;
209         } else {
210             return o instanceof OzoneProxy && getObjectID().equals(((OzoneProxy) o).getObjectID());
211         }
212     }
213     
214 }
215
216 // :indentSize=4:tabSize=4:noTabs=true:
217
Popular Tags