KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.Method 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  * Socket factory for vanilla TCP/IP sockets (the standard)
34  *
35  * @author Mark Matthews
36  */

37 public class StandardSocketFactory implements SocketFactory {
38     /** The underlying TCP/IP socket to use */
39     protected Socket JavaDoc rawSocket = null;
40
41     /** The hostname to connect to */
42     protected String JavaDoc host = null;
43
44     /** The port number to connect to */
45     protected int port = 3306;
46
47     /**
48      * Called by the driver after issuing the MySQL protocol handshake and
49      * reading the results of the handshake.
50      *
51      * @throws SocketException if a socket error occurs
52      * @throws IOException if an I/O error occurs
53      *
54      * @return The socket to use after the handshake
55      */

56     public Socket JavaDoc afterHandshake() throws SocketException JavaDoc, IOException JavaDoc {
57         return rawSocket;
58     }
59
60     /**
61      * Called by the driver before issuing the MySQL protocol handshake.
62      * Should return the socket instance that should be used during
63      * the handshake.
64      *
65      * @throws SocketException if a socket error occurs
66      * @throws IOException if an I/O error occurs
67      *
68      * @return the socket to use before the handshake
69      */

70     public Socket JavaDoc beforeHandshake() throws SocketException JavaDoc, IOException JavaDoc {
71         return rawSocket;
72     }
73
74     /**
75      * @see com.mysql.jdbc.SocketFactory#createSocket(Properties)
76      */

77     public Socket JavaDoc connect(String JavaDoc host, Properties JavaDoc props)
78         throws SocketException JavaDoc, IOException JavaDoc {
79         if (props != null) {
80             this.host = host;
81
82             String JavaDoc portStr = props.getProperty("PORT");
83
84             if (portStr != null) {
85                 port = Integer.parseInt(portStr);
86             }
87
88             boolean hasConnectTimeoutMethod = false;
89
90             Method JavaDoc connectWithTimeoutMethod = null;
91
92             try {
93                 // Have to do this with reflection, otherwise older JVMs croak
94
Class JavaDoc socketAddressClass = Class.forName(
95                         "java.net.SocketAddress");
96
97                 connectWithTimeoutMethod = Socket JavaDoc.class.getMethod("connect",
98                         new Class JavaDoc[] { socketAddressClass, Integer.TYPE });
99
100                 hasConnectTimeoutMethod = true;
101             } catch (NoClassDefFoundError JavaDoc noClassDefFound) {
102                 hasConnectTimeoutMethod = false;
103             } catch (NoSuchMethodException JavaDoc noSuchMethodEx) {
104                 hasConnectTimeoutMethod = false;
105             } catch (Throwable JavaDoc catchAll) {
106                 hasConnectTimeoutMethod = false;
107             }
108
109             int connectTimeout = 0;
110
111             String JavaDoc connectTimeoutStr = props.getProperty("connectTimeout");
112
113             if (connectTimeoutStr != null) {
114                 try {
115                     connectTimeout = Integer.parseInt(connectTimeoutStr);
116                 } catch (NumberFormatException JavaDoc nfe) {
117                     throw new SocketException JavaDoc("Illegal value '"
118                         + connectTimeoutStr + "' for connectTimeout");
119                 }
120             }
121
122             if (this.host != null) {
123                 if (!hasConnectTimeoutMethod || (connectTimeout == 0)) {
124                     rawSocket = new Socket JavaDoc(this.host, port);
125                 } else {
126                     // must explicitly state this due to classloader issues
127
// when running on older JVMs :(
128
try {
129                         Class JavaDoc inetSocketAddressClass = Class.forName(
130                                 "java.net.InetSocketAddress");
131                         Constructor JavaDoc addrConstructor = inetSocketAddressClass
132                             .getConstructor(new Class JavaDoc[] {
133                                     String JavaDoc.class, Integer.TYPE
134                                 });
135
136                         Object JavaDoc sockAddr = addrConstructor.newInstance(new Object JavaDoc[] {
137                                     this.host, new Integer JavaDoc(port)
138                                 });
139
140                         rawSocket = new Socket JavaDoc();
141                         connectWithTimeoutMethod.invoke(rawSocket,
142                             new Object JavaDoc[] { sockAddr, new Integer JavaDoc(connectTimeout) });
143                     } catch (Throwable JavaDoc t) {
144                         throw new SocketException JavaDoc(t.toString());
145                     }
146                 }
147
148                 try {
149                     rawSocket.setTcpNoDelay(true);
150                 } catch (Exception JavaDoc ex) {
151                     /* Ignore */
152                 }
153
154                 return rawSocket;
155             }
156         }
157
158         throw new SocketException JavaDoc("Unable to create socket");
159     }
160 }
161
Popular Tags