KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbcserver > StreamJDBCServerConnection


1 /**
2  * com.mckoi.database.jdbcserver.StreamJDBCServerConnection 22 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbcserver;
26
27 import com.mckoi.debug.DebugLogger;
28 import com.mckoi.database.Database;
29 import com.mckoi.database.jdbc.ProtocolConstants;
30 import com.mckoi.database.jdbc.DatabaseInterface;
31 import com.mckoi.util.LengthMarkedBufferedInputStream;
32 import java.io.*;
33
34 /**
35  * A generic JDBC stream protocol server that reads JDBC commands from a
36  * stream from each connection and dispatches the commands appropriately.
37  *
38  * @author Tobias Downer
39  */

40
41 abstract class StreamJDBCServerConnection extends JDBCProcessor
42                                                  implements ServerConnection {
43
44   /**
45    * The size in bytes of the buffer used for writing information onto the
46    * output stream to the client.
47    */

48   private static final int OUTPUT_BUFFER_SIZE = 32768;
49
50   /**
51    * The size in bytes of the buffer used for reading information from the
52    * input stream from the client.
53    */

54   private static final int INPUT_BUFFER_SIZE = 16384;
55
56   /**
57    * The LengthMarkedBufferedInputStream we use to poll for commands from the
58    * client.
59    */

60   private LengthMarkedBufferedInputStream marked_input;
61
62   /**
63    * The output stream to the client formatted as a DataOutputStream.
64    */

65   private DataOutputStream out;
66
67   /**
68    * Sets up the protocol connection.
69    */

70   StreamJDBCServerConnection(DatabaseInterface db_interface,
71                   InputStream in, OutputStream out, DebugLogger logger)
72                                                          throws IOException {
73     super(db_interface, logger);
74
75     this.marked_input = new LengthMarkedBufferedInputStream(in);
76     this.out = new DataOutputStream(
77                            new BufferedOutputStream(out, OUTPUT_BUFFER_SIZE));
78
79   }
80
81   // ---------- Implemented from JDBCConnection ----------
82

83   // NOTE: There's a security issue for this method. See JDBCProcessor
84
// for the details.
85
public void sendEvent(byte[] event_msg) throws IOException {
86     synchronized (out) {
87       // Command length...
88
out.writeInt(4 + 4 + event_msg.length);
89       // Dispatch id...
90
out.writeInt(-1);
91       // Command id...
92
out.writeInt(ProtocolConstants.DATABASE_EVENT);
93       // The message...
94
out.write(event_msg, 0, event_msg.length);
95       // Flush command to server.
96
out.flush();
97     }
98   }
99
100   // ---------- Implemented from ServerConnection ----------
101

102   /**
103    * Inspects the input stream and determines in there's a command pending
104    * to be processed.
105    */

106   public boolean requestPending() throws IOException {
107     int state = getState();
108     if (state == 100) {
109       return marked_input.pollForCommand(Integer.MAX_VALUE);
110     }
111     else {
112       return marked_input.pollForCommand(256);
113     }
114   }
115
116   /**
117    * Processes a request from this connection.
118    */

119   public void processRequest() throws IOException {
120     // Only allow 8 commands to execute in sequence before we free this
121
// worker to the worker pool.
122
// We have a limit incase of potential DOS problems.
123
int sequence_limit = 8;
124
125     // Read the command into a 'byte[]' array and pass to the command
126
// processor.
127
int com_length = marked_input.available();
128     while (com_length > 0) {
129       byte[] command = new byte[com_length];
130       int read_index = 0;
131       while (read_index < com_length) {
132         read_index +=
133             marked_input.read(command, read_index, (com_length - read_index));
134       }
135
136       // Process the command
137
byte[] response = processJDBCCommand(command);
138       if (response != null) {
139
140         synchronized (out) {
141           // Write the response to the client.
142
out.writeInt(response.length);
143           out.write(response);
144           out.flush();
145         }
146
147       }
148
149       // If there's another command pending then process that one also,
150
com_length = 0;
151       if (sequence_limit > 0) {
152         if (requestPending()) {
153           com_length = marked_input.available();
154           --sequence_limit;
155         }
156       }
157
158     } // while (com_length > 0)
159

160 // // Response...
161
// printByteArray(response);
162
}
163
164   /**
165    * Block waiting for a complete command to become available.
166    */

167   public void blockForRequest() throws IOException {
168     marked_input.blockForCommand();
169   }
170
171   /**
172    * Pings the client to check it's still alive.
173    */

174   public void ping() throws IOException {
175     synchronized (out) {
176       // Command length...
177
out.writeInt(8);
178       // Dispatch id...
179
out.writeInt(-1);
180       // Ping command id...
181
out.writeInt(ProtocolConstants.PING);
182       // Flush command to server.
183
out.flush();
184     }
185   }
186
187 }
188
Free Books   Free Magazines  
Popular Tags