KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > odmg > OzoneODMGDatabase


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: OzoneODMGDatabase.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
8

9 package org.ozoneDB.odmg;
10
11 import java.util.*;
12 import org.odmg.*;
13 import org.ozoneDB.ExternalDatabase;
14 import org.ozoneDB.LocalDatabase;
15 import org.ozoneDB.RemoteDatabase;
16 import org.ozoneDB.OzoneProxy;
17 import org.ozoneDB.OzoneRemote;
18 import org.ozoneDB.OzoneCompatible;
19
20
21 /**
22  * Implementation of the ODMG {@link org.odmg.Database} interface.
23  *
24  *
25  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
26  * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $
27  */

28 public class OzoneODMGDatabase implements EnhDatabase {
29     
30     private ExternalDatabase db;
31     
32     private OzoneODMG factory;
33     
34     private int accessMode = NOT_OPEN;
35     
36     
37     public OzoneODMGDatabase( OzoneODMG _factory ) {
38         factory = _factory;
39     }
40     
41     
42     public ExternalDatabase underlying() {
43         return db;
44     }
45     
46     
47     protected int mode() {
48         return accessMode;
49     }
50     
51     
52     /**
53      * Open this ODMG database.
54      * @param _url URL of the database (ozonedb:remote://host:port or ozonedb:local://datadir)
55      */

56     public synchronized void open( String JavaDoc _url, int _accessMode ) throws ODMGException {
57         if (db != null) {
58             throw new DatabaseOpenException( "Database is already open." );
59         }
60         
61         switch (_accessMode) {
62         case OPEN_READ_ONLY: {
63             break;
64             }
65         case OPEN_READ_WRITE: {
66             break;
67             }
68         case OPEN_EXCLUSIVE: {
69             throw new ODMGRuntimeException( "OPEN_EXCLUSIVE not supported." );
70             }
71         default:
72             throw new ODMGRuntimeException( "Illegal open mode." );
73         }
74         
75         try {
76             db = ExternalDatabase.openDatabase( _url );
77         } catch (Exception JavaDoc e) {
78             throw new DatabaseNotFoundException( e.toString() );
79         }
80         
81         accessMode = _accessMode;
82         factory.databaseOpened( this );
83     }
84     
85     
86     public void close() throws ODMGException {
87         if (db == null) {
88             throw new DatabaseClosedException( "Database not open." );
89         }
90         
91         try {
92             db.close();
93         } catch (Exception JavaDoc e) {
94             throw new ODMGException( e.toString() );
95         } finally {
96             db = null;
97             accessMode = NOT_OPEN;
98             factory.databaseClosed( this );
99         }
100     }
101     
102     
103     protected void finalize() throws Throwable JavaDoc {
104         if (db != null) {
105             close();
106         }
107     }
108     
109     
110     /**
111      * The ozone ODMG interface does not implement this method, use
112      * createObject() instead.
113      * <p>
114      * ozone uses proxies to control objects inside the database. Unfortunately
115      * ODMG is perfectly not aware of this kind of architecture.
116      */

117     public void makePersistent( Object JavaDoc object ) {
118         if (OzoneODMGTransaction.current() == null) {
119             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
120         }
121         
122         if (db == null) {
123             throw new DatabaseClosedException( "Database not open." );
124         }
125         
126         // throw exception if someone want to use this method as intended
127
// by ODMG
128
if (object instanceof OzoneCompatible) {
129             throw new ODMGRuntimeException( object.getClass().getName()
130                     + ": create a persistent instance via createPersistent." );
131         }
132         
133         // do nothing if the object is an proxy already
134
if (object instanceof OzoneProxy) {
135             return;
136         }
137     }
138     
139     
140     /**
141      * Create a new persistent instance of the given class.
142      * <p>
143      * THIS METHOD IS NOT IN THE ODMG 3.0 STANDARD!
144      * <p>
145      * It must be executed in the context of an open transaction.
146      * If the transaction in which this method is executed commits,
147      * then the object is made durable.
148      * ClassNotPersistenceCapableException is thrown if the implementation cannot make
149      * the object persistent because of the type of the object.
150      */

151     public Object JavaDoc createPersistent( Class JavaDoc cl ) {
152         if (OzoneODMGTransaction.current() == null) {
153             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
154         }
155         
156         if (db == null) {
157             throw new DatabaseClosedException( "Database not open." );
158         }
159         
160         if (cl.isAssignableFrom( OzoneCompatible.class )) {
161             throw new ClassNotPersistenceCapableException( cl.getName() );
162         }
163         
164         try {
165             return db.createObject( cl.getName(), ExternalDatabase.Public, null );
166         } catch (Exception JavaDoc e) {
167             throw new ODMGRuntimeException( e.toString() );
168         }
169     }
170     
171     
172     public void deletePersistent( Object JavaDoc object ) {
173         if (OzoneODMGTransaction.current() == null) {
174             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
175         }
176         
177         if (db == null) {
178             throw new DatabaseClosedException( "Database not open." );
179         }
180         
181         if (!(object instanceof OzoneProxy)) {
182             throw new ObjectNotPersistentException( object.getClass().getName() );
183         }
184         
185         try {
186             db.deleteObject( (OzoneRemote)object );
187         } catch (Exception JavaDoc e) {
188             throw new ODMGRuntimeException( e.toString() );
189         }
190     }
191     
192     
193     public void bind( Object JavaDoc object, String JavaDoc name ) {
194         if (OzoneODMGTransaction.current() == null) {
195             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
196         }
197         
198         if (db == null) {
199             throw new DatabaseClosedException( "Database not open." );
200         }
201         
202         if (!(object instanceof OzoneProxy)) {
203             throw new ClassNotPersistenceCapableException( object.getClass().getName() );
204         }
205         
206         try {
207             db.nameObject( (OzoneRemote)object, name );
208         } catch (Exception JavaDoc e) {
209             throw new ODMGRuntimeException( e.toString() );
210         }
211     }
212     
213     
214     public void unbind( String JavaDoc name ) throws ObjectNameNotFoundException {
215         if (OzoneODMGTransaction.current() == null) {
216             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
217         }
218         
219         if (db == null) {
220             throw new DatabaseClosedException( "Database not open." );
221         }
222         
223         Object JavaDoc obj = lookup( name );
224         try {
225             db.nameObject( (OzoneRemote)obj, null );
226         } catch (Exception JavaDoc e) {
227             throw new ODMGRuntimeException( e.toString() );
228         }
229     }
230     
231     
232     public Object JavaDoc lookup( String JavaDoc name ) throws ObjectNameNotFoundException {
233         if (OzoneODMGTransaction.current() == null) {
234             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
235         }
236         
237         if (db == null) {
238             throw new DatabaseClosedException( "Database not open." );
239         }
240         
241         Object JavaDoc result;
242         try {
243             result = db.objectForName( name );
244         } catch (Exception JavaDoc e) {
245             throw new ODMGRuntimeException( e.toString() );
246         }
247         
248         if (result == null) {
249             throw new ObjectNameNotFoundException( name );
250         }
251         
252         return result;
253     }
254     
255     
256     public boolean containsObject( Object JavaDoc obj ) {
257         if (OzoneODMGTransaction.current() == null) {
258             throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
259         }
260         
261         if (db == null) {
262             throw new DatabaseClosedException( "Database not open." );
263         }
264         
265         if (!(obj instanceof OzoneProxy)) {
266             throw new ClassNotPersistenceCapableException( obj.getClass().getName() );
267         }
268         
269         OzoneProxy proxy = (OzoneProxy)obj;
270         return proxy.link == db;
271     }
272     
273 }
274
Popular Tags