Source code: com/mysql/jdbc/NamedPipeSocketFactory.java
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
9 There are special exceptions to the terms and conditions of the GPL
10 as it is applied to this software. View the full text of the
11 exception exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
12 software distribution.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 */
24 package com.mysql.jdbc;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.RandomAccessFile;
30 import java.net.Socket;
31 import java.net.SocketException;
32 import java.util.Properties;
33
34
35 /**
36 * A socket factory for named pipes (on Windows)
37 *
38 * @author Mark Matthews
39 */
40 public class NamedPipeSocketFactory implements SocketFactory {
41 private static final String NAMED_PIPE_PROP_NAME = "namedPipePath";
42 private Socket namedPipeSocket;
43
44 /**
45 * Constructor for NamedPipeSocketFactory.
46 */
47 public NamedPipeSocketFactory() {
48 super();
49 }
50
51 /**
52 * @see com.mysql.jdbc.SocketFactory#afterHandshake()
53 */
54 public Socket afterHandshake() throws SocketException, IOException {
55 return this.namedPipeSocket;
56 }
57
58 /**
59 * @see com.mysql.jdbc.SocketFactory#beforeHandshake()
60 */
61 public Socket beforeHandshake() throws SocketException, IOException {
62 return this.namedPipeSocket;
63 }
64
65 /**
66 * @see com.mysql.jdbc.SocketFactory#connect(String, Properties)
67 */
68 public Socket connect(String host, int portNumber, Properties props)
69 throws SocketException, IOException {
70 String namedPipePath = props.getProperty(NAMED_PIPE_PROP_NAME);
71
72 if (namedPipePath == null) {
73 namedPipePath = "\\\\.\\pipe\\MySQL";
74 } else if (namedPipePath.length() == 0) {
75 throw new SocketException(
76 "Can not specify NULL or empty value for property '"
77 + NAMED_PIPE_PROP_NAME + "'.");
78 }
79
80 this.namedPipeSocket = new NamedPipeSocket(namedPipePath);
81
82 return this.namedPipeSocket;
83 }
84
85 /**
86 * A socket that encapsulates named pipes on Windows
87 */
88 class NamedPipeSocket extends Socket {
89 private RandomAccessFile namedPipeFile;
90 private boolean isClosed = false;
91
92 NamedPipeSocket(String filePath) throws IOException {
93 if ((filePath == null) || (filePath.length() == 0)) {
94 throw new IOException(
95 "Named pipe path can not be null or empty");
96 }
97
98 this.namedPipeFile = new RandomAccessFile(filePath, "rw");
99 }
100
101 /**
102 * @see java.net.Socket#isClosed()
103 */
104 public boolean isClosed() {
105 return this.isClosed;
106 }
107
108 /**
109 * @see java.net.Socket#getInputStream()
110 */
111 public InputStream getInputStream() throws IOException {
112 return new RandomAccessFileInputStream(this.namedPipeFile);
113 }
114
115 /**
116 * @see java.net.Socket#getOutputStream()
117 */
118 public OutputStream getOutputStream() throws IOException {
119 return new RandomAccessFileOutputStream(this.namedPipeFile);
120 }
121
122 /**
123 * @see java.net.Socket#close()
124 */
125 public synchronized void close() throws IOException {
126 this.namedPipeFile.close();
127 this.isClosed = true;
128 }
129 }
130
131 /**
132 * Enables OutputStream-type functionality for a RandomAccessFile
133 */
134 class RandomAccessFileInputStream extends InputStream {
135 RandomAccessFile raFile;
136
137 RandomAccessFileInputStream(RandomAccessFile file) {
138 this.raFile = file;
139 }
140
141 /**
142 * @see java.io.InputStream#available()
143 */
144 public int available() throws IOException {
145 return -1;
146 }
147
148 /**
149 * @see java.io.InputStream#close()
150 */
151 public void close() throws IOException {
152 this.raFile.close();
153 }
154
155 /**
156 * @see java.io.InputStream#read()
157 */
158 public int read() throws IOException {
159 return this.raFile.read();
160 }
161
162 /**
163 * @see java.io.InputStream#read(byte[], int, int)
164 */
165 public int read(byte[] b, int off, int len) throws IOException {
166 return this.raFile.read(b, off, len);
167 }
168
169 /**
170 * @see java.io.InputStream#read(byte[])
171 */
172 public int read(byte[] b) throws IOException {
173 return this.raFile.read(b);
174 }
175 }
176
177 /**
178 * Enables OutputStream-type functionality for a RandomAccessFile
179 */
180 class RandomAccessFileOutputStream extends OutputStream {
181 RandomAccessFile raFile;
182
183 RandomAccessFileOutputStream(RandomAccessFile file) {
184 this.raFile = file;
185 }
186
187 /**
188 * @see java.io.OutputStream#close()
189 */
190 public void close() throws IOException {
191 this.raFile.close();
192 }
193
194 /**
195 * @see java.io.OutputStream#write(byte[], int, int)
196 */
197 public void write(byte[] b, int off, int len) throws IOException {
198 this.raFile.write(b, off, len);
199 }
200
201 /**
202 * @see java.io.OutputStream#write(byte[])
203 */
204 public void write(byte[] b) throws IOException {
205 this.raFile.write(b);
206 }
207
208 /**
209 * @see java.io.OutputStream#write(int)
210 */
211 public void write(int b) throws IOException {
212 }
213 }
214 }