KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > dbscript > Commander


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.dbscript;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.sql.Connection JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.Statement JavaDoc;
14
15 /**
16  * A thread that can run a single command many times.
17  *
18  * @version $Revision: 1.7 $, $Date: 2006/01/18 14:40:05 $
19  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
20  * @author $Author: billhorsman $ (current maintainer)
21  * @since Proxool 0.5
22  */

23 class Commander implements Runnable JavaDoc {
24
25     private static final Log LOG = LogFactory.getLog(Commander.class);
26
27     private ConnectionAdapterIF adapter;
28
29     private CommandIF command;
30
31     private CommandFilterIF commandFilter;
32
33     private boolean finished = false;
34
35     public Commander(ConnectionAdapterIF adapter, CommandIF commandIF, CommandFilterIF commandFilter) {
36         this.adapter = adapter;
37         this.command = commandIF;
38         this.commandFilter = commandFilter;
39     }
40
41     public void run() {
42
43         try {
44             boolean keepGoing = true;
45             for (int i = 0; i < command.getLoops(); i++) {
46
47                 Connection JavaDoc connection = null;
48
49                 try {
50                     connection = adapter.getConnection();
51                     boolean executeCommand = true;
52                     if (commandFilter != null) {
53                             executeCommand = commandFilter.beforeCommand(connection, command);
54                     }
55
56                     if (executeCommand) {
57                         execute(connection, command.getSql());
58                     }
59
60                     if (commandFilter != null) {
61                             commandFilter.afterCommand(connection, command);
62                     }
63
64                 } catch (SQLException JavaDoc e) {
65                     if (commandFilter != null && !commandFilter.catchException(command, e)) {
66                         keepGoing = false;
67                     }
68                     if (command.isIgnoreException()) {
69                         // Silent
70
} else if (command.isLogException()) {
71                         LOG.debug("Ignoring exception in " + command.getName(), e);
72                     } else {
73                         LOG.error("Stopping command " + command.getName(), e);
74                         keepGoing = false;
75                     }
76                 } finally {
77                     try {
78                         adapter.closeConnection(connection);
79                     } catch (SQLException JavaDoc e) {
80                         LOG.error("Closing connection for " + Thread.currentThread().getName(), e);
81                     }
82                 }
83
84                 if (!keepGoing) {
85                     break;
86                 }
87
88                 // Give other threads a chance. (Hoping this will increase the chances
89
// of some sort of conflict)
90
Thread.yield();
91             }
92         } finally {
93             finished = true;
94         }
95
96     }
97
98     /**
99      * Is the thread running
100      * @return true if it's finished, else false
101      */

102     protected boolean isFinished() {
103         return finished;
104     }
105
106     /**
107      * Execute and SQL statement
108      * @param connection used to execute statement
109      * @param sql the SQL to perform
110      * @throws java.sql.SQLException if anything goes wrong
111      */

112     private static final void execute(Connection JavaDoc connection, String JavaDoc sql) throws SQLException JavaDoc {
113         Statement JavaDoc statement = null;
114         try {
115             statement = connection.createStatement();
116             statement.execute(sql);
117         } finally {
118             if (statement != null) {
119                 try {
120                     if (statement != null) {
121                         statement.close();
122                     }
123                 } catch (SQLException JavaDoc e) {
124                     LOG.error("Couldn't close statement", e);
125                 }
126             }
127         }
128     }
129
130 }
131
132 /*
133  Revision history:
134  $Log: Commander.java,v $
135  Revision 1.7 2006/01/18 14:40:05 billhorsman
136  Unbundled Jakarta's Commons Logging.
137
138  Revision 1.6 2003/03/03 11:12:03 billhorsman
139  fixed licence
140
141  Revision 1.5 2003/02/19 15:14:20 billhorsman
142  fixed copyright (copy and paste error,
143  not copyright change)
144
145  Revision 1.4 2003/02/06 17:41:02 billhorsman
146  now uses imported logging
147
148  Revision 1.3 2003/01/15 00:08:15 billhorsman
149  check for commandFilter existence to avoid unnecessary
150  log clutter
151
152  Revision 1.2 2002/11/09 15:59:18 billhorsman
153  fix doc
154
155  Revision 1.1 2002/11/09 14:45:07 billhorsman
156  now threaded and better exception handling
157
158 */

159
Popular Tags