package com.etong.system;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.etong.hr.DatabaseHRFactory;
import com.etong.hr.HRFactory;
import com.etong.statetaxbureau.workflow.BureauFactory;
import com.etong.workflow.DatabaseWorkflowFactory;
import com.etong.workflow.WorkflowFactory;
import com.roger.database.DBPersistence;
/**
* <p>Title: </p>
* <p>Description: 资源工厂类, 用来产生系统需要用到的资源, 如数据库的连接</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
public class ResourceFactory {
private static LogRelated lg = new LogRelated("D:/log","lgConnOracle");
private static int no = 0;
private static InitialContext JNDI_Context = null;
private static final java.sql.Connection getSQLServerConnection() {
java.sql.Connection con = null;
try {
Context ctx = getInitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("SQLDS");
con = ds.getConnection();
if (con == null) {
throw new WorkflowConnectionException("SQL数据库连接不上");
}
}
catch (Exception e) {
e.printStackTrace();
throw new WorkflowConnectionException("SQL数据库连接不上");
}
return con;
}
private static synchronized final java.sql.Connection getOracleConnection() {
java.sql.Connection con = null;
try {
if(no >= Integer.MAX_VALUE){
no = 0;
}
no++;
lg.log("no = " + no +";上下文初始前!---Context");
Context ctx = getInitialContext();
lg.log("no = " + no +";获得数据源前!----DataSource");
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("workflowDS");
lg.log("no = " + no +";获得连接前!---getConnection");
con = ds.getConnection();
lg.log("no = " + no +";获得连接后!---conn");
if (con == null) {
lg.log("no = " + no +";获得连接为空!");
throw new WorkflowConnectionException("工作流数据库连接不上");
}else{
lg.log("no = " + no + ";数据库连接成功!");
}
}
catch (Exception e) {
e.printStackTrace();
e.printStackTrace(lg.getLogPW());
throw new WorkflowConnectionException("工作流数据库连接不上");
}
return con;
}
private static Context getInitialContext() throws Exception {
//采用静态JNDI_Context来第一次读取上下文信息
if(JNDI_Context != null){
return JNDI_Context;
}
lg.log("no = " + no +";getInitialContext!---getInitialContext");
Properties pro = new Properties();
FileInputStream fileInputStream = new FileInputStream(new File("C:/pro.properties"));
pro.load(fileInputStream);
String url = pro.getProperty("url");
String user = pro.getProperty("username");
String password = pro.getProperty("password");
fileInputStream.close();
//结束
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
JNDI_Context = new InitialContext(properties);
return JNDI_Context;
}
public static final java.sql.Connection getCTAISConnection() {
java.sql.Connection con = null;
try {
Context ctx = getInitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("ctaisDS");
con = ds.getConnection();
if (con == null) {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
}
catch (Exception e) {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
return con;
}
public static final java.sql.Connection getConnection() {
if (isSQLServer()) {
return getSQLServerConnection();
}
else if
(isOracle()) {
return getOracleConnection();
}
else {
throw new WorkflowConnectionException("工作流数据库连接不上");
}
}
public static final DBPersistence createDBPersistence() {
DBPersistence db = new DBPersistence();
if (isSQLServer()) {
db.setConnection(getSQLServerConnection());
}
else if
(isOracle()) {
db.setConnection(getOracleConnection());
}
else {
throw new WorkflowConnectionException("工作流数据库连接不上");
}
return db;
}
public static final DBPersistence createCTAISPersistence() {
DBPersistence db = new DBPersistence();
if (isSQLServer()) {
db.setConnection(getSQLServerConnection());
}
else if
(isOracle()) {
db.setConnection(getCTAISConnection());
}
else {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
return db;
}
/**
* 创建一个人事工厂类
* @return 人事工厂类
*/
public static final HRFactory createHRFactory() {
return new DatabaseHRFactory();
}
/**
* 创建一个工作流工厂类
* @return 工作流工厂类
*/
public static final WorkflowFactory createWorkflowFactory() {
return new DatabaseWorkflowFactory();
}
/**
* 创建一个系统工厂类
* @return 系统工厂类
*/
public static final SystemFactory createSystemFactory() {
if (isSQLServer()) {
return new SQLServerSystemFactory();
}
else if
(isOracle()) {
return new OracleSystemFactory();
}
else {
throw new IllegalArgumentException("工作流数据库连接不上");
}
}
public static final BureauFactory createBureauFactory() {
return new BureauFactory();
}
/**
* 得到用户自定义的字段类型
* @return 字段类型
*/
public static final String[] getUserField() {
String[] s = {
"整型", "小数", "字符型", "日期型"};
return s;
}
public static final boolean isSQLServer() {
return false;
}
public static final boolean isOracle() {
return true;
}
public static void main(String[] args) throws Exception {
}
}
其中:LogRelated只是为了便于找问题,创建的一个日志文件而已.其源代码为.
package com.etong.system;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
public class LogRelated {
private String logDirName = "";
private String logFileName = "";
private PrintWriter logPW = null;
private String strFileTime = new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10);
public LogRelated(String logDirName, String logFileName) {
initialLog(logDirName, logFileName);
}
private void initialLog(String logDirName, String logFileName) {
this.logDirName = logDirName;
this.logFileName = logFileName;
if (logDirName.indexOf("\\") != -1) {
System.out.println("Input logDirName is incorrect!Please input like UNIX Directory!");
return;
}
File logFile = null;
if (logDirName == null || logDirName.equals("")) {
logFile = new File(logFileName + "_" + this.strFileTime + ".txt");
} else {
logFile = new File(logDirName + "/" + logFileName + "_" + this.strFileTime + ".txt");
}
if (!logFile.isFile()) {
logFile = logFile.getAbsoluteFile();
File parentDir = new File(logFile.getParent());
if (!parentDir.exists()) {
parentDir.mkdirs();
}
}
try {
logPW = new PrintWriter(new FileWriter(logFile, true), true);
} catch (IOException e) {
System.err.println("Cann't open log file: " + logFile);
logPW = new PrintWriter(System.err);
}
}
public void log(String logContent) {
if (!new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10).equals(this.strFileTime)) {
this.strFileTime = new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10);
initialLog(this.logDirName, this.logFileName);
}
try {
logPW.println(new java.sql.Timestamp(System.currentTimeMillis()) + ": " + logContent);
} catch (Exception ex) {
this.initialLog(this.logDirName, this.logFileName);
ex.printStackTrace(logPW);
this.log(logContent);
}
}
public String getLogFileName() {
return logFileName;
}
public void setLogDirName(String logDirName) {
this.logDirName = logDirName;
}
public void setLogFileName(String logFileName) {
this.logFileName = logFileName;
}
public void setLogPW(PrintWriter logPW) {
this.logPW = logPW;
}
public String getLogDirName() {
return logDirName;
}
public PrintWriter getLogPW() {
return logPW;
}
}