File size: 8,858 Bytes
2795186 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
/*
Copyright 2002-2004 MySQL AB, 2008 Sun Microsystems
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc.util;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import com.mysql.jdbc.StringUtils;
/**
* Controls a MySQL server using Java RunTime methods
*
* @version $Id: ServerController.java,v 1.1.2.1 2005/05/13 18:58:39 mmatthews
* Exp $
* @author Mark Matthews
*/
public class ServerController {
/**
* Where is the server installed?
*/
public static final String BASEDIR_KEY = "basedir";
/**
* Where are the databases installed?
*/
public static final String DATADIR_KEY = "datadir";
/**
* Where is the config file located?
*/
public static final String DEFAULTS_FILE_KEY = "defaults-file";
/**
* What is the name of the executable to run?
*/
public static final String EXECUTABLE_NAME_KEY = "executable";
/**
* What is the path to the mysql server executable (if not standard?)
*/
public static final String EXECUTABLE_PATH_KEY = "executablePath";
/**
* The default executable to run
*/
/**
* The process representing the MySQL server
*/
private Process serverProcess = null;
/**
* The list of properties for this server
*/
private Properties serverProps = null;
/**
* The system properties
*/
private Properties systemProps = null;
/**
* Creates a ServerController with the directory for the MySQL server.
*
* The 'datadir' is set to the same directory.
*
* @param baseDir
* the base directory for the MySQL server.
*/
public ServerController(String baseDir) {
setBaseDir(baseDir);
}
/**
* Creates a server controller for the MySQL server with the given basedir
* and datadir.
*
* @param basedir
* the basedir to use when starting MySQL.
* @param datadir
* the datadir to use when starting MySQL.
*/
public ServerController(String basedir, String datadir) {
}
/**
* Sets the basedir to use when starting MySQL.
*
* @param baseDir
* the basedir to use when starting MySQL.
*/
public void setBaseDir(String baseDir) {
getServerProps().setProperty(BASEDIR_KEY, baseDir);
}
/**
* Sets the data to use when starting MySQL.
*
* @param dataDir
* the basedir to use when starting MySQL.
*/
public void setDataDir(String dataDir) {
getServerProps().setProperty(DATADIR_KEY, dataDir);
}
/**
* Starts the server, returning a java.lang.Process instance that represents
* the mysql server.
*
* @return Process a java.lang.Process instance representing the mysql
* server process.
* @throws IOException
* if an error occurs while starting the mysql server.
*/
public Process start() throws IOException {
if (this.serverProcess != null) {
throw new IllegalArgumentException("Server already started");
} else {
this.serverProcess = Runtime.getRuntime().exec(getCommandLine());
return this.serverProcess;
}
}
/**
* Stops the server (if started)
*
* @param forceIfNecessary
* use forceStop if mysqladmin doesn't shut the server down
*
* @throws IOException
* if an error occurs while stopping the server
*/
public void stop(boolean forceIfNecessary) throws IOException {
if (this.serverProcess != null) {
String basedir = getServerProps().getProperty(BASEDIR_KEY);
StringBuffer pathBuf = new StringBuffer(basedir);
if (!basedir.endsWith(File.separator)) {
pathBuf.append(File.separator);
}
String defaultsFilePath = getServerProps().getProperty(
DEFAULTS_FILE_KEY);
pathBuf.append("bin");
pathBuf.append(File.separator);
pathBuf.append("mysqladmin shutdown");
System.out.println(pathBuf.toString());
Process mysqladmin = Runtime.getRuntime().exec(pathBuf.toString());
int exitStatus = -1;
try {
exitStatus = mysqladmin.waitFor();
} catch (InterruptedException ie) {
; // ignore
}
//
// Terminate the process if mysqladmin couldn't
// do it, and the user requested a force stop.
//
if (exitStatus != 0 && forceIfNecessary) {
forceStop();
}
}
}
/**
* Forcefully terminates the server process (if started).
*/
public void forceStop() {
if (this.serverProcess != null) {
this.serverProcess.destroy();
this.serverProcess = null;
}
}
/**
* Returns the list of properties that will be used to start/control the
* server.
*
* @return Properties the list of properties.
*/
public synchronized Properties getServerProps() {
if (this.serverProps == null) {
this.serverProps = new Properties();
}
return this.serverProps;
}
/**
* Returns the full commandline used to start the mysql server, including
* and arguments to be passed to the server process.
*
* @return String the commandline used to start the mysql server.
*/
private String getCommandLine() {
StringBuffer commandLine = new StringBuffer(getFullExecutablePath());
commandLine.append(buildOptionalCommandLine());
return commandLine.toString();
}
/**
* Returns the fully-qualifed path to the 'mysqld' executable
*
* @return String the path to the server executable.
*/
private String getFullExecutablePath() {
StringBuffer pathBuf = new StringBuffer();
String optionalExecutablePath = getServerProps().getProperty(
EXECUTABLE_PATH_KEY);
if (optionalExecutablePath == null) {
// build the path using the defaults
String basedir = getServerProps().getProperty(BASEDIR_KEY);
pathBuf.append(basedir);
if (!basedir.endsWith(File.separator)) {
pathBuf.append(File.separatorChar);
}
if (runningOnWindows()) {
pathBuf.append("bin");
} else {
pathBuf.append("libexec");
}
pathBuf.append(File.separatorChar);
} else {
pathBuf.append(optionalExecutablePath);
if (!optionalExecutablePath.endsWith(File.separator)) {
pathBuf.append(File.separatorChar);
}
}
String executableName = getServerProps().getProperty(
EXECUTABLE_NAME_KEY, "mysqld");
pathBuf.append(executableName);
return pathBuf.toString();
}
/**
* Builds the list of command-line arguments that will be passed to the
* mysql server to be started.
*
* @return String the list of command-line arguments.
*/
private String buildOptionalCommandLine() {
StringBuffer commandLineBuf = new StringBuffer();
if (this.serverProps != null) {
for (Iterator iter = this.serverProps.keySet().iterator(); iter
.hasNext();) {
String key = (String) iter.next();
String value = this.serverProps.getProperty(key);
if (!isNonCommandLineArgument(key)) {
if (value != null && value.length() > 0) {
commandLineBuf.append(" \"");
commandLineBuf.append("--");
commandLineBuf.append(key);
commandLineBuf.append("=");
commandLineBuf.append(value);
commandLineBuf.append("\"");
} else {
commandLineBuf.append(" --");
commandLineBuf.append(key);
}
}
}
}
return commandLineBuf.toString();
}
/**
* Returns true if the property does not belong as a command-line argument
*
* @return boolean if the property should not be a command-line argument.
*/
private boolean isNonCommandLineArgument(String propName) {
return propName.equals(EXECUTABLE_NAME_KEY)
|| propName.equals(EXECUTABLE_PATH_KEY);
}
/**
* Lazily creates a list of system properties.
*
* @return Properties the properties from System.getProperties()
*/
private synchronized Properties getSystemProperties() {
if (this.systemProps == null) {
this.systemProps = System.getProperties();
}
return this.systemProps;
}
/**
* Is this ServerController running on a Windows operating system?
*
* @return boolean if this ServerController is running on Windows
*/
private boolean runningOnWindows() {
return StringUtils.indexOfIgnoreCase(getSystemProperties().getProperty(
"os.name"), "WINDOWS") != -1;
}
}
|