001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.server;
005:
006: import org.mortbay.jetty.bio.SocketConnector;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.OutputStream;
011: import java.io.PushbackInputStream;
012: import java.net.InetAddress;
013: import java.net.Socket;
014: import java.net.SocketAddress;
015: import java.net.SocketException;
016: import java.nio.channels.SocketChannel;
017:
018: /**
019: * A Jettu connector that is handed sockets from the DSO listen port once they are identified as HTTP requests
020: */
021: public class TerracottaConnector extends SocketConnector {
022: private boolean shutdown = false;
023:
024: public void shutdown() {
025: synchronized (this ) {
026: shutdown = true;
027: notifyAll();
028: }
029: }
030:
031: public void handleSocketFromDSO(Socket s, byte[] data)
032: throws InterruptedException, IOException {
033: Connection connection = new Connection(new SocketWrapper(s,
034: data));
035: connection.dispatch();
036: }
037:
038: public void open() {
039: // don't call supper since it would open another server socket here
040: }
041:
042: public void accept(int acceptorID) throws InterruptedException {
043: // Jetty's accept thread will call here continuously , so we need to block it
044: synchronized (this ) {
045: while (!shutdown) {
046: wait();
047: }
048: }
049: }
050:
051: /**
052: * Wraps an existing socket such that we can wrap the input stream and provide the bytes already read
053: */
054: private static class SocketWrapper extends Socket {
055:
056: private final byte[] data;
057: private final Socket s;
058:
059: public SocketWrapper(Socket s, byte[] data) {
060: this .s = s;
061: this .data = data;
062: }
063:
064: public void bind(SocketAddress bindpoint) throws IOException {
065: s.bind(bindpoint);
066: }
067:
068: public void close() throws IOException {
069: s.close();
070: }
071:
072: public void connect(SocketAddress endpoint, int timeout)
073: throws IOException {
074: s.connect(endpoint, timeout);
075: }
076:
077: public void connect(SocketAddress endpoint) throws IOException {
078: s.connect(endpoint);
079: }
080:
081: public boolean equals(Object obj) {
082: return s.equals(obj);
083: }
084:
085: public SocketChannel getChannel() {
086: return s.getChannel();
087: }
088:
089: public InetAddress getInetAddress() {
090: return s.getInetAddress();
091: }
092:
093: public InputStream getInputStream() throws IOException {
094: PushbackInputStream pis = new PushbackInputStream(s
095: .getInputStream(), data.length);
096: pis.unread(data);
097: return pis;
098: }
099:
100: public boolean getKeepAlive() throws SocketException {
101: return s.getKeepAlive();
102: }
103:
104: public InetAddress getLocalAddress() {
105: return s.getLocalAddress();
106: }
107:
108: public int getLocalPort() {
109: return s.getLocalPort();
110: }
111:
112: public SocketAddress getLocalSocketAddress() {
113: return s.getLocalSocketAddress();
114: }
115:
116: public boolean getOOBInline() throws SocketException {
117: return s.getOOBInline();
118: }
119:
120: public OutputStream getOutputStream() throws IOException {
121: return s.getOutputStream();
122: }
123:
124: public int getPort() {
125: return s.getPort();
126: }
127:
128: public int getReceiveBufferSize() throws SocketException {
129: return s.getReceiveBufferSize();
130: }
131:
132: public SocketAddress getRemoteSocketAddress() {
133: return s.getRemoteSocketAddress();
134: }
135:
136: public boolean getReuseAddress() throws SocketException {
137: return s.getReuseAddress();
138: }
139:
140: public int getSendBufferSize() throws SocketException {
141: return s.getSendBufferSize();
142: }
143:
144: public int getSoLinger() throws SocketException {
145: return s.getSoLinger();
146: }
147:
148: public int getSoTimeout() throws SocketException {
149: return s.getSoTimeout();
150: }
151:
152: public boolean getTcpNoDelay() throws SocketException {
153: return s.getTcpNoDelay();
154: }
155:
156: public int getTrafficClass() throws SocketException {
157: return s.getTrafficClass();
158: }
159:
160: public int hashCode() {
161: return s.hashCode();
162: }
163:
164: public boolean isBound() {
165: return s.isBound();
166: }
167:
168: public boolean isClosed() {
169: return s.isClosed();
170: }
171:
172: public boolean isConnected() {
173: return s.isConnected();
174: }
175:
176: public boolean isInputShutdown() {
177: return s.isInputShutdown();
178: }
179:
180: public boolean isOutputShutdown() {
181: return s.isOutputShutdown();
182: }
183:
184: public void sendUrgentData(int d) throws IOException {
185: s.sendUrgentData(d);
186: }
187:
188: public void setKeepAlive(boolean on) throws SocketException {
189: s.setKeepAlive(on);
190: }
191:
192: public void setOOBInline(boolean on) throws SocketException {
193: s.setOOBInline(on);
194: }
195:
196: // public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
197: // s.setPerformancePreferences(connectionTime, latency, bandwidth);
198: // }
199:
200: public void setReceiveBufferSize(int size)
201: throws SocketException {
202: s.setReceiveBufferSize(size);
203: }
204:
205: public void setReuseAddress(boolean on) throws SocketException {
206: s.setReuseAddress(on);
207: }
208:
209: public void setSendBufferSize(int size) throws SocketException {
210: s.setSendBufferSize(size);
211: }
212:
213: public void setSoLinger(boolean on, int linger)
214: throws SocketException {
215: s.setSoLinger(on, linger);
216: }
217:
218: public void setSoTimeout(int timeout) throws SocketException {
219: s.setSoTimeout(timeout);
220: }
221:
222: public void setTcpNoDelay(boolean on) throws SocketException {
223: s.setTcpNoDelay(on);
224: }
225:
226: public void setTrafficClass(int tc) throws SocketException {
227: s.setTrafficClass(tc);
228: }
229:
230: public void shutdownInput() throws IOException {
231: s.shutdownInput();
232: }
233:
234: public void shutdownOutput() throws IOException {
235: s.shutdownOutput();
236: }
237:
238: public String toString() {
239: return s.toString();
240: }
241:
242: }
243:
244: }
|