View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server;
15  
16  import java.io.IOException;
17  
18  import org.eclipse.jetty.io.Buffer;
19  import org.eclipse.jetty.io.ByteArrayBuffer;
20  import org.eclipse.jetty.io.ByteArrayEndPoint;
21  import org.eclipse.jetty.util.StringUtil;
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,StringUtil.__ISO_8859_1);
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(StringUtil.__ISO_8859_1);
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 void accept(int acceptorID) throws IOException, InterruptedException
154     {
155         HttpConnection connection=null;
156         
157         while (isRunning())
158         {
159             synchronized (this)
160             {
161                 try
162                 {
163                     while(!_accepting)
164                         this.wait();
165                 }
166                 catch(InterruptedException e)
167                 {
168                     return;
169                 }
170             }
171             
172             try
173             {
174                 if (connection==null)
175                 {
176                     connection=new HttpConnection(this,_endp,getServer());
177                     connectionOpened(connection);
178                 }
179                 while (_in.length()>0)
180                     connection.handle();
181             }
182             finally
183             {
184                 if (!_keepOpen)
185                 {
186                     connectionClosed(connection);
187                     connection=null;
188                 }
189                 synchronized (this)
190                 {
191                     _accepting=false;
192                     this.notify();
193                 }
194             }
195         }
196     }
197     
198 
199     public void open() throws IOException
200     {
201     }
202 
203     public void close() throws IOException
204     {
205     }
206 
207     /* ------------------------------------------------------------------------------- */
208     public int getLocalPort()
209     {
210         return -1;
211     }
212 
213     
214 }