001: /*
002: * Created on May 2, 2003
003: *
004: * Dbmjui is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License version 2 as
006: * published by the Free Software Foundation.
007: *
008: * Dbmjui is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public
014: * License along with dbmjui; see the file COPYING. If not,
015: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
016: * Boston, MA 02111-1307, USA.
017: *
018: */
019: package fr.aliacom.dbmjui;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: import org.apache.log4j.Logger;
025:
026: import fr.aliacom.dbmjui.driver.DbmJuiDriverManager;
027: import fr.aliacom.dbmjui.driver.IBackupHelper;
028: import fr.aliacom.dbmjui.driver.IDbmJuiDriver;
029: import fr.aliacom.dbmjui.driver.IHistoryHelper;
030: import fr.aliacom.dbmjui.driver.IInformationHelper;
031: import fr.aliacom.dbmjui.driver.IInstanceHelper;
032:
033: /**
034: * Factory methods to get the approriate helpers
035: * for a given instance.
036: *
037: * It is responsible for locating an {@link fr.aliacom.dbmjui.driver.IDbmJuiDriver}
038: * driver suitable to work with a given instance.
039: *
040: * The driver for a given instance will be cached.
041: *
042: * @author tom
043: *
044: * (c) 2001, 2003 Thomas Cataldo
045: */
046: public final class HelperFactory {
047:
048: private static HelperFactory singleton;
049:
050: static {
051: singleton = new HelperFactory();
052: }
053:
054: public static HelperFactory getInstance() {
055: return singleton;
056: }
057:
058: private HashMap driverCache;
059: private Logger log;
060:
061: private HelperFactory() {
062: log = Logger.getLogger(HelperFactory.class);
063: driverCache = new HashMap();
064: }
065:
066: public IHistoryHelper getHistoryHelper(DbInstance dbi) {
067: return findSuitableDriver(dbi).getHistoryHelper();
068: }
069:
070: public IInformationHelper getInformationHelper(DbInstance dbi) {
071: return findSuitableDriver(dbi).getInformationHelper();
072: }
073:
074: public IInstanceHelper getInstanceHelper(DbInstance dbi) {
075: return findSuitableDriver(dbi).getInstanceHelper();
076: }
077:
078: public IBackupHelper getBackupHelper(DbInstance dbi) {
079: return findSuitableDriver(dbi).getBackupHelper();
080: }
081:
082: protected IDbmJuiDriver findSuitableDriver(DbInstance dbi) {
083: String version = dbi.getVersion();
084: Object driver = driverCache.get(version);
085: if (driver == null) {
086: Iterator it = DbmJuiDriverManager.getInstance()
087: .getDrivers();
088: while (it.hasNext()) {
089: IDbmJuiDriver aDriver = (IDbmJuiDriver) it.next();
090: if (aDriver != null && aDriver.worksWith(version)) {
091: driver = aDriver;
092: driverCache.put(version, driver);
093: aDriver.init();
094: break;
095: }
096: }
097: if (driver == null) {
098: log.fatal("No DbmJui driver found.");
099: }
100: }
101: return (IDbmJuiDriver) driver;
102: }
103:
104: }
|