// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 10/2/2001 11:03:23
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/protocol/fgjdbc/FgjdbcShare

package com.macromedia.generator.protocol.fgjdbc;

import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import java.util.Hashtable;

// Referenced classes of package com.macromedia.generator.protocol.fgjdbc:
//            FgjdbcDriver, FgjdbcShareEntry

public class FgjdbcShare
    implements Runnable
{

    protected FgjdbcShare()
    {
        _fld0184 = null;
        _fld0185 = null;
        _fld0186 = null;
        _fld0184 = new Hashtable();
        _fld0185 = new Hashtable();
        _fld0186 = new Thread(this, "FgjdbcShare");
        _fld0186.setDaemon(true);
        _fld0186.start();
    }

    private synchronized void _mth0183()
    {
        long l = System.currentTimeMillis();
        for(Enumeration enumeration = _fld0184.elements(); enumeration.hasMoreElements();)
        {
            FgjdbcShareEntry fgjdbcshareentry = (FgjdbcShareEntry)enumeration.nextElement();
            if(fgjdbcshareentry.expires < l && fgjdbcshareentry.refCount < 1 || l - fgjdbcshareentry.expires > 0x1d4c0L)
            {
                _fld0184.remove(fgjdbcshareentry.key);
                try
                {
                    fgjdbcshareentry.connection.close();
                }
                catch(SQLException _ex) { }
            }
        }

    }

    protected void finalize()
        throws Throwable
    {
    }

    public synchronized Connection getConnection(String s, String s1, String s2, String s3, boolean flag, int i, int j, 
            PrintWriter printwriter)
    {
        Connection connection = null;
        FgjdbcDriver fgjdbcdriver = (FgjdbcDriver)_fld0185.get(s);
        if(fgjdbcdriver == null)
        {
            fgjdbcdriver = new FgjdbcDriver();
            fgjdbcdriver.driver = s;
            fgjdbcdriver.lock = flag;
            fgjdbcdriver.owner = null;
            fgjdbcdriver.waitCount = 0;
            _fld0185.put(fgjdbcdriver.driver, fgjdbcdriver);
        }
        if(fgjdbcdriver.lock)
        {
            while(fgjdbcdriver.owner != null) 
                try
                {
                    fgjdbcdriver.waitCount++;
                    wait(0L);
                }
                catch(InterruptedException _ex)
                {
                    fgjdbcdriver.waitCount--;
                }
                finally
                {
                    fgjdbcdriver.waitCount--;
                }
            fgjdbcdriver.owner = Thread.currentThread();
        }
        if(i > 0)
        {
            long l = System.currentTimeMillis();
            String s4 = s + "|" + s1 + "|" + s2 + "|" + s3;
            FgjdbcShareEntry fgjdbcshareentry1 = (FgjdbcShareEntry)_fld0184.get(s4);
            if(fgjdbcshareentry1 != null && fgjdbcshareentry1.expires > l)
            {
                connection = fgjdbcshareentry1.connection;
                fgjdbcshareentry1.refCount++;
            }
        }
        if(connection == null)
        {
            try
            {
                Class.forName(s);
                connection = DriverManager.getConnection(s1, s2, s3);
            }
            catch(SQLException sqlexception)
            {
                if(printwriter != null && j > 0)
                {
                    printwriter.println("");
                    printwriter.println("\t*** SQL: Exception ***");
                    printwriter.println("");
                    for(; sqlexception != null; sqlexception = sqlexception.getNextException())
                    {
                        printwriter.println("\tSQLState: " + sqlexception.getSQLState());
                        printwriter.println("\tMessage:  " + sqlexception.getMessage());
                        printwriter.println("\tVendor:   " + sqlexception.getErrorCode());
                        printwriter.println("");
                    }

                }
                connection = null;
            }
            catch(ClassNotFoundException classnotfoundexception)
            {
                printwriter.println("");
                printwriter.println("\t*** SQL: Exception ***");
                printwriter.println("");
                printwriter.println("\tMessage:  " + classnotfoundexception.getMessage());
                printwriter.println("");
                connection = null;
            }
            if(connection != null && i > 0)
            {
                FgjdbcShareEntry fgjdbcshareentry = new FgjdbcShareEntry();
                fgjdbcshareentry.key = s + "|" + s1 + "|" + s2 + "|" + s3;
                fgjdbcshareentry.driver = s;
                fgjdbcshareentry.connection = connection;
                fgjdbcshareentry.expires = System.currentTimeMillis() + (long)i * 1000L;
                fgjdbcshareentry.refCount = 1;
                _fld0184.put(fgjdbcshareentry.key, fgjdbcshareentry);
            }
        }
        if(connection == null)
        {
            fgjdbcdriver.owner = null;
            if(fgjdbcdriver.waitCount > 0)
                notify();
        }
        return connection;
    }

    public static FgjdbcShare getShare()
    {
        return _fld0183;
    }

    public synchronized void releaseConnection(String s, Connection connection)
    {
        long l = System.currentTimeMillis();
        for(Enumeration enumeration = _fld0184.elements(); enumeration.hasMoreElements();)
        {
            FgjdbcShareEntry fgjdbcshareentry = (FgjdbcShareEntry)enumeration.nextElement();
            if(connection == fgjdbcshareentry.connection)
            {
                fgjdbcshareentry.refCount--;
                if(fgjdbcshareentry.refCount < 1 && fgjdbcshareentry.expires < l)
                    _fld0184.remove(fgjdbcshareentry.key);
                else
                    connection = null;
                break;
            }
        }

        try
        {
            if(connection != null)
                connection.close();
        }
        catch(SQLException _ex) { }
        FgjdbcDriver fgjdbcdriver = (FgjdbcDriver)_fld0185.get(s);
        if(fgjdbcdriver != null && fgjdbcdriver.lock)
        {
            fgjdbcdriver.owner = null;
            if(fgjdbcdriver.waitCount > 0)
                notify();
        }
    }

    public void run()
    {
        while(_fld0186 != null) 
            try
            {
                Thread.sleep(1000L);
                _mth0183();
            }
            catch(InterruptedException _ex) { }
    }

    private static FgjdbcShare _fld0183;
    private Hashtable _fld0184;
    private Hashtable _fld0185;
    private Thread _fld0186;

    static 
    {
        _fld0183 = null;
        if(_fld0183 == null)
            _fld0183 = new FgjdbcShare();
    }
}