⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// 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.15 2004/01/03 10:39:41 per_nyfelt 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.15 $Date: 2004/01/03 10:39:41 $
 * @see OzoneInterface
 */
public final class Database extends AbstractDatabase {

    protected transient Env env;

    public Database(Env _env) {
        this.env = _env;
        AbstractFactory.setDefaultDatabase(this);
    }


    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,OzoneObjectException {
        env.logWriter.newEntry( this, "createObject()...", LogWriter.DEBUG3 );
        try {
            ObjectContainer container = env.transactionManager.currentTA().createObject( 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 OzoneObjectException("Caught during deleteObject()", e);
        } catch (OzoneObjectException e) {
            throw e;
        } catch (Exception 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().copyObject( ((OzoneProxy)rObj).remoteID() );
        try {
            return container.ozoneProxy();
        } finally {
            container.unpin();
        }
    }


    public void deleteObject( OzoneRemote rObj ) throws RuntimeException,OzoneObjectException {
        try {
            if (false) {
                env.logWriter.newEntry( this, "deleteObject()...", LogWriter.DEBUG3 );
            }
            env.transactionManager.currentTA().deleteObject( ((OzoneProxy)rObj).remoteID() );
        } catch (OzoneObjectException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Caught during deleteObject(): ", e);
        }
    }


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


    public String[] objectNames() throws Exception {
       if (env.logWriter.hasTarget(LogWriter.DEBUG3)) {
            env.logWriter.newEntry(this, "objectNames()...", LogWriter.DEBUG3);
        }
        Object[] keys = env.transactionManager.currentTA().objectNames().toArray();
        String[] names = new String[keys.length];
        for (int i = 0; i < keys.length; i++) {
            names[i] = (String) keys[i];
        }
        return names;
    }

    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 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*/ org.ozoneDB.ObjectNotFoundException, java.io.IOException, java.lang.ClassNotFoundException, org.ozoneDB.TransactionException, org.ozoneDB.core.TransactionError {
        // env.logWriter.newEntry (this, "fetch()...", LogWriter.DEBUG3);

        Transaction         currentTransaction  =   getCurrentTransaction();
        ObjectID            id                  =   rObj.remoteID();

        ObjectContainer     container           =   currentTransaction.acquireObject( 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.
    }
		
    /**
        For internal use.
    */
    public Env getEnv() {
        return env;
    }
    
    /**
        For internal use.
    */
    public Transaction getCurrentTransaction() {
        TransactionManager  transactionManager  =   getEnv().getTransactionManager();
        Transaction         currentTransaction  =   transactionManager.currentTA();
        
        return currentTransaction;
    }
}
// :indentSize=4:tabSize=4:noTabs=true:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -