/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.jdbc.xa;

import javax.sql.XADataSource;

/**
 * 只支持 app 驱动
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class DB2XADataSource extends AbstractXADataSource {

    public DB2XADataSource(String url) {
        super(url);
    }

    public XADataSource init(String url) {
        try {
            XADataSource db2XaDS = (XADataSource) this.getClass().getClassLoader().loadClass("COM.ibm.db2.jdbc.DB2XADataSource").newInstance();
            URLProperties urlProp = parseSqlURL(url);
            if(urlProp.containsKey("serverName")) {
//        db2XaDS.setServerName(urlProp.getProperty("serverName"));
//        db2XaDS.getClass().getMethod("serServerName",new Class[]{String.class}).invoke(db2XaDS,new Object[]{urlProp.getProperty("serverName")});
            }

            if(urlProp.containsKey("portNumber")) {
//        db2XaDS.setPortNumber(Integer.parseInt(urlProp.getProperty("portNumber")));
                db2XaDS.getClass().getMethod("setPortNumber", new Class[]{int.class}).invoke(db2XaDS, new Object[]{new Integer(urlProp.getProperty("portNumber"))});
            }

            if(urlProp.containsKey("databaseName")) {
//        db2XaDS.setDatabaseName(urlProp.getProperty("databaseName"));
                db2XaDS.getClass().getMethod("setDatabaseName", new Class[]{String.class}).invoke(db2XaDS, new Object[]{urlProp.getProperty("databaseName")});
            }

            if(urlProp.containsKey("connectionAttribute")) {
//        db2XaDS.setConnectionAttribute(urlProp.getProperty("connectionAttribute"));
                db2XaDS.getClass().getMethod("setConnectionAttribute", new Class[]{String.class}).invoke(db2XaDS, new Object[]{urlProp.getProperty("connectionAttribute")});
            }

            if(urlProp.containsKey("language")) {
//        db2XaDS.setLanguage(urlProp.getProperty("language"));
                db2XaDS.getClass().getMethod("setLanguage", new Class[]{String.class}).invoke(db2XaDS, new Object[]{urlProp.getProperty("language")});
            }
            // 如果 ServerName 不为 null，将采用 net 驱动，但是 DB2 net 驱动有问题
//      db2XaDS.setServerName(null);

            return db2XaDS;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

    }
}