001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.ant;
024:
025: import java.io.IOException;
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028: import java.sql.SQLException;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import org.apache.tools.ant.BuildException;
034:
035: import biz.hammurapi.config.Context;
036: import biz.hammurapi.sql.ConnectionPerThreadDataSource;
037: import biz.hammurapi.sql.SQLProcessor;
038:
039: /**
040: * Defines database connection.
041: * @ant.element name="Database connection"
042: * @author Pavel Vlasov
043: * @version $Revision: 1.8 $
044: */
045: public class ConnectionEntry {
046: private String driverClass;
047: private String url;
048: private String user;
049: private String password;
050: private Collection onConnects = new ArrayList();
051:
052: /**
053: * Driver class.
054: * @ant.required
055: */
056: public void setDriverClass(String driverClass) {
057: this .driverClass = driverClass;
058: }
059:
060: /**
061: * Database password.
062: * @ant.non-required
063: * @param password
064: */
065: public void setPassword(String password) {
066: this .password = password;
067: }
068:
069: /**
070: * Connection url
071: * @ant.required
072: * @param url
073: */
074: public void setUrl(String url) {
075: this .url = url;
076: }
077:
078: /**
079: * Database user
080: * @ant.non-required
081: * @param user
082: */
083: public void setUser(String user) {
084: this .user = user;
085: }
086:
087: public SQLProcessor getProcessor(Context nameMap) {
088: SQLProcessor ret = new SQLProcessor(getDataSource(), nameMap);
089: processOnConnects(ret);
090: return ret;
091: }
092:
093: /**
094: * @param ret
095: */
096: private void processOnConnects(SQLProcessor ret) {
097: Iterator it = onConnects.iterator();
098: while (it.hasNext()) {
099: try {
100: ((Script) it.next()).execute(ret);
101: } catch (IOException e) {
102: throw new BuildException(e);
103: } catch (SQLException e) {
104: throw new BuildException(e);
105: }
106: }
107: }
108:
109: private Collection dataSources = new ArrayList();
110:
111: /**
112: * @return
113: * @throws ClassNotFoundException
114: */
115: public ConnectionPerThreadDataSource getDataSource() {
116: try {
117: ConnectionPerThreadDataSource ds = new ConnectionPerThreadDataSource(
118: driverClass, url, user, password, null);
119: dataSources.add(ds);
120: return ds;
121: } catch (ClassNotFoundException e) {
122: throw new BuildException("Cannot load driver class", e);
123: }
124: }
125:
126: public Connection getConnection() throws SQLException,
127: ClassNotFoundException {
128: Class.forName(driverClass);
129: return DriverManager.getConnection(url, user, password);
130:
131: }
132:
133: public Script createOnConnect() {
134: Script ret = new Script();
135: onConnects.add(ret);
136: return ret;
137: }
138:
139: public void shutdown() {
140: Iterator it = dataSources.iterator();
141: while (it.hasNext()) {
142: ((ConnectionPerThreadDataSource) it.next()).shutdown();
143: }
144: }
145: }
|