View Javadoc

1   //========================================================================
2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty;
16  
17  import java.io.IOException;
18  
19  import org.mortbay.io.Buffer;
20  import org.mortbay.io.ByteArrayBuffer;
21  import org.mortbay.io.ByteArrayEndPoint;
22  
23  public class LocalConnector extends AbstractConnector
24  {
25      ByteArrayEndPoint _endp;
26      ByteArrayBuffer _in;
27      ByteArrayBuffer _out;
28      
29      Server _server;
30      boolean _accepting;
31      boolean _keepOpen;
32      
33      public LocalConnector()
34      {
35          setPort(1);
36      }
37  
38      /* ------------------------------------------------------------ */
39      public Object getConnection()
40      {
41          return _endp;
42      }
43      
44  
45      /* ------------------------------------------------------------ */
46      public void setServer(Server server)
47      {
48          super.setServer(server);
49          this._server=server;
50      }
51  
52      /* ------------------------------------------------------------ */
53      public void clear()
54      {
55          _in.clear();
56          _out.clear();
57      }
58  
59      /* ------------------------------------------------------------ */
60      public void reopen()
61      {
62          _in.clear();
63          _out.clear();
64          _endp = new ByteArrayEndPoint();
65          _endp.setIn(_in);
66          _endp.setOut(_out);
67          _endp.setGrowOutput(true);
68          _accepting=false;
69      }
70  
71      /* ------------------------------------------------------------ */
72      public void doStart()
73          throws Exception
74      {   
75          _in=new ByteArrayBuffer(8192);
76          _out=new ByteArrayBuffer(8192);
77          _endp = new ByteArrayEndPoint();
78          _endp.setIn(_in);
79          _endp.setOut(_out);
80          _endp.setGrowOutput(true);
81          _accepting=false;
82          
83          super.doStart();
84      }
85  
86      /* ------------------------------------------------------------ */
87      public String getResponses(String requests)
88          throws Exception
89      {
90          return getResponses(requests,false);
91      }
92      
93      /* ------------------------------------------------------------ */
94      public String getResponses(String requests, boolean keepOpen)
95      throws Exception
96      {
97          // System.out.println("\nREQUESTS :\n"+requests);
98          // System.out.flush();
99          ByteArrayBuffer buf=new ByteArrayBuffer(requests);
100         if (_in.space()<buf.length())
101         {
102             ByteArrayBuffer n = new ByteArrayBuffer(_in.length()+buf.length());
103             n.put(_in);
104             _in=n;
105             _endp.setIn(_in);
106         }
107         _in.put(buf);
108         
109         synchronized (this)
110         {
111             _keepOpen=keepOpen;
112             _accepting=true;
113             this.notify();
114             
115             while(_accepting)
116                 this.wait();
117         }
118         
119         // System.err.println("\nRESPONSES:\n"+out);
120         _out=_endp.getOut();
121         return _out.toString();
122     }
123     
124     /* ------------------------------------------------------------ */
125     public ByteArrayBuffer getResponses(ByteArrayBuffer buf, boolean keepOpen)
126     throws Exception
127     {
128         if (_in.space()<buf.length())
129         {
130             ByteArrayBuffer n = new ByteArrayBuffer(_in.length()+buf.length());
131             n.put(_in);
132             _in=n;
133             _endp.setIn(_in);
134         }
135         _in.put(buf);
136         
137         synchronized (this)
138         {
139             _keepOpen=keepOpen;
140             _accepting=true;
141             this.notify();
142             
143             while(_accepting)
144                 this.wait();
145         }
146         
147         // System.err.println("\nRESPONSES:\n"+out);
148         _out=_endp.getOut();
149         return _out;
150     }
151 
152     /* ------------------------------------------------------------ */
153     protected Buffer newBuffer(int size)
154     {
155         return new ByteArrayBuffer(size);
156     }
157 
158     /* ------------------------------------------------------------ */
159     protected void accept(int acceptorID) throws IOException, InterruptedException
160     {
161         HttpConnection connection=null;
162         
163         while (isRunning())
164         {
165             synchronized (this)
166             {
167                 try
168                 {
169                     while(!_accepting)
170                         this.wait();
171                 }
172                 catch(InterruptedException e)
173                 {
174                     return;
175                 }
176             }
177             
178             try
179             {
180                 if (connection==null)
181                 {
182                     connection=new HttpConnection(this,_endp,getServer());
183                     connectionOpened(connection);
184                 }
185                 while (_in.length()>0)
186                     connection.handle();
187             }
188             finally
189             {
190                 if (!_keepOpen)
191                 {
192                     connectionClosed(connection);
193                     connection.destroy();
194                     connection=null;
195                 }
196                 synchronized (this)
197                 {
198                     _accepting=false;
199                     this.notify();
200                 }
201             }
202         }
203     }
204     
205 
206     public void open() throws IOException
207     {
208     }
209 
210     public void close() throws IOException
211     {
212     }
213 
214     /* ------------------------------------------------------------------------------- */
215     public int getLocalPort()
216     {
217         return -1;
218     }
219 
220     
221 }