DataStoreInterfaceFactory.java :  » Content-Management-System » harmonise » org » openharmonise » rm » dsi » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » rm » dsi » DataStoreInterfaceFactory.java
/*
 * The contents of this file are subject to the 
 * Mozilla Public License Version 1.1 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * See the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Initial Developer of the Original Code is Simulacra Media Ltd.
 * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
 *
 * All Rights Reserved.
 *
 * Contributor(s):
 */
package org.openharmonise.rm.dsi;

import java.util.logging.*;
import java.util.logging.Logger;

import org.openharmonise.commons.dsi.*;
import org.openharmonise.rm.config.ConfigException;


/**
 * A factory class which creates new <code>AbstractDataStoreInterface</code>
 * instances appropriate to the database available to the current deployment
 * of Harmonise.
 * 
 * @author Michael Bell
 * @version $Revision: 1.2 $
 *
 */
public class DataStoreInterfaceFactory {
  
  /**
   * The configuration parameter name for class name for the concrete 
   * implementation of <code>AbstractDataStoreInterface</code>
   */
  public static final String DSI_PNAME = "DSI_CLASS";
  
  /**
   * The configuration parameter name associated with the database URI.
   */
  public static final String DBURL_PNAME = "DB_URL";
  
  /**
   * The configuration parameter name associated with the database user name.
   */
  public static final String DBUSR_PNAME = "DB_USR";
  
  /**
   * The configuration parameter name associated with the database password.
   */
  public static final String DBPWD_PNAME = "DB_PWD";
  
  /**
   * The configuration parameter name associated with the JDBC driver class name to be used
   * to connect to the database.
   */
  public static final String DBDRV_PNAME = "DB_DRIVERNAME";
  
  /**
   * The cached value of data store interface class name
   */
  private static String m_dsi_classname = null;
  
  /**
   * The cached data store interface
   */
  private static AbstractDataStoreInterface m_dsi = null;
  
  /**
   * Logger for this class
   */
  private static final Logger m_logger = Logger.getLogger(DataStoreInterfaceFactory.class.getName());

  /**
   * Returns the default data store interface for this deployment of
   * Harmonise.
   * 
   * @return the default data store interface for this deployment of
   * Harmonise.
   * @throws DataStoreException if there is an error getting the required
   * configuration data or instantiating the <code>AbstractDataStoreInterface</code>
   */
  public static AbstractDataStoreInterface getDataStoreInterface()
    throws DataStoreException {

    if (m_dsi == null) {

      String sDriver;
            String sURL;
            String sUsr;
            String sPwd;
            try {
                m_dsi_classname = DatabaseSettings.getInstance().getDsiClass();
                sDriver = DatabaseSettings.getInstance().getDbDriver();
                sURL = DatabaseSettings.getInstance().getDbUrl();
                sUsr = DatabaseSettings.getInstance().getDbUser();
                sPwd = DatabaseSettings.getInstance().getDbPwd();
            }
            catch (ConfigException e) {
                throw new DataStoreException(e);
            }

      m_dsi =
        getDataStoreInterface(
          m_dsi_classname,
          sDriver,
          sURL,
          sUsr,
          sPwd);

    }

    return m_dsi;
  }

  /**
   * Returns an <code>AbstractDataStoreInterface<code> from the specified
   * parameters.
   * 
   * @param sDSIclass the data store interface class name
   * @param sDriver the JDBC driver class name
   * @param sURL the database URI
   * @param sUsr the database user name
   * @param sPwd the database password
   * @return  an <code>AbstractDataStoreInterface<code> from the specified
   * parameters
   * @throws DataStoreException if any of the given parameters are incorrect
   */
  public static AbstractDataStoreInterface getDataStoreInterface(
    String sDSIclass,
    String sDriver,
    String sURL,
    String sUsr,
    String sPwd)
    throws DataStoreException {
    AbstractDataStoreInterface dbinterf = null;

    if (sDSIclass == null) {
      throw new DataStoreException("No datastoreinterface class name found");
    }

    try {
      Class cls = Class.forName(sDSIclass);

      dbinterf = (AbstractDataStoreInterface) cls.newInstance();

      dbinterf.setDataStoreDetails(sDriver, sURL, sUsr, sPwd);
      dbinterf.initialise(
        AbstractDataStoreInterface.DB_CONNECTION_BROKER);
    } catch (Exception e) {
      m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
      throw new DataStoreException("Couldn't create new datastoreinterface");
    }

    return dbinterf;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.