KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > crontab > MMBaseQueryJob


1 /*
2  This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7  */

8 package org.mmbase.applications.crontab;
9
10 import java.sql.*;
11 import javax.sql.DataSource JavaDoc;
12 import org.mmbase.storage.implementation.database.GenericDataSource;
13 import org.mmbase.module.core.MMBase;
14 import org.mmbase.util.logging.*;
15
16 /**
17  * If periodicly some SQL query must be executed (e.g. database administration tasks), then the
18  * following Job can be used.
19  *
20  * @author Michiel Meeuwissen
21  * @version $Id: MMBaseQueryJob.java,v 1.1 2005/12/27 09:29:13 michiel Exp $
22  */

23
24 public class MMBaseQueryJob extends AbstractCronJob {
25     private static final Logger log = Logging.getLoggerInstance(MMBaseQueryJob.class);
26
27     private String JavaDoc sql;
28     protected void init() {
29         // determin what needs to be done in run().
30
sql = cronEntry.getConfiguration();
31
32     }
33
34     public final void run() {
35         try {
36             MMBase mmbase = MMBase.getMMBase();
37
38             // why is there no 'getDataSource' method?
39
DataSource JavaDoc dataSource = ((org.mmbase.storage.implementation.database.DatabaseStorageManagerFactory) mmbase.getStorageManagerFactory()).getDataSource();
40             // some hackery
41
Connection connection;
42             if (dataSource instanceof GenericDataSource) {
43                 // we don't want a pooled query, because they are killed.
44
connection = ((GenericDataSource) dataSource).getDirectConnection();
45             } else {
46                 connection = dataSource.getConnection();
47             }
48             long start = System.currentTimeMillis();
49             Statement statement = connection.createStatement();
50             ResultSet results = statement.executeQuery(sql);
51             log.service("Executed " + sql + " in " + (System.currentTimeMillis() - start) + " ms");
52             ResultSetMetaData meta;
53
54             try {
55                 meta = results.getMetaData();
56                 StringBuffer JavaDoc heading = new StringBuffer JavaDoc();
57                 for (int i = 1; i <= meta.getColumnCount(); i++) {
58                     heading.append(results.getMetaData().getColumnName(i));
59                     if (i < meta.getColumnCount()) heading.append("|");
60                 }
61                 log.service(heading.toString());
62                 while(results.next()) {
63                     StringBuffer JavaDoc row = new StringBuffer JavaDoc();
64                     for (int i = 1; i <= meta.getColumnCount(); i++) {
65                         heading.append(results.getString(i));
66                         if (i < meta.getColumnCount()) heading.append("|");
67                     }
68                     log.service(row);
69                 }
70             } catch (Exception JavaDoc e) {
71                 // perhaps the result set simply is not available for use any more (this class is
72
// mainly used for administrative tasks). That is no error.
73
log.service(e.getMessage() + ": Cannot show results of '" + sql + "'");
74             }
75             if (results != null) results.close();
76             if (statement != null) statement.close();
77             if (connection != null) connection.close();
78         } catch (Exception JavaDoc e) {
79             log.error(e.getMessage(), e);
80         }
81     }
82 }
83
Popular Tags