KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > Database


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: Database.java,v 1.15 2004/01/03 10:39:41 per_nyfelt Exp $
8

9 package org.ozoneDB;
10
11 import org.ozoneDB.core.DbRemote.CommandThread;
12 import org.ozoneDB.core.DbRemote.DbXMLForObj;
13 import org.ozoneDB.core.*;
14 import org.ozoneDB.util.LogWriter;
15 import org.ozoneDB.xml.util.SAXChunkConsumer;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Node JavaDoc;
18 import org.xml.sax.ContentHandler JavaDoc;
19
20
21 /**
22  * This class represents the database for OzoneObjects within the server.<p>
23  *
24  * Note: The method parameters of type {@link OzoneRemote} are supposed to by
25  * proxy objects (of type {@link OzoneProxy}). However, since we are inside the
26  * server it's possible to pass an database object (of type {@link
27  * OzoneCompatible}) as a parameter. In this case the parameter should be
28  * substituted. Currently this is done by the invoke() method only.
29  *
30  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
31  * @author <a HREF="http://www.medium.net/">Medium.net</a>
32  * @version $Revision: 1.15 $Date: 2004/01/03 10:39:41 $
33  * @see OzoneInterface
34  */

35 public final class Database extends AbstractDatabase {
36
37     protected transient Env env;
38
39     public Database(Env _env) {
40         this.env = _env;
41         AbstractFactory.setDefaultDatabase(this);
42     }
43
44
45     public void reloadClasses() throws Exception JavaDoc {
46         throw new Exception JavaDoc("reloadClasses() must not be called from within the server.");
47     }
48
49
50     public User currentOwner() {
51         Thread JavaDoc thread = Thread.currentThread();
52         /*
53         if (thread instanceof Transaction) {
54            return ((Transaction)thread).owner();
55            }
56          */

57         if (thread instanceof CommandThread) {
58             return ((CommandThread) thread).owner();
59         } else {
60             env.fatalError(this, "Current thread is not a transaction or command!", null);
61             return null;
62         }
63     }
64
65
66     public OzoneProxy createObject(String JavaDoc className,int access,String JavaDoc objName,String JavaDoc sig,Object JavaDoc[] args) throws RuntimeException JavaDoc,OzoneObjectException {
67         env.logWriter.newEntry( this, "createObject()...", LogWriter.DEBUG3 );
68         try {
69             ObjectContainer container = env.transactionManager.currentTA().createObject( className, access, objName, sig, args, null );
70
71             try {
72                 OzoneProxy result = container.ozoneProxy();
73                 // env.logWriter.newEntry ("return: " + result.getClass().getName(), LogWriter.DEBUG);
74
return result;
75             } finally {
76                 container.unpin();
77             }
78         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
79             throw new OzoneObjectException("Caught during deleteObject()", e);
80         } catch (OzoneObjectException e) {
81             throw e;
82         } catch (Exception JavaDoc e) {
83             env.logWriter.newEntry(this,"createObject(): caught",e,LogWriter.DEBUG3 );
84             throw new RuntimeException JavaDoc("Caught during createObject(): ", e);
85         }
86     }
87
88
89     public OzoneProxy copyObject(OzoneRemote rObj) throws Exception JavaDoc {
90         env.logWriter.newEntry(this, "copyObject()...", LogWriter.DEBUG3);
91         ObjectContainer container = env.transactionManager.currentTA().copyObject( ((OzoneProxy)rObj).remoteID() );
92         try {
93             return container.ozoneProxy();
94         } finally {
95             container.unpin();
96         }
97     }
98
99
100     public void deleteObject( OzoneRemote rObj ) throws RuntimeException JavaDoc,OzoneObjectException {
101         try {
102             if (false) {
103                 env.logWriter.newEntry( this, "deleteObject()...", LogWriter.DEBUG3 );
104             }
105             env.transactionManager.currentTA().deleteObject( ((OzoneProxy)rObj).remoteID() );
106         } catch (OzoneObjectException e) {
107             throw e;
108         } catch (Exception JavaDoc e) {
109             throw new RuntimeException JavaDoc("Caught during deleteObject(): ", e);
110         }
111     }
112
113
114     public void nameObject(OzoneRemote rObj, String JavaDoc name) throws Exception JavaDoc {
115         if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
116             env.logWriter.newEntry(this, "nameObject()...", LogWriter.DEBUG3);
117         }
118         env.transactionManager.currentTA().nameObject(((OzoneProxy) rObj).remoteID(), name);
119     }
120
121
122     public String JavaDoc[] objectNames() throws Exception JavaDoc {
123        if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
124             env.logWriter.newEntry(this, "objectNames()...", LogWriter.DEBUG3);
125         }
126         Object JavaDoc[] keys = env.transactionManager.currentTA().objectNames().toArray();
127         String JavaDoc[] names = new String JavaDoc[keys.length];
128         for (int i = 0; i < keys.length; i++) {
129             names[i] = (String JavaDoc) keys[i];
130         }
131         return names;
132     }
133
134     public OzoneProxy objectForName(String JavaDoc name) throws Exception JavaDoc {
135         env.logWriter.newEntry(this, "objectForName()... ", LogWriter.DEBUG3);
136         return env.transactionManager.currentTA().objectForName(name);
137     }
138
139     public OzoneProxy objectForHandle(String JavaDoc handle) throws Exception JavaDoc {
140         env.logWriter.newEntry(this, "objectForHandle()... ", LogWriter.DEBUG3);
141         return env.transactionManager.currentTA().objectForID(new ObjectID(handle));
142     }
143
144     public Object JavaDoc invoke(OzoneProxy rObj, String JavaDoc methodName, String JavaDoc sig, Object JavaDoc[] args, int lockLevel)
145             throws Exception JavaDoc {
146
147         //one argument could have been "this", then we have to give
148
//a proxy instead of the actual object
149
for (int i = 0; i < args.length; i++) {
150             args[i] = ResultConverter.substituteOzoneCompatibles(args[i]);
151         }
152
153         Object JavaDoc result = env.transactionManager.currentTA().invokeObject(rObj.remoteID(), methodName, sig, args, lockLevel);
154
155         //also return could have been "this"...
156
result = ResultConverter.substituteOzoneCompatibles(result);
157         
158         return result;
159     }
160
161
162     public Object JavaDoc invoke(OzoneProxy rObj, int methodIndex, Object JavaDoc[] args, int lockLevel) throws Exception JavaDoc {
163
164         //one argument could have been "this", then we have to give
165
//a proxy instead of the actual object
166
for (int i = 0; i < args.length; i++) {
167             args[i] = ResultConverter.substituteOzoneCompatibles(args[i]);
168         }
169
170         Object JavaDoc result = env.transactionManager.currentTA().invokeObject(rObj.remoteID(), methodIndex, args, lockLevel);
171
172         //also return could have been "this"...
173
result = ResultConverter.substituteOzoneCompatibles(result);
174         
175         return result;
176     }
177
178
179     public OzoneCompatible fetch(OzoneProxy rObj, int lockLevel) throws /*Exception*/ org.ozoneDB.ObjectNotFoundException, java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc, org.ozoneDB.TransactionException, org.ozoneDB.core.TransactionError {
180         // env.logWriter.newEntry (this, "fetch()...", LogWriter.DEBUG3);
181

182         Transaction currentTransaction = getCurrentTransaction();
183         ObjectID id = rObj.remoteID();
184
185         ObjectContainer container = currentTransaction.acquireObject( id, lockLevel );
186
187         try {
188             return container.target();
189         } finally {
190             container.unpin();
191         }
192     }
193
194
195     public Node JavaDoc xmlForObject(OzoneRemote rObj, Document JavaDoc domFactory) throws Exception JavaDoc {
196
197         // creating the chunk is not really needed but should work ;)
198
DbXMLForObj command = new DbXMLForObj((OzoneProxy) rObj);
199         command.perform(env.transactionManager.currentTA());
200         byte[] bytes = (byte[]) command.result;
201
202         SAXChunkConsumer consumer = new SAXChunkConsumer(domFactory, null);
203         consumer.processChunk(bytes);
204
205         return consumer.getResultNode();
206     }
207
208
209     public void xmlForObject(OzoneRemote rObj, ContentHandler JavaDoc ch) throws Exception JavaDoc {
210
211         // creating the chunk is not really needed but should work ;)
212
DbXMLForObj command = new DbXMLForObj((OzoneProxy) rObj);
213         command.perform(env.transactionManager.currentTA());
214         byte[] bytes = (byte[]) command.result;
215
216         SAXChunkConsumer consumer = new SAXChunkConsumer(ch);
217         consumer.processChunk(bytes);
218     }
219
220     /**
221         Internal method. This method is called by {@link OzoneProxy}s when they are dying (during finalize()). This
222         is required, as the database may track the references the database client has to objects within the database
223         in order to properly support garbage collection. If this method is called from anyone else than from the
224         {@link OzoneProxy#finalize()}-Method, data loss may occur!
225
226         @param proxy the OzoneProxy object which is dying. It may call this method exaclty once.
227     */

228     public void notifyProxyDeath(OzoneProxy proxy) {
229         // We do nothing here as we do not care wether OzoneProxys within the local databases die.
230
}
231         
232     /**
233         For internal use.
234     */

235     public Env getEnv() {
236         return env;
237     }
238     
239     /**
240         For internal use.
241     */

242     public Transaction getCurrentTransaction() {
243         TransactionManager transactionManager = getEnv().getTransactionManager();
244         Transaction currentTransaction = transactionManager.currentTA();
245         
246         return currentTransaction;
247     }
248 }
249 // :indentSize=4:tabSize=4:noTabs=true:
250
Popular Tags