KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > ajp > AJP13Listener


1 // ========================================================================
2
// $Id: AJP13Listener.java,v 1.20 2006/10/08 14:13:05 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.ajp;
17
18 import java.io.IOException JavaDoc;
19 import java.net.InetAddress JavaDoc;
20 import java.net.Socket JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.mortbay.log.LogFactory;
24 import org.mortbay.http.HttpConnection;
25 import org.mortbay.http.HttpHandler;
26 import org.mortbay.http.HttpListener;
27 import org.mortbay.http.HttpMessage;
28 import org.mortbay.http.HttpRequest;
29 import org.mortbay.http.HttpServer;
30 import org.mortbay.util.InetAddrPort;
31 import org.mortbay.util.ThreadedServer;
32
33 /* ------------------------------------------------------------ */
34 /**
35  * AJP 1.3 Protocol Listener. This listener takes requests from the mod_jk or
36  * mod_jk2 modules used by web servers such as apache and IIS to forward
37  * requests to a servlet container.
38  * <p>
39  * This code uses the AJP13 code from tomcat3.3 as the protocol specification,
40  * but is new implementation.
41  *
42  * @version $Id: AJP13Listener.java,v 1.20 2006/10/08 14:13:05 gregwilkins Exp $
43  * @author Greg Wilkins (gregw)
44  */

