KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > OzoneProxy


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: OzoneProxy.java,v 1.5 2003/09/24 12:42:23 leomekenkamp Exp $
8

9 package org.ozoneDB;
10
11 import java.io.*;
12
13 import org.ozoneDB.DxLib.DxHashMap;
14 import org.ozoneDB.DxLib.DxMap;
15 import org.ozoneDB.core.Env;
16 import org.ozoneDB.core.ObjectID;
17 import org.ozoneDB.core.GarbageCollector;
18 import org.ozoneDB.core.xml.Consts;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20
21 /**
22  * Proxy of an OzoneRemote object.
23  *
24  *
25  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
26  * @author <a HREF="http://www.medium.net/">Medium.net</a>
27  * @version $Revision: 1.5 $Date: 2003/09/24 12:42:23 $
28  */

29 public class OzoneProxy implements OzoneRemote, Externalizable {
30
31     private final static long serialVersionUID = 3L;
32
33     /**
34      * Table of stream to OzoneInterface pairs. The method {@link #readExternal(ObjectInput)} checks this
35      * table to find corresponding database link.
36      */

37     public static DxMap linkTable = new DxHashMap();
38
39
40     public transient OzoneInterface link = null;
41
42     public ObjectID remoteID;
43
44
45     /**
46      * This constructor will only be called, when the object is constructed
47      * from a stream.
48      */

49     public OzoneProxy() {
50         //we assume that we are inside the kernel
51
Env env = Env.currentEnv();
52         if (env != null) {
53             link = env.database;
54         }
55     }
56
57
58     /**
59      * This constructor is only be called, when this object is constructed
60      * inside the database. However, it may be the result of a RMI call.
61      */

62     public OzoneProxy(ObjectID id, OzoneInterface link) {
63         this.link = link;
64         remoteID = (ObjectID) id.clone();
65     }
66
67
68     public OzoneProxy(OzoneProxy rhs) {
69         link = rhs.link;
70         remoteID = (ObjectID) rhs.remoteID.clone();
71     }
72
73
74     public boolean isSame(OzoneProxy proxy) {
75         return remoteID.equals(proxy.remoteID);
76     }
77
78
79     /**
80      * Base implementation of equals(). May be overwritten by an implementation
81      * of the actual database object. In that case the method is handled, as all
82      * remote methods, on the server side.
83      */

84     public boolean equals(Object JavaDoc obj) {
85         if (obj instanceof OzoneProxy && obj != null) {
86             return isSame((OzoneProxy) obj);
87         } else {
88             return false;
89         }
90     }
91
92
93     /**
94      * Base implementation of hashCode(). May be overwritten by an implementation
95      * of the actual database object. In that case the method is handled, as all
96      * remote methods, on the server side.
97      */

98     public int hashCode() {
99         return remoteID.hashCode();
100     }
101
102
103     /**
104      * Base implementation of toString() that runs client side only.
105      * When you want to override this method with one that runs on the server
106      * side, as all remote methods, you need to define <code>public String toString()</code>
107      * in your <code>OzoneRemote</code> extending interface so that OPP will
108      * generate a remote call. For instance: <code>
109      *
110      * public interface Foo extends OzoneRemote {
111      *
112      * // ensure OPP will create a remote call
113      * public String toString();
114      *
115      * // other methods
116      * }
117      *
118      * </code>
119      * Providing an implementation for <code>toString()</code> in your
120      * <code>OzoneObject</code> extending class is optional; if you do not, then
121      * the implementation of <code>toString()</code> in <code>OzoneObject</code>
122      * will be used server-side conform normal java method override rules.
123      *
124      * @return String string representation of this proxy, containing a string
125      * representation of the ozone id of the object this proxy represents
126      */

127     public String JavaDoc toString() {
128         return getClass().toString() + " remoteID:" + remoteID.toString();
129     }
130
131
132     public ObjectID remoteID() {
133         return remoteID;
134     }
135
136     /**
137       Returns the ObjectID of the represented ozone object. ObjectIDs are equal for equal
138       ozone objects and different for different ozone objects. They are comparable, so that
139       ozone objects may use {@link org.ozoneDB.core.ObjectID#compareTo(Object)} in comparison functions.
140       <p>
141         Currently, ObjectID exposes other methods than {@link org.ozoneDB.core.ObjectID#equals(Object)} and
142         {@link org.ozoneDB.core.ObjectID#compareTo(Object)}. However, they should not be used, as ObjectIDs should
143         be, apart from this method, opaque.
144       </p>
145      @return the ObjectID of the represented ozone object
146     */

147     public ObjectID getObjectID() {
148       return remoteID();
149     }
150
151     /**
152      * Retrieves a handle to a specific instance of an OzoneObject. <br>
153      * A handle is
154      * the externalizeable equivalent of OzoneProxy. The purpose of handles is to enable
155      * detached systems (such as web interfaces) that are not able to use OzoneProxy
156      * objects directly a means to access specific objects where object naming is not
157      * practical or convenient. A handle is valid for the lifetime of
158      * the OzoneObject it references. Unlike OzoneProxy, retrieving a handle on an
159      * object does nothing to guarantee its existence. While the handle is valid
160      * for the lifetime of the object it refers to, that object's lifetime may
161      * be shorter than the handle's.
162      * <br><br>
163      * The resultant string representation will be composed
164      * entirely of alphanumeric characters [A-Z], [a-z], and [0-9]; as such,
165      * it can safely and reliably be used in URLs without need for escaping.
166      *
167      */

168     public String JavaDoc getHandle() {
169         return remoteID.toString();
170     }
171     
172     /**
173      * @deprecated use getHandle()
174      */

175     public String JavaDoc handle() {
176         return getHandle();
177     }
178
179     /**
180      * <p>Adds the required attributes for a simple XLink which points to this
181      * proxy to an attribute list.</p>
182      *
183      * @param atts The SAX attribute list to which the attributes will be added.
184      */

185     public void createProxyLinkAttributes(AttributesImpl JavaDoc atts) {
186         atts.addAttribute(Consts.ATTR_XLINK_NAMESPACE,
187                           Consts.ATTR_XLINK_TYPE_LOCAL, Consts.ATTR_XLINK_TYPE_RAW,
188                           "PCDATA", "simple");
189         atts.addAttribute(Consts.ATTR_XLINK_NAMESPACE,
190                           Consts.ATTR_XLINK_HREF_LOCAL, Consts.ATTR_XLINK_HREF_RAW,
191                           "PCDATA", String.valueOf(remoteID));
192     }
193
194
195     /**
196      * Method to use the proxy without a generated stub. The method signature
197      * will be generated from the actual argument types. This may be in
198      * some cases!
199      */

200     public Object JavaDoc invoke(String JavaDoc methodName, int lockLevel) throws Exception JavaDoc {
201         Object JavaDoc[] args = {};
202         return link.invoke(this, methodName, null, args, lockLevel);
203     }
204
205
206     /**
207      * Method to use the proxy without a generated stub.
208      */

209     public Object JavaDoc invoke(String JavaDoc methodName, Object JavaDoc arg1, int lockLevel) throws Exception JavaDoc {
210         Object JavaDoc[] args = {arg1};
211         return link.invoke(this, methodName, null, args, lockLevel);
212     }
213
214
215     /**
216      * Method to use the proxy without a generated stub.
217      */

218     public Object JavaDoc invoke(String JavaDoc methodName, Object JavaDoc arg1, Object JavaDoc arg2, int lockLevel) throws Exception JavaDoc {
219         Object JavaDoc[] args = {arg1, arg2};
220         return link.invoke(this, methodName, null, args, lockLevel);
221     }
222
223
224     /**
225      * Method to use the proxy without a generated stub.
226      */

227     public Object JavaDoc invoke(String JavaDoc methodName, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, int lockLevel) throws Exception JavaDoc {
228         Object JavaDoc[] args = {arg1, arg2, arg3};
229         return link.invoke(this, methodName, null, args, lockLevel);
230     }
231
232     /**
233         Notify the database link that this reference dies.
234     */

235     public void finalize() {
236         if (link!=null) {
237             try {
238                 link.notifyProxyDeath(this);
239             } finally {
240                 link = null; // We notify the proxy death only once.
241
}
242         }
243     }
244
245     public void writeExternal(ObjectOutput out) throws IOException {
246         if (out instanceof GarbageCollector.GarbageCollectorProxyObjectIdentificationObjectOutputStream) {
247             ((GarbageCollector.GarbageCollectorProxyObjectIdentificationObjectOutputStream) out).notifyOzoneProxyEncountered(this);
248         }
249
250         out.writeLong(remoteID.value());
251         // out.writeObject (remoteID);
252
}
253
254
255     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
256         remoteID = new ObjectID(in.readLong());
257         // remoteID = (ObjectID)in.readObject();
258

259         ExternalDatabase db = (ExternalDatabase) linkTable.elementForKey(in);
260         if (db != null) {
261             link = db.linkForProxy(this);
262             // System.out.println ("*** Proxy link updated.");
263
// new Exception().fillInStackTrace().printStackTrace();
264
}
265     }
266
267 }
268
Popular Tags