// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 10/2/2001 11:03:24
// 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/FgjdbcURLConnection

package com.macromedia.generator.protocol.fgjdbc;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import java.util.*;

// Referenced classes of package com.macromedia.generator.protocol.fgjdbc:
//            FgjdbcCache, FgjdbcShare

public class FgjdbcURLConnection extends URLConnection
{

    FgjdbcURLConnection(URL url)
    {
        super(url);
        _fld0187 = 0;
        E6 = null;
    }

    private boolean _mth0104(SQLWarning sqlwarning)
        throws SQLException
    {
        boolean flag = false;
        if(sqlwarning != null)
        {
            if(_fld0187 > 1)
            {
                logMessage("");
                logMessage("*** SQL: Warning ***");
                logMessage("");
            }
            for(; sqlwarning != null; sqlwarning = sqlwarning.getNextWarning())
                if(_fld0187 > 1)
                {
                    logMessage("SQLState: " + sqlwarning.getSQLState());
                    logMessage("Message:  " + sqlwarning.getMessage());
                    logMessage("Vendor:   " + sqlwarning.getErrorCode());
                    logMessage("");
                }

            flag = true;
        }
        return flag;
    }

    public void connect()
        throws IOException
    {
        int i = 0;
        int j = 0;
        int k = 0;
        boolean flag = true;
        Hashtable hashtable = null;
        try
        {
            hashtable = _mth017C(url.getFile());
        }
        catch(IllegalArgumentException _ex)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLInvalidArgument") + url.toExternalForm());
            throw new IOException(getResString("SQLInvalidArgument") + url.toExternalForm());
        }
        String s = (String)hashtable.get("driver");
        String s1 = (String)hashtable.get("url");
        String s2 = (String)hashtable.get("userid");
        String s3 = (String)hashtable.get("password");
        String s4 = (String)hashtable.get("query");
        String s5 = (String)hashtable.get("lock");
        String s6 = (String)hashtable.get("cache");
        String s7 = (String)hashtable.get("share");
        String s8 = (String)hashtable.get("timeout");
        if(s == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLMissingDrv") + url.toExternalForm());
            throw new IOException(getResString("SQLMissingDrv") + url.toExternalForm());
        }
        if(s1 == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLMissingURL") + url.toExternalForm());
            throw new IOException(getResString("SQLMissingURL") + url.toExternalForm());
        }
        if(s2 == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLMissingUserID") + url.toExternalForm());
            throw new IOException(getResString("SQLMissingUserID") + url.toExternalForm());
        }
        if(s3 == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLMissingPassword") + url.toExternalForm());
            throw new IOException(getResString("SQLMissingPassword") + url.toExternalForm());
        }
        if(s4 == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLMissingQuery") + url.toExternalForm());
            throw new IOException(getResString("SQLMissingQuery") + url.toExternalForm());
        }
        if(s5 == null)
            s5 = "true";
        if(s6 == null)
            s6 = "0";
        if(s7 == null)
            s7 = "0";
        if(s8 == null)
            s8 = "0";
        try
        {
            i = Integer.valueOf(s6).intValue();
        }
        catch(NumberFormatException _ex)
        {
            i = 0;
        }
        try
        {
            j = Integer.valueOf(s7).intValue();
        }
        catch(NumberFormatException _ex)
        {
            j = 0;
        }
        try
        {
            k = Integer.valueOf(s8).intValue();
        }
        catch(NumberFormatException _ex)
        {
            k = 0;
        }
        flag = s5.equalsIgnoreCase("true");
        if(_fld0187 > 2)
        {
            logMessage("JDBC DEBUG OUTPUT ENABLED");
            logMessage("");
            String s10;
            for(Enumeration enumeration = hashtable.keys(); enumeration.hasMoreElements(); logMessage("JDBC parameter: " + s10 + " = '" + (String)hashtable.get(s10) + "'"))
                s10 = (String)enumeration.nextElement();

        }
        String s9 = _mth0187(s, s1, s2, s3, s4, flag, i, j, k);
        if(s9 == null)
        {
            if(_fld0187 > 0)
                logMessage(getResString("SQLProcessError") + url.toExternalForm());
            throw new IOException(getResString("SQLProcessError") + url.toExternalForm());
        }
        if(_fld0187 > 2)
        {
            logMessage("JDBC Query Response Text Stream:");
            logMessage(s9);
        }
        _fld0178 = new ByteArrayInputStream(s9.getBytes());
        connected = true;
    }

    public synchronized InputStream getInputStream()
        throws IOException
    {
        if(!connected)
            connect();
        return _fld0178;
    }

    public String getResString(String s)
    {
        ResourceBundle resourcebundle = ResourceBundle.getBundle("com.macromedia.generator.resources.ResourceData", Locale.getDefault());
        return resourcebundle.getString(s);
    }

    public void logMessage(String s)
    {
        if(E6 != null)
            E6.println("\t" + s);
    }

    private static String _mth017B(String s, StringBuffer stringbuffer)
    {
        stringbuffer.setLength(0);
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            switch(c)
            {
            case 43: // '+'
                stringbuffer.append(' ');
                break;

            case 37: // '%'
                try
                {
                    stringbuffer.append((char)Integer.parseInt(s.substring(i + 1, i + 3), 16));
                }
                catch(NumberFormatException _ex)
                {
                    throw new IllegalArgumentException();
                }
                i += 2;
                break;

            default:
                stringbuffer.append(c);
                break;
            }
        }

        return stringbuffer.toString();
    }

    private static Hashtable _mth017C(String s)
    {
        if(s == null)
            throw new IllegalArgumentException();
        int i = s.indexOf(63);
        if(i == -1)
            throw new IllegalArgumentException();
        String s1 = s.substring(i + 1, s.length());
        Hashtable hashtable = new Hashtable();
        StringBuffer stringbuffer = new StringBuffer();
        String s3;
        String s4;
        for(StringTokenizer stringtokenizer = new StringTokenizer(s1, "&"); stringtokenizer.hasMoreTokens(); hashtable.put(s3, s4))
        {
            String s2 = stringtokenizer.nextToken();
            int j = s2.indexOf(61);
            if(j == -1)
                throw new IllegalArgumentException();
            s3 = _mth017B(s2.substring(0, j), stringbuffer);
            s4 = _mth017B(s2.substring(j + 1, s2.length()), stringbuffer);
        }

        return hashtable;
    }

    private String _mth0187(String s, String s1, String s2, String s3, String s4, boolean flag, int i, 
            int j, int k)
    {
        FgjdbcShare fgjdbcshare = FgjdbcShare.getShare();
        FgjdbcCache fgjdbccache = FgjdbcCache.getCache();
        String s5 = i <= 0 ? null : fgjdbccache.getCacheValue(s, s1, s2, s3, s4);
        if(s5 == null)
        {
            Connection connection = fgjdbcshare.getConnection(s, s1, s2, s3, flag, j, _fld0187, E6);
            if(connection != null)
            {
                Object obj = null;
                Object obj1 = null;
                try
                {
                    _mth0104(connection.getWarnings());
                    if(_fld0187 > 2)
                    {
                        DatabaseMetaData databasemetadata = connection.getMetaData();
                        logMessage("");
                        logMessage("Connected to " + databasemetadata.getURL());
                        logMessage("Driver " + databasemetadata.getDriverName());
                        logMessage("Version " + databasemetadata.getDriverVersion());
                        logMessage("");
                    }
                    Statement statement = connection.createStatement();
                    if(k > 0)
                        statement.setQueryTimeout(k);
                    ResultSet resultset = statement.executeQuery(s4);
                    s5 = _mth0188(resultset);
                    resultset.close();
                    statement.close();
                }
                catch(SQLException sqlexception)
                {
                    if(_fld0187 > 0)
                    {
                        logMessage("");
                        logMessage("*** SQL: Exception ***");
                        logMessage("");
                    }
                    for(; sqlexception != null; sqlexception = sqlexception.getNextException())
                        if(_fld0187 > 0)
                        {
                            logMessage("SQLState: " + sqlexception.getSQLState());
                            logMessage("Message:  " + sqlexception.getMessage());
                            logMessage("Vendor:   " + sqlexception.getErrorCode());
                            logMessage("");
                        }

                    s5 = null;
                }
                catch(Exception exception)
                {
                    if(_fld0187 > 0)
                    {
                        logMessage("");
                        logMessage("*** SQL: Java Exception ***");
                        logMessage("");
                        logMessage("Message:  " + exception.getMessage());
                        logMessage("");
                    }
                    s5 = null;
                }
                fgjdbcshare.releaseConnection(s, connection);
            } else
            if(_fld0187 > 0)
            {
                logMessage("");
                logMessage("*** SQL: Failed to get JDBC connection ***");
                logMessage("");
            }
            if(i > 0 && s5 != null)
                fgjdbccache.setCacheValue(s, s1, s2, s3, s4, s5, i);
        }
        return s5;
    }

    private String _mth0188(ResultSet resultset)
        throws SQLException
    {
        String s = null;
        ResultSetMetaData resultsetmetadata = resultset.getMetaData();
        int i = resultsetmetadata.getColumnCount();
        if(i > 0)
        {
            StringBuffer stringbuffer = new StringBuffer();
            stringbuffer.setLength(0);
            for(int j = 0; j < i; j++)
            {
                stringbuffer.append("\"" + resultsetmetadata.getColumnLabel(j + 1) + "\"");
                if(j + 1 < i)
                    stringbuffer.append(",");
            }

            stringbuffer.append(System.getProperty("line.separator", "\n"));
            for(; resultset.next(); stringbuffer.append(System.getProperty("line.separator", "\n")))
            {
                for(int k = 0; k < i; k++)
                {
                    String s1 = resultset.getString(k + 1);
                    if(s1 != null)
                        stringbuffer.append("\"" + s1 + "\"");
                    else
                        stringbuffer.append("\"\"");
                    if(k + 1 < i)
                        stringbuffer.append(",");
                }

            }

            s = stringbuffer.toString();
        }
        return s;
    }

    public void setLogWriter(int i, PrintWriter printwriter)
    {
        _fld0187 = i;
        E6 = printwriter;
    }

    int _fld0187;
    PrintWriter E6;
    InputStream _fld0178;
}