DerbyControlpublic final class DerbyControl extends Object This class uses Java reflection to invoke Derby
NetworkServerControl class.
This class is used to start/stop/ping derby database.
The reason for creating this class instead of directly
invoking NetworkServerControl from the StartDatabaseCommand
class is so that a separate JVM is launched when starting the
database and the control is return to CLI. |
Fields Summary |
---|
public static final String | DB_LOG_FILENAME | private final String | derbyCommand | private final String | derbyHost | private final String | derbyPort | private final String | derbyHome | private final boolean | redirect |
Constructors Summary |
---|
public DerbyControl(String dc, String dht, String dp, String redirect, String dhe)
//constructor
this.derbyCommand = dc;
this.derbyHost = dht;
this.derbyPort = dp;
this.derbyHome = dhe;
this.redirect = Boolean.valueOf(redirect).booleanValue();
if (this.redirect) {
try {
String dbLog = "";
if (this.derbyHome == null) {
// if derbyHome is null then redirect the output to a temporary file
// which gets deleted after the jvm exists.
dbLog = createTempLogFile();
}
else {
dbLog = createDBLog(this.derbyHome);
}
//redirect stdout and stderr to a file
PrintStream printStream = new PrintStream(new FileOutputStream(dbLog, true), true);
System.setOut(printStream);
System.setErr(printStream);
}
catch (Throwable t) {
t.printStackTrace();
//exit with an error code of 2
System.exit(2);
}
}
//do not set derby.system.home if derbyHome is empty
if (this.derbyHome!=null && this.derbyHome.length()>0) {
System.setProperty("derby.system.home", this.derbyHome);
}
//set the property to not overwrite log file
System.setProperty("derby.infolog.append", "true");
| public DerbyControl(String dc, String dht, String dp)
this(dc,dht,dp,"true", null);
| public DerbyControl(String dc, String dht, String dp, String redirect)
this(dc,dht,dp,redirect,null);
|
Methods Summary |
---|
private java.lang.String | createDBLog(java.lang.String dbHome)create a db.log file that stdout/stderr will redirect to.
dbhome is the derby.system.home directory where derb.log
gets created. if user specified --dbhome options, derby.log
will be created there.
//dbHome must exist and have write permission
final File fDBHome = new File(dbHome);
String dbLogFileName = "";
final StringManager lsm = StringManager.getManager(DerbyControl.class);
if (fDBHome.isDirectory() && fDBHome.canWrite()) {
final File fDBLog = new File(dbHome, DB_LOG_FILENAME);
dbLogFileName = fDBLog.toString();
//if the file exists, check if it is writeable
if (fDBLog.exists() && !fDBLog.canWrite()) {
System.out.println(lsm.getString("UnableToAccessDatabaseLog", dbLogFileName));
System.out.println(lsm.getString("ContinueStartingDatabase"));
//if exist but not able to write then create a temporary
//log file and persist on starting the database
dbLogFileName = createTempLogFile();
}
else if (!fDBLog.exists()) {
//create log file
fDBLog.createNewFile();
}
}
else {
System.out.println(lsm.getString("InvalidDirectory", dbHome));
System.out.println(lsm.getString("ContinueStartingDatabase"));
//if directory does not exist then create a temporary log file
//and persist on starting the database
dbLogFileName = createTempLogFile();
}
return dbLogFileName;
| private java.lang.String | createTempLogFile()creates a temporary log file.
String tempFileName = "";
try {
final File fTemp = File.createTempFile("foo", null);
fTemp.deleteOnExit();
tempFileName = fTemp.toString();
}
catch (IOException ioe) {
final StringManager lsm = StringManager.getManager(DerbyControl.class);
throw new CommandException(lsm.getString("UnableToAccessDatabaseLog", tempFileName));
}
return tempFileName;
| private void | invokeNetworkServerControl()This methos invokes the Derby's NetworkServerControl to start/stop/ping
the database.
try {
Class networkServer = Class.forName("org.apache.derby.drda.NetworkServerControl");
Method networkServerMethod = networkServer.getDeclaredMethod("main",
new Class[]{String[].class});
Object [] paramObj = new Object[]{new String[]{derbyCommand, "-h", derbyHost, "-p", derbyPort}};
networkServerMethod.invoke(networkServer, paramObj);
}
catch (Throwable t) {
t.printStackTrace();
System.exit(2);
}
| public static void | main(java.lang.String[] args)
if (args.length<3){
System.out.println("paramters not specified.");
System.out.println("DerbyControl <derby command> <derby host> <derby port> <derby home> <redirect output>");
System.exit(1);
}
DerbyControl derbyControl = null;
if (args.length == 3)
derbyControl = new DerbyControl(args[0], args[1], args[2]);
else if (args.length == 4 )
derbyControl = new DerbyControl(args[0], args[1], args[2], args[3]);
else if (args.length > 4)
derbyControl = new DerbyControl(args[0], args[1], args[2], args[3], args[4]);
if (derbyControl != null)
derbyControl.invokeNetworkServerControl();
|
|