1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | |
23 | |
24 | */ |
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 | /** |
37 | * Controls a MySQL server using Java RunTime methods |
38 | * |
39 | * @version $Id: ServerController.java,v 1.1.2.1 2005/05/13 18:58:39 mmatthews |
40 | * Exp $ |
41 | * @author Mark Matthews |
42 | */ |
43 | public class ServerController { |
44 | |
45 | /** |
46 | * Where is the server installed? |
47 | */ |
48 | public static final String BASEDIR_KEY = "basedir"; |
49 | |
50 | /** |
51 | * Where are the databases installed? |
52 | */ |
53 | public static final String DATADIR_KEY = "datadir"; |
54 | |
55 | /** |
56 | * Where is the config file located? |
57 | */ |
58 | |
59 | public static final String DEFAULTS_FILE_KEY = "defaults-file"; |
60 | |
61 | /** |
62 | * What is the name of the executable to run? |
63 | */ |
64 | |
65 | public static final String EXECUTABLE_NAME_KEY = "executable"; |
66 | |
67 | /** |
68 | * What is the path to the mysql server executable (if not standard?) |
69 | */ |
70 | |
71 | public static final String EXECUTABLE_PATH_KEY = "executablePath"; |
72 | |
73 | /** |
74 | * The default executable to run |
75 | */ |
76 | |
77 | /** |
78 | * The process representing the MySQL server |
79 | */ |
80 | private Process serverProcess = null; |
81 | |
82 | /** |
83 | * The list of properties for this server |
84 | */ |
85 | private Properties serverProps = null; |
86 | |
87 | /** |
88 | * The system properties |
89 | */ |
90 | private Properties systemProps = null; |
91 | |
92 | /** |
93 | * Creates a ServerController with the directory for the MySQL server. |
94 | * |
95 | * The 'datadir' is set to the same directory. |
96 | * |
97 | * @param baseDir |
98 | * the base directory for the MySQL server. |
99 | */ |
100 | public ServerController(String baseDir) { |
101 | setBaseDir(baseDir); |
102 | } |
103 | |
104 | /** |
105 | * Creates a server controller for the MySQL server with the given basedir |
106 | * and datadir. |
107 | * |
108 | * @param basedir |
109 | * the basedir to use when starting MySQL. |
110 | * @param datadir |
111 | * the datadir to use when starting MySQL. |
112 | */ |
113 | public ServerController(String basedir, String datadir) { |
114 | } |
115 | |
116 | /** |
117 | * Sets the basedir to use when starting MySQL. |
118 | * |
119 | * @param baseDir |
120 | * the basedir to use when starting MySQL. |
121 | */ |
122 | public void setBaseDir(String baseDir) { |
123 | getServerProps().setProperty(BASEDIR_KEY, baseDir); |
124 | } |
125 | |
126 | /** |
127 | * Sets the data to use when starting MySQL. |
128 | * |
129 | * @param dataDir |
130 | * the basedir to use when starting MySQL. |
131 | */ |
132 | public void setDataDir(String dataDir) { |
133 | getServerProps().setProperty(DATADIR_KEY, dataDir); |
134 | } |
135 | |
136 | /** |
137 | * Starts the server, returning a java.lang.Process instance that represents |
138 | * the mysql server. |
139 | * |
140 | * @return Process a java.lang.Process instance representing the mysql |
141 | * server process. |
142 | * @throws IOException |
143 | * if an error occurs while starting the mysql server. |
144 | */ |
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 | /** |
156 | * Stops the server (if started) |
157 | * |
158 | * @param forceIfNecessary |
159 | * use forceStop if mysqladmin doesn't shut the server down |
160 | * |
161 | * @throws IOException |
162 | * if an error occurs while stopping the server |
163 | */ |
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 | ; // ignore |
192 | } |
193 | |
194 | // |
195 | // Terminate the process if mysqladmin couldn't |
196 | // do it, and the user requested a force stop. |
197 | // |
198 | if (exitStatus != 0 && forceIfNecessary) { |
199 | forceStop(); |
200 | } |
201 | } |
202 | } |
203 | |
204 | /** |
205 | * Forcefully terminates the server process (if started). |
206 | */ |
207 | public void forceStop() { |
208 | if (this.serverProcess != null) { |
209 | this.serverProcess.destroy(); |
210 | this.serverProcess = null; |
211 | } |
212 | } |
213 | |
214 | /** |
215 | * Returns the list of properties that will be used to start/control the |
216 | * server. |
217 | * |
218 | * @return Properties the list of properties. |
219 | */ |
220 | public synchronized Properties getServerProps() { |
221 | if (this.serverProps == null) { |
222 | this.serverProps = new Properties(); |
223 | } |
224 | |
225 | return this.serverProps; |
226 | } |
227 | |
228 | /** |
229 | * Returns the full commandline used to start the mysql server, including |
230 | * and arguments to be passed to the server process. |
231 | * |
232 | * @return String the commandline used to start the mysql server. |
233 | */ |
234 | private String getCommandLine() { |
235 | StringBuffer commandLine = new StringBuffer(getFullExecutablePath()); |
236 | commandLine.append(buildOptionalCommandLine()); |
237 | |
238 | return commandLine.toString(); |
239 | } |
240 | |
241 | /** |
242 | * Returns the fully-qualifed path to the 'mysqld' executable |
243 | * |
244 | * @return String the path to the server executable. |
245 | */ |
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 | // build the path using the defaults |
254 | 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 | /** |
285 | * Builds the list of command-line arguments that will be passed to the |
286 | * mysql server to be started. |
287 | * |
288 | * @return String the list of command-line arguments. |
289 | */ |
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 | /** |
320 | * Returns true if the property does not belong as a command-line argument |
321 | * |
322 | * @return boolean if the property should not be a command-line argument. |
323 | */ |
324 | private boolean isNonCommandLineArgument(String propName) { |
325 | return propName.equals(EXECUTABLE_NAME_KEY) |
326 | || propName.equals(EXECUTABLE_PATH_KEY); |
327 | } |
328 | |
329 | /** |
330 | * Lazily creates a list of system properties. |
331 | * |
332 | * @return Properties the properties from System.getProperties() |
333 | */ |
334 | private synchronized Properties getSystemProperties() { |
335 | if (this.systemProps == null) { |
336 | this.systemProps = System.getProperties(); |
337 | } |
338 | |
339 | return this.systemProps; |
340 | } |
341 | |
342 | /** |
343 | * Is this ServerController running on a Windows operating system? |
344 | * |
345 | * @return boolean if this ServerController is running on Windows |
346 | */ |
347 | private boolean runningOnWindows() { |
348 | return StringUtils.indexOfIgnoreCase(getSystemProperties().getProperty( |
349 | "os.name"), "WINDOWS") != -1; |
350 | } |
351 | } |