1 17 18 package org.apache.coyote.ajp; 19 20 import java.net.InetAddress ; 21 import java.net.Socket ; 22 import java.net.URLEncoder ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.concurrent.Executor ; 26 27 import javax.management.MBeanRegistration ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 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 51 public class AjpProtocol 52 implements ProtocolHandler, MBeanRegistration { 53 54 55 protected static org.apache.commons.logging.Log log = 56 org.apache.commons.logging.LogFactory.getLog(AjpProtocol.class); 57 58 61 protected static StringManager sm = 62 StringManager.getManager(Constants.Package); 63 64 65 67 68 public AjpProtocol() { 69 cHandler = new AjpConnectionHandler(this); 70 setSoLinger(Constants.DEFAULT_CONNECTION_LINGER); 71 setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT); 72 setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY); 74 } 75 76 77 79 80 protected ObjectName tpOname; 81 82 83 protected ObjectName rgOname; 84 85 86 89 protected JIoEndpoint ep = new JIoEndpoint(); 90 91 92 95 protected Hashtable attributes = new Hashtable (); 96 97 98 102 protected boolean tomcatAuthentication = true; 103 104 105 108 protected String requiredSecret = null; 109 110 111 114 protected int packetSize = Constants.MAX_PACKET_SIZE; 115 116 117 120 private Adapter adapter; 121 122 123 126 private AjpConnectionHandler cHandler; 127 128 129 131 132 135 public void setAttribute(String name, Object 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 getAttribute(String 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 getAttributeNames() { 151 return attributes.keySet().iterator(); 152 } 153 154 155 158 public void setProperty(String name, String value) { 159 setAttribute(name, value); 160 } 161 162 163 166 public String getProperty(String name) { 167 return (String ) getAttribute(name); 168 } 169 170 171 174 public void setAdapter(Adapter adapter) { 175 this.adapter = adapter; 176 } 177 178 179 public Adapter getAdapter() { 180 return adapter; 181 } 182 183 184 186 public void init() throws Exception { 187 ep.setName(getName()); 188 ep.setHandler(cHandler); 189 190 try { 191 ep.init(); 192 } catch (Exception 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 { 203 if (this.domain != null ) { 204 try { 205 tpOname = new ObjectName 206 (domain + ":" + "type=ThreadPool,name=" + getName()); 207 Registry.getRegistry(null, null) 208 .registerComponent(ep, tpOname, null ); 209 } catch (Exception e) { 210 log.error("Can't register threadpool" ); 211 } 212 rgOname = new ObjectName 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 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 { 229 try { 230 ep.pause(); 231 } catch (Exception 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 { 240 try { 241 ep.resume(); 242 } catch (Exception 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 { 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 public Executor getExecutor() { 262 return ep.getExecutor(); 263 } 264 265 public void setExecutor(Executor 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 getAddress() { 312 return ep.getAddress(); 313 } 314 315 316 public void setAddress(InetAddress ia) { 317 ep.setAddress(ia); 318 setAttribute("address", "" + ia); 319 } 320 321 322 public String getName() { 323 String 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 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 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 <AjpProcessor> localProcessor = new ThreadLocal <AjpProcessor>(); 400 401 public AjpConnectionHandler(AjpProtocol proto) { 402 this.proto = proto; 403 } 404 405 public boolean process(Socket 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 rpName = new ObjectName 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 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 e) { 439 AjpProtocol.log.debug 441 (sm.getString 442 ("ajpprotocol.proto.socketexception.debug"), e); 443 } catch (java.io.IOException e) { 444 AjpProtocol.log.debug 446 (sm.getString 447 ("ajpprotocol.proto.ioexception.debug"), e); 448 } 449 catch (Throwable e) { 453 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 470 471 protected String domain; 472 protected ObjectName oname; 473 protected MBeanServer mserver; 474 475 public ObjectName getObjectName() { 476 return oname; 477 } 478 479 public String getDomain() { 480 return domain; 481 } 482 483 public ObjectName preRegister(MBeanServer server, 484 ObjectName name) throws Exception { 485 oname=name; 486 mserver=server; 487 domain=name.getDomain(); 488 return name; 489 } 490 491 public void postRegister(Boolean registrationDone) { 492 } 493 494 public void preDeregister() throws Exception { 495 } 496 497 public void postDeregister() { 498 } 499 500 501 } 502 | Popular Tags |