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  import org.mortbay.util.StringUtil;
23  
24  public class LocalConnector extends AbstractConnector
25  {
26      ByteArrayEndPoint _endp;
27      ByteArrayBuffer _in;
28      ByteArrayBuffer _out;
29      
30      Server _server;
31      boolean _accepting;
32      boolean _keepOpen;
33      
34      public LocalConnector()
35      {
36          setPort(1);
37      }
38  
39      /* ------------------------------------------------------------ */
40      public Object getConnection()
41      {
42          return _endp;
43      }
44      
45  
46      /* ------------------------------------------------------------ */
47      public void setServer(Server server)
48      {
49          super.setServer(server);
50          this._server=server;
51      }
52  
53      /* ------------------------------------------------------------ */
54      public void clear()
55      {
56          _in.clear();
57          _out.clear();
58      }
59  
60      /* ------------------------------------------------------------ */
61      public void reopen()
62      {
63          _in.clear();
64          _out.clear();
65          _endp = new ByteArrayEndPoint();
66          _endp.setIn(_in);
67          _endp.setOut(_out);
68          _endp.setGrowOutput(true);
69          _accepting=false;
70      }
71  
72      /* ------------------------------------------------------------ */
73      public void doStart()
74          throws Exception
75      {   
76          _in=new ByteArrayBuffer(8192);
77          _out=new ByteArrayBuffer(8192);
78          _endp = new ByteArrayEndPoint();
79          _endp.setIn(_in);
80          _endp.setOut(_out);
81          _endp.setGrowOutput(true);
82          _accepting=false;
83          
84          super.doStart();
85      }
86  
87      /* ------------------------------------------------------------ */
88      public String getResponses(String requests)
89          throws Exception
90      {
91          return getResponses(requests,false);
92      }
93      
94      /* ------------------------------------------------------------ */
95      public String getResponses(String requests, boolean keepOpen)
96      throws Exception
97      {
98          // System.out.println("\nREQUESTS :\n"+requests);
99          // System.out.flush();
100         ByteArrayBuffer buf=new ByteArrayBuffer(requests,StringUtil.__ISO_8859_1);
101         if (_in.space()<buf.length())
102         {
103             ByteArrayBuffer n = new ByteArrayBuffer(_in.length()+buf.length());
104             n.put(_in);
105             _in=n;
106             _endp.setIn(_in);
107         }
108         _in.put(buf);
109         
110         synchronized (this)
111         {
112             _keepOpen=keepOpen;
113             _accepting=true;
114             this.notify();
115             
116             while(_accepting)
117                 this.wait();
118         }
119         
120         // System.err.println("\nRESPONSES:\n"+out);
121         _out=_endp.getOut();
122         return _out.toString(StringUtil.__ISO_8859_1);
123     }
124     
125     /* ------------------------------------------------------------ */
126     public ByteArrayBuffer getResponses(ByteArrayBuffer buf, boolean keepOpen)
127     throws Exception
128     {
129         if (_in.space()<buf.length())
130         {
131             ByteArrayBuffer n = new ByteArrayBuffer(_in.length()+buf.length());
132             n.put(_in);
133             _in=n;
134             _endp.setIn(_in);
135         }
136         _in.put(buf);
137         
138         synchronized (this)
139         {
140             _keepOpen=keepOpen;
141             _accepting=true;
142             this.notify();
143             
144             while(_accepting)
145                 this.wait();
146         }
147         
148         // System.err.println("\nRESPONSES:\n"+out);
149         _out=_endp.getOut();
150         return _out;
151     }
152 
153     /* ------------------------------------------------------------ */
154     protected Buffer newBuffer(int size)
155     {
156         return new ByteArrayBuffer(size);
157     }
158 
159     /* ------------------------------------------------------------ */
160     protected void accept(int acceptorID) throws IOException, InterruptedException
161     {
162         HttpConnection connection=null;
163         
164         while (isRunning())
165         {
166             synchronized (this)
167             {
168                 try
169                 {
170                     while(!_accepting)
171                         this.wait();
172                 }
173                 catch(InterruptedException e)
174                 {
175                     return;
176                 }
177             }
178             
179             try
180             {
181                 if (connection==null)
182                 {
183                     connection=new HttpConnection(this,_endp,getServer());
184                     connectionOpened(connection);
185                 }
186                 while (_in.length()>0)
187                     connection.handle();
188             }
189             finally
190             {
191                 if (!_keepOpen)
192                 {
193                     connectionClosed(connection);
194                     connection.destroy();
195                     connection=null;
196                 }
197                 synchronized (this)
198                 {
199                     _accepting=false;
200                     this.notify();
201                 }
202             }
203         }
204     }
205     
206 
207     public void open() throws IOException
208     {
209     }
210 
211     public void close() throws IOException
212     {
213     }
214 
215     /* ------------------------------------------------------------------------------- */
216     public int getLocalPort()
217     {
218         return -1;
219     }
220 
221     
222 }