KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > ajp > AjpProtocol


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.coyote.ajp;
19
20 import java.net.InetAddress JavaDoc;
21 import java.net.Socket JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.concurrent.Executor JavaDoc;
26
27 import javax.management.MBeanRegistration JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.apache.coyote.ActionCode;
32 import org.apache.coyote.ActionHook;
33 import org.apache.coyote.Adapter;
34 import org.apache.coyote.ProtocolHandler;
35 import org.apache.coyote.RequestGroupInfo;
36 import org.apache.coyote.RequestInfo;
37 import org.apache.tomcat.util.modeler.Registry;
38 import org.apache.tomcat.util.net.JIoEndpoint;
39 import org.apache.tomcat.util.net.JIoEndpoint.Handler;
40 import org.apache.tomcat.util.res.StringManager;
41
42
43 /**
44  * Abstract the protocol implementation, including threading, etc.
45  * Processor is single threaded and specific to stream-based protocols,
46  * will not fit Jk protocols like JNI.
47  *
48  * @author Remy Maucherat
49  * @author Costin Manolache
50  */

51 public class AjpProtocol
52     implements ProtocolHandler, MBeanRegistration JavaDoc {
53     
54     
55     protected static org.apache.commons.logging.Log log =
56         org.apache.commons.logging.LogFactory.getLog(AjpProtocol.class);
57
58     /**
59      * The string manager for this package.
60      */

61     protected static StringManager sm =
62         StringManager.getManager(Constants.Package);
63
64
65     // ------------------------------------------------------------ Constructor
66

67
68     public AjpProtocol() {
69         cHandler = new AjpConnectionHandler(this);
70         setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
71         setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
72         //setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
73
setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
74     }
75
76     
77     // ----------------------------------------------------- Instance Variables
78

79
80     protected ObjectName JavaDoc tpOname;
81     
82     
83     protected ObjectName JavaDoc rgOname;
84
85
86     /**
87      * Associated java.io endpoint.
88      */

89     protected JIoEndpoint ep = new JIoEndpoint();
90
91
92     /**
93      * Configuration attributes.
94      */

95     protected Hashtable JavaDoc attributes = new Hashtable JavaDoc();
96
97
98     /**
99      * Should authentication be done in the native webserver layer,
100      * or in the Servlet container ?
101      */

102     protected boolean tomcatAuthentication = true;
103
104
105     /**
106      * Required secret.
107      */

108     protected String JavaDoc requiredSecret = null;
109
110
111     /**
112      * AJP packet size.
113      */

114     protected int packetSize = Constants.MAX_PACKET_SIZE;
115
116     
117     /**
118      * Adapter which will process the requests recieved by this endpoint.
119      */

120     private Adapter adapter;
121     
122     
123     /**
124      * Connection handler for AJP.
125      */

126     private AjpConnectionHandler cHandler;
127
128
129     // --------------------------------------------------------- Public Methods
130

131
132     /**
133      * Pass config info
134      */

135     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
136         if (log.isTraceEnabled()) {
137             log.trace(sm.getString("ajpprotocol.setattribute", name, value));
138         }
139         attributes.put(name, value);
140     }
141
142     public Object JavaDoc getAttribute(String JavaDoc key) {
143         if (log.isTraceEnabled()) {
144             log.trace(sm.getString("ajpprotocol.getattribute", key));
145         }
146         return attributes.get(key);
147     }
148
149
150     public Iterator JavaDoc getAttributeNames() {
151         return attributes.keySet().iterator();
152     }
153
154
155     /**
156      * Set a property.
157      */

158     public void setProperty(String JavaDoc name, String JavaDoc value) {
159         setAttribute(name, value);
160     }
161
162
163     /**
164      * Get a property
165      */

166     public String JavaDoc getProperty(String JavaDoc name) {
167         return (String JavaDoc) getAttribute(name);
168     }
169
170
171     /**
172      * The adapter, used to call the connector
173      */

174     public void setAdapter(Adapter adapter) {
175         this.adapter = adapter;
176     }
177
178
179     public Adapter getAdapter() {
180         return adapter;
181     }
182
183
184     /** Start the protocol
185      */