45 public class AJP13Listener extends ThreadedServer implements HttpListener
46 {
47     private static Log log=LogFactory.getLog(AJP13Listener.class);
48
49     /* ------------------------------------------------------------------- */
50     private HttpServer _server;
51     private boolean _lastOut=false;
52     private boolean _lastLow=false;
53     private String JavaDoc _integralScheme=HttpMessage.__SSL_SCHEME;
54     private String JavaDoc _confidentialScheme=HttpMessage.__SSL_SCHEME;
55     private int _integralPort=0;
56     private int _confidentialPort=0;
57     private boolean _identifyListener=false;
58     private int _bufferSize=8192;
59     private int _bufferReserve=512;
60     private String JavaDoc[] _remoteServers;
61     private HttpHandler _handler;
62
63     /* ------------------------------------------------------------------- */
64     public AJP13Listener()
65     {
66     }
67
68     /* ------------------------------------------------------------------- */
69     public AJP13Listener(InetAddrPort address)
70     {
71         super(address);
72     }
73
74     /* ------------------------------------------------------------ */
75     public void setHttpServer(HttpServer server)
76     {
77         _server=server;
78     }
79
80     /* ------------------------------------------------------------ */
81     public HttpServer getHttpServer()
82     {
83         return _server;
84     }
85
86     /* ------------------------------------------------------------ */
87     public int getBufferSize()
88     {
89         return _bufferSize;
90     }
91
92     /* ------------------------------------------------------------ */
93     public void setBufferSize(int size)
94     {
95         _bufferSize=size;
96         if (_bufferSize>8192)
97             log.warn("AJP Data buffer > 8192: "+size);
98     }
99
100     /* ------------------------------------------------------------ */
101     public int getBufferReserve()
102     {
103         return _bufferReserve;
104     }
105
106     /* ------------------------------------------------------------ */
107     public void setBufferReserve(int size)
108     {
109         _bufferReserve=size;
110     }
111
112     /* ------------------------------------------------------------ */
113     public boolean getIdentifyListener()
114     {
115         return _identifyListener;
116     }
117
118     /* ------------------------------------------------------------ */
119     /**
120      * @param identifyListener
121      * If true, the listener name is added to all requests as the
122      * org.mortbay.http.HttListener attribute
123      */

124     public void setIdentifyListener(boolean identifyListener)
125     {
126         _identifyListener=identifyListener;
127     }
128
129     /* --------------------------------------------------------------- */
130     public String JavaDoc getDefaultScheme()
131     {
132         return HttpMessage.__SCHEME;
133     }
134
135     /* --------------------------------------------------------------- */
136     public void start() throws Exception JavaDoc
137     {
138         super.start();
139         log.info("Started AJP13Listener on "+getInetAddrPort());
140         log.info("NOTICE: AJP13 is not a secure protocol. Please protect the port "+getInetAddrPort());
141     }
142
143     /* --------------------------------------------------------------- */
144     public void stop() throws InterruptedException JavaDoc
145     {
146         super.stop();
147         log.info("Stopped AJP13Listener on "+getInetAddrPort());
148     }
149
150     /* ------------------------------------------------------------ */
151     /**
152      * @return Array of accepted remote server hostnames or IPs.
153      */

154     public String JavaDoc[] getRemoteServers()
155     {
156         return _remoteServers;
157     }
158
159     /* ------------------------------------------------------------ */
160     /**
161      * Set accepted remote servers. The AJP13 protocol is not secure and
162      * contains no authentication. If remote servers are set, then this listener
163      * will only accept connections from hosts with matching addresses or
164      * hostnames.
165      *
166      * @param servers
167      * Array of accepted remote server hostnames or IPs
168      */

169     public void setRemoteServers(String JavaDoc[] servers)
170     {
171         _remoteServers=servers;
172     }
173
174     /* ------------------------------------------------------------ */
175     /**
176      * Handle Job. Implementation of ThreadPool.handle(), calls
177      * handleConnection.
178      *
179      * @param socket
180      * A Connection.
181      */

182     public void handleConnection(Socket JavaDoc socket) throws IOException JavaDoc
183     {
184         // Check acceptable remote servers
185
if (_remoteServers!=null&&_remoteServers.length>0)
186         {
187             boolean match=false;
188             InetAddress JavaDoc inetAddress=socket.getInetAddress();
189             String JavaDoc hostAddr=inetAddress.getHostAddress();
190             String JavaDoc hostName=inetAddress.getHostName();
191             for (int i=0; i<_remoteServers.length; i++)
192             {
193                 if (hostName.equals(_remoteServers[i])||hostAddr.equals(_remoteServers[i]))
194                 {
195                     match=true;
196                     break;
197                 }
198             }
199             if (!match)
200             {
201                 log.warn("AJP13 Connection from un-approved host: "+inetAddress);
202                 return;
203             }
204         }
205
206         // Handle the connection
207
socket.setTcpNoDelay(true);
208         socket.setSoTimeout(getMaxIdleTimeMs());
209         AJP13Connection connection=createConnection(socket);
210         try
211         {
212             connection.handle();
213         }
214         finally
215         {
216             connection.destroy();
217         }
218     }
219
220     /* ------------------------------------------------------------ */
221     /**
222      * Create an AJP13Connection instance. This method can be used to override
223      * the connection instance.
224      *
225      * @param socket
226      * The underlying socket.
227      */

228     protected AJP13Connection createConnection(Socket JavaDoc socket) throws IOException JavaDoc
229     {
230         return new AJP13Connection(this,socket.getInputStream(),socket.getOutputStream(),socket,getBufferSize());
231     }
232
233     /* ------------------------------------------------------------ */
234     /**
235      * Customize the request from connection. This method extracts the socket
236      * from the connection and calls the customizeRequest(Socket,HttpRequest)
237      * method.
238      *
239      * @param request
240      */

241     public void customizeRequest(HttpConnection connection, HttpRequest request)
242     {
243         if (_identifyListener)
244             request.setAttribute(HttpListener.ATTRIBUTE,getName());
245
246         Socket JavaDoc socket=(Socket JavaDoc)(connection.getConnection());
247         customizeRequest(socket,request);
248     }
249
250     /* ------------------------------------------------------------ */
251     /**
252      * Customize request from socket. Derived versions of SocketListener may
253      * specialize this method to customize the request with attributes of the
254      * socket used (eg SSL session ids).
255      *
256      * @param request
257      */

258     protected void customizeRequest(Socket JavaDoc socket, HttpRequest request)
259     {
260     }
261
262     /* ------------------------------------------------------------ */
263     /**
264      * Persist the connection.
265      *
266      * @param connection
267      */

268     public void persistConnection(HttpConnection connection)
269     {
270     }
271
272     /* ------------------------------------------------------------ */
273     /**
274      * @return True if low on idle threads.
275      */

276     public boolean isLowOnResources()
277     {
278         boolean low=getThreads()==getMaxThreads()&&getIdleThreads()<getMinThreads();
279         if (low&&!_lastLow)
280             log.info("LOW ON THREADS: "+this);
281         else if (!low&&_lastLow)
282         {
283             log.info("OK on threads: "+this);
284             _lastOut=false;
285         }
286         _lastLow=low;
287         return low;
288     }
289
290     /* ------------------------------------------------------------ */
291     /**
292      * @return True if out of resources.
293      */

294     public boolean isOutOfResources()
295     {
296         boolean out=getThreads()==getMaxThreads()&&getIdleThreads()==0;
297         if (out&&!_lastOut)
298             log.warn("OUT OF THREADS: "+this);
299
300         _lastOut=out;
301         return out;
302     }
303
304     /* ------------------------------------------------------------ */
305     public boolean isIntegral(HttpConnection connection)
306     {
307         return ((AJP13Connection)connection).isSSL();
308     }
309
310     /* ------------------------------------------------------------ */
311     public boolean isConfidential(HttpConnection connection)
312     {
313         return ((AJP13Connection)connection).isSSL();
314     }
315
316     /* ------------------------------------------------------------ */
317     public String JavaDoc getIntegralScheme()
318     {
319         return _integralScheme;
320     }
321
322     /* ------------------------------------------------------------ */
323     public void setIntegralScheme(String JavaDoc integralScheme)
324     {
325         _integralScheme=integralScheme;
326     }
327
328     /* ------------------------------------------------------------ */
329     public int getIntegralPort()
330     {
331         return _integralPort;
332     }
333
334     /* ------------------------------------------------------------ */
335     public void setIntegralPort(int integralPort)
336     {
337         _integralPort=integralPort;
338     }
339
340     /* ------------------------------------------------------------ */
341     public String JavaDoc getConfidentialScheme()
342     {
343         return _confidentialScheme;
344     }
345
346     /* ------------------------------------------------------------ */
347     public void setConfidentialScheme(String JavaDoc confidentialScheme)
348     {
349         _confidentialScheme=confidentialScheme;
350     }
351
352     /* ------------------------------------------------------------ */
353     public int getConfidentialPort()
354     {
355         return _confidentialPort;
356     }
357
358     /* ------------------------------------------------------------ */
359     public void setConfidentialPort(int confidentialPort)
360     {
361         _confidentialPort=confidentialPort;
362     }
363
364     /* ------------------------------------------------------------ */
365     public HttpHandler getHttpHandler()
366     {
367         return _handler;
368     }
369
370     /* ------------------------------------------------------------ */
371     public void setHttpHandler(HttpHandler handler)
372     {
373         _handler=handler;
374     }
375 }
376
Popular Tags