📄 dbconnectparamanager.java
字号:
package llm.pool.relation;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class DBConnectParaManager {
private static Logger log = Logger.getLogger( DBConnectParaManager.class.getName() );
private HashMap hashMapConfig;
public DBConnectParaManager() {
}
public HashMap getParaHashMap() throws LlmDBException {
return hashMapConfig;
}
public DBConnectPara getDBConnectPara( String jndiName ) throws LlmDBException {
DBConnectPara dbcp = (DBConnectPara) hashMapConfig.get(jndiName);
if ( null == dbcp )
throw new LlmDBException("取配置文件时出错!无法获得JNDI名为 " + jndiName + " 的配置,请联系管理员。");
return dbcp;
}
// 首先查找 properties ,不在时,找 XML 文件
public void init() throws LlmDBException {
try {
hashMapConfig = new HashMap();
String xmlPath = getXmlFilePath();
log.debug( "成功获得数据库配置XML文件的物理路径: " + xmlPath );
Element root = loadDocument( getXmlFilePath() );
getXMLConfig( root );
} catch ( Exception e ) {
throw new LlmDBException( "取配置文件时出错!请联系管理员。", e );
}
}
private String getXmlFilePath() {
String classPath = this.getClass().getClassLoader().getResource("").getFile();
if ( null == classPath || classPath.trim().length() == 0 ) {
throw new RuntimeException( "文件路径出错!" );
}
int index = classPath.indexOf("classes");
if ( index >= 0 ) {
classPath = classPath.substring( 0, index );
}
return classPath + Contents.DATABASE_CONFIG_XML_PATH;
}
private int getConfigInteger(String numString) {
if (null == numString || 0 == numString.trim().length()) {
log.debug("参数传递为null或是\"\",不能转换。");
return 5;
}
try {
return Integer.valueOf(numString).intValue();
} catch (Exception e) {
log.debug("参数 " + numString + " 转换为整数出错。", e);
return 5;
}
}
public void print(String jndiName) {
DBConnectPara dbcp = (DBConnectPara) hashMapConfig.get(jndiName);
if (null != dbcp) {
System.out.println("数据库源:");
System.out.println(dbcp.getJndiName());
System.out.println(dbcp.getDataSourceName());
System.out.println(dbcp.getJndiDesc());
System.out.println(dbcp.getDbType());
System.out.println(dbcp.getDbDriver());
System.out.println(dbcp.getDbUrl());
System.out.println(dbcp.getDbUser());
System.out.println(dbcp.getDbPassword());
System.out.println(dbcp.getDbConnInit());
System.out.println(dbcp.getDbConnIdle());
System.out.println(dbcp.getDbConnMax());
System.out.println(dbcp.getDbConnIncrem());
System.out.println(dbcp.getAppCharset());
System.out.println(dbcp.getDbReadCharset());
System.out.println(dbcp.getDbWriteCharset());
System.out.println(dbcp.getConnectType());
} else {
log.error("数据库源 " + jndiName + " 不存在");
}
}
private Element loadDocument( String location ) throws ParserConfigurationException, SAXException, IOException {
Document doc = null;
File file = new File( location );
InputStream inputStream = new DataInputStream(new BufferedInputStream(new
FileInputStream(file)));
InputSource xmlInp = new InputSource(inputStream);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
Element root = doc.getDocumentElement();
root.normalize();
inputStream.close();
return root;
}
private void getXMLConfig(Element root ) {
DBConnectPara dbcp = null;
String str = "";
NodeList list = root.getElementsByTagName( Contents.DATABASE_MAPPING );
int listLength = list.getLength();
log.debug(" 配置的连接池数目: " + listLength );
for ( int loop = 0; loop < listLength; loop ++ ) {
Node node = list.item(loop);
if (node != null) {
if (node instanceof Element) {
dbcp = new DBConnectPara();
// Element element = (Element) node;
str = getSubTagValue(node, Contents.DATABASE_CONFIG_JNDI_NAME);
log.debug( " 数据源索引名: " + str );
dbcp.setJndiName( str == null?"":str.trim() );
str = getSubTagValue(node, Contents.DATABASE_CONFIG_DATASOURCE_NAME);
log.debug( " 数据源名称: " + str );
dbcp.setDataSourceName( str == null?"":str.trim() );
str = getSubTagValue(node, Contents.DATABASE_CONFIG_JNDI_DESC);
log.debug( " 数据源说明: " + str );
dbcp.setJndiDesc( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_DB_TYPE );
dbcp.setDbType( getConfigInteger( str ) );
log.debug( "数据库类型(ORACLE = 0; SYBASE = 1; SQLSERVER = 2; MYSQL = 3):" + str );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_DRIVER_CLASS );
log.debug( "JDBC 驱动程序:" + str );
dbcp.setDbDriver( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_URL );
log.debug( "数据库 URL :" + str );
dbcp.setDbUrl( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_USER );
log.debug( "数据库用户名:" + str );
dbcp.setDbUser( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_PASSWORD );
log.debug( "数据库密码:" + str );
dbcp.setDbPassword( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_INIT_NUM );
log.debug( "初始化连接数:" + str );
dbcp.setDbConnInit( getConfigInteger( str ) );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_IDLE_NUM );
log.debug( "最大空闲连接数:" + str );
dbcp.setDbConnIdle( getConfigInteger( str ) );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_MAX_NUM );
log.debug( "最大连接数:" + str );
dbcp.setDbConnMax( getConfigInteger( str ) );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_INCREMENT_NUM );
log.debug( "增量连接数:" + str );
dbcp.setDbConnIncrem( getConfigInteger( str ) );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_CHARSET_APP );
log.debug( "应用字符集(如果字符不需要转换,请不要填写,系统将不会再处理):" + str );
dbcp.setAppCharset( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_CHARSET_DB_READ );
log.debug( "数据库读字符集:" + str );
dbcp.setDbReadCharset( str == null?"":str.trim() );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_CHARSET_DB_WRITE );
log.debug( "数据库写字符集:" + str );
dbcp.setDbWriteCharset( str == null?"":str.trim() );
if( dbcp.getAppCharset().equals( "" )
|| dbcp.getDbReadCharset().equals( "" )
|| dbcp.getDbWriteCharset().equals( "" ) )
dbcp.setChangeCharset( false );
else
dbcp.setChangeCharset( true );
str = getSubTagValue( node, Contents.DATABASE_CONFIG_CONNECT_TYPE );
log.debug( "是否从数据源中获取连接:" + str );
str = str.trim();
if ( Contents.CONNECT_TYPE_LLM_SIMPLE_POOL.equalsIgnoreCase( str ) ) {
dbcp.setConnectType( Contents.CONNECT_TYPE_LLM_SIMPLE_POOL );
} else if ( Contents.CONNECT_TYPE_LLM_MUDULE_POOL.equalsIgnoreCase( str ) ) {
dbcp.setConnectType( Contents.CONNECT_TYPE_LLM_MUDULE_POOL );
} else if ( Contents.CONNECT_TYPE_LLM_COMMON_POOL.equalsIgnoreCase( str ) ) {
dbcp.setConnectType( Contents.CONNECT_TYPE_LLM_COMMON_POOL );
} else if ( "true".equalsIgnoreCase( str ) || "yes".equalsIgnoreCase( str ) ) {
dbcp.setConnectType( Contents.CONNECT_TYPE_LLM_MUDULE_POOL );
} else {
dbcp.setConnectType( Contents.CONNECT_TYPE_LLM_SIMPLE_POOL );
}
hashMapConfig.put( dbcp.getJndiName(), dbcp );
}
}
}
}
// private static String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute)
// {
// String returnString = "";
// NodeList list = root.getElementsByTagName(tagName);
// for(int loop = 0; loop < list.getLength(); loop++)
// {
// Node node = list.item(loop);
// if(node != null)
// {
// NodeList children = node.getChildNodes();
// for(int innerLoop = 0; innerLoop < children.getLength(); innerLoop++)
// {
// Node child = children.item(innerLoop);
// if(child != null && child.getNodeName() != null && child.getNodeName().equals(subTagName) && (child instanceof Element))
// return ((Element)child).getAttribute(attribute);
// }
// }
// }
// return returnString;
// }
private static String getSubTagValue(Node node, String subTagName) {
String returnString = "";
if( node != null ) {
NodeList children = node.getChildNodes();
for(int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
Node child = children.item(innerLoop);
if(child != null && child.getNodeName() != null && child.getNodeName().equals(subTagName)) {
Node grandChild = child.getFirstChild();
if( null != grandChild && null != grandChild.getNodeValue())
return grandChild.getNodeValue();
}
}
}
return returnString;
}
public static void main( String[] args ) throws LlmDBException, ParserConfigurationException, SAXException, IOException {
DBConnectParaManager dbConnectParaManager = new DBConnectParaManager();
dbConnectParaManager.init();
dbConnectParaManager.print("mysqlDs");
dbConnectParaManager.print("SYSTEM");
dbConnectParaManager.print("sqlserverDS");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -