⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connconfigure.java

📁 Speedframework--基于类型元数据的羽量级ORM.完全基于Java类型元数据和反射机制同时不需要任何对象关系映射配置文件的轻量级ORM框架,它充分利用了类型本身的信息,使用Metadata
💻 JAVA
字号:
package org.speedframework.configure;

import java.sql.DriverManager;
import org.jdom.Element;
import org.jdom.Document;
import org.speedframework.exception.ConfigureException;
import javax.sql.DataSource;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.speedframework.util.ConfigureHelper;
import org.speedframework.exception.SpeedException;
import java.util.List;
import java.util.Iterator;
import org.speedframework.util.ConfigureParam;
import org.apache.log4j.Logger;
import java.util.Properties;

/**
 * <p>Title: SpeedFrameworkWork持久层框架</p>
 *
 * <p>Description: 配置管理类</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: SpeedFrameworkWork team</p>
 * @author 李志峰  电话:13824431576 程伟杰 电话:
 * @version 1.0
 */
public class ConnConfigure {
  private Logger log = Logger.getLogger(ConnConfigure.class);
  private static Document conf;
  private static Context context;
  private static final String confile="/speed.cfg.xml";
  /**
   * 提取连接(指定id)
   * @param id String
   * @throws SpeedException
   * @throws ConfigureException
   * @return Connection
   */

  public Connection getConnection(String id) throws
      SpeedException, ConfigureException {
    conf = ConfigureHelper.loadFile(confile);
    String type = ConfigureHelper.getConnectionType(conf, id);
    return this.ConnectionManager(id, type);

  }

  /**
   * 提取连接
   * @throws SpeedException
   * @throws ConfigureException
   * @return Connection
   */

  public Connection getConnection() throws
      SpeedException, ConfigureException {
    conf = ConfigureHelper.loadFile(confile);
    String type = ConfigureHelper.getConnectionType(conf);
    return this.ConnectionManager(type);

  }

  /**
   * 连接管理(多数据库或连接方式 指定id)
   * @param type String
   * @throws SpeedException
   * @return Connection
   */
  private Connection ConnectionManager(String id, String type) throws
      SpeedException {
    Connection con = null;
    Element node = null;
    if (!id.equals("")) {
      List list = conf.getRootElement().getChildren("connection-factory");
      for (int i = 0; i < list.size(); i++) {
        Element e = (Element) list.get(i);
        if (e.getAttribute("id").getValue().equals(id)) {
          node = e;
        }
      }
    }
    else {
      node = conf.getRootElement().getChild("connection-factory");
    }

    List conf_list = node.getChildren();
    String drive_class = null, url = null, username = null, password = null,
        jndi_name = null, jndi_username = null, jndi_password = null,
        jndi_class = null, jndi_url = null, INITIAL_CONTEXT_FACTORY = null,
        PROVIDER_URL = null;
    try {
      Iterator it = conf_list.iterator();
      if (type.equals("connection")) {
        while (it.hasNext()) {
          Element c_node = (Element) it.next();
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.DRIVER)) {
            drive_class = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.URL)) {
            url = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.USERNAME)) {
            username = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.PASSWORD)) {
            password = c_node.getValue();
          }
        }
        Class.forName(drive_class);
        con = DriverManager.getConnection(url, username, password);
      }
      if (type.equals("datasource")) {
        while (it.hasNext()) {
          Element c_node = (Element) it.next();
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.JNDI_NAME)) {
            jndi_name = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.JNDI_USERNAME)) {
            jndi_username = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.PASSWORD)) {
            jndi_password = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.JNDI_CLASS)) {
            jndi_class = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.JNDI_URL)) {
            jndi_url = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.INITIAL_CONTEXT_FACTORY)) {
            INITIAL_CONTEXT_FACTORY = c_node.getValue();
          }
          if (c_node.getAttribute("name").getValue().equals(
              ConfigureParam.PROVIDER_URL)) {
            PROVIDER_URL = c_node.getValue();
          }

        }
        Properties prop = null;
        if (INITIAL_CONTEXT_FACTORY != null && PROVIDER_URL != null) {
          prop = new Properties();
          prop.put(Context.INITIAL_CONTEXT_FACTORY,
                   INITIAL_CONTEXT_FACTORY);
          prop.put(Context.PROVIDER_URL, PROVIDER_URL);
          context = new InitialContext(prop);
          con = ( (DataSource) context.lookup(jndi_name)).getConnection();
        }
        else {
          context = new InitialContext();

          if (jndi_username != null && jndi_password != null) {
            con = ( (DataSource) context.lookup("java:comp/env/" + jndi_name)).
                getConnection(
                jndi_username, jndi_password);
          }
          else {
            con = ( (DataSource) context.lookup("java:comp/env/" + jndi_name)).
                getConnection();
          }
        }
      }

    }
    catch (Exception ex) {
      log.error(ex);
      throw new SpeedException("connection error " + ex.getMessage());
    }
    return con;
  }

  /**
   * 连接管理(多数据库或连接方式 单一连接)默认
   * @param type String
   * @throws SpeedException
   * @return Connection
   */
  private Connection ConnectionManager(String type) throws SpeedException {
    return this.ConnectionManager("", type);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -