KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > server > TerracottaConnector


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.server;
5
6 import org.mortbay.jetty.bio.SocketConnector;
7
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.io.PushbackInputStream JavaDoc;
12 import java.net.InetAddress JavaDoc;
13 import java.net.Socket JavaDoc;
14 import java.net.SocketAddress JavaDoc;
15 import java.net.SocketException JavaDoc;
16 import java.nio.channels.SocketChannel JavaDoc;
17
18 /**
19  * A Jettu connector that is handed sockets from the DSO listen port once they are identified as HTTP requests
20  */

21 public class TerracottaConnector extends SocketConnector {
22   private boolean shutdown = false;
23
24   public void shutdown() {
25     synchronized (this) {
26       shutdown = true;
27       notifyAll();
28     }
29   }
30
31   public void handleSocketFromDSO(Socket JavaDoc s, byte[] data) throws InterruptedException JavaDoc, IOException JavaDoc {
32     Connection connection = new Connection(new SocketWrapper(s, data));
33     connection.dispatch();
34   }
35
36   public void open() {
37     // don't call supper since it would open another server socket here
38
}
39
40   public void accept(int acceptorID) throws InterruptedException JavaDoc {
41     // Jetty's accept thread will call here continuously , so we need to block it
42
synchronized (this) {
43       while (!shutdown) {
44         wait();
45       }
46     }
47   }
48
49   /**
50    * Wraps an existing socket such that we can wrap the input stream and provide the bytes already read
51    */

52   private static class SocketWrapper extends Socket JavaDoc {
53
54     private final byte[] data;
55     private final Socket JavaDoc s;
56
57     public SocketWrapper(Socket JavaDoc s, byte[] data) {
58       this.s = s;
59       this.data = data;
60     }
61
62     public void bind(SocketAddress JavaDoc bindpoint) throws IOException JavaDoc {
63       s.bind(bindpoint);
64     }
65
66     public void close() throws IOException JavaDoc {
67       s.close();
68     }
69
70     public void connect(SocketAddress JavaDoc endpoint, int timeout) throws IOException JavaDoc {
71       s.connect(endpoint, timeout);
72     }
73
74     public void connect(SocketAddress JavaDoc endpoint) throws IOException JavaDoc {
75       s.connect(endpoint);
76     }
77
78     public boolean equals(Object JavaDoc obj) {
79       return s.equals(obj);
80     }
81
82     public SocketChannel JavaDoc getChannel() {
83       return s.getChannel();
84     }
85
86     public InetAddress JavaDoc getInetAddress() {
87       return s.getInetAddress();
88     }
89
90     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
91       PushbackInputStream JavaDoc pis = new PushbackInputStream JavaDoc(s.getInputStream(), data.length);
92       pis.unread(data);
93       return pis;
94     }
95
96     public boolean getKeepAlive() throws SocketException JavaDoc {
97       return s.getKeepAlive();
98     }
99
100     public InetAddress JavaDoc getLocalAddress() {
101       return s.getLocalAddress();
102     }
103
104     public int getLocalPort() {
105       return s.getLocalPort();
106     }
107
108     public SocketAddress JavaDoc getLocalSocketAddress() {
109       return s.getLocalSocketAddress();
110     }
111
112     public boolean getOOBInline() throws SocketException JavaDoc {
113       return s.getOOBInline();
114     }
115
116     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
117       return s.getOutputStream();
118     }
119
120     public int getPort() {
121       return s.getPort();
122     }
123
124     public int getReceiveBufferSize() throws SocketException JavaDoc {
125       return s.getReceiveBufferSize();
126     }
127
128     public SocketAddress JavaDoc getRemoteSocketAddress() {
129       return s.getRemoteSocketAddress();
130     }
131
132     public boolean getReuseAddress() throws SocketException JavaDoc {
133       return s.getReuseAddress();
134     }
135
136     public int getSendBufferSize() throws SocketException JavaDoc {
137       return s.getSendBufferSize();
138     }
139
140     public int getSoLinger() throws SocketException JavaDoc {
141       return s.getSoLinger();
142     }
143
144     public int getSoTimeout() throws SocketException JavaDoc {
145       return s.getSoTimeout();
146     }
147
148     public boolean getTcpNoDelay() throws SocketException JavaDoc {
149       return s.getTcpNoDelay();
150     }
151
152     public int getTrafficClass() throws SocketException JavaDoc {
153       return s.getTrafficClass();
154     }
155
156     public int hashCode() {
157       return s.hashCode();
158     }
159
160     public boolean isBound() {
161       return s.isBound();
162     }
163
164     public boolean isClosed() {
165       return s.isClosed();
166     }
167
168     public boolean isConnected() {
169       return s.isConnected();
170     }
171
172     public boolean isInputShutdown() {
173       return s.isInputShutdown();
174     }
175
176     public boolean isOutputShutdown() {
177       return s.isOutputShutdown();
178     }
179
180     public void sendUrgentData(int d) throws IOException JavaDoc {
181       s.sendUrgentData(d);
182     }
183
184     public void setKeepAlive(boolean on) throws SocketException JavaDoc {
185       s.setKeepAlive(on);
186     }
187
188     public void setOOBInline(boolean on) throws SocketException JavaDoc {
189       s.setOOBInline(on);
190     }
191
192     // public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
193
// s.setPerformancePreferences(connectionTime, latency, bandwidth);
194
// }
195

196     public void setReceiveBufferSize(int size) throws SocketException JavaDoc {
197       s.setReceiveBufferSize(size);
198     }
199
200     public void setReuseAddress(boolean on) throws SocketException JavaDoc {
201       s.setReuseAddress(on);
202     }
203
204     public void setSendBufferSize(int size) throws SocketException JavaDoc {
205       s.setSendBufferSize(size);
206     }
207
208     public void setSoLinger(boolean on, int linger) throws SocketException JavaDoc {
209       s.setSoLinger(on, linger);
210     }
211
212     public void setSoTimeout(int timeout) throws SocketException JavaDoc {
213       s.setSoTimeout(timeout);
214     }
215
216     public void setTcpNoDelay(boolean on) throws SocketException JavaDoc {
217       s.setTcpNoDelay(on);
218     }
219
220     public void setTrafficClass(int tc) throws SocketException JavaDoc {
221       s.setTrafficClass(tc);
222     }
223
224     public void shutdownInput() throws IOException JavaDoc {
225       s.shutdownInput();
226     }
227
228     public void shutdownOutput() throws IOException JavaDoc {
229       s.shutdownOutput();
230     }
231
232     public String JavaDoc toString() {
233       return s.toString();
234     }
235
236   }
237
238 }
239
Popular Tags