~pbms-core/pbms/5.11-beta

« back to all changes in this revision

Viewing changes to mybs/java/src/com/mysql/jdbc/NamedPipeSocketFactory.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
26
 
 
27
import java.io.IOException;
 
28
import java.io.InputStream;
 
29
import java.io.OutputStream;
 
30
import java.io.RandomAccessFile;
 
31
 
 
32
import java.net.Socket;
 
33
import java.net.SocketException;
 
34
 
 
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 {
 
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
        private 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
}