001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.dsi;
020:
021: import java.util.logging.*;
022: import java.util.logging.Logger;
023:
024: import org.openharmonise.commons.dsi.*;
025: import org.openharmonise.rm.config.ConfigException;
026:
027: /**
028: * A factory class which creates new <code>AbstractDataStoreInterface</code>
029: * instances appropriate to the database available to the current deployment
030: * of Harmonise.
031: *
032: * @author Michael Bell
033: * @version $Revision: 1.2 $
034: *
035: */
036: public class DataStoreInterfaceFactory {
037:
038: /**
039: * The configuration parameter name for class name for the concrete
040: * implementation of <code>AbstractDataStoreInterface</code>
041: */
042: public static final String DSI_PNAME = "DSI_CLASS";
043:
044: /**
045: * The configuration parameter name associated with the database URI.
046: */
047: public static final String DBURL_PNAME = "DB_URL";
048:
049: /**
050: * The configuration parameter name associated with the database user name.
051: */
052: public static final String DBUSR_PNAME = "DB_USR";
053:
054: /**
055: * The configuration parameter name associated with the database password.
056: */
057: public static final String DBPWD_PNAME = "DB_PWD";
058:
059: /**
060: * The configuration parameter name associated with the JDBC driver class name to be used
061: * to connect to the database.
062: */
063: public static final String DBDRV_PNAME = "DB_DRIVERNAME";
064:
065: /**
066: * The cached value of data store interface class name
067: */
068: private static String m_dsi_classname = null;
069:
070: /**
071: * The cached data store interface
072: */
073: private static AbstractDataStoreInterface m_dsi = null;
074:
075: /**
076: * Logger for this class
077: */
078: private static final Logger m_logger = Logger
079: .getLogger(DataStoreInterfaceFactory.class.getName());
080:
081: /**
082: * Returns the default data store interface for this deployment of
083: * Harmonise.
084: *
085: * @return the default data store interface for this deployment of
086: * Harmonise.
087: * @throws DataStoreException if there is an error getting the required
088: * configuration data or instantiating the <code>AbstractDataStoreInterface</code>
089: */
090: public static AbstractDataStoreInterface getDataStoreInterface()
091: throws DataStoreException {
092:
093: if (m_dsi == null) {
094:
095: String sDriver;
096: String sURL;
097: String sUsr;
098: String sPwd;
099: try {
100: m_dsi_classname = DatabaseSettings.getInstance()
101: .getDsiClass();
102: sDriver = DatabaseSettings.getInstance().getDbDriver();
103: sURL = DatabaseSettings.getInstance().getDbUrl();
104: sUsr = DatabaseSettings.getInstance().getDbUser();
105: sPwd = DatabaseSettings.getInstance().getDbPwd();
106: } catch (ConfigException e) {
107: throw new DataStoreException(e);
108: }
109:
110: m_dsi = getDataStoreInterface(m_dsi_classname, sDriver,
111: sURL, sUsr, sPwd);
112:
113: }
114:
115: return m_dsi;
116: }
117:
118: /**
119: * Returns an <code>AbstractDataStoreInterface<code> from the specified
120: * parameters.
121: *
122: * @param sDSIclass the data store interface class name
123: * @param sDriver the JDBC driver class name
124: * @param sURL the database URI
125: * @param sUsr the database user name
126: * @param sPwd the database password
127: * @return an <code>AbstractDataStoreInterface<code> from the specified
128: * parameters
129: * @throws DataStoreException if any of the given parameters are incorrect
130: */
131: public static AbstractDataStoreInterface getDataStoreInterface(
132: String sDSIclass, String sDriver, String sURL, String sUsr,
133: String sPwd) throws DataStoreException {
134: AbstractDataStoreInterface dbinterf = null;
135:
136: if (sDSIclass == null) {
137: throw new DataStoreException(
138: "No datastoreinterface class name found");
139: }
140:
141: try {
142: Class cls = Class.forName(sDSIclass);
143:
144: dbinterf = (AbstractDataStoreInterface) cls.newInstance();
145:
146: dbinterf.setDataStoreDetails(sDriver, sURL, sUsr, sPwd);
147: dbinterf
148: .initialise(AbstractDataStoreInterface.DB_CONNECTION_BROKER);
149: } catch (Exception e) {
150: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
151: throw new DataStoreException(
152: "Couldn't create new datastoreinterface");
153: }
154:
155: return dbinterf;
156: }
157: }
|