00001
00025
package org.objectweb.cjdbc.common.util;
00026
00027
import java.io.File;
00028
import java.io.FilenameFilter;
00029
import java.sql.Connection;
00030
import java.sql.DriverManager;
00031
import java.sql.Statement;
00032
import java.util.ArrayList;
00033
import java.util.Arrays;
00034
00035
import org.apache.tools.ant.BuildException;
00036
import org.apache.tools.ant.Task;
00037
00045 public class ControllerTask extends Task
00046 {
00047 private String
configuration;
00048 private String
action;
00049 private String
controllerFile;
00050 private boolean manageHsqldb =
true;
00051 private boolean verbose =
false;
00052 private static final String
SINGLE_DB =
"SingleDB";
00053 private static final String
RAIDB0 =
"Raidb0";
00054 private static final String
RAIDB1 =
"Raidb1";
00055 private static final String
RAIDB2 =
"Raidb2";
00056 private static final String
DIRECT =
"Direct";
00057
00058 private static final String
HSQLDB_PORT_STANDALONE =
"9000";
00059 private static final String
HSQLDB_PORT_DUAL_1 =
"9001";
00060 private static final String
HSQLDB_PORT_DUAL_2 =
"9002";
00061
00062 private static final String[]
FILES_TO_CLEAN =
new String[]{
00063
"cjdbc.log",
"cjdbc.report",
"request.log",
"test1.script",
"test1.data",
00064
"test2.script",
"test2.data",
"test1.properties",
"test2.properties"};
00065 private static final String[]
CONFIGS =
new String[]{
00066
SINGLE_DB,
RAIDB0,
RAIDB1,
RAIDB2,
DIRECT };
00067
00075 public void execute() throws BuildException
00076 {
00077 System.out.println(
"Manage hsql is set to " +
manageHsqldb);
00078
if (
action.equalsIgnoreCase(
"start"))
00079
startAction();
00080
else
00081
stopAction();
00082 }
00083
00084 private void startAction() throws BuildException
00085 {
00086 System.out.println(
"Starting CJDBC Controller in mode:" +
configuration);
00087
if (
configuration == null)
00088
throw new BuildException(
00089
"Need a configuration file to start the controller");
00090
if (
configuration.equals(
DIRECT))
00091
startDirect();
00092
else if (
configuration.equals(
SINGLE_DB))
00093
startSingleDB();
00094
else if (
configuration.equals(
RAIDB0))
00095
startRaidb0();
00096
else if (
configuration.equals(
RAIDB1))
00097
startRaidb1();
00098
else if (
configuration.equals(
RAIDB2))
00099
startRaidb2();
00100
else
00101
throw new BuildException(
"Doing nothing ...");
00102 }
00103
00104 private void stopAction() throws BuildException
00105 {
00106 System.out.println(
"Stopping CJDBC Controller from mode:" +
configuration);
00107
if (
configuration.equals(
DIRECT))
00108
stopDirect();
00109
else if (
configuration.equals(
SINGLE_DB))
00110
stopSingleDB();
00111
else if (
configuration.equals(
RAIDB0))
00112
stopRaidb0();
00113
else if (
configuration.equals(
RAIDB1))
00114
stopRaidb1();
00115
else if (
configuration.equals(
RAIDB2))
00116
stopRaidb2();
00117
else
00118 {
00119
throw new BuildException(
"Doing nothing ...");
00120 }
00121
00122
if (
manageHsqldb)
00123 {
00124
cleanFiles();
00125 }
00126 }
00127
00128 private void cleanFiles()
00129 {
00130 File f =
new File(
".");
00131 String[] list = f.list(
new FilenameFilter()
00132 {
00133
public boolean accept(File dir, String name)
00134 {
00135
for (
int i = 0; i <
FILES_TO_CLEAN.length; i++)
00136 {
00137 if (name.equals(
FILES_TO_CLEAN[i]))
00138
return true;
00139 }
00140
return false;
00141 }
00142 });
00143
for (
int i = 0; i < list.length; i++)
00144 {
00145
if (verbose)
00146 System.out.println(
"Cleaning:" + list[i]);
00147
new File(list[i]).delete();
00148 }
00149 }
00150
00151 private void startController() throws BuildException
00152 {
00153 System.out.println(
"Starting cjdbc with file:" + controllerFile);
00154
try
00155 {
00156 Runtime run = Runtime.getRuntime();
00157 String path =
"loadtest/configfiles:3rdparty/hsqldb/lib/hsqldb.jar:lib/jakarta-regexp-1.3.jar:lib/xercesImpl.jar:lib/xmlutil.jar:lib/Octopus-2.2.jar:lib/OctopusGenerator.jar:lib/csvjdbc.jar:lib/c-jdbc-controller.jar:drivers/c-jdbc-driver.jar:3rdparty/hsqldb/lib/hsqldb.jar:drivers:xml:lib/crimson.jar:lib/jgroups-core.jar:lib/log4j.jar:lib/commons-cli.jar:lib/jmx/mx4j-tools.jar:lib/jmx/xsl:lib/jmx/xml-apis.jar:lib/jmx/mx4j-jmx.jar:lib/jmx/xalan.jar";
00158 run
00159 .exec(
"java -classpath build/classes:"
00160 + path
00161 +
" -Dorg.xml.sax.driver=org.apache.crimson.parser.XMLReaderImpl org.objectweb.cjdbc.controller.core.Controller -f "
00162 + controllerFile);
00163 }
00164
catch (Exception e)
00165 {
00166
throw new BuildException(e.getMessage());
00167 }
00168 }
00169
00170 private void stopController() throws BuildException
00171 {
00172
00173 }
00174
00175 private void startHsqldb(String port)
throws BuildException
00176 {
00177 startHsqldb(port,
"test");
00178 }
00179
00180 private void startHsqldb(String port, String db)
throws BuildException
00181 {
00182 System.out.println(
"Starting hsqldb on port:" + port);
00183
try
00184 {
00185 Runtime run = Runtime.getRuntime();
00186 run
00187 .exec(
"java -classpath 3rdparty/hsqldb/lib/hsqldb.jar org.hsqldb.Server -port "
00188 + port +
" -database " + db);
00189
synchronized (
this)
00190 {
00191 wait(3000);
00192 }
00193 }
00194
catch (Exception e)
00195 {
00196
throw new BuildException(e.getMessage());
00197 }
00198 }
00199
00200 private void stopHsqldb(String port)
00201 {
00202 System.out.println(
"Stopping hsqldb on port:" + port);
00203
try
00204 {
00205 Class.forName(
"org.hsqldb.jdbcDriver");
00206 Connection c = DriverManager.getConnection(
00207
"jdbc:hsqldb:hsql://localhost:" + port,
"TEST",
"");
00208
Statement s = c.createStatement();
00209 s.
execute(
"SHUTDOWN IMMEDIATELY;");
00210 }
00211
catch (Exception e)
00212 {
00213
00214 System.out.println(e.getMessage());
00215 }
00216 }
00217
00218 private void startDirect()
00219 {
00220 startHsqldb(HSQLDB_PORT_STANDALONE,
"test1");
00221 }
00222
00223 private void stopDirect()
00224 {
00225 stopHsqldb(HSQLDB_PORT_STANDALONE);
00226 }
00227
00228 private void startSingleDB()
00229 {
00230 startHsqldb(HSQLDB_PORT_STANDALONE);
00231 }
00232
00233 private void stopSingleDB()
00234 {
00235 stopHsqldb(HSQLDB_PORT_STANDALONE);
00236 }
00237
00238 private void startRaidb0()
00239 {
00240 startHsqldb(HSQLDB_PORT_DUAL_1);
00241 startHsqldb(HSQLDB_PORT_DUAL_2);
00242 }
00243
00244 private void stopRaidb0()
00245 {
00246 stopHsqldb(HSQLDB_PORT_DUAL_1);
00247 stopHsqldb(HSQLDB_PORT_DUAL_2);
00248 }
00249
00250 private void startRaidb1()
00251 {
00252
if (manageHsqldb)
00253 {
00254 startHsqldb(HSQLDB_PORT_DUAL_1,
"test1");
00255 startHsqldb(HSQLDB_PORT_DUAL_2,
"test2");
00256 }
00257 startController();
00258 }
00259
00260 private void stopRaidb1()
00261 {
00262 stopController();
00263
if (manageHsqldb)
00264 {
00265 stopHsqldb(HSQLDB_PORT_DUAL_1);
00266 stopHsqldb(HSQLDB_PORT_DUAL_2);
00267 }
00268 }
00269
00270 private void startRaidb2()
00271 {
00272 startHsqldb(HSQLDB_PORT_DUAL_1);
00273 startHsqldb(HSQLDB_PORT_DUAL_2);
00274 }
00275
00276 private void stopRaidb2()
00277 {
00278 stopHsqldb(HSQLDB_PORT_DUAL_1);
00279 stopHsqldb(HSQLDB_PORT_DUAL_2);
00280 }
00281
00290 public void setConfiguration(String config)
throws BuildException
00291 {
00292 ArrayList possible =
new ArrayList(Arrays.asList(CONFIGS));
00293
if (possible.contains(config) ==
false)
00294
throw new BuildException(
"Configuration " + config +
" not supported");
00295
else
00296
this.configuration = config;
00297 }
00298
00304 public void setAction(String action)
00305 {
00306
this.action = action;
00307 }
00308
00314 public void setControllerfile(String file)
00315 {
00316
this.controllerFile = file;
00317 }
00318
00324 public void setManageHsqldb(
boolean manage)
00325 {
00326
this.manageHsqldb = manage;
00327 }
00328 }