Package org.ozoneDB

Source Code of org.ozoneDB.Database

// 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: Database.java,v 1.3 2002/06/08 00:49:38 mediumnet Exp $

package org.ozoneDB;

import org.ozoneDB.core.DbRemote.CommandThread;
import org.ozoneDB.core.DbRemote.DbXMLForObj;
import org.ozoneDB.core.*;
import org.ozoneDB.util.LogWriter;
import org.ozoneDB.xml.util.SAXChunkConsumer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;


/**
* This class represents the database for OzoneObjects within the server.<p>
*
* Note: The method parameters of type {@link OzoneRemote} are supposed to by
* proxy objects (of type {@link OzoneProxy}). However, since we are inside the
* server it's possible to pass an database object (of type {@link
* OzoneCompatible}) as a parameter. In this case the parameter should be
* substituted. Currently this is done by the invoke() method only.
*
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @author <a href="http://www.medium.net/">Medium.net</a>
* @version $Revision: 1.3 $Date: 2002/06/08 00:49:38 $
* @see OzoneInterface
*/
public final class Database extends AbstractDatabase implements OzoneInterface {

    protected transient Env env;


    public Database() {
    }


    public Database(Env _env) {
        env = _env;
    }


    public void reloadClasses() throws Exception {
        throw new Exception("reloadClasses() must not be called from within the server.");
    }


    public User currentOwner() {
        Thread thread = Thread.currentThread();
        /*
        if (thread instanceof Transaction) {
           return ((Transaction)thread).owner();
           }
         */
        if (thread instanceof CommandThread) {
            return ((CommandThread) thread).owner();
        } else {
            env.fatalError(this, "Current thread is not a transaction or command!", null);
            return null;
        }
    }


    public OzoneProxy createObject(String className,int access,String objName,String sig,Object[] args) throws RuntimeException,ExceptionInOzoneObjectException {
//      env.logWriter.newEntry( this, "createObject()...", LogWriter.DEBUG3 );
        try {
            ObjectContainer container = env.transactionManager.currentTA().createObjectAndPin( className, access, objName, sig, args, null );

            try {
                OzoneProxy result = container.ozoneProxy();
                // env.logWriter.newEntry ("return: " + result.getClass().getName(), LogWriter.DEBUG);
                return result;
            } finally {
                container.unpin();
            }
        } catch (java.lang.reflect.InvocationTargetException e) {
            throw new ExceptionInOzoneObjectException("Caught during deleteObject()",e.getTargetException());
        } catch (ExceptionInOzoneObjectException e) {
            throw e;
        } catch (Exception e) {
             // only supported from JDK1.4 on
//          throw new RuntimeException("Caught during createObject()",e);
            env.logWriter.newEntry(this,"createObject(): caught",e,LogWriter.DEBUG3 );
            throw new RuntimeException("Caught during createObject(): "+e);
        }
    }


    public OzoneProxy copyObject(OzoneRemote rObj) throws Exception {
        env.logWriter.newEntry(this, "copyObject()...", LogWriter.DEBUG3);
        ObjectContainer container = env.transactionManager.currentTA().copyObjectAndPin( ((OzoneProxy)rObj).remoteID() );
        try {
            return container.ozoneProxy();
        } finally {
            container.unpin();
        }
    }


    public void deleteObject( OzoneRemote rObj ) throws RuntimeException,ExceptionInOzoneObjectException {
        try {
            if (false) {
                env.logWriter.newEntry( this, "deleteObject()...", LogWriter.DEBUG3 );
            }
            env.transactionManager.currentTA().deleteObject( ((OzoneProxy)rObj).remoteID() );
        } catch (ExceptionInOzoneObjectException e) {
            throw e;
        } catch (Exception e) {
            // only supported from JDK1.4 on
//          throw new RuntimeException("Caught during deleteObject()",e);
            throw new RuntimeException("Caught during deleteObject(): "+e);
        }
    }


    public void nameObject(OzoneRemote rObj, String name) throws Exception {
        if (false) {
            env.logWriter.newEntry(this, "nameObject()...", LogWriter.DEBUG3);
        }
        env.transactionManager.currentTA().nameObject(((OzoneProxy) rObj).remoteID(), name);
    }