186     public void init() throws Exception JavaDoc {
187         ep.setName(getName());
188         ep.setHandler(cHandler);
189
190         try {
191             ep.init();
192         } catch (Exception JavaDoc ex) {
193             log.error(sm.getString("ajpprotocol.endpoint.initerror"), ex);
194             throw ex;
195         }
196         if (log.isInfoEnabled()) {
197             log.info(sm.getString("ajpprotocol.init", getName()));
198         }
199     }
200
201
202     public void start() throws Exception JavaDoc {
203         if (this.domain != null ) {
204             try {
205                 tpOname = new ObjectName JavaDoc
206                     (domain + ":" + "type=ThreadPool,name=" + getName());
207                 Registry.getRegistry(null, null)
208                     .registerComponent(ep, tpOname, null );
209             } catch (Exception JavaDoc e) {
210                 log.error("Can't register threadpool" );
211             }
212             rgOname = new ObjectName JavaDoc
213                 (domain + ":type=GlobalRequestProcessor,name=" + getName());
214             Registry.getRegistry(null, null).registerComponent
215                 (cHandler.global, rgOname, null);
216         }
217
218         try {
219             ep.start();
220         } catch (Exception JavaDoc ex) {
221             log.error(sm.getString("ajpprotocol.endpoint.starterror"), ex);
222             throw ex;
223         }
224         if (log.isInfoEnabled())
225             log.info(sm.getString("ajpprotocol.start", getName()));
226     }
227
228     public void pause() throws Exception JavaDoc {
229         try {
230             ep.pause();
231         } catch (Exception JavaDoc ex) {
232             log.error(sm.getString("ajpprotocol.endpoint.pauseerror"), ex);
233             throw ex;
234         }
235         if (log.isInfoEnabled())
236             log.info(sm.getString("ajpprotocol.pause", getName()));
237     }
238
239     public void resume() throws Exception JavaDoc {
240         try {
241             ep.resume();
242         } catch (Exception JavaDoc ex) {
243             log.error(sm.getString("ajpprotocol.endpoint.resumeerror"), ex);
244             throw ex;
245         }
246         if (log.isInfoEnabled())
247             log.info(sm.getString("ajpprotocol.resume", getName()));
248     }
249
250     public void destroy() throws Exception JavaDoc {
251         if (log.isInfoEnabled())
252             log.info(sm.getString("ajpprotocol.stop", getName()));
253         ep.destroy();
254         if (tpOname!=null)
255             Registry.getRegistry(null, null).unregisterComponent(tpOname);
256         if (rgOname != null)
257             Registry.getRegistry(null, null).unregisterComponent(rgOname);
258     }
259
260     // *
261
public Executor JavaDoc getExecutor() {
262         return ep.getExecutor();
263     }
264     
265     // *
266
public void setExecutor(Executor JavaDoc executor) {
267         ep.setExecutor(executor);
268     }
269     
270     public int getMaxThreads() {
271         return ep.getMaxThreads();
272     }
273
274     public void setMaxThreads(int maxThreads) {
275         ep.setMaxThreads(maxThreads);
276         setAttribute("maxThreads", "" + maxThreads);
277     }
278
279     public void setThreadPriority(int threadPriority) {
280         ep.setThreadPriority(threadPriority);
281         setAttribute("threadPriority", "" + threadPriority);
282     }
283     
284     public int getThreadPriority() {
285         return ep.getThreadPriority();
286     }
287     
288
289     public int getBacklog() {
290         return ep.getBacklog();
291     }
292
293
294     public void setBacklog( int i ) {
295         ep.setBacklog(i);
296         setAttribute("backlog", "" + i);
297     }
298
299
300     public int getPort() {
301         return ep.getPort();
302     }
303
304
305     public void setPort( int port ) {
306         ep.setPort(port);
307         setAttribute("port", "" + port);
308     }
309
310
311     public InetAddress JavaDoc getAddress() {
312         return ep.getAddress();
313     }
314
315
316     public void setAddress(InetAddress JavaDoc ia) {
317         ep.setAddress(ia);
318         setAttribute("address", "" + ia);
319     }
320
321
322     public String JavaDoc getName() {
323         String JavaDoc encodedAddr = "";
324         if (getAddress() != null) {
325             encodedAddr = "" + getAddress();
326             if (encodedAddr.startsWith("/"))
327                 encodedAddr = encodedAddr.substring(1);
328             encodedAddr = URLEncoder.encode(encodedAddr) + "-";
329         }
330         return ("ajp-" + encodedAddr + ep.getPort());
331     }
332
333
334     public boolean getTcpNoDelay() {
335         return ep.getTcpNoDelay();
336     }
337
338
339     public void setTcpNoDelay(boolean b) {
340         ep.setTcpNoDelay(b);
341         setAttribute("tcpNoDelay", "" + b);
342     }
343
344
345     public boolean getTomcatAuthentication() {
346         return tomcatAuthentication;
347     }
348
349
350     public void setTomcatAuthentication(boolean tomcatAuthentication) {
351         this.tomcatAuthentication = tomcatAuthentication;
352     }
353
354
355     public int getSoLinger() {
356         return ep.getSoLinger();
357     }
358
359
360     public void setSoLinger(int i) {
361         ep.setSoLinger(i);
362         setAttribute("soLinger", "" + i);
363     }
364
365
366     public int getSoTimeout() {
367         return ep.getSoTimeout();
368     }
369
370
371     public void setSoTimeout( int i ) {
372         ep.setSoTimeout(i);
373         setAttribute("soTimeout", "" + i);
374     }
375
376     
377     public void setRequiredSecret(String JavaDoc requiredSecret) {
378         this.requiredSecret = requiredSecret;
379     }
380     
381     
382     public int getPacketSize() {
383         return packetSize;
384     }
385
386
387     public void setPacketSize(int i) {
388         packetSize = i;
389     }
390
391     
392     // -------------------------------------- AjpConnectionHandler Inner Class
393

