KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > NamedPipeSocketFactory


1 /*
2    Copyright (C) 2002 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 as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.RandomAccessFile JavaDoc;
25
26 import java.net.Socket JavaDoc;
27 import java.net.SocketException JavaDoc;
28
29 import java.util.Properties JavaDoc;
30
31
32 /**
33  * A socket factory for named pipes (on Windows)
34  *
35  * @author Mark Matthews
36  */

37 public class NamedPipeSocketFactory implements SocketFactory {
38     private static final String JavaDoc NAMED_PIPE_PROP_NAME = "namedPipePath";
39     private Socket JavaDoc namedPipeSocket;
40
41     /**
42      * Constructor for NamedPipeSocketFactory.
43      */

44     public NamedPipeSocketFactory() {
45         super();
46     }
47
48     /**
49      * @see com.mysql.jdbc.SocketFactory#afterHandshake()
50      */

51     public Socket JavaDoc afterHandshake() throws SocketException JavaDoc, IOException JavaDoc {
52         return this.namedPipeSocket;
53     }
54
55     /**
56      * @see com.mysql.jdbc.SocketFactory#beforeHandshake()
57      */

58     public Socket JavaDoc beforeHandshake() throws SocketException JavaDoc, IOException JavaDoc {
59         return this.namedPipeSocket;
60     }
61
62     /**
63      * @see com.mysql.jdbc.SocketFactory#connect(String, Properties)
64      */

65     public Socket JavaDoc connect(String JavaDoc host, Properties JavaDoc props)
66         throws SocketException JavaDoc, IOException JavaDoc {
67         String JavaDoc namedPipePath = props.getProperty(NAMED_PIPE_PROP_NAME);
68
69         if (namedPipePath == null) {
70             namedPipePath = "\\\\.\\pipe\\MySQL";
71         } else if (namedPipePath.length() == 0) {
72             throw new SocketException JavaDoc(
73                 "Can not specify NULL or empty value for property '"
74                 + NAMED_PIPE_PROP_NAME + "'.");
75         }
76
77         this.namedPipeSocket = new NamedPipeSocket(namedPipePath);
78
79         return this.namedPipeSocket;
80     }
81
82     /**
83      * A socket that encapsulates named pipes on Windows
84      */

85     class NamedPipeSocket extends Socket JavaDoc {
86         private RandomAccessFile JavaDoc namedPipeFile;
87         private boolean isClosed = false;
88
89         NamedPipeSocket(String JavaDoc filePath) throws IOException JavaDoc {
90             if ((filePath == null) || (filePath.length() == 0)) {
91                 throw new IOException JavaDoc(
92                     "Named pipe path can not be null or empty");
93             }
94
95             this.namedPipeFile = new RandomAccessFile JavaDoc(filePath, "rw");
96         }
97
98         /**
99          * @see java.net.Socket#isClosed()
100          */

101         public boolean isClosed() {
102             return this.isClosed;
103         }
104
105         /**
106          * @see java.net.Socket#getInputStream()
107          */

108         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
109             return new RandomAccessFileInputStream(this.namedPipeFile);
110         }
111
112         /**
113          * @see java.net.Socket#getOutputStream()
114          */

115         public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
116             return new RandomAccessFileOutputStream(this.namedPipeFile);
117         }
118
119         /**
120          * @see java.net.Socket#close()
121          */

122         public synchronized void close() throws IOException JavaDoc {
123             this.namedPipeFile.close();
124             this.isClosed = true;
125         }
126     }
127
128     /**
129      * Enables OutputStream-type functionality for a RandomAccessFile
130      */

131     class RandomAccessFileInputStream extends InputStream JavaDoc {
132         RandomAccessFile JavaDoc raFile;
133
134         RandomAccessFileInputStream(RandomAccessFile JavaDoc file) {
135             this.raFile = file;
136         }
137
138         /**
139          * @see java.io.InputStream#available()
140          */

141         public int available() throws IOException JavaDoc {
142             return -1;
143         }
144
145         /**
146          * @see java.io.InputStream#close()
147          */

148         public void close() throws IOException JavaDoc {
149             this.raFile.close();
150         }
151
152         /**
153          * @see java.io.InputStream#read()
154          */

155         public int read() throws IOException JavaDoc {
156             return this.raFile.read();
157         }
158
159         /**
160          * @see java.io.InputStream#read(byte[], int, int)
161          */

162         public int read(byte[] b, int off, int len) throws IOException JavaDoc {
163             return this.raFile.read(b, off, len);
164         }
165
166         /**
167          * @see java.io.InputStream#read(byte[])
168          */

169         public int read(byte[] b) throws IOException JavaDoc {
170             return this.raFile.read(b);
171         }
172     }
173
174     /**
175      * Enables OutputStream-type functionality for a RandomAccessFile
176      */

177     class RandomAccessFileOutputStream extends OutputStream JavaDoc {
178         RandomAccessFile JavaDoc raFile;
179
180         RandomAccessFileOutputStream(RandomAccessFile JavaDoc file) {
181             this.raFile = file;
182         }
183
184         /**
185          * @see java.io.OutputStream#close()
186          */

187         public void close() throws IOException JavaDoc {
188             this.raFile.close();
189         }
190
191         /**
192          * @see java.io.OutputStream#write(byte[], int, int)
193          */

194         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
195             this.raFile.write(b, off, len);
196         }
197
198         /**
199          * @see java.io.OutputStream#write(byte[])
200          */

201         public void write(byte[] b) throws IOException JavaDoc {
202             this.raFile.write(b);
203         }
204
205         /**
206          * @see java.io.OutputStream#write(int)
207          */

208         public void write(int b) throws IOException JavaDoc {
209         }
210     }
211 }
212
Popular Tags