Source code: org/hsqldb/ServerConnection.java
1 /* Copyrights and Licenses
2 *
3 * This product includes Hypersonic SQL.
4 * Originally developed by Thomas Mueller and the Hypersonic SQL Group.
5 *
6 * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice, this list of conditions
10 * and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * - All advertising materials mentioning features or use of this software must display the
15 * following acknowledgment: "This product includes Hypersonic SQL."
16 * - Products derived from this software may not be called "Hypersonic SQL" nor may
17 * "Hypersonic SQL" appear in their names without prior written permission of the
18 * Hypersonic SQL Group.
19 * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
20 * product includes Hypersonic SQL."
21 * This software is provided "as is" and any expressed or implied warranties, including, but
22 * not limited to, the implied warranties of merchantability and fitness for a particular purpose are
23 * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any
24 * direct, indirect, incidental, special, exemplary, or consequential damages (including, but
25 * not limited to, procurement of substitute goods or services; loss of use, data, or profits;
26 * or business interruption). However caused any on any theory of liability, whether in contract,
27 * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
28 * software, even if advised of the possibility of such damage.
29 * This software consists of voluntary contributions made by many individuals on behalf of the
30 * Hypersonic SQL Group.
31 *
32 *
33 * For work added by the HSQL Development Group:
34 *
35 * Copyright (c) 2001-2002, The HSQL Development Group
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
40 *
41 * Redistributions of source code must retain the above copyright notice, this
42 * list of conditions and the following disclaimer, including earlier
43 * license statements (above) and comply with all above license conditions.
44 *
45 * Redistributions in binary form must reproduce the above copyright notice,
46 * this list of conditions and the following disclaimer in the documentation
47 * and/or other materials provided with the distribution, including earlier
48 * license statements (above) and comply with all above license conditions.
49 *
50 * Neither the name of the HSQL Development Group nor the names of its
51 * contributors may be used to endorse or promote products derived from this
52 * software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
58 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 */
66
67
68 package org.hsqldb;
69
70 import java.io.BufferedInputStream;
71 import java.io.BufferedOutputStream;
72 import java.io.DataInputStream;
73 import java.io.DataOutputStream;
74 import java.io.IOException;
75 import java.net.ServerSocket;
76 import java.net.Socket;
77 import java.sql.SQLException;
78 import java.util.Observable;
79
80 // fredt@users 20020215 - patch 461556 by paul-h@users - server factory
81 // fredt@users 20020424 - patch 1.7.0 by fredt - shutdown without exit
82 // fredt@users 20021002 - patch 1.7.1 by fredt - changed notification method
83
84 /**
85 * All ServerConnection objects are listed in a Vector in mServer
86 * and removed when closed.<p>
87 *
88 * When a connection is dropped or closed the Server.notify() method is
89 * called. Upon notification, tf the DB is shutdown as a result of SHUTDOWN,
90 * the server stops all
91 * ServerConnection threads. At this point, only the skeletal Server
92 * object remains and everything else will be garbage collected.
93 * (fredt@users)<p>
94 *
95 * @version 1.7.1
96 */
97 class ServerConnection implements Runnable {
98
99 private String user;
100 private Session session;
101 private Socket mSocket;
102 private Server mServer;
103 private DataInputStream mInput;
104 private DataOutputStream mOutput;
105 private static int mCurrentThread = 0;
106 private int mThread;
107
108 /**
109 *
110 * @param socket
111 * @param server
112 */
113 ServerConnection(Socket socket, Server server) {
114
115 mSocket = socket;
116 mServer = server;
117
118 synchronized (ServerConnection.class) {
119 mThread = mCurrentThread++;
120 }
121
122 mServer.serverConnList.addElement(this);
123 }
124
125 void close() {
126
127 // fredt@user - closing the socket is to stop this thread
128 try {
129 mSocket.close();
130 } catch (IOException e) {}
131
132 mServer.serverConnList.removeElement(this);
133 mServer.notify(Server.CONNECTION_CLOSED);
134 }
135
136 /**
137 * Method declaration
138 *
139 *
140 * @return
141 */
142 private Session init() {
143
144 try {
145 mSocket.setTcpNoDelay(true);
146
147 mInput = new DataInputStream(
148 new BufferedInputStream(mSocket.getInputStream()));
149 mOutput = new DataOutputStream(
150 new BufferedOutputStream(mSocket.getOutputStream()));
151 user = mInput.readUTF();
152
153 String password = mInput.readUTF();
154 Session c;
155
156 try {
157 mServer.trace(mThread + ":trying to connect user " + user);
158
159 return mServer.mDatabase.connect(user, password);
160 } catch (SQLException e) {
161 write(new Result(e.getMessage(),
162 e.getErrorCode()).getBytes());
163 }
164 } catch (Exception e) {
165 mServer.trace(mThread + ":couldn't connect " + user);
166 }
167
168 close();
169
170 return null;
171 }
172
173 /**
174 * Method declaration
175 *
176 */
177 public void run() {
178
179 session = init();
180
181 if (session != null) {
182 try {
183 while (true) {
184
185 // fredt@users 20011220 - patch 448121 by sma@users - large binary values
186 byte[] bytes = new byte[mInput.readInt()];
187
188 mInput.readFully(bytes);
189
190 String sql = new String(bytes, "utf-8");
191
192 mServer.trace(mThread + ":" + sql);
193
194 if (sql == null) {
195 break;
196 }
197
198 write(mServer.mDatabase.execute(sql, session).getBytes());
199
200 if (mServer.mDatabase.isShutdown()) {
201 break;
202 }
203 }
204 } catch (IOException e) {
205 mServer.trace(mThread + ":disconnected " + user);
206
207 // fredt - todo - after the client abrubtly drops, should perform equivalent
208 // of Session.disconnect() to clear any TEMP tables
209 } catch (SQLException e) {
210
211 // is thrown by Result.getBytes()
212 String s = e.getMessage();
213
214 e.printStackTrace();
215 }
216
217 close();
218 }
219 }
220
221 /**
222 * Method declaration
223 *
224 *
225 * @param b
226 *
227 * @throws IOException
228 */
229 void write(byte b[]) throws IOException {
230
231 mOutput.writeInt(b.length);
232 mOutput.write(b);
233 mOutput.flush();
234 }
235 }