001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: OzoneODMGDatabase.java,v 1.1 2002/05/08 15:03:21 per_nyfelt Exp $
008:
009: package org.ozoneDB.odmg;
010:
011: import java.util.*;
012: import org.odmg.*;
013: import org.ozoneDB.ExternalDatabase;
014: import org.ozoneDB.LocalDatabase;
015: import org.ozoneDB.RemoteDatabase;
016: import org.ozoneDB.OzoneProxy;
017: import org.ozoneDB.OzoneRemote;
018: import org.ozoneDB.OzoneCompatible;
019:
020: /**
021: * Implementation of the ODMG {@link org.odmg.Database} interface.
022: *
023: *
024: * @author <a href="http://www.softwarebuero.de/">SMB</a>
025: * @version $Revision: 1.1 $Date: 2002/05/08 15:03:21 $
026: */
027: public class OzoneODMGDatabase implements EnhDatabase {
028:
029: private ExternalDatabase db;
030:
031: private OzoneODMG factory;
032:
033: private int accessMode = NOT_OPEN;
034:
035: public OzoneODMGDatabase(OzoneODMG _factory) {
036: factory = _factory;
037: }
038:
039: public ExternalDatabase underlying() {
040: return db;
041: }
042:
043: protected int mode() {
044: return accessMode;
045: }
046:
047: /**
048: * Open this ODMG database.
049: * @param _url URL of the database (ozonedb:remote://host:port or ozonedb:local://datadir)
050: */
051: public synchronized void open(String _url, int _accessMode)
052: throws ODMGException {
053: if (db != null) {
054: throw new DatabaseOpenException("Database is already open.");
055: }
056:
057: switch (_accessMode) {
058: case OPEN_READ_ONLY: {
059: break;
060: }
061: case OPEN_READ_WRITE: {
062: break;
063: }
064: case OPEN_EXCLUSIVE: {
065: throw new ODMGRuntimeException(
066: "OPEN_EXCLUSIVE not supported.");
067: }
068: default:
069: throw new ODMGRuntimeException("Illegal open mode.");
070: }
071:
072: try {
073: db = ExternalDatabase.openDatabase(_url);
074: } catch (Exception e) {
075: throw new DatabaseNotFoundException(e.toString());
076: }
077:
078: accessMode = _accessMode;
079: factory.databaseOpened(this );
080: }
081:
082: public void close() throws ODMGException {
083: if (db == null) {
084: throw new DatabaseClosedException("Database not open.");
085: }
086:
087: try {
088: db.close();
089: } catch (Exception e) {
090: throw new ODMGException(e.toString());
091: } finally {
092: db = null;
093: accessMode = NOT_OPEN;
094: factory.databaseClosed(this );
095: }
096: }
097:
098: protected void finalize() throws Throwable {
099: if (db != null) {
100: close();
101: }
102: }
103:
104: /**
105: * The ozone ODMG interface does not implement this method, use
106: * createObject() instead.
107: * <p>
108: * ozone uses proxies to control objects inside the database. Unfortunately
109: * ODMG is perfectly not aware of this kind of architecture.
110: */
111: public void makePersistent(Object object) {
112: if (OzoneODMGTransaction.current() == null) {
113: throw new TransactionNotInProgressException(
114: "Thread has not joined a transaction.");
115: }
116:
117: if (db == null) {
118: throw new DatabaseClosedException("Database not open.");
119: }
120:
121: // throw exception if someone want to use this method as intended
122: // by ODMG
123: if (object instanceof OzoneCompatible) {
124: throw new ODMGRuntimeException(
125: object.getClass().getName()
126: + ": create a persistent instance via createPersistent.");
127: }
128:
129: // do nothing if the object is an proxy already
130: if (object instanceof OzoneProxy) {
131: return;
132: }
133: }
134:
135: /**
136: * Create a new persistent instance of the given class.
137: * <p>
138: * THIS METHOD IS NOT IN THE ODMG 3.0 STANDARD!
139: * <p>
140: * It must be executed in the context of an open transaction.
141: * If the transaction in which this method is executed commits,
142: * then the object is made durable.
143: * ClassNotPersistenceCapableException is thrown if the implementation cannot make
144: * the object persistent because of the type of the object.
145: */
146: public Object createPersistent(Class cl) {
147: if (OzoneODMGTransaction.current() == null) {
148: throw new TransactionNotInProgressException(
149: "Thread has not joined a transaction.");
150: }
151:
152: if (db == null) {
153: throw new DatabaseClosedException("Database not open.");
154: }
155:
156: if (cl.isAssignableFrom(OzoneCompatible.class)) {
157: throw new ClassNotPersistenceCapableException(cl.getName());
158: }
159:
160: try {
161: return db.createObject(cl.getName(),
162: ExternalDatabase.Public, null);
163: } catch (Exception e) {
164: throw new ODMGRuntimeException(e.toString());
165: }
166: }
167:
168: public void deletePersistent(Object object) {
169: if (OzoneODMGTransaction.current() == null) {
170: throw new TransactionNotInProgressException(
171: "Thread has not joined a transaction.");
172: }
173:
174: if (db == null) {
175: throw new DatabaseClosedException("Database not open.");
176: }
177:
178: if (!(object instanceof OzoneProxy)) {
179: throw new ObjectNotPersistentException(object.getClass()
180: .getName());
181: }
182:
183: try {
184: db.deleteObject((OzoneRemote) object);
185: } catch (Exception e) {
186: throw new ODMGRuntimeException(e.toString());
187: }
188: }
189:
190: public void bind(Object object, String name) {
191: if (OzoneODMGTransaction.current() == null) {
192: throw new TransactionNotInProgressException(
193: "Thread has not joined a transaction.");
194: }
195:
196: if (db == null) {
197: throw new DatabaseClosedException("Database not open.");
198: }
199:
200: if (!(object instanceof OzoneProxy)) {
201: throw new ClassNotPersistenceCapableException(object
202: .getClass().getName());
203: }
204:
205: try {
206: db.nameObject((OzoneRemote) object, name);
207: } catch (Exception e) {
208: throw new ODMGRuntimeException(e.toString());
209: }
210: }
211:
212: public void unbind(String name) throws ObjectNameNotFoundException {
213: if (OzoneODMGTransaction.current() == null) {
214: throw new TransactionNotInProgressException(
215: "Thread has not joined a transaction.");
216: }
217:
218: if (db == null) {
219: throw new DatabaseClosedException("Database not open.");
220: }
221:
222: Object obj = lookup(name);
223: try {
224: db.nameObject((OzoneRemote) obj, null);
225: } catch (Exception e) {
226: throw new ODMGRuntimeException(e.toString());
227: }
228: }
229:
230: public Object lookup(String name)
231: throws ObjectNameNotFoundException {
232: if (OzoneODMGTransaction.current() == null) {
233: throw new TransactionNotInProgressException(
234: "Thread has not joined a transaction.");
235: }
236:
237: if (db == null) {
238: throw new DatabaseClosedException("Database not open.");
239: }
240:
241: Object result;
242: try {
243: result = db.objectForName(name);
244: } catch (Exception 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: public boolean containsObject(Object obj) {
256: if (OzoneODMGTransaction.current() == null) {
257: throw new TransactionNotInProgressException(
258: "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
267: .getClass().getName());
268: }
269:
270: OzoneProxy proxy = (OzoneProxy) obj;
271: return proxy.link == db;
272: }
273:
274: }
|