1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.sql.Connection;
|
---|
21 | import java.sql.DatabaseMetaData;
|
---|
22 | import java.sql.Driver;
|
---|
23 | import java.sql.SQLException;
|
---|
24 | import java.util.Hashtable;
|
---|
25 | import java.util.Properties;
|
---|
26 | import org.apache.tools.ant.AntClassLoader;
|
---|
27 | import org.apache.tools.ant.BuildException;
|
---|
28 | import org.apache.tools.ant.Project;
|
---|
29 | import org.apache.tools.ant.Task;
|
---|
30 | import org.apache.tools.ant.types.Path;
|
---|
31 | import org.apache.tools.ant.types.Reference;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Handles JDBC configuration needed by SQL type tasks.
|
---|
35 | * <p>
|
---|
36 | * The following example class prints the contents of the first column of each row in TableName.
|
---|
37 | *</p>
|
---|
38 | *<code><pre>
|
---|
39 | package examples;
|
---|
40 | import java.sql.Connection;
|
---|
41 | import java.sql.ResultSet;
|
---|
42 | import java.sql.SQLException;
|
---|
43 | import java.sql.Statement;
|
---|
44 |
|
---|
45 | import org.apache.tools.ant.BuildException;
|
---|
46 | import org.apache.tools.ant.taskdefs.JDBCTask;
|
---|
47 |
|
---|
48 | public class SQLExampleTask extends JDBCTask {
|
---|
49 |
|
---|
50 | private String tableName;
|
---|
51 |
|
---|
52 | public void execute() throws BuildException {
|
---|
53 | Connection conn = getConnection();
|
---|
54 | Statement stmt=null;
|
---|
55 | try {
|
---|
56 | if (tableName == null) {
|
---|
57 | throw new BuildException("TableName must be specified",location);
|
---|
58 | }
|
---|
59 | String sql = "SELECT * FROM "+tableName;
|
---|
60 | stmt= conn.createStatement();
|
---|
61 | ResultSet rs = stmt.executeQuery(sql);
|
---|
62 | while (rs.next()) {
|
---|
63 | log(rs.getObject(1).toString());
|
---|
64 | }
|
---|
65 | } catch (SQLException e) {
|
---|
66 |
|
---|
67 | } finally {
|
---|
68 | if (stmt != null) {
|
---|
69 | try {stmt.close();}catch (SQLException ingore) {}
|
---|
70 | }
|
---|
71 | if (conn != null) {
|
---|
72 | try {conn.close();}catch (SQLException ingore) {}
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | public void setTableName(String tableName) {
|
---|
77 | this.tableName = tableName;
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | </pre></code>
|
---|
84 |
|
---|
85 |
|
---|
86 | * @since Ant 1.5
|
---|
87 | *
|
---|
88 | */
|
---|
89 |
|
---|
90 | public abstract class JDBCTask extends Task {
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Used for caching loaders / driver. This is to avoid
|
---|
95 | * getting an OutOfMemoryError when calling this task
|
---|
96 | * multiple times in a row.
|
---|
97 | */
|
---|
98 | private static Hashtable loaderMap = new Hashtable(3);
|
---|
99 |
|
---|
100 | private boolean caching = true;
|
---|
101 |
|
---|
102 | private Path classpath;
|
---|
103 |
|
---|
104 | private AntClassLoader loader;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Autocommit flag. Default value is false
|
---|
108 | */
|
---|
109 | private boolean autocommit = false;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * DB driver.
|
---|
113 | */
|
---|
114 | private String driver = null;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * DB url.
|
---|
118 | */
|
---|
119 | private String url = null;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * User name.
|
---|
123 | */
|
---|
124 | private String userId = null;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Password
|
---|
128 | */
|
---|
129 | private String password = null;
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * RDBMS Product needed for this SQL.
|
---|
133 | **/
|
---|
134 | private String rdbms = null;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * RDBMS Version needed for this SQL.
|
---|
138 | **/
|
---|
139 | private String version = null;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Sets the classpath for loading the driver.
|
---|
143 | * @param classpath The classpath to set
|
---|
144 | */
|
---|
145 | public void setClasspath(Path classpath) {
|
---|
146 | this.classpath = classpath;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Caching loaders / driver. This is to avoid
|
---|
151 | * getting an OutOfMemoryError when calling this task
|
---|
152 | * multiple times in a row; default: true
|
---|
153 | * @param enable
|
---|
154 | */
|
---|
155 | public void setCaching(boolean enable) {
|
---|
156 | caching = enable;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Add a path to the classpath for loading the driver.
|
---|
161 | */
|
---|
162 | public Path createClasspath() {
|
---|
163 | if (this.classpath == null) {
|
---|
164 | this.classpath = new Path(getProject());
|
---|
165 | }
|
---|
166 | return this.classpath.createPath();
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Set the classpath for loading the driver
|
---|
171 | * using the classpath reference.
|
---|
172 | */
|
---|
173 | public void setClasspathRef(Reference r) {
|
---|
174 | createClasspath().setRefid(r);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Class name of the JDBC driver; required.
|
---|
179 | * @param driver The driver to set
|
---|
180 | */
|
---|
181 | public void setDriver(String driver) {
|
---|
182 | this.driver = driver;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Sets the database connection URL; required.
|
---|
187 | * @param url The url to set
|
---|
188 | */
|
---|
189 | public void setUrl(String url) {
|
---|
190 | this.url = url;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Sets the password; required.
|
---|
195 | * @param password The password to set
|
---|
196 | */
|
---|
197 | public void setPassword(String password) {
|
---|
198 | this.password = password;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Auto commit flag for database connection;
|
---|
203 | * optional, default false.
|
---|
204 | * @param autocommit The autocommit to set
|
---|
205 | */
|
---|
206 | public void setAutocommit(boolean autocommit) {
|
---|
207 | this.autocommit = autocommit;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Execute task only if the lower case product name
|
---|
212 | * of the DB matches this
|
---|
213 | * @param rdbms The rdbms to set
|
---|
214 | */
|
---|
215 | public void setRdbms(String rdbms) {
|
---|
216 | this.rdbms = rdbms;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Sets the version string, execute task only if
|
---|
221 | * rdbms version match; optional.
|
---|
222 | * @param version The version to set
|
---|
223 | */
|
---|
224 | public void setVersion(String version) {
|
---|
225 | this.version = version;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Verify we are connected to the correct RDBMS
|
---|
230 | */
|
---|
231 | protected boolean isValidRdbms(Connection conn) {
|
---|
232 | if (rdbms == null && version == null) {
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | try {
|
---|
237 | DatabaseMetaData dmd = conn.getMetaData();
|
---|
238 |
|
---|
239 | if (rdbms != null) {
|
---|
240 | String theVendor = dmd.getDatabaseProductName().toLowerCase();
|
---|
241 |
|
---|
242 | log("RDBMS = " + theVendor, Project.MSG_VERBOSE);
|
---|
243 | if (theVendor == null || theVendor.indexOf(rdbms) < 0) {
|
---|
244 | log("Not the required RDBMS: " + rdbms, Project.MSG_VERBOSE);
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (version != null) {
|
---|
250 | // XXX maybe better toLowerCase(Locale.US)
|
---|
251 | String theVersion = dmd.getDatabaseProductVersion().toLowerCase();
|
---|
252 |
|
---|
253 | log("Version = " + theVersion, Project.MSG_VERBOSE);
|
---|
254 | if (theVersion == null
|
---|
255 | || !(theVersion.startsWith(version)
|
---|
256 | || theVersion.indexOf(" " + version) >= 0)) {
|
---|
257 | log("Not the required version: \"" + version + "\"", Project.MSG_VERBOSE);
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | } catch (SQLException e) {
|
---|
262 | // Could not get the required information
|
---|
263 | log("Failed to obtain required RDBMS information", Project.MSG_ERR);
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 |
|
---|
267 | return true;
|
---|
268 | }
|
---|
269 |
|
---|
270 | protected static Hashtable getLoaderMap() {
|
---|
271 | return loaderMap;
|
---|
272 | }
|
---|
273 |
|
---|
274 | protected AntClassLoader getLoader() {
|
---|
275 | return loader;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Creates a new Connection as using the driver, url, userid and password
|
---|
280 | * specified.
|
---|
281 | *
|
---|
282 | * The calling method is responsible for closing the connection.
|
---|
283 | *
|
---|
284 | * @return Connection the newly created connection.
|
---|
285 | * @throws BuildException if the UserId/Password/Url is not set or there
|
---|
286 | * is no suitable driver or the driver fails to load.
|
---|
287 | */
|
---|
288 | protected Connection getConnection() throws BuildException {
|
---|
289 | if (userId == null) {
|
---|
290 | throw new BuildException("User Id attribute must be set!", getLocation());
|
---|
291 | }
|
---|
292 | if (password == null) {
|
---|
293 | throw new BuildException("Password attribute must be set!", getLocation());
|
---|
294 | }
|
---|
295 | if (url == null) {
|
---|
296 | throw new BuildException("Url attribute must be set!", getLocation());
|
---|
297 | }
|
---|
298 | try {
|
---|
299 |
|
---|
300 | log("connecting to " + getUrl(), Project.MSG_VERBOSE);
|
---|
301 | Properties info = new Properties();
|
---|
302 | info.put("user", getUserId());
|
---|
303 | info.put("password", getPassword());
|
---|
304 | Connection conn = getDriver().connect(getUrl(), info);
|
---|
305 |
|
---|
306 | if (conn == null) {
|
---|
307 | // Driver doesn't understand the URL
|
---|
308 | throw new SQLException("No suitable Driver for " + url);
|
---|
309 | }
|
---|
310 |
|
---|
311 | conn.setAutoCommit(autocommit);
|
---|
312 | return conn;
|
---|
313 | } catch (SQLException e) {
|
---|
314 | throw new BuildException(e, getLocation());
|
---|
315 | }
|
---|
316 |
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Gets an instance of the required driver.
|
---|
321 | * Uses the ant class loader and the optionally the provided classpath.
|
---|
322 | * @return Driver
|
---|
323 | * @throws BuildException
|
---|
324 | */
|
---|
325 | private Driver getDriver() throws BuildException {
|
---|
326 | if (driver == null) {
|
---|
327 | throw new BuildException("Driver attribute must be set!", getLocation());
|
---|
328 | }
|
---|
329 |
|
---|
330 | Driver driverInstance = null;
|
---|
331 | try {
|
---|
332 | Class dc;
|
---|
333 | if (classpath != null) {
|
---|
334 | // check first that it is not already loaded otherwise
|
---|
335 | // consecutive runs seems to end into an OutOfMemoryError
|
---|
336 | // or it fails when there is a native library to load
|
---|
337 | // several times.
|
---|
338 | // this is far from being perfect but should work
|
---|
339 | // in most cases.
|
---|
340 | synchronized (loaderMap) {
|
---|
341 | if (caching) {
|
---|
342 | loader = (AntClassLoader) loaderMap.get(driver);
|
---|
343 | }
|
---|
344 | if (loader == null) {
|
---|
345 | log("Loading " + driver
|
---|
346 | + " using AntClassLoader with classpath "
|
---|
347 | + classpath, Project.MSG_VERBOSE);
|
---|
348 | loader = getProject().createClassLoader(classpath);
|
---|
349 | if (caching) {
|
---|
350 | loaderMap.put(driver, loader);
|
---|
351 | }
|
---|
352 | } else {
|
---|
353 | log("Loading " + driver
|
---|
354 | + " using a cached AntClassLoader.",
|
---|
355 | Project.MSG_VERBOSE);
|
---|
356 | }
|
---|
357 | }
|
---|
358 | dc = loader.loadClass(driver);
|
---|
359 | } else {
|
---|
360 | log("Loading " + driver + " using system loader.",
|
---|
361 | Project.MSG_VERBOSE);
|
---|
362 | dc = Class.forName(driver);
|
---|
363 | }
|
---|
364 | driverInstance = (Driver) dc.newInstance();
|
---|
365 | } catch (ClassNotFoundException e) {
|
---|
366 | throw new BuildException(
|
---|
367 | "Class Not Found: JDBC driver " + driver + " could not be loaded",
|
---|
368 | getLocation());
|
---|
369 | } catch (IllegalAccessException e) {
|
---|
370 | throw new BuildException(
|
---|
371 | "Illegal Access: JDBC driver " + driver + " could not be loaded",
|
---|
372 | getLocation());
|
---|
373 | } catch (InstantiationException e) {
|
---|
374 | throw new BuildException(
|
---|
375 | "Instantiation Exception: JDBC driver " + driver + " could not be loaded",
|
---|
376 | getLocation());
|
---|
377 | }
|
---|
378 | return driverInstance;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | public void isCaching(boolean value) {
|
---|
383 | caching = value;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Gets the classpath.
|
---|
388 | * @return Returns a Path
|
---|
389 | */
|
---|
390 | public Path getClasspath() {
|
---|
391 | return classpath;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Gets the autocommit.
|
---|
396 | * @return Returns a boolean
|
---|
397 | */
|
---|
398 | public boolean isAutocommit() {
|
---|
399 | return autocommit;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Gets the url.
|
---|
404 | * @return Returns a String
|
---|
405 | */
|
---|
406 | public String getUrl() {
|
---|
407 | return url;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Gets the userId.
|
---|
412 | * @return Returns a String
|
---|
413 | */
|
---|
414 | public String getUserId() {
|
---|
415 | return userId;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Set the user name for the connection; required.
|
---|
420 | * @param userId The userId to set
|
---|
421 | */
|
---|
422 | public void setUserid(String userId) {
|
---|
423 | this.userId = userId;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Gets the password.
|
---|
428 | * @return Returns a String
|
---|
429 | */
|
---|
430 | public String getPassword() {
|
---|
431 | return password;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Gets the rdbms.
|
---|
436 | * @return Returns a String
|
---|
437 | */
|
---|
438 | public String getRdbms() {
|
---|
439 | return rdbms;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Gets the version.
|
---|
444 | * @return Returns a String
|
---|
445 | */
|
---|
446 | public String getVersion() {
|
---|
447 | return version;
|
---|
448 | }
|
---|
449 |
|
---|
450 | }
|
---|