001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty;
016:
017: import java.io.IOException;
018:
019: import org.mortbay.io.Buffer;
020: import org.mortbay.io.ByteArrayBuffer;
021: import org.mortbay.io.ByteArrayEndPoint;
022:
023: public class LocalConnector extends AbstractConnector {
024: ByteArrayEndPoint endp;
025: ByteArrayBuffer in;
026: ByteArrayBuffer out;
027:
028: Server server;
029: boolean accepting;
030: boolean _keepOpen;
031:
032: public LocalConnector() {
033: setPort(1);
034: }
035:
036: /* ------------------------------------------------------------ */
037: public Object getConnection() {
038: return endp;
039: }
040:
041: /* ------------------------------------------------------------ */
042: public void setServer(Server server) {
043: super .setServer(server);
044: this .server = server;
045: }
046:
047: /* ------------------------------------------------------------ */
048: public void clear() {
049: in.clear();
050: out.clear();
051: }
052:
053: /* ------------------------------------------------------------ */
054: public void reopen() {
055: in.clear();
056: out.clear();
057: endp = new ByteArrayEndPoint();
058: endp.setIn(in);
059: endp.setOut(out);
060: accepting = false;
061: }
062:
063: /* ------------------------------------------------------------ */
064: public void doStart() throws Exception {
065: super .doStart();
066:
067: in = new ByteArrayBuffer(8192);
068: out = new ByteArrayBuffer(8192);
069: endp = new ByteArrayEndPoint();
070: endp.setIn(in);
071: endp.setOut(out);
072: accepting = false;
073: }
074:
075: /* ------------------------------------------------------------ */
076: public String getResponses(String requests) throws Exception {
077: return getResponses(requests, false);
078: }
079:
080: /* ------------------------------------------------------------ */
081: public String getResponses(String requests, boolean keepOpen)
082: throws Exception {
083: // System.out.println("\nREQUESTS :\n"+requests);
084: // System.out.flush();
085:
086: in.put(new ByteArrayBuffer(requests));
087:
088: synchronized (this ) {
089: _keepOpen = keepOpen;
090: accepting = true;
091: this .notify();
092:
093: while (accepting)
094: this .wait();
095: }
096:
097: // System.err.println("\nRESPONSES:\n"+out);
098: return out.toString();
099: }
100:
101: /* ------------------------------------------------------------ */
102: protected Buffer newBuffer(int size) {
103: return new ByteArrayBuffer(size);
104: }
105:
106: /* ------------------------------------------------------------ */
107: protected void accept(int acceptorID) throws IOException,
108: InterruptedException {
109: HttpConnection connection = null;
110:
111: while (isRunning()) {
112: synchronized (this ) {
113: try {
114: while (!accepting)
115: this .wait();
116: } catch (InterruptedException e) {
117: return;
118: }
119: }
120:
121: try {
122: if (connection == null) {
123: connection = new HttpConnection(this , endp,
124: getServer());
125: connectionOpened(connection);
126: }
127: while (in.length() > 0)
128: connection.handle();
129: } finally {
130: if (!_keepOpen) {
131: connectionClosed(connection);
132: connection.destroy();
133: connection = null;
134: }
135: synchronized (this ) {
136: accepting = false;
137: this .notify();
138: }
139: }
140: }
141: }
142:
143: public void open() throws IOException {
144: }
145:
146: public void close() throws IOException {
147: }
148:
149: /* ------------------------------------------------------------------------------- */
150: public int getLocalPort() {
151: return -1;
152: }
153:
154: }
|