    public OzoneProxy objectForName(String name) throws Exception {
        env.logWriter.newEntry(this, "objectForName()... ", LogWriter.DEBUG3);
        return env.transactionManager.currentTA().objectForName(name);
    }

    public OzoneProxy objectForHandle(String handle) throws Exception {
        env.logWriter.newEntry(this, "objectForHandle()... ", LogWriter.DEBUG3);
        return env.transactionManager.currentTA().objectForID(new ObjectID(handle));
    }

    public OzoneProxy[] objectsOfClass(String name) throws Exception {
        env.logWriter.newEntry(this, "objectsForClass()...", LogWriter.DEBUG3);
        throw new RuntimeException("objectsForClass(): not implemented.");
    }


    public Object invoke(OzoneProxy rObj, String methodName, String sig, Object[] args, int lockLevel)
            throws Exception {

        //one argument could have been "this", then we have to give
        //a proxy instead of the actual object
        for (int i = 0; i < args.length; i++) {
            args[i] = ResultConverter.substituteOzoneCompatibles(args[i]);
        }

        Object result =
                env.transactionManager.currentTA().invokeObject(rObj.remoteID(), methodName, sig, args, lockLevel);

        //also return could have been "this"...
        result = ResultConverter.substituteOzoneCompatibles(result);
        return result;
    }


    public Object invoke(OzoneProxy rObj, int methodIndex, Object[] args, int lockLevel) throws Exception {

        //one argument could have been "this", then we have to give
        //a proxy instead of the actual object
        for (int i = 0; i < args.length; i++) {
            args[i] = ResultConverter.substituteOzoneCompatibles(args[i]);
        }

        Object result =
                env.transactionManager.currentTA().invokeObject(rObj.remoteID(), methodIndex, args, lockLevel);

        //also return could have been "this"...
        result = ResultConverter.substituteOzoneCompatibles(result);
        return result;
    }


    public OzoneCompatible fetch(OzoneProxy rObj, int lockLevel) throws Exception {
        // env.logWriter.newEntry (this, "fetch()...", LogWriter.DEBUG3);

        TransactionManager  transactionManager  =   env.transactionManager;
        Transaction         currentTransaction  =   transactionManager.currentTA();
        ObjectID            id                  =   rObj.remoteID();

        ObjectContainer     container           =   currentTransaction.acquireObjectAndPin( id, lockLevel );

        try {
            return container.target();
        } finally {
            container.unpin();
        }
    }


    public Node xmlForObject(OzoneRemote rObj, Document domFactory) throws Exception {

        // creating the chunk is not really needed but should work ;)
        DbXMLForObj command = new DbXMLForObj((OzoneProxy) rObj);
        command.perform(env.transactionManager.currentTA());
        byte[] bytes = (byte[]) command.result;

        SAXChunkConsumer consumer = new SAXChunkConsumer(domFactory, null);
        consumer.processChunk(bytes);

        return consumer.getResultNode();
    }


    public void xmlForObject(OzoneRemote rObj, ContentHandler ch) throws Exception {

        // creating the chunk is not really needed but should work ;)
        DbXMLForObj command = new DbXMLForObj((OzoneProxy) rObj);
        command.perform(env.transactionManager.currentTA());
        byte[] bytes = (byte[]) command.result;

        SAXChunkConsumer consumer = new SAXChunkConsumer(ch);
        consumer.processChunk(bytes);
    }

    /**
        Internal method. This method is called by {@link OzoneProxy}s when they are dying (during finalize()). This
        is required, as the database may track the references the database client has to objects within the database
        in order to properly support garbage collection. If this method is called from anyone else than from the
        {@link OzoneProxy}.finalize()-Method, data loss may occur!

        @param proxy the OzoneProxy object which is dying. It may call this method exaclty once.
    */
    public void notifyProxyDeath(OzoneProxy proxy) {
        // We do nothing here as we do not care wether OzoneProxys within the local databases die.
    }
}
TOP

Related Classes of org.ozoneDB.Database

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.