2
Copyright (C) 2002-2004 MySQL AB
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.
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.
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.
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
25
package com.mysql.jdbc;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.io.RandomAccessFile;
32
import java.net.Socket;
33
import java.net.SocketException;
35
import java.util.Properties;
38
* A socket factory for named pipes (on Windows)
40
* @author Mark Matthews
42
public class NamedPipeSocketFactory implements SocketFactory {
44
* A socket that encapsulates named pipes on Windows
46
class NamedPipeSocket extends Socket {
47
private boolean isClosed = false;
49
private RandomAccessFile namedPipeFile;
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$
57
this.namedPipeFile = new RandomAccessFile(filePath, "rw"); //$NON-NLS-1$
61
* @see java.net.Socket#close()
63
public synchronized void close() throws IOException {
64
this.namedPipeFile.close();
69
* @see java.net.Socket#getInputStream()
71
public InputStream getInputStream() throws IOException {
72
return new RandomAccessFileInputStream(this.namedPipeFile);
76
* @see java.net.Socket#getOutputStream()
78
public OutputStream getOutputStream() throws IOException {
79
return new RandomAccessFileOutputStream(this.namedPipeFile);
83
* @see java.net.Socket#isClosed()
85
public boolean isClosed() {
91
* Enables OutputStream-type functionality for a RandomAccessFile
93
class RandomAccessFileInputStream extends InputStream {
94
RandomAccessFile raFile;
96
RandomAccessFileInputStream(RandomAccessFile file) {
101
* @see java.io.InputStream#available()
103
public int available() throws IOException {
108
* @see java.io.InputStream#close()
110
public void close() throws IOException {
115
* @see java.io.InputStream#read()
117
public int read() throws IOException {
118
return this.raFile.read();
122
* @see java.io.InputStream#read(byte[])
124
public int read(byte[] b) throws IOException {
125
return this.raFile.read(b);
129
* @see java.io.InputStream#read(byte[], int, int)
131
public int read(byte[] b, int off, int len) throws IOException {
132
return this.raFile.read(b, off, len);
137
* Enables OutputStream-type functionality for a RandomAccessFile
139
class RandomAccessFileOutputStream extends OutputStream {
140
RandomAccessFile raFile;
142
RandomAccessFileOutputStream(RandomAccessFile file) {
147
* @see java.io.OutputStream#close()
149
public void close() throws IOException {
154
* @see java.io.OutputStream#write(byte[])
156
public void write(byte[] b) throws IOException {
157
this.raFile.write(b);
161
* @see java.io.OutputStream#write(byte[], int, int)
163
public void write(byte[] b, int off, int len) throws IOException {
164
this.raFile.write(b, off, len);
168
* @see java.io.OutputStream#write(int)
170
public void write(int b) throws IOException {
174
private static final String NAMED_PIPE_PROP_NAME = "namedPipePath"; //$NON-NLS-1$
176
private Socket namedPipeSocket;
179
* Constructor for NamedPipeSocketFactory.
181
public NamedPipeSocketFactory() {
186
* @see com.mysql.jdbc.SocketFactory#afterHandshake()
188
public Socket afterHandshake() throws SocketException, IOException {
189
return this.namedPipeSocket;
193
* @see com.mysql.jdbc.SocketFactory#beforeHandshake()
195
public Socket beforeHandshake() throws SocketException, IOException {
196
return this.namedPipeSocket;
200
* @see com.mysql.jdbc.SocketFactory#connect(String, Properties)
202
public Socket connect(String host, int portNumber /* ignored */,
203
Properties props) throws SocketException, IOException {
204
String namedPipePath = props.getProperty(NAMED_PIPE_PROP_NAME);
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$
215
this.namedPipeSocket = new NamedPipeSocket(namedPipePath);
217
return this.namedPipeSocket;