001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/SecondaryMVNCoreConfig.java,v 1.2 2007/04/05 09:13:49 phuongpdd Exp $
003: * $Author: phuongpdd $
004: * $Revision: 1.2 $
005: * $Date: 2007/04/05 09:13:49 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2006 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Phuong Pham Dinh Duy
032: */
033: package net.myvietnam.mvncore;
034:
035: import java.io.File;
036:
037: import net.myvietnam.mvncore.configuration.DOM4JConfiguration;
038: import net.myvietnam.mvncore.db.DBOptions;
039: import net.myvietnam.mvncore.util.FileUtil;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: public class SecondaryMVNCoreConfig {
045:
046: private static Log log = LogFactory
047: .getLog(SecondaryMVNCoreConfig.class);
048:
049: private static final String OPTION_FILE_NAME = "secondary-database.xml";
050:
051: private static DBOptions mainDbOptions = new DBOptions(
052: "ExternalDatabase");
053:
054: public static DBOptions getDbOptions() {
055: return mainDbOptions;
056: }
057:
058: static {
059:
060: String classPath = FileUtil.getServletClassesPath();
061: String configFilename = classPath + OPTION_FILE_NAME;
062: try {
063: DOM4JConfiguration conf = new DOM4JConfiguration(new File(
064: configFilename));
065:
066: configDatabase(conf);
067: } catch (Exception e) {
068: String message = "com.mvnforum.SecondaryMVNCoreConfig: Can't read the configuration file: '"
069: + configFilename
070: + "'. Make sure the file is in your CLASSPATH";
071: log.error(message, e);
072: }
073: }
074:
075: private static void configDatabase(DOM4JConfiguration conf) {
076:
077: boolean useDataSource = conf.getBoolean(
078: "dboptions.use_datasource", false);
079: mainDbOptions.setUseDataSource(useDataSource);
080:
081: int dbType = conf.getInt("dboptions.database_type", 0);
082: mainDbOptions.setDbType(dbType);
083:
084: if (useDataSource) {
085: String dataSourceName = conf
086: .getString("dboptions.datasource_name");
087: mainDbOptions.setDataSourceName(dataSourceName);
088: } else {
089: String dbDriverClass = conf.getString(
090: "dboptions.driver_class_name", "");
091: mainDbOptions.setDriverClass(dbDriverClass);
092:
093: String dbUrl = conf.getString("dboptions.database_url", "");
094: mainDbOptions.setDbUrl(dbUrl);
095:
096: String dbUsername = conf.getString(
097: "dboptions.database_user", "");
098: mainDbOptions.setUsername(dbUsername);
099:
100: String dbPassword = conf.getString(
101: "dboptions.database_password", "");
102: mainDbOptions.setPassword(dbPassword);
103:
104: int dbConMax = conf.getInt("dboptions.max_connection", 20);
105: mainDbOptions.setConMax(dbConMax);
106:
107: int dbTimeout = conf.getInt("dboptions.max_time_to_wait",
108: 2000);
109: mainDbOptions.setTimeout(dbTimeout);
110:
111: int dbRefreshMinutes = conf.getInt(
112: "dboptions.minutes_between_refresh", 30);
113: if (dbRefreshMinutes < 1) {
114: dbRefreshMinutes = 1; //min is 1 minute
115: }
116: mainDbOptions.setRefreshMinutes(dbRefreshMinutes);
117: }
118: }
119: }
|