394
395     protected static class AjpConnectionHandler implements Handler {
396         protected AjpProtocol proto;
397         protected static int count = 0;
398         protected RequestGroupInfo global=new RequestGroupInfo();
399         protected ThreadLocal JavaDoc<AjpProcessor> localProcessor = new ThreadLocal JavaDoc<AjpProcessor>();
400
401         public AjpConnectionHandler(AjpProtocol proto) {
402             this.proto = proto;
403         }
404
405         public boolean process(Socket JavaDoc socket) {
406             AjpProcessor processor = null;
407             try {
408                 processor = localProcessor.get();
409                 if (processor == null) {
410                     processor = new AjpProcessor(proto.packetSize, proto.ep);
411                     processor.setAdapter(proto.adapter);
412                     processor.setTomcatAuthentication(proto.tomcatAuthentication);
413                     processor.setRequiredSecret(proto.requiredSecret);
414                     localProcessor.set(processor);
415                     if (proto.getDomain() != null) {
416                         synchronized (this) {
417                             try {
418                                 RequestInfo rp = processor.getRequest().getRequestProcessor();
419                                 rp.setGlobalProcessor(global);
420                                 ObjectName JavaDoc rpName = new ObjectName JavaDoc
421                                     (proto.getDomain() + ":type=RequestProcessor,worker="
422                                         + proto.getName() + ",name=AjpRequest" + count++ );
423                                 Registry.getRegistry(null, null)
424                                     .registerComponent(rp, rpName, null);
425                             } catch (Exception JavaDoc ex) {
426                                 log.warn(sm.getString("ajpprotocol.request.register"));
427                             }
428                         }
429                     }
430                 }
431
432                 if (processor instanceof ActionHook) {
433                     ((ActionHook) processor).action(ActionCode.ACTION_START, null);
434                 }
435
436                 return processor.process(socket);
437
438             } catch(java.net.SocketException JavaDoc e) {
439                 // SocketExceptions are normal
440
AjpProtocol.log.debug
441                     (sm.getString
442                      ("ajpprotocol.proto.socketexception.debug"), e);
443             } catch (java.io.IOException JavaDoc e) {
444                 // IOExceptions are normal
445
AjpProtocol.log.debug
446                     (sm.getString
447                      ("ajpprotocol.proto.ioexception.debug"), e);
448             }
449             // Future developers: if you discover any other
450
// rare-but-nonfatal exceptions, catch them here, and log as
451
// above.
452
catch (Throwable JavaDoc e) {
453                 // any other exception or error is odd. Here we log it
454
// with "ERROR" level, so it will show up even on
455
// less-than-verbose logs.
456
AjpProtocol.log.error
457                     (sm.getString("ajpprotocol.proto.error"), e);
458             } finally {
459                 if (processor instanceof ActionHook) {
460                     ((ActionHook) processor).action(ActionCode.ACTION_STOP, null);
461                 }
462             }
463             return false;
464         }
465     }
466
467
468     // -------------------- Various implementation classes --------------------
469

470
471     protected String JavaDoc domain;
472     protected ObjectName JavaDoc oname;
473     protected MBeanServer JavaDoc mserver;
474
475     public ObjectName JavaDoc getObjectName() {
476         return oname;
477     }
478
479     public String JavaDoc getDomain() {
480         return domain;
481     }
482
483     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server,
484                                   ObjectName JavaDoc name) throws Exception JavaDoc {
485         oname=name;
486         mserver=server;
487         domain=name.getDomain();
488         return name;
489     }
490
491     public void postRegister(Boolean JavaDoc registrationDone) {
492     }
493
494     public void preDeregister() throws Exception JavaDoc {
495     }
496
497     public void postDeregister() {
498     }
499     
500  
501 }
502
Popular Tags