1 | /* |
---|
2 | Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. |
---|
3 | |
---|
4 | |
---|
5 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
6 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
7 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
8 | this software, see the FLOSS License Exception |
---|
9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
12 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
13 | of the License. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
16 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
17 | See the GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License along with this |
---|
20 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
21 | Floor, Boston, MA 02110-1301 USA |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | */ |
---|
26 | package com.mysql.jdbc; |
---|
27 | |
---|
28 | import java.io.IOException; |
---|
29 | import java.io.InputStream; |
---|
30 | import java.io.OutputStream; |
---|
31 | import java.io.RandomAccessFile; |
---|
32 | import java.net.Socket; |
---|
33 | import java.net.SocketException; |
---|
34 | import java.sql.SQLException; |
---|
35 | import java.util.Properties; |
---|
36 | |
---|
37 | /** |
---|
38 | * A socket factory for named pipes (on Windows) |
---|
39 | * |
---|
40 | * @author Mark Matthews |
---|
41 | */ |
---|
42 | public class NamedPipeSocketFactory implements SocketFactory, SocketMetadata { |
---|
43 | /** |
---|
44 | * A socket that encapsulates named pipes on Windows |
---|
45 | */ |
---|
46 | class NamedPipeSocket extends Socket { |
---|
47 | private boolean isClosed = false; |
---|
48 | |
---|
49 | private RandomAccessFile namedPipeFile; |
---|
50 | |
---|
51 | NamedPipeSocket(String filePath) throws IOException { |
---|
52 | if ((filePath == null) || (filePath.length() == 0)) { |
---|
53 | throw new IOException(Messages |
---|
54 | .getString("NamedPipeSocketFactory.4")); //$NON-NLS-1$ |
---|
55 | } |
---|
56 | |
---|
57 | this.namedPipeFile = new RandomAccessFile(filePath, "rw"); //$NON-NLS-1$ |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * @see java.net.Socket#close() |
---|
62 | */ |
---|
63 | public synchronized void close() throws IOException { |
---|
64 | this.namedPipeFile.close(); |
---|
65 | this.isClosed = true; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * @see java.net.Socket#getInputStream() |
---|
70 | */ |
---|
71 | public InputStream getInputStream() throws IOException { |
---|
72 | return new RandomAccessFileInputStream(this.namedPipeFile); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * @see java.net.Socket#getOutputStream() |
---|
77 | */ |
---|
78 | public OutputStream getOutputStream() throws IOException { |
---|
79 | return new RandomAccessFileOutputStream(this.namedPipeFile); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * @see java.net.Socket#isClosed() |
---|
84 | */ |
---|
85 | public boolean isClosed() { |
---|
86 | return this.isClosed; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Enables OutputStream-type functionality for a RandomAccessFile |
---|
92 | */ |
---|
93 | class RandomAccessFileInputStream extends InputStream { |
---|
94 | RandomAccessFile raFile; |
---|
95 | |
---|
96 | RandomAccessFileInputStream(RandomAccessFile file) { |
---|
97 | this.raFile = file; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * @see java.io.InputStream#available() |
---|
102 | */ |
---|
103 | public int available() throws IOException { |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * @see java.io.InputStream#close() |
---|
109 | */ |
---|
110 | public void close() throws IOException { |
---|
111 | this.raFile.close(); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * @see java.io.InputStream#read() |
---|
116 | */ |
---|
117 | public int read() throws IOException { |
---|
118 | return this.raFile.read(); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @see java.io.InputStream#read(byte[]) |
---|
123 | */ |
---|
124 | public int read(byte[] b) throws IOException { |
---|
125 | return this.raFile.read(b); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * @see java.io.InputStream#read(byte[], int, int) |
---|
130 | */ |
---|
131 | public int read(byte[] b, int off, int len) throws IOException { |
---|
132 | return this.raFile.read(b, off, len); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Enables OutputStream-type functionality for a RandomAccessFile |
---|
138 | */ |
---|
139 | class RandomAccessFileOutputStream extends OutputStream { |
---|
140 | RandomAccessFile raFile; |
---|
141 | |
---|
142 | RandomAccessFileOutputStream(RandomAccessFile file) { |
---|
143 | this.raFile = file; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * @see java.io.OutputStream#close() |
---|
148 | */ |
---|
149 | public void close() throws IOException { |
---|
150 | this.raFile.close(); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * @see java.io.OutputStream#write(byte[]) |
---|
155 | */ |
---|
156 | public void write(byte[] b) throws IOException { |
---|
157 | this.raFile.write(b); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * @see java.io.OutputStream#write(byte[], int, int) |
---|
162 | */ |
---|
163 | public void write(byte[] b, int off, int len) throws IOException { |
---|
164 | this.raFile.write(b, off, len); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * @see java.io.OutputStream#write(int) |
---|
169 | */ |
---|
170 | public void write(int b) throws IOException { |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | public static final String NAMED_PIPE_PROP_NAME = "namedPipePath"; //$NON-NLS-1$ |
---|
175 | |
---|
176 | private Socket namedPipeSocket; |
---|
177 | |
---|
178 | /** |
---|
179 | * Constructor for NamedPipeSocketFactory. |
---|
180 | */ |
---|
181 | public NamedPipeSocketFactory() { |
---|
182 | super(); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * @see com.mysql.jdbc.SocketFactory#afterHandshake() |
---|
187 | */ |
---|
188 | public Socket afterHandshake() throws SocketException, IOException { |
---|
189 | return this.namedPipeSocket; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * @see com.mysql.jdbc.SocketFactory#beforeHandshake() |
---|
194 | */ |
---|
195 | public Socket beforeHandshake() throws SocketException, IOException { |
---|
196 | return this.namedPipeSocket; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * @see com.mysql.jdbc.SocketFactory#connect(String, Properties) |
---|
201 | */ |
---|
202 | public Socket connect(String host, int portNumber /* ignored */, |
---|
203 | Properties props) throws SocketException, IOException { |
---|
204 | String namedPipePath = props.getProperty(NAMED_PIPE_PROP_NAME); |
---|
205 | |
---|
206 | if (namedPipePath == null) { |
---|
207 | namedPipePath = "\\\\.\\pipe\\MySQL"; //$NON-NLS-1$ |
---|
208 | } else if (namedPipePath.length() == 0) { |
---|
209 | throw new SocketException(Messages |
---|
210 | .getString("NamedPipeSocketFactory.2") //$NON-NLS-1$ |
---|
211 | + NAMED_PIPE_PROP_NAME |
---|
212 | + Messages.getString("NamedPipeSocketFactory.3")); //$NON-NLS-1$ |
---|
213 | } |
---|
214 | |
---|
215 | this.namedPipeSocket = new NamedPipeSocket(namedPipePath); |
---|
216 | |
---|
217 | return this.namedPipeSocket; |
---|
218 | } |
---|
219 | |
---|
220 | public boolean isLocallyConnected(ConnectionImpl conn) throws SQLException { |
---|
221 | // Until I learn otherwise (or learn how to detect it), I assume that we are |
---|
222 | return true; |
---|
223 | } |
---|
224 | } |
---|