1 17 18 package org.apache.coyote.ajp; 19 20 import java.net.InetAddress ; 21 import java.net.URLEncoder ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.concurrent.Executor ; 25 26 import javax.management.MBeanRegistration ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 30 import org.apache.coyote.ActionCode; 31 import org.apache.coyote.ActionHook; 32 import org.apache.coyote.Adapter; 33 import org.apache.coyote.ProtocolHandler; 34 import org.apache.coyote.RequestGroupInfo; 35 import org.apache.coyote.RequestInfo; 36 import org.apache.tomcat.util.modeler.Registry; 37 import org.apache.tomcat.util.net.AprEndpoint; 38 import org.apache.tomcat.util.net.SocketStatus; 39 import org.apache.tomcat.util.net.AprEndpoint.Handler; 40 import org.apache.tomcat.util.res.StringManager; 41 42 43 51 public class AjpAprProtocol 52 implements ProtocolHandler, MBeanRegistration { 53 54 55 protected static org.apache.commons.logging.Log log = 56 org.apache.commons.logging.LogFactory.getLog(AjpAprProtocol.class); 57 58 61 protected static StringManager sm = 62 StringManager.getManager(Constants.Package); 63 64 65 67 68 public AjpAprProtocol() { 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 AprEndpoint ep = new AprEndpoint(); 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 ep.setUseSendfile(false); 190 191 try { 192 ep.init(); 193 } catch (Exception ex) { 194 log.error(sm.getString("ajpprotocol.endpoint.initerror"), ex); 195 throw ex; 196 } 197 if (log.isInfoEnabled()) { 198 log.info(sm.getString("ajpprotocol.init", getName())); 199 } 200 } 201 202 203 public void start() throws Exception { 204 if (this.domain != null ) { 205 try { 206 tpOname = new ObjectName 207 (domain + ":" + "type=ThreadPool,name=" + getName()); 208 Registry.getRegistry(null, null) 209 .registerComponent(ep, tpOname, null ); 210 } catch (Exception e) { 211 log.error("Can't register threadpool" ); 212 } 213 rgOname = new ObjectName 214 (domain + ":type=GlobalRequestProcessor,name=" + getName()); 215 Registry.getRegistry(null, null).registerComponent 216 (cHandler.global, rgOname, null); 217 } 218 219 try { 220 ep.start(); 221 } catch (Exception ex) { 222 log.error(sm.getString("ajpprotocol.endpoint.starterror"), ex); 223 throw ex; 224 } 225 if (log.isInfoEnabled()) 226 log.info(sm.getString("ajpprotocol.start", getName())); 227 } 228 229 public void pause() throws Exception { 230 try { 231 ep.pause(); 232 } catch (Exception ex) { 233 log.error(sm.getString("ajpprotocol.endpoint.pauseerror"), ex); 234 throw ex; 235 } 236 if (log.isInfoEnabled()) 237 log.info(sm.getString("ajpprotocol.pause", getName())); 238 } 239 240 public void resume() throws Exception { 241 try { 242 ep.resume(); 243 } catch (Exception ex) { 244 log.error(sm.getString("ajpprotocol.endpoint.resumeerror"), ex); 245 throw ex; 246 } 247 if (log.isInfoEnabled()) 248 log.info(sm.getString("ajpprotocol.resume", getName())); 249 } 250 251 public void destroy() throws Exception { 252 if (log.isInfoEnabled()) 253 log.info(sm.getString("ajpprotocol.stop", getName())); 254 ep.destroy(); 255 if (tpOname!=null) 256 Registry.getRegistry(null, null).unregisterComponent(tpOname); 257 if (rgOname != null) 258 Registry.getRegistry(null, null).unregisterComponent(rgOname); 259 } 260 261 262 public Executor getExecutor() { 264 return ep.getExecutor(); 265 } 266 267 public void setExecutor(Executor executor) { 269 ep.setExecutor(executor); 270 } 271 272 public int getMaxThreads() { 273 return ep.getMaxThreads(); 274 } 275 276 public void setMaxThreads(int maxThreads) { 277 ep.setMaxThreads(maxThreads); 278 setAttribute("maxThreads", "" + maxThreads); 279 } 280 281 public void setThreadPriority(int threadPriority) { 282 ep.setThreadPriority(threadPriority); 283 setAttribute("threadPriority", "" + threadPriority); 284 } 285 286 public int getThreadPriority() { 287 return ep.getThreadPriority(); 288 } 289 290 291 public int getBacklog() { 292 return ep.getBacklog(); 293 } 294 295 296 public void setBacklog( int i ) { 297 ep.setBacklog(i); 298 setAttribute("backlog", "" + i); 299 } 300 301 302 public int getPort() { 303 return ep.getPort(); 304 } 305 306 307 public void setPort( int port ) { 308 ep.setPort(port); 309 setAttribute("port", "" + port); 310 } 311 312 313 public boolean getUseSendfile() { 314 return ep.getUseSendfile(); 315 } 316 317 318 public void setUseSendfile(boolean useSendfile) { 319 } 321 322 323 public InetAddress getAddress() { 324 return ep.getAddress(); 325 } 326 327 328 public void setAddress(InetAddress ia) { 329 ep.setAddress(ia); 330 setAttribute("address", "" + ia); 331 } 332 333 334 public String getName() { 335 String encodedAddr = ""; 336 if (getAddress() != null) { 337 encodedAddr = "" + getAddress(); 338 if (encodedAddr.startsWith("/")) 339 encodedAddr = encodedAddr.substring(1); 340 encodedAddr = URLEncoder.encode(encodedAddr) + "-"; 341 } 342 return ("ajp-" + encodedAddr + ep.getPort()); 343 } 344 345 346 public boolean getTcpNoDelay() { 347 return ep.getTcpNoDelay(); 348 } 349 350 351 public void setTcpNoDelay(boolean b) { 352 ep.setTcpNoDelay(b); 353 setAttribute("tcpNoDelay", "" + b); 354 } 355 356 357 public boolean getTomcatAuthentication() { 358 return tomcatAuthentication; 359 } 360 361 362 public void setTomcatAuthentication(boolean tomcatAuthentication) { 363 this.tomcatAuthentication = tomcatAuthentication; 364 } 365 366 367 public int getFirstReadTimeout() { 368 return ep.getFirstReadTimeout(); 369 } 370 371 372 public void setFirstReadTimeout(int i) { 373 ep.setFirstReadTimeout(i); 374 setAttribute("firstReadTimeout", "" + i); 375 } 376 377 378 public int getPollTime() { 379 return ep.getPollTime(); 380 } 381 382 383 public void setPollTime(int i) { 384 ep.setPollTime(i); 385 setAttribute("pollTime", "" + i); 386 } 387 388 389 public void setPollerSize(int i) { 390 ep.setPollerSize(i); 391 setAttribute("pollerSize", "" + i); 392 } 393 394 395 public int getPollerSize() { 396 return ep.getPollerSize(); 397 } 398 399 400 public int getSoLinger() { 401 return ep.getSoLinger(); 402 } 403 404 405 public void setSoLinger(int i) { 406 ep.setSoLinger(i); 407 setAttribute("soLinger", "" + i); 408 } 409 410 411 public int getSoTimeout() { 412 return ep.getSoTimeout(); 413 } 414 415 416 public void setSoTimeout( int i ) { 417 ep.setSoTimeout(i); 418 setAttribute("soTimeout", "" + i); 419 } 420 421 422 public void setRequiredSecret(String requiredSecret) { 423 this.requiredSecret = requiredSecret; 424 } 425 426 427 public int getPacketSize() { 428 return packetSize; 429 } 430 431 432 public void setPacketSize(int i) { 433 packetSize = i; 434 } 435 436 437 439 440 protected static class AjpConnectionHandler implements Handler { 441 protected AjpAprProtocol proto; 442 protected static int count = 0; 443 protected RequestGroupInfo global=new RequestGroupInfo(); 444 protected ThreadLocal <AjpAprProcessor> localProcessor = new ThreadLocal <AjpAprProcessor>(); 445 446 public AjpConnectionHandler(AjpAprProtocol proto) { 447 this.proto = proto; 448 } 449 450 public SocketState event(long socket, SocketStatus status) { 452 return SocketState.CLOSED; 453 } 454 455 public SocketState process(long socket) { 456 AjpAprProcessor processor = null; 457 try { 458 processor = localProcessor.get(); 459 if (processor == null) { 460 processor = new AjpAprProcessor(proto.packetSize, proto.ep); 461 processor.setAdapter(proto.adapter); 462 processor.setTomcatAuthentication(proto.tomcatAuthentication); 463 processor.setRequiredSecret(proto.requiredSecret); 464 localProcessor.set(processor); 465 if (proto.getDomain() != null) { 466 synchronized (this) { 467 try { 468 RequestInfo rp = processor.getRequest().getRequestProcessor(); 469 rp.setGlobalProcessor(global); 470 ObjectName rpName = new ObjectName 471 (proto.getDomain() + ":type=RequestProcessor,worker=" 472 + proto.getName() + ",name=AjpRequest" + count++ ); 473 Registry.getRegistry(null, null) 474 .registerComponent(rp, rpName, null); 475 } catch (Exception ex) { 476 log.warn(sm.getString("ajpprotocol.request.register")); 477 } 478 } 479 } 480 } 481 482 if (processor instanceof ActionHook) { 483 ((ActionHook) processor).action(ActionCode.ACTION_START, null); 484 } 485 486 if (processor.process(socket)) { 487 return SocketState.OPEN; 488 } else { 489 return SocketState.CLOSED; 490 } 491 492 } catch(java.net.SocketException e) { 493 AjpAprProtocol.log.debug 495 (sm.getString 496 ("ajpprotocol.proto.socketexception.debug"), e); 497 } catch (java.io.IOException e) { 498 AjpAprProtocol.log.debug 500 (sm.getString 501 ("ajpprotocol.proto.ioexception.debug"), e); 502 } 503 catch (Throwable e) { 507 AjpAprProtocol.log.error 511 (sm.getString("ajpprotocol.proto.error"), e); 512 } finally { 513 if (processor instanceof ActionHook) { 514 ((ActionHook) processor).action(ActionCode.ACTION_STOP, null); 515 } 516 } 517 return SocketState.CLOSED; 518 } 519 } 520 521 522 524 525 protected String domain; 526 protected ObjectName oname; 527 protected MBeanServer mserver; 528 529 public ObjectName getObjectName() { 530 return oname; 531 } 532 533 public String getDomain() { 534 return domain; 535 } 536 537 public ObjectName preRegister(MBeanServer server, 538 ObjectName name) throws Exception { 539 oname=name; 540 mserver=server; 541 domain=name.getDomain(); 542 return name; 543 } 544 545 public void postRegister(Boolean registrationDone) { 546 } 547 548 public void preDeregister() throws Exception { 549 } 550 551 public void postDeregister() { 552 } 553 554 555 } 556 | Popular Tags |