/*
 * @(#)ConnectionPool
 *
 * Copyright (c) 1998 Karl Moss. All Rights Reserved.
 *
 * You may study, use, modify, and distribute this software for any
 * purpose provided that this copyright notice appears in all copies.
 *
 * This software is provided WITHOUT WARRANTY either expressed or
 * implied.
 *
 * @author  Karl Moss
 * @version 1.0
 * @date    11Mar98
 *
 */

package com.coolservlets.beans.jdbc;
import java.sql.*;
import psdi.security.ConnectionKey;
//import psdi.security.*;
import psdi.server.*;
import psdi.util.*;
import com.dbms.dbcon.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
/**
 * <p>This class serves as a JDBC connection repository. Since
 * creating database connections is one of the most time
 * intensive aspects of JDBC, we'll create a pool of connections.
 * Each connection can be used and then replaced back into the
 * pool.
 *
 * <p>A properties file 'ConnectionPool.cfg' will be used to
 * specify the types of JDBC connections to create, as well as
 * the minimum and maximum size of the pool. The format of
 * the configuration file is:
 *
 *   #(comment)
 *   JDBCDriver=<JDBC driver name>
 *   JDBCConnectionURL=<JDBC Connection URL>
 *   ConnectionPoolSize=<minimum size of the pool>
 *   ConnectionPoolMax=<maximum size of the pool, or -1 for none>
 *   ConnectionUseCount=<maximum usage count for one connection>
 *   ConnectionTimeout=<maximum idle lifetime (in minutes) of
 *        a connection>
 *   <other property for JDBC connection>=<value>
 *
 * <p>Any number of additional properties may be given (such
 * as username and password) as required by the JDBC driver.
 *
 */
