📄 jmsbasicdatasource.java
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * * * Copyright 2008 Jun Li(SiChuan University, the School of * * Software Engineering). All rights reserved. * * * * Licensed to the JMS under one or more contributor license * * agreements. See the LICENCE file distributed with this * * work for additional information regarding copyright * * ownership. The JMS licenses this file you may not use this * * file except in compliance with the License. * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.jpxx.mail.Service;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.util.Date;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;import org.apache.log4j.Logger;import org.jpxx.mail.Constants;import org.jpxx.mail.Factory;/** * * @author Jun Li * @version 0.0.3, Date: 2008/05/14 */public class JMSBasicDataSource { private static JMSBasicDataSource singletonInstance = null; private static BasicDataSource ds = null; /** * Creates an instance of Logger and initializes it. * It is to write log for <code>JMSBasicDataSource</code>. */ private Logger log = Factory.getSingletonInstance().getLogger(this); private JMSBasicDataSource() { File f = new File(Constants.DBCP_CONFIG_FILE_PATH); if (!f.exists()) { log.error(Constants.DBCP_CONFIG_FILE_PATH + " Not Exist,system will create it." + " Please edit this config file and then restart server."); try { f.createNewFile(); FileWriter fw = new FileWriter(f); fw.write("#This config file is to support database\r\n"); fw.write("#@since version 0.0.3\r\n"); fw.write("#@author Jun Li\r\n"); fw.write("#Create date:" + new Date() + "\r\n"); fw.write("\r\n"); fw.write("#Database driver\r\n"); fw.write("#Example: driver=com.mysql.jdbc.Driver\r\n"); fw.write("driver=com.mysql.jdbc.Driver\r\n"); fw.write("#The database user\r\n"); fw.write("#Example: user=root\r\n"); fw.write("user=root\r\n"); fw.write("#Database use's password\r\n"); fw.write("pass=\r\n"); fw.write("#The database url\r\n"); fw.write("#Example: url=jdbc:mysql://[host][,failoverhost...]" + "[:port]/[database][?propertyName1][=propertyValue1]" + "[&propertyName2][=propertyValue2]...\r\n"); fw.write("url=jdbc:mysql://127.0.0.1:3306/jms\r\n"); fw.close(); } catch (IOException e) { log.error(e); } System.exit(-1); } else { Properties p = new Properties(); try { p.load(new FileInputStream(Constants.DBCP_CONFIG_FILE_PATH)); ds = new BasicDataSource(); String url = p.getProperty("url"); String user = p.getProperty("user"); String pass = p.getProperty("pass"); String driver = p.getProperty("driver"); ds.setDriverClassName(driver); ds.setUsername(user); ds.setPassword(pass); ds.setUrl(url); } catch (IOException e) { log.error(e); } } } /** * Gets singleton instance of JMSBasicDataSource * @return Singleton Instance of JMSBasicDataSource */ public static JMSBasicDataSource getSingletonInstance() { if (singletonInstance == null) { singletonInstance = new JMSBasicDataSource(); } return singletonInstance; } /** * Gets BasicDataSource * @return DataSource */ public DataSource getDataSource() { if (ds == null) { log.error("Please edit the " + Constants.DBCP_CONFIG_FILE_PATH + " config file and then restart server."); } return ds; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -