InvokeServer.java :  » Database-DBMS » Ozone-1.1 » org » ozoneDB » core » Java Open Source

Java Open Source » Database DBMS » Ozone 1.1 
Ozone 1.1 » org » ozoneDB » core » InvokeServer.java
// You can redistribute this software and/or modify it under the terms of
// the Ozone Core 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: InvokeServer.java,v 1.3 2002/06/25 11:34:38 mediumnet Exp $

package org.ozoneDB.core;

import java.io.IOException;
import java.net.Socket;
import org.ozoneDB.DxLib.*;
import org.ozoneDB.DxLib.net.*;
import org.ozoneDB.*;
import org.ozoneDB.core.DbRemote.*;
import org.ozoneDB.util.*;


/**
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @version $Revision: 1.3 $Date: 2002/06/25 11:34:38 $
 */
public class InvokeServer extends DxMultiServer {
    
    protected transient Env env;
    
    
    public InvokeServer( Env _env, int port ) throws IOException{
        super( port );
        env = _env;
    }
    
    
    public void startup() throws Exception {
        env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
    } 
    
    
    public void shutdown() throws Exception {
        env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
        close();
    } 
    
    
    public void handleClientEvent(DxMultiServerClient client,Object event) {
//      env.logWriter.newEntry( this, "handleClientEvent()...", LogWriter.DEBUG3 );
        try {
            DbCommand cmd = (DbCommand)event;
            cmd.env = env;
            
            if (cmd instanceof DbOpen) {
                DbOpen command = (DbOpen)cmd;
                User user = env.userManager.userForName( command.userName() );
                
                if (user == null) {
                    client.send( new PermissionDeniedExc( "No such user: " + command.userName() + "." ) );
                } else {
                    ((DbInvokeClient)client).user = user;
                    ((CommandThread)Thread.currentThread()).setOwner( user );
                    env.logWriter.newEntry( this, "user logged in: " + command.userName(), LogWriter.INFO );
                    client.send( null );
                } 
            } else  if (cmd instanceof DbCloseConn) {
                removeClient( client );
            } else  if (cmd instanceof DbReloadClasses) {
                env.classManager.dropClasses();
                // AbstractObjectContainer.flushMethodCache();
                client.send( new Integer( 0 ) );
            } else {
              /*
                User user = ((DbInvokeClient)client).user;
                env.transactionManager.handleCommand( cmd, user );
                */
                DbInvokeClient dbInvokeClient = (DbInvokeClient) client;
                
                cmd.setProxyObjectGate(dbInvokeClient.getProxyObjectGate());
                env.transactionManager.handleCommand(cmd,dbInvokeClient);

                if (cmd.shouldResultBeSentToClient()) {
                    client.send(cmd.result);
                }
            } 
        } catch (Exception e) {
            // exceptions that are catched here are related to any network problem
            // since user and internal exceptions are catched in Transactions or
            // the TransactionManager
            
            // TODO: should this shutdown the connection?
            env.logWriter.newEntry( this, "handleClientEvent(): " + e, e, LogWriter.WARN );
        } 
    } 
    
    
    public void handleClientException( DxMultiServerClient client, Exception e ) {
        env.logWriter.newEntry( this, "handleClientException(): " + e, e, LogWriter.WARN );
        removeClient( client );
    } 
    
    
    public DxMultiServerClient newClient( Socket sock ) {
        try {
            DbInvokeClient dc = new DbInvokeClient( sock, this );
            env.logWriter.newEntry( this, "connection established...", LogWriter.INFO );
            return dc;
        } 
        catch (Exception e) {
            env.logWriter.newEntry( this, "newClient(): ", e, LogWriter.WARN );
            return null;
        } 
    } 
    
    
    public void removeClient( DxMultiServerClient client ) {
        env.logWriter.newEntry( this, "close connection...", LogWriter.DEBUG3 );
        env.logWriter.newEntry( this, "close pending transaction...", LogWriter.DEBUG3 );
        env.transactionManager.handleCommand(new DbCloseConn(),(User) null);
        
        super.removeClient( client );
        String userName = ((DbInvokeClient)client).user != null ? ((DbInvokeClient)client).user.name() : "none";
        env.logWriter.newEntry( this, "connection closed (user: " + userName + ")", LogWriter.INFO );
    } 
    
    
    public Thread newThread( Runnable run ) {
        Thread thread;
        if (run == acceptor) {
            thread = new Thread( threadGroup(), run );
            thread.setPriority( Env.ACCEPT_THREAD_PRIORITY );
        } else {
            thread = new CommandThread( threadGroup(), run );
            thread.setPriority( Env.TRANSACTION_THREAD_PRIORITY );
        } 
        thread.setDaemon( true );
        return thread;
    } 
    
    /**
    Starts filtering references to database objects ({@link OzoneProxy}s) which
    are exported to clients at all client connections.
    Every reference which is exported will be notified to the given GarbageCollector.
    Additionally, references which are known to be used by clients are notified to the
    given GarbageCollector within this call.
    */
    public void startFilterDatabaseObjectReferencesExports(GarbageCollector garbageCollector) {
      synchronized (this) {
        DxIterator i = iterator();

        while (i.next()!=null) {
                ((DbInvokeClient) i.object()).getProxyObjectGate().startFilterDatabaseObjectReferencesExports(garbageCollector);
        }
      }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.