001: package com.jat.integration;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005:
006: import com.jat.core.config.Config;
007: import com.jat.core.init.Initable;
008: import com.jat.core.log.LogManager;
009: import java.util.Vector;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description: this class handle configurable data sources {@link com.jat.integration.DataSource}.
014: * </p>
015: * <p><b>Configuration:</b><br/>
016: * Put data source list into "data_source" section with:
017: * <ul>
018: * <li>data source name</li>
019: * <li>data source class (a {@link com.jat.integration.DataSource} implementation class)</li>
020: * </ul>
021: * <i>Example:</i>
022: * <blockquote>
023: * [data_source]<br>
024: * data_source1.name = <i>myDataSource</i><br>
025: * data_source1.class = <i>myDataSourceClass</i><br>
026: * data_source2.name = <i>mySecondDataSource</i><br>
027: * data_source2.class = <i>mySecondDataSourceClass</i><br>
028: * </blockquote>
029: * <br>For each data source name create a new section named as the data source name.
030: * <br>Put properties into this section according with your data source class
031: * </p>
032: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
033: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
034: * @author stf
035: * @version 1.2
036: * @since 1.2
037: * @see com.jat.integration.DataSource
038: */
039:
040: public final class DataSourceFactory implements Initable {
041:
042: public final static String CONFIG_SECTION = "data_source";
043: public final static String CONFIG_DATA_SOURCE_KEY = "data_source";
044: public final static String CONFIG_CLASS_KEY = ".class";
045: public final static String CONFIG_NAME_KEY = ".name";
046: public final static String CONFIG_ENABLED = ".enabled";
047:
048: public DataSourceFactory() {
049: }
050:
051: public void init() throws Exception {
052: LogManager.sendDebug(this .getClass().getName()
053: + "::init: start");
054: dataSourceFactory = this ;
055: dataSources = new Hashtable();
056: Vector keys = Config.getCurrent().getSubKeys(CONFIG_SECTION,
057: CONFIG_DATA_SOURCE_KEY);
058: for (Enumeration e = keys.elements(); e.hasMoreElements();) {
059: String key = (String) e.nextElement();
060: String className = Config.getCurrent().getValue(
061: CONFIG_SECTION, key + CONFIG_CLASS_KEY);
062: String name = Config.getCurrent().getValue(CONFIG_SECTION,
063: key + CONFIG_NAME_KEY);
064: boolean enabled = true;
065: try {
066: enabled = Config.getCurrent().getValue(CONFIG_SECTION,
067: key + CONFIG_ENABLED).equalsIgnoreCase("true");
068: } catch (Exception ignored) {
069: }
070: if (enabled) {
071: LogManager.sendDebug(this .getClass().getName()
072: + "::init: initing data source " + name);
073: try {
074: DataSource dataSource = (DataSource) Class.forName(
075: className).newInstance();
076: dataSource.init(name);
077: dataSources.put(name, dataSource);
078: LogManager.sendDebug(this .getClass().getName()
079: + "::init: data source '" + name + "'("
080: + className + ") successfully inited");
081: } catch (Exception ex) {
082: LogManager.sendError(this .getClass().getName()
083: + "::init: exception loading data source '"
084: + name + "': " + ex);
085: }
086: } else
087: LogManager.sendDebug(this .getClass().getName()
088: + "::init: data source '" + name
089: + "' is disabled");
090: }
091: LogManager.sendDebug(this .getClass().getName() + "::init: end");
092: }
093:
094: public DataSource getDataSource(String name)
095: throws IntegrationException {
096: DataSource ds = (DataSource) dataSources.get(name);
097: if (ds == null) {
098: LogManager.sendWarning(this .getClass().getName()
099: + "::getDataSource: Data source '" + name
100: + "' not found");
101: throw new IntegrationException("Data source '" + name
102: + "' not found");
103: }
104: return ds;
105: }
106:
107: public static DataSourceFactory getFactory() {
108: return dataSourceFactory;
109: }
110:
111: public Enumeration names() {
112: return this .dataSources.keys();
113: }
114:
115: private static Hashtable dataSources = null;
116: private static DataSourceFactory dataSourceFactory = null;
117:
118: /** @link dependency
119: * @stereotype instantiate*/
120: /*# DataSource lnkDataSource; */
121: }
|