1 25 package com.mysql.jdbc.util; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 30 import java.util.Iterator ; 31 import java.util.Locale ; 32 import java.util.Properties ; 33 34 import com.mysql.jdbc.StringUtils; 35 36 43 public class ServerController { 44 45 48 public static final String BASEDIR_KEY = "basedir"; 49 50 53 public static final String DATADIR_KEY = "datadir"; 54 55 58 59 public static final String DEFAULTS_FILE_KEY = "defaults-file"; 60 61 64 65 public static final String EXECUTABLE_NAME_KEY = "executable"; 66 67 70 71 public static final String EXECUTABLE_PATH_KEY = "executablePath"; 72 73 76 77 80 private Process serverProcess = null; 81 82 85 private Properties serverProps = null; 86 87 90 private Properties systemProps = null; 91 92 100 public ServerController(String baseDir) { 101 setBaseDir(baseDir); 102 } 103 104 113 public ServerController(String basedir, String datadir) { 114 } 115 116 122 public void setBaseDir(String baseDir) { 123 getServerProps().setProperty(BASEDIR_KEY, baseDir); 124 } 125 126 132 public void setDataDir(String dataDir) { 133 getServerProps().setProperty(DATADIR_KEY, dataDir); 134 } 135 136 145 public Process start() throws IOException { 146 if (this.serverProcess != null) { 147 throw new IllegalArgumentException ("Server already started"); 148 } else { 149 this.serverProcess = Runtime.getRuntime().exec(getCommandLine()); 150 151 return this.serverProcess; 152 } 153 } 154 155 164 public void stop(boolean forceIfNecessary) throws IOException { 165 if (this.serverProcess != null) { 166 167 String basedir = getServerProps().getProperty(BASEDIR_KEY); 168 169 StringBuffer pathBuf = new StringBuffer (basedir); 170 171 if (!basedir.endsWith(File.separator)) { 172 pathBuf.append(File.separator); 173 } 174 175 String defaultsFilePath = getServerProps().getProperty( 176 DEFAULTS_FILE_KEY); 177 178 pathBuf.append("bin"); 179 pathBuf.append(File.separator); 180 pathBuf.append("mysqladmin shutdown"); 181 182 System.out.println(pathBuf.toString()); 183 184 Process mysqladmin = Runtime.getRuntime().exec(pathBuf.toString()); 185 186 int exitStatus = -1; 187 188 try { 189 exitStatus = mysqladmin.waitFor(); 190 } catch (InterruptedException ie) { 191 ; } 193 194 if (exitStatus != 0 && forceIfNecessary) { 199 forceStop(); 200 } 201 } 202 } 203 204 207 public void forceStop() { 208 if (this.serverProcess != null) { 209 this.serverProcess.destroy(); 210 this.serverProcess = null; 211 } 212 } 213 214 220 public synchronized Properties getServerProps() { 221 if (this.serverProps == null) { 222 this.serverProps = new Properties (); 223 } 224 225 return this.serverProps; 226 } 227 228 234 private String getCommandLine() { 235 StringBuffer commandLine = new StringBuffer (getFullExecutablePath()); 236 commandLine.append(buildOptionalCommandLine()); 237 238 return commandLine.toString(); 239 } 240 241 246 private String getFullExecutablePath() { 247 StringBuffer pathBuf = new StringBuffer (); 248 249 String optionalExecutablePath = getServerProps().getProperty( 250 EXECUTABLE_PATH_KEY); 251 252 if (optionalExecutablePath == null) { 253 String basedir = getServerProps().getProperty(BASEDIR_KEY); 255 pathBuf.append(basedir); 256 257 if (!basedir.endsWith(File.separator)) { 258 pathBuf.append(File.separatorChar); 259 } 260 261 if (runningOnWindows()) { 262 pathBuf.append("bin"); 263 } else { 264 pathBuf.append("libexec"); 265 } 266 267 pathBuf.append(File.separatorChar); 268 } else { 269 pathBuf.append(optionalExecutablePath); 270 271 if (!optionalExecutablePath.endsWith(File.separator)) { 272 pathBuf.append(File.separatorChar); 273 } 274 } 275 276 String executableName = getServerProps().getProperty( 277 EXECUTABLE_NAME_KEY, "mysqld"); 278 279 pathBuf.append(executableName); 280 281 return pathBuf.toString(); 282 } 283 284 290 private String buildOptionalCommandLine() { 291 StringBuffer commandLineBuf = new StringBuffer (); 292 293 if (this.serverProps != null) { 294 295 for (Iterator iter = this.serverProps.keySet().iterator(); iter 296 .hasNext();) { 297 String key = (String ) iter.next(); 298 String value = this.serverProps.getProperty(key); 299 300 if (!isNonCommandLineArgument(key)) { 301 if (value != null && value.length() > 0) { 302 commandLineBuf.append(" \""); 303 commandLineBuf.append("--"); 304 commandLineBuf.append(key); 305 commandLineBuf.append("="); 306 commandLineBuf.append(value); 307 commandLineBuf.append("\""); 308 } else { 309 commandLineBuf.append(" --"); 310 commandLineBuf.append(key); 311 } 312 } 313 } 314 } 315 316 return commandLineBuf.toString(); 317 } 318 319 324 private boolean isNonCommandLineArgument(String propName) { 325 return propName.equals(EXECUTABLE_NAME_KEY) 326 || propName.equals(EXECUTABLE_PATH_KEY); 327 } 328 329 334 private synchronized Properties getSystemProperties() { 335 if (this.systemProps == null) { 336 this.systemProps = System.getProperties(); 337 } 338 339 return this.systemProps; 340 } 341 342 347 private boolean runningOnWindows() { 348 return StringUtils.indexOfIgnoreCase(getSystemProperties().getProperty( 349 "os.name"), "WINDOWS") != -1; 350 } 351 } 352 | Popular Tags |