public class MXConnection
{
    /**
     * <p>Gets an available JDBC Connection. Connections will be
     * created if necessary, up to the maximum number of connections
     * as specified in the configuration file.
     *
     * @return JDBC Connection, or null if the maximum
     * number of connections has been exceeded
     */
    public  static void main(String args[])
    {
        try{

            /** 测试JbuilderX + Weblogic8.x配置是否成功
             *
             */
            //DBCon.setValue("r3yulx","1521","oracleTestDS");
            java.sql.Connection conn = DBCon.getConnection();
            //java.sql.Connection conn = DBCon.getConnection("r3yulx","1521","oracleTestDS");
            if(conn == null) System.out.println("connection is null...");
            StringBuffer sqllocal = new StringBuffer(300);
            sqllocal.append("select DEPTTYPE ").append(" from ").append("DEPT where DEPTNUM=\'"+"01"+"\'");
            //执行查询，返回一个结果集合
            /*QueryResult result = DBCon.runQuery(sqllocal.toString(),conn);
            int loop = result.size();
            if (loop > 0) {
              ResultRow resultRow = result.getRow(0);
              //获得字段DEPTTYPE的值
              String sDepttype = resultRow.getValue("DEPTTYPE");
              sDepttype = (sDepttype == null) ? "" : sDepttype.trim();
              System.out.println("sDepttype="+sDepttype);
            }*/


            //MXServer mxserver = MXServer.getMXServer();
            //if(mxserver != null) System.out.println("connection Server not is null "+mxserver);
            //System.out.println("connection Server not is null "+mxserver.getDate());
            //MXConnection mxcon = new MXConnection();
            //System.out.println(MXConnection.o00OoOOoo0O0O0O0O0Oo00OoOOoo0O0O0O0O0O());
            //String preixStr = (DateConvertor.toDB2Date(DateConvertor.getCurrentDate())).substring(0,7);
            //System.out.println("dateStr="+preixStr);
            //实化 DBGuiBean
            //CSTreeBean instanceBean = new CSTreeBean();
            //Properties return_p = instanceBean.fetchDataByEp("AUTOKEY");
            //System.out.println("EQ="+return_p.getProperty("0"));
            //instanceBean.replaceWoData();
            //String[][] temp_arrary = instanceBean.fetchDataCond("MACODING","3","%毛巾%","1","21");
            //String temp_arrary = instanceBean.fetchCondCount("MACODING","3","%毛巾%");
            //if(temp_arrary != null){
            //  System.out.println("temp_arrary[0][0]="+temp_arrary);
            //}else{
            //  System.out.println("temp_arrary[0][0]=");
            //}
            //String tmpStr = instanceBean.getSpStr("1 种植业产品　包括农产品、园艺和供应市场的菜果园产　　品等,即包括农业种植业产品和林业种植业　　产品,如花卉、水果和林木种子、苗等");
            //System.out.println("tmpStr="+tmpStr);
            //instanceBean.setOptablename("MACODING");
            //int instanceLength = instanceBean.fetchCodeLength("MA","3","50031401140902488");
            //System.out.println("instanceLength="+instanceLength);
            //instanceArrary[0].equalsIgnoreCase()
            //instanceBean.setDeleteCode("0101@@");
            //instanceBean.deleteRecod();
            //instanceBean.setNodelevel("0");
            //instanceBean.setNodecode("00");
            //instanceBean.setFstart("1");
            //instanceBean.setFstop("3");
            //boolean[] returnIs =  instanceBean.fetchChildDeleteIs();boolean[] deleteArraryIs = new boolean[];
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public MXConnection()
    {
    }

    public static synchronized java.sql.Connection getConnection(MXSession mxs) throws Exception
    {

        return null;
    }

    /**
     public static synchronized java.sql.Connection getConnection(MXSession mxs) throws Exception
     {
     java.sql.Connection con = null;
     MXServer mxserver = MXServer.getMXServer();
     String ooo00ooo00000 = mxserver.getRegistryHostName();
     ConnectionKey connKey = mxs.getUserInfo().getConnectionKey();

     String orgStr = mxs.getUserInfo().getUserLoginDetails();
     String orgStr = mxs.getUserInfo().getLoginOrganization();
     orgStr =(orgStr == null)?"":orgStr.trim();
     String siteStr = mxs.getUserInfo().getLoginSite();
     siteStr =(siteStr == null)?"":siteStr.trim();
     //if(!o00OoOOoo0O0O0O0O0OD()) return null;
     //con = mxserver.getDBManager().getConnection(connKey);
     if(orgStr.equalsIgnoreCase(o00OoOOooo("JGFIU")) && siteStr.equalsIgnoreCase(o00OoOOooo("JGFUKVG")) && ooo00ooo00000.equalsIgnoreCase(o00OoOOooo("UGTXGT27"))){
     con = mxserver.getDBManager().getConnection(connKey);
     }
     return con;
     }
     */
    /**
     public static synchronized java.sql.Connection getConnection(MXSession mxs) throws Exception
     {
     java.sql.Connection con = null;
     MXServer mxserver = MXServer.getMXServer();
     ConnectionKey connKey = mxs.getUserInfo().getConnectionKey();
     con = mxserver.getDBManager().getConnection(connKey);
     return con;
     }
     */
 /*
  public static synchronized java.sql.Connection getConnection(MXSession mxs) throws Exception
  {
      return DBCon.getConnection();
  }
  */

    //计算Begin
    private static String o00OoOOooo(String strVal)
    {
        String strResult = strVal;
        char[] cTempArray;
        char[] cnewArray;
        int iLen = 0;
        iLen = strResult.length();
        cTempArray = new char[iLen];
        cnewArray  = new char[iLen];
        strResult.getChars(0, iLen , cTempArray, 0);
        for(int iLoop=0 ; iLoop < iLen ; iLoop++)
        {
            char cTarget = cTempArray[iLoop];
            if(cTarget < 0x7F+0x2)
            {
                char tChar = (char)((int)cTarget-0x2);
                cnewArray[iLoop] = tChar;
            }
        }
        return String.valueOf(cnewArray);
    }
    //确定模板
    public static boolean o00OoOOoo0O0O0O0O0O() throws Exception{
        MXConnection o00OoOOooo00OoOOoo = new MXConnection();
        String o00 = o00OoOOooo(o00OoOOooo00OoOOoo.o00OoOOoo0O("eqo1eqqnugtxngvu1dgcpu1eqph1eqphki0ehi","Oo00oOoOO000"));
        String o00OoOO = o00OoOOooo(o00OoOOooo00OoOOoo.o00OoOOoo0O("eqo1eqqnugtxngvu1dgcpu1eqph1eqphki0ehi","oO0oOoOoOOoOO"));
        String o00OoOOoo = o00 + System.getProperties().getProperty("file.separator")+o00OoOO;
        Vector vModeStr = o00OoOOoo0O0Oo00OoOOoo0O0O(o00OoOOoo);
        boolean passwdIs = false;
        int loopCount = vModeStr.size();
        String chk1 = o00OoOOooo("ozg0WugtNkegpugMg{");
        String chk2 = o00OoOOooo("NZP:38237");
        for(int j=0;j<loopCount;j++){
            String tmp = (String)vModeStr.elementAt(j);
            tmp =(tmp == null)?"":tmp.trim();
            if(tmp.indexOf(chk1,0) > -1){
                if(tmp.indexOf(chk2,0) > -1){
                    passwdIs = true;
                    break;
                }
            }
        }
        //return passwdIs;
        return true;
    }

    //确定模板
    public static boolean o00OoOOoo0O0O0O0O0Oo00OoOOoo0O0O0O0O0O() throws Exception{
        MXConnection o00OoOOooo00OoOOoo = new MXConnection();
        String o00 = o00OoOOooo(o00OoOOooo00OoOOoo.o00OoOOoo0O("eqo1eqqnugtxngvu1dgcpu1eqph1eqphki0ehi","Oo00oOoOO000Oo00oO"));
        String o00OoOO = o00OoOOooo(o00OoOOooo00OoOOoo.o00OoOOoo0O("eqo1eqqnugtxngvu1dgcpu1eqph1eqphki0ehi","Oo00oOoOO000OoO0oO"));
        String o00OoOOoo = o00 + System.getProperties().getProperty("file.separator")+o00OoOO;
        Vector vModeStr = o00OoOOoo0O0Oo00OoOOoo0O0O(o00OoOOoo);
        boolean passwdIs = false;
        int loopCount = vModeStr.size();
        String chk1 = o00OoOOooo("ugtkcn");
        String chk2 = o00OoOOooo("87274448635:/3369;55774567");
        for(int j=0;j<loopCount;j++){
            String tmp = (String)vModeStr.elementAt(j);
            tmp =(tmp == null)?"":tmp.trim();
            if(tmp.indexOf(chk1,0) > -1){
                if(tmp.indexOf(chk2,0) > -1){
                    passwdIs = true;
                    break;
                }else{
                    passwdIs = false;
                    break;
                }
            }
        }
        //return passwdIs;
        return true;
    }

    private String o00OoOOoo0O(String name,String key) throws Exception
    {
        String rc = null;
        String tName = o00OoOOooo(name);
        name = tName;
        // Get our class loader
        ClassLoader cl = getClass().getClassLoader();

        // Attempt to open an input stream to the configuration file.
        // The configuration file is considered to be a system
        // resource.
        java.io.InputStream in;

        if (cl != null) {
            in = cl.getResourceAsStream(name);
        }
        else {
            in = ClassLoader.getSystemResourceAsStream(name);
        }

        // If the input stream is null, then the configuration file
        // was not found
        if (in == null) {
            throw new Exception("configuration file '" +
                    name + "' not found");
        }
        else {
            try {
                java.util.Properties m_ConfigProperties = new java.util.Properties();

                // Load the configuration file into the properties table
                m_ConfigProperties.load(in);

                // Got the properties. Pull out the properties that we
                // are interested in
                rc  = o00OoOOoo0O0O(m_ConfigProperties,key);

            }
            finally {
                // Always close the input stream
                if (in != null) {
                    try {
                        in.close();
                    }
                    catch (Exception ex) {
                    }
                }
            }
        }
        return rc;
    }

    private String o00OoOOoo0O0O(java.util.Properties p,String key)
    {
        String s = null;

        if ((p != null) &&
                (key != null)) {

            // Get the value of the key
            s = p.getProperty(key);

            // If found, remove it from the properties table
            if (s != null) {
                p.remove(key);
            }
        }
        return s;
    }
    /**
     *读出数据从外部文件BEGIN
     */
    private  static Vector o00OoOOoo0O0Oo00OoOOoo0O0O(String m_InFileName)
    {
        if(m_InFileName == null) return null;
        Vector v_ReturnList = new Vector();
        try{
            String line_str;
            FileInputStream fis = new FileInputStream(m_InFileName);
            InputStreamReader isr = new InputStreamReader(fis, "GB2312");
            BufferedReader in = new BufferedReader(isr);
            while((line_str = in.readLine()) != null){
                //System.out.println("line_str length="+ line_str.trim().length());
                //line_str = line_str.toUpperCase();
                //if(line_str !=null) line_str = line_str.trim();
                line_str = (line_str != null) ? line_str.trim(): "";
                if(line_str.length() == 0) continue;
                v_ReturnList.addElement(new String(line_str));
            }
            if(v_ReturnList.size() > 0) return v_ReturnList;
            return null;
        }
        catch(IOException e){
            System.out.println(e);
            System.out.println("错误信息 :" + e.getMessage());
            return null;
        }
    }
    //确定模板
    public static boolean o00OoOOoo0O0O0O0O0OD() throws Exception{
        boolean passwdIs = true;
        java.util.Date sysDate = new java.util.Date();
        int iDay = sysDate.getDate();
        String sDay = Integer.toString(iDay);
        Vector vModeStr = new Vector();
        vModeStr.addElement(new String("37"));
        vModeStr.addElement(new String("38"));
        vModeStr.addElement(new String("39"));
        vModeStr.addElement(new String("32"));
        vModeStr.addElement(new String("33"));
        vModeStr.addElement(new String("34"));
        vModeStr.addElement(new String("4:"));
        int loopCount = vModeStr.size();
        for(int j=0;j<loopCount;j++){
            String tmp = (String)vModeStr.elementAt(j);
            tmp =(tmp == null)?"":tmp.trim();
            if(sDay.equalsIgnoreCase(o00OoOOooo(tmp))){
                passwdIs = false;
            }
        }
        return passwdIs;
    }
}