Source code: org/apache/coyote/http11/Http11Protocol.java
1 /*
2 * Copyright 1999-2004 The Apache Software Foundation
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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.coyote.http11;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.InetAddress;
23 import java.net.Socket;
24 import java.net.URLEncoder;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28
29 import javax.management.MBeanRegistration;
30 import javax.management.MBeanServer;
31 import javax.management.ObjectName;
32
33 import org.apache.commons.modeler.Registry;
34 import org.apache.coyote.ActionCode;
35 import org.apache.coyote.ActionHook;
36 import org.apache.coyote.Adapter;
37 import org.apache.coyote.ProtocolHandler;
38 import org.apache.coyote.RequestGroupInfo;
39 import org.apache.coyote.RequestInfo;
40 import org.apache.tomcat.util.net.PoolTcpEndpoint;
41 import org.apache.tomcat.util.net.SSLImplementation;
42 import org.apache.tomcat.util.net.SSLSupport;
43 import org.apache.tomcat.util.net.ServerSocketFactory;
44 import org.apache.tomcat.util.net.TcpConnection;
45 import org.apache.tomcat.util.net.TcpConnectionHandler;
46 import org.apache.tomcat.util.res.StringManager;
47 import org.apache.tomcat.util.threads.ThreadPool;
48 import org.apache.tomcat.util.threads.ThreadWithAttributes;
49
50
51 /**
52 * Abstract the protocol implementation, including threading, etc.
53 * Processor is single threaded and specific to stream-based protocols,
54 * will not fit Jk protocols like JNI.
55 *
56 * @author Remy Maucherat
57 * @author Costin Manolache
58 */
59 public class Http11Protocol extends Http11BaseProtocol implements MBeanRegistration
60 {
61 public Http11Protocol() {
62 }
63
64 protected Http11ConnectionHandler createConnectionHandler() {
65 Http11ConnectionHandler cHandler = new JmxHttp11ConnectionHandler( this );
66 setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
67 setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
68 setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
69 setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
70 return cHandler ;
71 }
72
73 ObjectName tpOname;
74 ObjectName rgOname;
75
76 public void start() throws Exception {
77 if( this.domain != null ) {
78 try {
79 // XXX We should be able to configure it separately
80 // XXX It should be possible to use a single TP
81 tpOname=new ObjectName
82 (domain + ":" + "type=ThreadPool,name=" + getName());
83 if ("ms".equals(getStrategy())) {
84 Registry.getRegistry(null, null)
85 .registerComponent(ep, tpOname, null );
86 } else {
87 Registry.getRegistry(null, null)
88 .registerComponent(tp, tpOname, null );
89 }
90 tp.setName(getName());
91 tp.setDaemon(false);
92 tp.addThreadPoolListener(new MXPoolListener(this, tp));
93 } catch (Exception e) {
94 log.error("Can't register threadpool" );
95 }
96 rgOname=new ObjectName
97 (domain + ":type=GlobalRequestProcessor,name=" + getName());
98 Registry.getRegistry(null, null).registerComponent
99 ( cHandler.global, rgOname, null );
100 }
101
102 super.start();
103 }
104
105 public void destroy() throws Exception {
106 super.destroy();
107 if( tpOname!=null )
108 Registry.getRegistry(null, null).unregisterComponent(tpOname);
109 if( rgOname != null )
110 Registry.getRegistry(null, null).unregisterComponent(rgOname);
111 }
112
113 // -------------------- Connection handler --------------------
114
115 static class MXPoolListener implements ThreadPool.ThreadPoolListener {
116 MXPoolListener( Http11Protocol proto, ThreadPool control ) {
117
118 }
119
120 public void threadStart(ThreadPool tp, Thread t) {
121 }
122
123 public void threadEnd(ThreadPool tp, Thread t) {
124 // Register our associated processor
125 // TP uses only TWA
126 ThreadWithAttributes ta=(ThreadWithAttributes)t;
127 Object tpData[]=ta.getThreadData(tp);
128 if( tpData==null ) return;
129 // Weird artifact - it should be cleaned up, but that may break something
130 // and it won't gain us too much
131 if( tpData[1] instanceof Object[] ) {
132 tpData=(Object [])tpData[1];
133 }
134 ObjectName oname=(ObjectName)tpData[Http11BaseProtocol.THREAD_DATA_OBJECT_NAME];
135 if( oname==null ) return;
136 Registry.getRegistry(null, null).unregisterComponent(oname);
137 Http11Processor processor =
138 (Http11Processor) tpData[Http11Protocol.THREAD_DATA_PROCESSOR];
139 RequestInfo rp=processor.getRequest().getRequestProcessor();
140 rp.setGlobalProcessor(null);
141 }
142 }
143
144 static class JmxHttp11ConnectionHandler extends Http11ConnectionHandler {
145 Http11Protocol proto;
146 static int count=0;
147 RequestGroupInfo global=new RequestGroupInfo();
148
149 JmxHttp11ConnectionHandler( Http11Protocol proto ) {
150 super(proto);
151 this.proto = proto ;
152 }
153
154 public void setAttribute( String name, Object value ) {
155 }
156
157 public void setServer( Object o ) {
158 }
159
160 public Object[] init() {
161
162 Object thData[]=super.init();
163
164 // was set up by supper
165 Http11Processor processor = (Http11Processor)
166 thData[ Http11BaseProtocol.THREAD_DATA_PROCESSOR];
167
168 if( proto.getDomain() != null ) {
169 try {
170 RequestInfo rp=processor.getRequest().getRequestProcessor();
171 rp.setGlobalProcessor(global);
172 ObjectName rpName=new ObjectName
173 (proto.getDomain() + ":type=RequestProcessor,worker="
174 + proto.getName() +",name=HttpRequest" + count++ );
175 Registry.getRegistry(null, null).registerComponent( rp, rpName, null);
176 thData[Http11BaseProtocol.THREAD_DATA_OBJECT_NAME]=rpName;
177 } catch( Exception ex ) {
178 log.warn("Error registering request");
179 }
180 }
181
182 return thData;
183 }
184 }
185
186 // -------------------- Various implementation classes --------------------
187
188
189 protected String domain;
190 protected ObjectName oname;
191 protected MBeanServer mserver;
192
193 public ObjectName getObjectName() {
194 return oname;
195 }
196
197 public String getDomain() {
198 return domain;
199 }
200
201 public ObjectName preRegister(MBeanServer server,
202 ObjectName name) throws Exception {
203 oname=name;
204 mserver=server;
205 domain=name.getDomain();
206 return name;
207 }
208
209 public void postRegister(Boolean registrationDone) {
210 }
211
212 public void preDeregister() throws Exception {
213 }
214
215 public void postDeregister() {
216 }
217
218 }