ConnectionEntry.java

biz/hammurapi/ant/ConnectionEntry.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 57:9
Java Inspector 089 Undocumented parameter driverClass 2 57:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 66:9
Java Inspector 089 Parameter password is not documented 2 66:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 75:9
Java Inspector 089 Parameter url is not documented 2 75:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 84:9
Java Inspector 089 Parameter user is not documented 2 84:9
Java Inspector 089 Undocumented method 2 88:9
Java Inspector 089 Method is not properly documented 2 116:9
Java Inspector 089 Javadoc contains tag for exception which method doesn't throw ClassNotFoundException 2 116:9
Java Inspector 089 Method return value is not properly documented 2 116:9
Java Inspector 089 Undocumented method 2 119:33
Java Inspector 089 Undocumented method 2 132:9
Java Inspector 089 Undocumented method 2 140:9
Java Inspector 089 Undocumented method 2 146:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 128:50

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.ant;
24
25import java.io.IOException;
26import java.sql.Connection;
27import java.sql.DriverManager;
28import java.sql.SQLException;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.Iterator;
32
33import org.apache.tools.ant.BuildException;
34
35import biz.hammurapi.config.Context;
36import biz.hammurapi.sql.ConnectionPerThreadDataSource;
37import biz.hammurapi.sql.SQLProcessor;
38
39
40/**
41 * Defines database connection.
42 * @ant.type name="Database connection" category="Common"
43 * @author Pavel Vlasov
44 * @version $Revision: 1.8 $
45 */
46public class ConnectionEntry {
47 private String driverClass;
48 private String url;
49 private String user;
50 private String password;
51 private Collection onConnects=new ArrayList();
52
53 /**
54 * Driver class.
55 * @ant.required
56 */
57 public void setDriverClass(String driverClass) {
58 this.driverClass = driverClass;
59 }
60
61 /**
62 * Database password.
63 * @ant.not-required
64 * @param password
65 */
66 public void setPassword(String password) {
67 this.password = password;
68 }
69
70 /**
71 * Connection url
72 * @ant.required
73 * @param url
74 */
75 public void setUrl(String url) {
76 this.url = url;
77 }
78
79 /**
80 * Database user
81 * @ant.not-required
82 * @param user
83 */
84 public void setUser(String user) {
85 this.user = user;
86 }
87
88 public SQLProcessor getProcessor(Context nameMap) {
89 SQLProcessor ret = new SQLProcessor(getDataSource(), nameMap);
90 processOnConnects(ret);
91 return ret;
92 }
93
94 /**
95 * @param ret
96 */
97 private void processOnConnects(SQLProcessor ret) {
98 Iterator it=onConnects.iterator();
99 while (it.hasNext()) {
100 try {
101 ((Script) it.next()).execute(ret);
102 } catch (IOException e) {
103 throw new BuildException(e);
104 } catch (SQLException e) {
105 throw new BuildException(e);
106 }
107 }
108 }
109
110 private Collection dataSources=new ArrayList();
111
112 /**
113 * @return
114 * @throws ClassNotFoundException
115 */
116 public ConnectionPerThreadDataSource getDataSource() {
117 try {
118 ConnectionPerThreadDataSource ds = new ConnectionPerThreadDataSource(driverClass, url, user, password, null) {
119 public Connection getConnection() throws SQLException {
120 Connection ret = super.getConnection();
121 processOnConnects(new SQLProcessor(ret, null));
122 return ret;
123 }
124 };
125 dataSources.add(ds);
126 return ds;
127 } catch (ClassNotFoundException e) {
128 throw new BuildException("Cannot load driver class", e);
129 }
130 }
131
132 public Connection getConnection() throws SQLException, ClassNotFoundException {
133 Class.forName(driverClass);
134 Connection ret = DriverManager.getConnection(url, user, password);
135 processOnConnects(new SQLProcessor(ret, null));
136 return ret;
137
138 }
139
140 public Script createOnConnect() {
141 Script ret = new Script();
142 onConnects.add(ret);
143 return ret;
144 }
145
146 public void shutdown() {
147 Iterator it=dataSources.iterator();
148 while (it.hasNext()) {
149 ((ConnectionPerThreadDataSource) it.next()).shutdown();
150 }
151 }
152}
153