1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.InetAddress ; 27 import java.net.ServerSocket ; 28 import java.net.Socket ; 29 import java.security.AccessControlException ; 30 import java.util.Random ; 31 32 import javax.management.MBeanRegistration ; 33 import javax.management.MBeanServer ; 34 import javax.management.ObjectName ; 35 36 import org.apache.catalina.Context; 37 import org.apache.catalina.Lifecycle; 38 import org.apache.catalina.LifecycleException; 39 import org.apache.catalina.LifecycleListener; 40 import org.apache.catalina.Server; 41 import org.apache.catalina.ServerFactory; 42 import org.apache.catalina.Service; 43 import org.apache.catalina.deploy.NamingResources; 44 import org.apache.catalina.util.LifecycleSupport; 45 import org.apache.catalina.util.StringManager; 46 import org.apache.catalina.util.ServerInfo; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.apache.tomcat.util.buf.StringCache; 50 import org.apache.tomcat.util.modeler.Registry; 51 52 53 54 61 public final class StandardServer 62 implements Lifecycle, Server , MBeanRegistration 63 { 64 private static Log log = LogFactory.getLog(StandardServer.class); 65 66 67 69 70 73 private static String SERVER_LISTENER_CLASS_NAME = 74 "org.apache.catalina.mbeans.ServerLifecycleListener"; 75 76 77 79 80 83 public StandardServer() { 84 85 super(); 86 ServerFactory.setServer(this); 87 88 globalNamingResources = new NamingResources(); 89 globalNamingResources.setContainer(this); 90 91 if (isUseNaming()) { 92 if (namingContextListener == null) { 93 namingContextListener = new NamingContextListener(); 94 addLifecycleListener(namingContextListener); 95 } 96 } 97 98 } 99 100 101 103 104 107 private javax.naming.Context globalNamingContext = null; 108 109 110 113 private NamingResources globalNamingResources = null; 114 115 116 119 private static final String info = 120 "org.apache.catalina.core.StandardServer/1.0"; 121 122 123 126 private LifecycleSupport lifecycle = new LifecycleSupport(this); 127 128 129 132 private NamingContextListener namingContextListener = null; 133 134 135 138 private int port = 8005; 139 140 141 145 private Random random = null; 146 147 148 151 private Service services[] = new Service[0]; 152 153 154 157 private String shutdown = "SHUTDOWN"; 158 159 160 163 private static final StringManager sm = 164 StringManager.getManager(Constants.Package); 165 166 167 170 private boolean started = false; 171 172 173 176 private boolean initialized = false; 177 178 179 182 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 183 184 private boolean stopAwait = false; 185 186 188 189 192 public javax.naming.Context getGlobalNamingContext() { 193 194 return (this.globalNamingContext); 195 196 } 197 198 199 204 public void setGlobalNamingContext 205 (javax.naming.Context globalNamingContext) { 206 207 this.globalNamingContext = globalNamingContext; 208 209 } 210 211 212 215 public NamingResources getGlobalNamingResources() { 216 217 return (this.globalNamingResources); 218 219 } 220 221 222 227 public void setGlobalNamingResources 228 (NamingResources globalNamingResources) { 229 230 NamingResources oldGlobalNamingResources = 231 this.globalNamingResources; 232 this.globalNamingResources = globalNamingResources; 233 this.globalNamingResources.setContainer(this); 234 support.firePropertyChange("globalNamingResources", 235 oldGlobalNamingResources, 236 this.globalNamingResources); 237 238 } 239 240 241 246 public String getInfo() { 247 248 return (info); 249 250 } 251 252 256 public String getServerInfo() { 257 258 return ServerInfo.getServerInfo(); 259 } 260 261 264 public int getPort() { 265 266 return (this.port); 267 268 } 269 270 271 276 public void setPort(int port) { 277 278 this.port = port; 279 280 } 281 282 283 286 public String getShutdown() { 287 288 return (this.shutdown); 289 290 } 291 292 293 298 public void setShutdown(String shutdown) { 299 300 this.shutdown = shutdown; 301 302 } 303 304 305 307 308 313 public void addService(Service service) { 314 315 service.setServer(this); 316 317 synchronized (services) { 318 Service results[] = new Service[services.length + 1]; 319 System.arraycopy(services, 0, results, 0, services.length); 320 results[services.length] = service; 321 services = results; 322 323 if (initialized) { 324 try { 325 service.initialize(); 326 } catch (LifecycleException e) { 327 log.error(e); 328 } 329 } 330 331 if (started && (service instanceof Lifecycle)) { 332 try { 333 ((Lifecycle) service).start(); 334 } catch (LifecycleException e) { 335 ; 336 } 337 } 338 339 support.firePropertyChange("service", null, service); 341 } 342 343 } 344 345 public void stopAwait() { 346 stopAwait=true; 347 } 348 349 354 public void await() { 355 if( port == -2 ) { 357 return; 359 } 360 if( port==-1 ) { 361 while( true ) { 362 try { 363 Thread.sleep( 100000 ); 364 } catch( InterruptedException ex ) { 365 } 366 if( stopAwait ) return; 367 } 368 } 369 370 ServerSocket serverSocket = null; 372 try { 373 serverSocket = 374 new ServerSocket (port, 1, 375 InetAddress.getByName("127.0.0.1")); 376 } catch (IOException e) { 377 log.error("StandardServer.await: create[" + port 378 + "]: ", e); 379 System.exit(1); 380 } 381 382 while (true) { 384 385 Socket socket = null; 387 InputStream stream = null; 388 try { 389 socket = serverSocket.accept(); 390 socket.setSoTimeout(10 * 1000); stream = socket.getInputStream(); 392 } catch (AccessControlException ace) { 393 log.warn("StandardServer.accept security exception: " 394 + ace.getMessage(), ace); 395 continue; 396 } catch (IOException e) { 397 log.error("StandardServer.await: accept: ", e); 398 System.exit(1); 399 } 400 401 StringBuffer command = new StringBuffer (); 403 int expected = 1024; while (expected < shutdown.length()) { 405 if (random == null) 406 random = new Random (System.currentTimeMillis()); 407 expected += (random.nextInt() % 1024); 408 } 409 while (expected > 0) { 410 int ch = -1; 411 try { 412 ch = stream.read(); 413 } catch (IOException e) { 414 log.warn("StandardServer.await: read: ", e); 415 ch = -1; 416 } 417 if (ch < 32) break; 419 command.append((char) ch); 420 expected--; 421 } 422 423 try { 425 socket.close(); 426 } catch (IOException e) { 427 ; 428 } 429 430 boolean match = command.toString().equals(shutdown); 432 if (match) { 433 break; 434 } else 435 log.warn("StandardServer.await: Invalid command '" + 436 command.toString() + "' received"); 437 438 } 439 440 try { 442 serverSocket.close(); 443 } catch (IOException e) { 444 ; 445 } 446 447 } 448 449 450 456 public Service findService(String name) { 457 458 if (name == null) { 459 return (null); 460 } 461 synchronized (services) { 462 for (int i = 0; i < services.length; i++) { 463 if (name.equals(services[i].getName())) { 464 return (services[i]); 465 } 466 } 467 } 468 return (null); 469 470 } 471 472 473 476 public Service[] findServices() { 477 478 return (services); 479 480 } 481 482 485 public ObjectName [] getServiceNames() { 486 ObjectName onames[]=new ObjectName [ services.length ]; 487 for( int i=0; i<services.length; i++ ) { 488 onames[i]=((StandardService)services[i]).getObjectName(); 489 } 490 return onames; 491 } 492 493 494 500 public void removeService(Service service) { 501 502 synchronized (services) { 503 int j = -1; 504 for (int i = 0; i < services.length; i++) { 505 if (service == services[i]) { 506 j = i; 507 break; 508 } 509 } 510 if (j < 0) 511 return; 512 if (services[j] instanceof Lifecycle) { 513 try { 514 ((Lifecycle) services[j]).stop(); 515 } catch (LifecycleException e) { 516 ; 517 } 518 } 519 int k = 0; 520 Service results[] = new Service[services.length - 1]; 521 for (int i = 0; i < services.length; i++) { 522 if (i != j) 523 results[k++] = services[i]; 524 } 525 services = results; 526 527 support.firePropertyChange("service", service, null); 529 } 530 531 } 532 533 534 536 537 542 public void addPropertyChangeListener(PropertyChangeListener listener) { 543 544 support.addPropertyChangeListener(listener); 545 546 } 547 548 549 554 public void removePropertyChangeListener(PropertyChangeListener listener) { 555 556 support.removePropertyChangeListener(listener); 557 558 } 559 560 561 564 public String toString() { 565 566 StringBuffer sb = new StringBuffer ("StandardServer["); 567 sb.append(getPort()); 568 sb.append("]"); 569 return (sb.toString()); 570 571 } 572 573 574 585 public synchronized void storeConfig() throws Exception { 586 587 ObjectName sname = null; 588 try { 589 sname = new ObjectName ("Catalina:type=StoreConfig"); 590 if(mserver.isRegistered(sname)) { 591 mserver.invoke(sname, "storeConfig", null, null); 592 } else 593 log.error("StoreConfig mbean not registered" + sname); 594 } catch (Throwable t) { 595 log.error(t); 596 } 597 598 } 599 600 601 612 public synchronized void storeContext(Context context) throws Exception { 613 614 ObjectName sname = null; 615 try { 616 sname = new ObjectName ("Catalina:type=StoreConfig"); 617 if(mserver.isRegistered(sname)) { 618 mserver.invoke(sname, "store", 619 new Object [] {context}, 620 new String [] { "java.lang.String"}); 621 } else 622 log.error("StoreConfig mbean not registered" + sname); 623 } catch (Throwable t) { 624 log.error(t); 625 } 626 627 } 628 629 630 633 private boolean isUseNaming() { 634 boolean useNaming = true; 635 String useNamingProperty = System.getProperty("catalina.useNaming"); 637 if ((useNamingProperty != null) 638 && (useNamingProperty.equals("false"))) { 639 useNaming = false; 640 } 641 return useNaming; 642 } 643 644 645 647 648 653 public void addLifecycleListener(LifecycleListener listener) { 654 655 lifecycle.addLifecycleListener(listener); 656 657 } 658 659 660 664 public LifecycleListener[] findLifecycleListeners() { 665 666 return lifecycle.findLifecycleListeners(); 667 668 } 669 670 671 676 public void removeLifecycleListener(LifecycleListener listener) { 677 678 lifecycle.removeLifecycleListener(listener); 679 680 } 681 682 683 692 public void start() throws LifecycleException { 693 694 if (started) { 696 log.debug(sm.getString("standardServer.start.started")); 697 return; 698 } 699 700 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); 702 703 lifecycle.fireLifecycleEvent(START_EVENT, null); 704 started = true; 705 706 synchronized (services) { 708 for (int i = 0; i < services.length; i++) { 709 if (services[i] instanceof Lifecycle) 710 ((Lifecycle) services[i]).start(); 711 } 712 } 713 714 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); 716 717 } 718 719 720 729 public void stop() throws LifecycleException { 730 731 if (!started) 733 return; 734 735 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null); 737 738 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 739 started = false; 740 741 for (int i = 0; i < services.length; i++) { 743 if (services[i] instanceof Lifecycle) 744 ((Lifecycle) services[i]).stop(); 745 } 746 747 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); 749 750 } 751 752 public void init() throws Exception { 753 initialize(); 754 } 755 756 760 public void initialize() 761 throws LifecycleException 762 { 763 if (initialized) { 764 log.info(sm.getString("standardServer.initialize.initialized")); 765 return; 766 } 767 lifecycle.fireLifecycleEvent(INIT_EVENT, null); 768 initialized = true; 769 770 if( oname==null ) { 771 try { 772 oname=new ObjectName ( "Catalina:type=Server"); 773 Registry.getRegistry(null, null) 774 .registerComponent(this, oname, null ); 775 } catch (Exception e) { 776 log.error("Error registering ",e); 777 } 778 } 779 780 try { 782 ObjectName oname2 = 783 new ObjectName (oname.getDomain() + ":type=StringCache"); 784 Registry.getRegistry(null, null) 785 .registerComponent(new StringCache(), oname2, null ); 786 } catch (Exception e) { 787 log.error("Error registering ",e); 788 } 789 790 for (int i = 0; i < services.length; i++) { 792 services[i].initialize(); 793 } 794 } 795 796 protected String type; 797 protected String domain; 798 protected String suffix; 799 protected ObjectName oname; 800 protected MBeanServer mserver; 801 802 public ObjectName getObjectName() { 803 return oname; 804 } 805 806 public String getDomain() { 807 return domain; 808 } 809 810 public ObjectName preRegister(MBeanServer server, 811 ObjectName name) throws Exception { 812 oname=name; 813 mserver=server; 814 domain=name.getDomain(); 815 return name; 816 } 817 818 public void postRegister(Boolean registrationDone) { 819 } 820 821 public void preDeregister() throws Exception { 822 } 823 824 public void postDeregister() { 825 } 826 827 } 828 | Popular Tags |