Package org.ozoneDB.odmg

Source Code of org.ozoneDB.odmg.OzoneODMGDatabase

// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: OzoneODMGDatabase.java,v 1.1 2002/05/08 15:03:21 per_nyfelt Exp $

package org.ozoneDB.odmg;

import java.util.*;
import org.odmg.*;
import org.ozoneDB.ExternalDatabase;
import org.ozoneDB.LocalDatabase;
import org.ozoneDB.RemoteDatabase;
import org.ozoneDB.OzoneProxy;
import org.ozoneDB.OzoneRemote;
import org.ozoneDB.OzoneCompatible;


/**
* Implementation of the ODMG {@link org.odmg.Database} interface.
*
*
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @version $Revision: 1.1 $Date: 2002/05/08 15:03:21 $
*/
public class OzoneODMGDatabase implements EnhDatabase {
   
    private ExternalDatabase db;
   
    private OzoneODMG factory;
   
    private int accessMode = NOT_OPEN;
   
   
    public OzoneODMGDatabase( OzoneODMG _factory ) {
        factory = _factory;
    }
   
   
    public ExternalDatabase underlying() {
        return db;
    }
   
   
    protected int mode() {
        return accessMode;
    }
   
   
    /**
     * Open this ODMG database.
     * @param _url URL of the database (ozonedb:remote://host:port or ozonedb:local://datadir)
     */
    public synchronized void open( String _url, int _accessMode ) throws ODMGException {
        if (db != null) {
            throw new DatabaseOpenException( "Database is already open." );
        }
       
        switch (_accessMode) {
        case OPEN_READ_ONLY: {
            break;
            }
        case OPEN_READ_WRITE: {
            break;
            }
        case OPEN_EXCLUSIVE: {
            throw new ODMGRuntimeException( "OPEN_EXCLUSIVE not supported." );
            }
        default:
            throw new ODMGRuntimeException( "Illegal open mode." );
        }
       
        try {
            db = ExternalDatabase.openDatabase( _url );
        } catch (Exception e) {
            throw new DatabaseNotFoundException( e.toString() );
        }
       
        accessMode = _accessMode;
        factory.databaseOpened( this );
    }
   
   
    public void close() throws ODMGException {
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        try {
            db.close();
        } catch (Exception e) {
            throw new ODMGException( e.toString() );
        } finally {
            db = null;
            accessMode = NOT_OPEN;
            factory.databaseClosed( this );
        }
    }
   
   
    protected void finalize() throws Throwable {
        if (db != null) {
            close();
        }
    }
   
   
    /**
     * The ozone ODMG interface does not implement this method, use
     * createObject() instead.
     * <p>
     * ozone uses proxies to control objects inside the database. Unfortunately
     * ODMG is perfectly not aware of this kind of architecture.
     */
    public void makePersistent( Object object ) {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        // throw exception if someone want to use this method as intended
        // by ODMG
        if (object instanceof OzoneCompatible) {
            throw new ODMGRuntimeException( object.getClass().getName()
                    + ": create a persistent instance via createPersistent." );
        }
       
        // do nothing if the object is an proxy already
        if (object instanceof OzoneProxy) {
            return;
        }
    }
   
   
    /**
     * Create a new persistent instance of the given class.
     * <p>
     * THIS METHOD IS NOT IN THE ODMG 3.0 STANDARD!
     * <p>
     * It must be executed in the context of an open transaction.
     * If the transaction in which this method is executed commits,
     * then the object is made durable.
     * ClassNotPersistenceCapableException is thrown if the implementation cannot make
     * the object persistent because of the type of the object.
     */
    public Object createPersistent( Class cl ) {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        if (cl.isAssignableFrom( OzoneCompatible.class )) {
            throw new ClassNotPersistenceCapableException( cl.getName() );
        }
       
        try {
            return db.createObject( cl.getName(), ExternalDatabase.Public, null );
        } catch (Exception e) {
            throw new ODMGRuntimeException( e.toString() );
        }
    }
   
   
    public void deletePersistent( Object object ) {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        if (!(object instanceof OzoneProxy)) {
            throw new ObjectNotPersistentException( object.getClass().getName() );
        }
       
        try {
            db.deleteObject( (OzoneRemote)object );
        } catch (Exception e) {
            throw new ODMGRuntimeException( e.toString() );
        }
    }
   
   
    public void bind( Object object, String name ) {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        if (!(object instanceof OzoneProxy)) {
            throw new ClassNotPersistenceCapableException( object.getClass().getName() );
        }
       
        try {
            db.nameObject( (OzoneRemote)object, name );
        } catch (Exception e) {
            throw new ODMGRuntimeException( e.toString() );
        }
    }
   
   
    public void unbind( String name ) throws ObjectNameNotFoundException {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        Object obj = lookup( name );
        try {
            db.nameObject( (OzoneRemote)obj, null );
        } catch (Exception e) {
            throw new ODMGRuntimeException( e.toString() );
        }
    }
   
   
    public Object lookup( String name ) throws ObjectNameNotFoundException {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        Object result;
        try {
            result = db.objectForName( name );
        } catch (Exception e) {
            throw new ODMGRuntimeException( e.toString() );
        }
       
        if (result == null) {
            throw new ObjectNameNotFoundException( name );
        }
       
        return result;
    }
   
   
    public boolean containsObject( Object obj ) {
        if (OzoneODMGTransaction.current() == null) {
            throw new TransactionNotInProgressException( "Thread has not joined a transaction." );
        }
       
        if (db == null) {
            throw new DatabaseClosedException( "Database not open." );
        }
       
        if (!(obj instanceof OzoneProxy)) {
            throw new ClassNotPersistenceCapableException( obj.getClass().getName() );
        }
       
        OzoneProxy proxy = (OzoneProxy)obj;
        return proxy.link == db;
    }
   
}
TOP

Related Classes of org.ozoneDB.odmg.OzoneODMGDatabase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.