001 /*
002 * 10/09/2002 - 22:56:11
003 *
004 * $RCSfile: ConnectionManager.java,v $ - JDBF Object Relational mapping system
005 * Copyright (C) 2002 JDBF Development Team
006 *
007 * http://jdbf.sourceforge.net
008 *
009 * This program is free software; you can redistribute it and/or
010 * modify it under the terms of the GNU Lesser General Public License
011 * as published by the Free Software Foundation; either version 2
012 * of the License, or (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017 * GNU Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public License
020 * along with this program; if not, write to the Free Software
021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022 */
023 /*
024 $Id: ConnectionManager.java,v 1.10 2004/05/20 22:42:47 gmartone Exp $
025 */
026 package org.jdbf.engine.sql.connection;
027
028 import java.sql.*;
029 import java.util.*;
030 import java.util.logging.Logger;
031 import java.util.logging.Level;
032
033 import org.jdbf.engine.configuration.Configuration;
034 import org.jdbf.engine.configuration.ConfigurationImpl;
035 import org.jdbf.engine.Configurable;
036 import org.jdbf.castor.Messages;
037 import org.jdbf.engine.mapping.DatabaseMap;
038 import org.jdbf.engine.mapping.MappingException;
039 /**
040 * <code>ConnectionManager</code> handles the database connection
041 * for many vendors. SetConfiguration method, read information about
042 * database connection from configuration file and creates database
043 * connection.<br>
044 *
045 * Te database connection is created with flag autoCommit setted to
046 * false value.
047 *
048 *
049 * @author Giovanni Martone<br>
050 * @version $Revision: 1.10 $<br>
051 * last changed by $Author: gmartone $
052 *
053 */
054 public class ConnectionManager implements Configurable {
055
056 /**
057 * Class name
058 */
059 private static final String CLASS_NAME = "org.jdbf.engine.sql.connection.ConnectionManager";
060 /**
061 * Logger object
062 */
063 private Logger logger;
064
065
066 /** A list of the availabe connections. */
067 private Map conns = Collections.synchronizedMap(
068 new HashMap());
069 /**
070 *
071 * Creates an empty object
072 *
073 */
074 public ConnectionManager() {
075 logger = Logger.getLogger(CLASS_NAME);
076 }
077 /**
078 * Add a new ConnectionSource with a specific name
079 *
080 * @param name name of connection
081 * @param connection - ConnectionSource object
082 *
083 */
084 public void addConnection(String name,ConnectionSource connection){
085
086 conns.put(name, connection);
087 }
088
089 /**
090 * Create a collection of database connection
091 *
092 * Connection is setted with autoCommit property
093 * to false value.
094 *
095 * @param dbs
096 * @exception MappingException if an error occurs
097 */
098 public void createConnection(List dbs)
099 throws MappingException{
100
101 DatabaseMap dbMap = null;
102 logger.log(Level.INFO,Messages.message("ConnectionManager.createConnection"));
103 try{
104
105 for(int i = 0; i < dbs.size(); i++){
106 dbMap = (DatabaseMap) dbs.get(i);
107
108 Class.forName(dbMap.getDbDriver());
109 Connection conn = DriverManager.getConnection(dbMap.getDbServer(),
110 dbMap.getDbLogin(),dbMap.getDbPassword());
111 Object params[] = {dbMap.getName(),
112 dbMap.getDbServer(),
113 dbMap.getDbLogin(),
114 dbMap.getDbPassword()
115 };
116 conn.setAutoCommit(false);
117
118 logger.log(Level.FINEST,Messages.format(
119 "ConnectionManager.connection",params));
120
121 addConnection(dbMap.getName(),new ConnectionSource(conn,
122 dbMap.getVendor()));
123
124 logger.log(Level.FINEST,Messages.format(
125 "Connection.connectionAdded",dbMap.getName()));
126 }
127 }
128 catch(ClassNotFoundException e){
129 logger.throwing(CLASS_NAME,"createConnection()",
130 new MappingException("class.noClassDefFound",dbMap.getDbDriver())
131 );
132 throw new MappingException("class.noClassDefFound",dbMap.getDbDriver());
133 }
134 catch(SQLException e){
135 logger.throwing(CLASS_NAME,"createConnection()",
136 new MappingException("mapping.connNotFound",dbMap.getDbServer())
137 );
138 throw new MappingException("mapping.connNotFound",dbMap.getDbServer());
139 }
140 }
141 /**
142 * Closes the connection specified in name.
143 *
144 *
145 * @param name - name of connection to close
146 * @throws MappingException if error occurred
147 */
148 protected void closeConnection(String name) throws MappingException,SQLException{
149
150 logger.log(Level.INFO,Messages.format("ConnectionManager.closeConn",name));
151 ConnectionSource conn = (ConnectionSource)conns.get(name);
152
153 if(conn == null){
154
155 logger.throwing(CLASS_NAME,"closeConnection()",
156 new MappingException("mapping.connNotFound",name)
157 );
158 throw new MappingException("mapping.connNotFound",name);
159 }
160 else
161 conn.closeConnection();
162 }
163
164 /**
165 * Closes all the connections
166 *
167 * @throws MappingException if error occurred
168 */
169 protected void closeAllConnections() throws MappingException,SQLException{
170
171 Iterator iter = conns.keySet().iterator();
172 while(iter.hasNext()){
173 String nameConn = (String)iter.next();
174 closeConnection(nameConn);
175 }
176 }
177
178 /**
179 * Destroy object.
180 *
181 * This method releases all database connection
182 *
183 * @throws Exception
184 */
185 public synchronized void destroy()throws Exception{
186 closeAllConnections();
187 }
188 /**
189 * Finalize object
190 *
191 * @throws Throwable
192 */
193 protected void finalize() throws Throwable{
194 super.finalize();
195 }
196
197 /**
198 * Returns the connection specified in nameConn
199 *
200 * @param nameConn name of connection
201 * @return java.sql.Connection object
202 * @throws MappingExpcetion if connection not exits
203 */
204 public Connection getConnection(String nameConn) throws MappingException{
205
206 logger.log(Level.INFO,Messages.format("ConnectionManager.getConn",nameConn));
207 try{
208 ConnectionSource conn = getConnectionSource(nameConn);
209 return conn.getConnection();
210 }
211 catch(SQLException e){
212 logger.throwing(CLASS_NAME,"createConnection()",
213 new MappingException(e)
214 );
215 throw new MappingException(e);
216
217 }
218 }
219
220 /**
221 * Return the ConnectionSource object specified in nameConn
222 *
223 * @param nameConn name of connection
224 * @return ConnectionSource
225 * @throws MappingExcpetion if connection not exits
226 *
227 */
228 public ConnectionSource getConnectionSource(String nameConn)
229 throws MappingException{
230
231 ConnectionSource conn = (ConnectionSource)conns.get(nameConn);
232 if(conn == null){
233 logger.throwing(CLASS_NAME,"createConnection()",
234 new MappingException("mapping.connNotFound",nameConn)
235 );
236 throw new MappingException("mapping.connNotFound",nameConn);
237 }
238 else
239 return conn;
240 }
241
242 /**
243 * Releases connection specified in conn
244 *
245 * @param name
246 * @throws MappingException if error occured
247 */
248 public void releaseConnection(String name)
249 throws MappingException{
250
251 logger.log(Level.INFO,Messages.format("ConnectionManager.releaseConn",name));
252 ConnectionSource conn = (ConnectionSource)conns.get(name);
253 if(conn == null){
254 logger.throwing(CLASS_NAME,"createConnection()",
255 new MappingException("mapping.connNotFound",name)
256 );
257 throw new MappingException("mapping.connNotFound",name);
258 }
259 else
260 conn.releaseConnection();
261 }
262
263 /**
264 * Loads and creates database configurations from maps.
265 *
266 * This method read form configuration file the information
267 * about databases from root specified in conf.
268 *
269 * @param conf holds configuration for loading the database maps
270 * @exception MappingException if an error occurs
271 */
272 public void setConfiguration(Configuration conf)
273 throws MappingException{
274
275 Iterator iter = conf.getConfigurations("database");
276 ArrayList dbs = new ArrayList();
277 while(iter.hasNext()){
278 conf = (ConfigurationImpl)iter.next();
279 DatabaseMap dbMap = new DatabaseMap();
280 ConfigurationImpl child = (ConfigurationImpl)conf.getConfiguration("name");
281 logger.log(Level.FINEST,"name " + child.getValue());
282 dbMap.setName(child.getValue());
283 child = (ConfigurationImpl)conf.getConfiguration("vendor");
284 logger.log(Level.FINEST,"vendor " + child.getValue());
285 dbMap.setVendor(child.getValue());
286 child = (ConfigurationImpl)conf.getConfiguration("dbDriver");
287 logger.log(Level.FINEST,"dbDriver " + child.getValue());
288 String dbDriver = child.getValue();
289 if(dbDriver == null){
290 logger.throwing(CLASS_NAME,"createConnection()",
291 new MappingException("mapping.missingDriver")
292 );
293 throw new MappingException(Messages.message("mapping.missingDriver"));
294 }
295 else
296 dbMap.setDbDriver(dbDriver);
297
298 child = (ConfigurationImpl)conf.getConfiguration("dbServer");
299 logger.log(Level.FINEST,"dbServer " + child.getValue());
300 dbMap.setDbServer(child.getValue());
301 child = (ConfigurationImpl)conf.getConfiguration("dbLogin");
302 logger.log(Level.FINEST,"dbLogin " + child.getValue());
303 dbMap.setDbLogin(child.getValue());
304 child = (ConfigurationImpl)conf.getConfiguration("dbPassword");
305 logger.log(Level.FINEST,"dbPassword " + child.getValue());
306 dbMap.setDbPassword(child.getValue());
307
308 dbs.add(dbMap);
309 }
310 createConnection(dbs);
311 }
312 }
313 //-------------------------------------------------------------------
314 /*
315 $Log: ConnectionManager.java,v $
316 Revision 1.10 2004/05/20 22:42:47 gmartone
317 Changed for task 99073 (Coverage Javadocs)
318 */
|