KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > dsi > DataStoreInterfaceFactory


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.dsi;
20
21 import java.util.logging.*;
22 import java.util.logging.Logger JavaDoc;
23
24 import org.openharmonise.commons.dsi.*;
25 import org.openharmonise.rm.config.ConfigException;
26
27
28 /**
29  * A factory class which creates new <code>AbstractDataStoreInterface</code>
30  * instances appropriate to the database available to the current deployment
31  * of Harmonise.
32  *
33  * @author Michael Bell
34  * @version $Revision: 1.2 $
35  *
36  */

37 public class DataStoreInterfaceFactory {
38     
39     /**
40      * The configuration parameter name for class name for the concrete
41      * implementation of <code>AbstractDataStoreInterface</code>
42      */

43     public static final String JavaDoc DSI_PNAME = "DSI_CLASS";
44     
45     /**
46      * The configuration parameter name associated with the database URI.
47      */

48     public static final String JavaDoc DBURL_PNAME = "DB_URL";
49     
50     /**
51      * The configuration parameter name associated with the database user name.
52      */

53     public static final String JavaDoc DBUSR_PNAME = "DB_USR";
54     
55     /**
56      * The configuration parameter name associated with the database password.
57      */

58     public static final String JavaDoc DBPWD_PNAME = "DB_PWD";
59     
60     /**
61      * The configuration parameter name associated with the JDBC driver class name to be used
62      * to connect to the database.
63      */

64     public static final String JavaDoc DBDRV_PNAME = "DB_DRIVERNAME";
65     
66     /**
67      * The cached value of data store interface class name
68      */

69     private static String JavaDoc m_dsi_classname = null;
70     
71     /**
72      * The cached data store interface
73      */

74     private static AbstractDataStoreInterface m_dsi = null;
75     
76     /**
77      * Logger for this class
78      */

79     private static final Logger JavaDoc m_logger = Logger.getLogger(DataStoreInterfaceFactory.class.getName());
80
81     /**
82      * Returns the default data store interface for this deployment of
83      * Harmonise.
84      *
85      * @return the default data store interface for this deployment of
86      * Harmonise.
87      * @throws DataStoreException if there is an error getting the required
88      * configuration data or instantiating the <code>AbstractDataStoreInterface</code>
89      */

90     public static AbstractDataStoreInterface getDataStoreInterface()
91         throws DataStoreException {
92
93         if (m_dsi == null) {
94
95             String JavaDoc sDriver;
96             String JavaDoc sURL;
97             String JavaDoc sUsr;
98             String JavaDoc sPwd;
99             try {
100                 m_dsi_classname = DatabaseSettings.getInstance().getDsiClass();
101                 sDriver = DatabaseSettings.getInstance().getDbDriver();
102                 sURL = DatabaseSettings.getInstance().getDbUrl();
103                 sUsr = DatabaseSettings.getInstance().getDbUser();
104                 sPwd = DatabaseSettings.getInstance().getDbPwd();
105             }
106             catch (ConfigException e) {
107                 throw new DataStoreException(e);
108             }
109
110             m_dsi =
111                 getDataStoreInterface(
112                     m_dsi_classname,
113                     sDriver,
114                     sURL,
115                     sUsr,
116                     sPwd);
117
118         }
119
120         return m_dsi;
121     }
122
123     /**
124      * Returns an <code>AbstractDataStoreInterface<code> from the specified
125      * parameters.
126      *
127      * @param sDSIclass the data store interface class name
128      * @param sDriver the JDBC driver class name
129      * @param sURL the database URI
130      * @param sUsr the database user name
131      * @param sPwd the database password
132      * @return an <code>AbstractDataStoreInterface<code> from the specified
133      * parameters
134      * @throws DataStoreException if any of the given parameters are incorrect
135      */

136     public static AbstractDataStoreInterface getDataStoreInterface(
137         String JavaDoc sDSIclass,
138         String JavaDoc sDriver,
139         String JavaDoc sURL,
140         String JavaDoc sUsr,
141         String JavaDoc sPwd)
142         throws DataStoreException {
143         AbstractDataStoreInterface dbinterf = null;
144
145         if (sDSIclass == null) {
146             throw new DataStoreException("No datastoreinterface class name found");
147         }
148
149         try {
150             Class JavaDoc cls = Class.forName(sDSIclass);
151
152             dbinterf = (AbstractDataStoreInterface) cls.newInstance();
153
154             dbinterf.setDataStoreDetails(sDriver, sURL, sUsr, sPwd);
155             dbinterf.initialise(
156                 AbstractDataStoreInterface.DB_CONNECTION_BROKER);
157         } catch (Exception JavaDoc e) {
158             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
159             throw new DataStoreException("Couldn't create new datastoreinterface");
160         }
161
162         return dbinterf;
163     }
164 }
Popular Tags