1 | /* |
2 | Copyright (C) 2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License version 2 as |
6 | published by the Free Software Foundation. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | |
17 | */ |
18 | package com.mysql.management.driverlaunched; |
19 | |
20 | import java.io.File; |
21 | import java.io.IOException; |
22 | import java.net.Socket; |
23 | import java.net.SocketException; |
24 | import java.util.Enumeration; |
25 | import java.util.HashMap; |
26 | import java.util.Map; |
27 | import java.util.Properties; |
28 | |
29 | import com.mysql.jdbc.SocketFactory; |
30 | import com.mysql.jdbc.StandardSocketFactory; |
31 | import com.mysql.management.MysqldResource; |
32 | import com.mysql.management.MysqldResourceI; |
33 | import com.mysql.management.util.Files; |
34 | |
35 | /** |
36 | * This class is final simply as a hint to the compiler, it may be un-finalized |
37 | * safely. |
38 | * |
39 | * @author Eric Herman <eric@mysql.com> |
40 | */ |
41 | public final class ServerLauncherSocketFactory implements SocketFactory { |
42 | |
43 | public static final String SERVER_DOT = "server."; |
44 | |
45 | private static int launchCount = 0; |
46 | |
47 | private MysqldFactory resourceFactory; |
48 | |
49 | private SocketFactory socketFactory; |
50 | |
51 | public ServerLauncherSocketFactory() { |
52 | setResourceFactory(new MysqldFactory.Default()); |
53 | setSocketFactory(new StandardSocketFactory()); |
54 | } |
55 | |
56 | public Socket connect(String host, int portNumber, Properties props) |
57 | throws SocketException, IOException { |
58 | ensureMysqlStarted(portNumber, props); |
59 | |
60 | return getSocketFactory().connect(host, portNumber, props); |
61 | } |
62 | |
63 | private void ensureMysqlStarted(int port, Properties props) { |
64 | Map serverOpts = new HashMap(); |
65 | for (Enumeration enums = props.propertyNames(); enums.hasMoreElements();) { |
66 | String key = enums.nextElement().toString(); |
67 | if (key.startsWith(SERVER_DOT)) { |
68 | String val = replaceNullStringWithNull(props.getProperty(key)); |
69 | serverOpts.put(key.substring(SERVER_DOT.length()), val); |
70 | } |
71 | } |
72 | serverOpts.put(MysqldResourceI.PORT, Integer.toString(port)); |
73 | Object baseDirStr = serverOpts.get(MysqldResourceI.BASEDIR); |
74 | File baseDir = new Files().newFile(baseDirStr); |
75 | |
76 | String dataDirString = (String) serverOpts.get(MysqldResourceI.DATADIR); |
77 | |
78 | File dataDir = null; |
79 | if (dataDirString != null) { |
80 | File ddir = new File(dataDirString); |
81 | dataDir = new Files().validCononicalDir(ddir); |
82 | } |
83 | |
84 | MysqldResourceI mysqld = resourceFactory.newMysqldResource(baseDir, |
85 | dataDir); |
86 | |
87 | if (mysqld.isRunning()) { |
88 | Object runningPort = mysqld.getServerOptions().get( |
89 | MysqldResourceI.PORT); |
90 | if (!runningPort.equals(Integer.toString(port))) { |
91 | String msg = "Mysqld at " + mysqld.getBaseDir() |
92 | + " is running on port " + runningPort + " not " + port; |
93 | throw new RuntimeException(msg); |
94 | } |
95 | return; |
96 | } |
97 | |
98 | mysqld.setVersion((String) serverOpts |
99 | .get(MysqldResourceI.MYSQLD_VERSION)); |
100 | |
101 | launchCount++; |
102 | String threadName = "driver_launched_mysqld_" + launchCount; |
103 | mysqld.start(threadName, serverOpts); |
104 | } |
105 | |
106 | String replaceNullStringWithNull(String str) { |
107 | return String.valueOf((Object) null).equals(str) ? null : str; |
108 | } |
109 | |
110 | public Socket afterHandshake() throws SocketException, IOException { |
111 | return getSocketFactory().afterHandshake(); |
112 | } |
113 | |
114 | public Socket beforeHandshake() throws SocketException, IOException { |
115 | return getSocketFactory().beforeHandshake(); |
116 | } |
117 | |
118 | void setResourceFactory(MysqldFactory resourceFactory) { |
119 | this.resourceFactory = resourceFactory; |
120 | } |
121 | |
122 | MysqldFactory getResourceFactory() { |
123 | return resourceFactory; |
124 | } |
125 | |
126 | void setSocketFactory(SocketFactory socketFactory) { |
127 | this.socketFactory = socketFactory; |
128 | } |
129 | |
130 | SocketFactory getSocketFactory() { |
131 | return socketFactory; |
132 | } |
133 | |
134 | // ------------------------------------------------------------- |
135 | public synchronized static boolean shutdown(File baseDir, File dataDir) { |
136 | MysqldResource mysqld = new MysqldResource(baseDir, dataDir); |
137 | mysqld.shutdown(); |
138 | return mysqld.isRunning(); |
139 | } |
140 | } |