1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.core;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.deploy.NamingResourcesImpl;
import org.apache.catalina.mbeans.MBeanFactory;
import org.apache.catalina.startup.Catalina;
import org.apache.catalina.util.LifecycleMBeanBase;
import org.apache.catalina.util.ServerInfo;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.buf.StringCache;
import org.apache.tomcat.util.modeler.Registry;
import org.apache.tomcat.util.res.StringManager;
import org.apache.tomcat.util.threads.TaskThreadFactory;
/**
* Standard implementation of the <b>Server</b> interface, available for use (but not required) when deploying and
* starting Catalina.
*
* @author Craig R. McClanahan
*/
public final class StandardServer extends LifecycleMBeanBase implements Server {
private static final Log log = LogFactory.getLog(StandardServer.class);
private static final StringManager sm = StringManager.getManager(StandardServer.class);
// ------------------------------------------------------------ Constructor
/**
* Construct a default instance of this class.
*/
public StandardServer() {
super();
globalNamingResources = new NamingResourcesImpl();
globalNamingResources.setContainer(this);
if (isUseNaming()) {
namingContextListener = new NamingContextListener();
addLifecycleListener(namingContextListener);
} else {
namingContextListener = null;
}
}
// ----------------------------------------------------- Instance Variables
/**
* Global naming resources context.
*/
private javax.naming.Context globalNamingContext = null;
/**
* Global naming resources.
*/
private NamingResourcesImpl globalNamingResources;
/**
* The naming context listener for this web application.
*/
private final NamingContextListener namingContextListener;
/**
* The port number on which we wait for shutdown commands.
*/
private int port = 8005;
private int portOffset = 0;
/**
* The address on which we wait for shutdown commands.
*/
private String address = "localhost";
/**
* A random number generator that is <strong>only</strong> used if the shutdown command string is longer than 1024
* characters.
*/
private Random random = null;
/**
* The set of Services associated with this Server.
*/
private Service[] services = new Service[0];
private final ReentrantReadWriteLock servicesLock = new ReentrantReadWriteLock();
private final Lock servicesReadLock = servicesLock.readLock();
private final Lock servicesWriteLock = servicesLock.writeLock();
/**
* The shutdown command string we are looking for.
*/
private String shutdown = "SHUTDOWN";
/**
* The property change support for this component.
*/
final PropertyChangeSupport support = new PropertyChangeSupport(this);
private volatile boolean stopAwait = false;
private Catalina catalina = null;
private ClassLoader parentClassLoader = null;
/**
* Thread that currently is inside our await() method.
*/
private volatile Thread awaitThread = null;
/**
* Server socket that is used to wait for the shutdown command.
*/
private volatile ServerSocket awaitSocket = null;
private File catalinaHome = null;
private File catalinaBase = null;
private final Object namingToken = new Object();
/**
* The number of threads available to process utility tasks in this service.
*/
private int utilityThreads = 2;
/**
* The utility threads daemon flag.
*/
private boolean utilityThreadsAsDaemon = false;
/**
* Utility executor with scheduling capabilities.
*/
private ScheduledThreadPoolExecutor utilityExecutor = null;
private final Object utilityExecutorLock = new Object();
/**
* Utility executor wrapper.
*/
private ScheduledExecutorService utilityExecutorWrapper = null;
/**
* Controller for the periodic lifecycle event.
*/
private ScheduledFuture<?> periodicLifecycleEventFuture = null;
private ScheduledFuture<?> monitorFuture;
/**
* The lifecycle event period in seconds.
*/
private int periodicEventDelay = 10;
// ------------------------------------------------------------- Properties
@Override
public Object getNamingToken() {
return namingToken;
}
@Override
public javax.naming.Context getGlobalNamingContext() {
return this.globalNamingContext;
}
/**
* Set the global naming resources context.
*
* @param globalNamingContext The new global naming resource context
*/
public void setGlobalNamingContext(javax.naming.Context globalNamingContext) {
this.globalNamingContext = globalNamingContext;
}
@Override
public NamingResourcesImpl getGlobalNamingResources() {
return this.globalNamingResources;
}
@Override
public void setGlobalNamingResources(NamingResourcesImpl globalNamingResources) {
NamingResourcesImpl oldGlobalNamingResources = this.globalNamingResources;
this.globalNamingResources = globalNamingResources;
this.globalNamingResources.setContainer(this);
support.firePropertyChange("globalNamingResources", oldGlobalNamingResources, this.globalNamingResources);
}
/**
* Report the current Tomcat Server Release number
*
* @return Tomcat release identifier
*/
public String getServerInfo() {
return ServerInfo.getServerInfo();
}
/**
* Return the current server built timestamp
*
* @return server built timestamp.
*/
public String getServerBuilt() {
return ServerInfo.getServerBuilt();
}
/**
* Return the current server's version number.
*
* @return server's version number.
*/
public String getServerNumber() {
return ServerInfo.getServerNumber();
}
@Override
public int getPort() {
return this.port;
}
@Override
public void setPort(int port) {
this.port = port;
}
@Override
public int getPortOffset() {
return portOffset;
}
@Override
public void setPortOffset(int portOffset) {
if (portOffset < 0) {
throw new IllegalArgumentException(
sm.getString("standardServer.portOffset.invalid", Integer.valueOf(portOffset)));
}
this.portOffset = portOffset;
}
@Override
public int getPortWithOffset() {
// Non-positive port values have special meanings and the offset should
// not apply.
int port = getPort();
if (port > 0) {
return port + getPortOffset();
} else {
return port;
}
}
@Override
public String getAddress() {
return this.address;
}
@Override
public void setAddress(String address) {
this.address = address;
}
@Override
public String getShutdown() {
return this.shutdown;
}
@Override
public void setShutdown(String shutdown) {
this.shutdown = shutdown;
}
@Override
public Catalina getCatalina() {
return catalina;
}
@Override
public void setCatalina(Catalina catalina) {
this.catalina = catalina;
}
@Override
public int getUtilityThreads() {
return utilityThreads;
}
/**
* Handles the special values.
*/
private static int getUtilityThreadsInternal(int utilityThreads) {
int result = utilityThreads;
if (result <= 0) {
result = Runtime.getRuntime().availableProcessors() + result;
if (result < 2) {
result = 2;
}
}
return result;
}
@Override
public void setUtilityThreads(int utilityThreads) {
// Use local copies to ensure thread safety
int oldUtilityThreads = this.utilityThreads;
if (getUtilityThreadsInternal(utilityThreads) < getUtilityThreadsInternal(oldUtilityThreads)) {
return;
}
this.utilityThreads = utilityThreads;
synchronized (utilityExecutorLock) {
if (oldUtilityThreads != utilityThreads && utilityExecutor != null) {
reconfigureUtilityExecutor(getUtilityThreadsInternal(utilityThreads));
}
}
}
/*
* Callers must be holding utilityExecutorLock
*/
private void reconfigureUtilityExecutor(int threads) {
// The ScheduledThreadPoolExecutor doesn't use MaximumPoolSize, only CorePoolSize is available
if (utilityExecutor != null) {
utilityExecutor.setCorePoolSize(threads);
} else {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(threads,
new TaskThreadFactory("Catalina-utility-", utilityThreadsAsDaemon, Thread.MIN_PRIORITY));
scheduledThreadPoolExecutor.setKeepAliveTime(10, TimeUnit.SECONDS);
scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true);
scheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
utilityExecutor = scheduledThreadPoolExecutor;
utilityExecutorWrapper = new org.apache.tomcat.util.threads.ScheduledThreadPoolExecutor(utilityExecutor);
}
}
/**
* Get if the utility threads are daemon threads.
*
* @return the threads daemon flag
*/
public boolean getUtilityThreadsAsDaemon() {
return utilityThreadsAsDaemon;
}
/**
* Set the utility threads daemon flag. The default value is true.
*
* @param utilityThreadsAsDaemon the new thread daemon flag
*/
public void setUtilityThreadsAsDaemon(boolean utilityThreadsAsDaemon) {
this.utilityThreadsAsDaemon = utilityThreadsAsDaemon;
}
/**
* @return The period between two lifecycle events, in seconds
*/
public int getPeriodicEventDelay() {
return periodicEventDelay;
}
/**
* Set the new period between two lifecycle events in seconds.
*
* @param periodicEventDelay The period in seconds, negative or zero will disable events
*/
public void setPeriodicEventDelay(int periodicEventDelay) {
this.periodicEventDelay = periodicEventDelay;
}
// --------------------------------------------------------- Server Methods
@Override
public void addService(Service service) {
service.setServer(this);
servicesWriteLock.lock();
try {
Service[] results = new Service[services.length + 1];
System.arraycopy(services, 0, results, 0, services.length);
results[services.length] = service;
services = results;
} finally {
servicesWriteLock.unlock();
}
if (getState().isAvailable()) {
try {
service.start();
} catch (LifecycleException e) {
// Ignore
}
}
// Report this property change to interested listeners
support.firePropertyChange("service", null, service);
}
public void stopAwait() {
stopAwait = true;
Thread t = awaitThread;
if (t != null) {
ServerSocket s = awaitSocket;
if (s != null) {
awaitSocket = null;
try {
s.close();
} catch (IOException e) {
// Ignored
}
}
t.interrupt();
try {
t.join(1000);
} catch (InterruptedException e) {
// Ignored
}
}
}
@Override
public void await() {
// Negative values - don't wait on port - tomcat is embedded or we just don't like ports
if (getPortWithOffset() == -2) {
// undocumented yet - for embedding apps that are around, alive.
return;
}
Thread currentThread = Thread.currentThread();
if (getPortWithOffset() == -1) {
try {
awaitThread = currentThread;
while (!stopAwait) {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
// continue and check the flag
}
}
} finally {
awaitThread = null;
}
return;
}
// Set up a server socket to wait on
try {
awaitSocket = new ServerSocket(getPortWithOffset(), 1, InetAddress.getByName(address));
} catch (IOException e) {
log.error(sm.getString("standardServer.awaitSocket.fail", address, String.valueOf(getPortWithOffset()),
String.valueOf(getPort()), String.valueOf(getPortOffset())), e);
return;
}
try {
awaitThread = currentThread;
// Loop waiting for a connection and a valid command
while (!stopAwait) {
ServerSocket serverSocket = awaitSocket;
if (serverSocket == null) {
break;
}
// Wait for the next connection
Socket socket = null;
StringBuilder command = new StringBuilder();
try {
InputStream stream;
long acceptStartTime = System.currentTimeMillis();
try {
socket = serverSocket.accept();
socket.setSoTimeout(10 * 1000); // Ten seconds
stream = socket.getInputStream();
} catch (SocketTimeoutException ste) {
// This should never happen but bug 56684 suggests that
// it does.
log.warn(sm.getString("standardServer.accept.timeout",
Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
continue;
} catch (IOException e) {
if (stopAwait) {
// Wait was aborted with socket.close()
break;
}
log.error(sm.getString("standardServer.accept.error"), e);
break;
}
// Read a set of characters from the socket
int expected = 1024; // Cut off to avoid DoS attack
while (expected < shutdown.length()) {
if (random == null) {
random = new Random();
}
expected += random.nextInt() % 1024;
}
while (expected > 0) {
int ch;
try {
ch = stream.read();
} catch (IOException e) {
log.warn(sm.getString("standardServer.accept.readError"), e);
ch = -1;
}
// Control character or EOF (-1) terminates loop
if (ch < 32 || ch == 127) {
break;
}
command.append((char) ch);
expected--;
}
} finally {
// Close the socket now that we are done with it
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
// Ignore
}
}
// Match against our command string
boolean match = command.toString().equals(shutdown);
if (match) {
log.info(sm.getString("standardServer.shutdownViaPort"));
break;
} else {
log.warn(sm.getString("standardServer.invalidShutdownCommand", command.toString()));
}
}
} finally {
ServerSocket serverSocket = awaitSocket;
awaitThread = null;
awaitSocket = null;
// Close the server socket and return
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// Ignore
}
}
}
}
@Override
public Service findService(String name) {
if (name == null) {
return null;
}
servicesReadLock.lock();
try {
for (Service service : services) {
if (name.equals(service.getName())) {
return service;
}
}
} finally {
servicesReadLock.unlock();
}
return null;
}
@Override
public Service[] findServices() {
servicesReadLock.lock();
try {
return services.clone();
} finally {
servicesReadLock.unlock();
}
}
/**
* @return the JMX service names.
*/
public ObjectName[] getServiceNames() {
servicesReadLock.lock();
try {
ObjectName[] onames = new ObjectName[services.length];
for (int i = 0; i < services.length; i++) {
onames[i] = ((StandardService) services[i]).getObjectName();
}
return onames;
} finally {
servicesReadLock.unlock();
}
}
@Override
public void removeService(Service service) {
servicesWriteLock.lock();
try {
int j = -1;
for (int i = 0; i < services.length; i++) {
if (service == services[i]) {
j = i;
break;
}
}
if (j < 0) {
return;
}
int k = 0;
Service[] results = new Service[services.length - 1];
for (int i = 0; i < services.length; i++) {
if (i != j) {
results[k++] = services[i];
}
}
services = results;
} finally {
servicesWriteLock.unlock();
}
try {
service.stop();
} catch (LifecycleException e) {
// Ignore
}
// Report this property change to interested listeners
support.firePropertyChange("service", service, null);
}
@Override
public File getCatalinaBase() {
if (catalinaBase != null) {
return catalinaBase;
}
catalinaBase = getCatalinaHome();
return catalinaBase;
}
@Override
public void setCatalinaBase(File catalinaBase) {
this.catalinaBase = catalinaBase;
}
@Override
public File getCatalinaHome() {
return catalinaHome;
}
@Override
public void setCatalinaHome(File catalinaHome) {
this.catalinaHome = catalinaHome;
}
// --------------------------------------------------------- Public Methods
/**
* Add a property change listener to this component.
*
* @param listener The listener to add
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
/**
* Remove a property change listener from this component.
*
* @param listener The listener to remove
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}
@Override
public String toString() {
return "StandardServer[" + getPort() + ']';
}
/**
* Write the configuration information for this entire <code>Server</code> out to the server.xml configuration file.
*
* @exception InstanceNotFoundException if the managed resource object cannot be found
* @exception MBeanException if the initializer of the object throws an exception, or
* persistence is not supported
* @exception javax.management.RuntimeOperationsException if an exception is reported by the persistence mechanism
*/
public synchronized void storeConfig() throws InstanceNotFoundException, MBeanException {
try {
// Note: Hard-coded domain used since this object is per Server/JVM
ObjectName sname = new ObjectName("Catalina:type=StoreConfig");
MBeanServer server = Registry.getRegistry(null).getMBeanServer();
if (server.isRegistered(sname)) {
server.invoke(sname, "storeConfig", null, null);
} else {
log.error(sm.getString("standardServer.storeConfig.notAvailable", sname));
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("standardServer.storeConfig.error"), t);
}
}
/**
* Write the configuration information for <code>Context</code> out to the specified configuration file.
*
* @param context the context which should save its configuration
*
* @exception InstanceNotFoundException if the managed resource object cannot be found
* @exception MBeanException if the initializer of the object throws an exception or
* persistence is not supported
* @exception javax.management.RuntimeOperationsException if an exception is reported by the persistence mechanism
*/
public synchronized void storeContext(Context context) throws InstanceNotFoundException, MBeanException {
try {
// Note: Hard-coded domain used since this object is per Server/JVM
ObjectName sname = new ObjectName("Catalina:type=StoreConfig");
MBeanServer server = Registry.getRegistry(null).getMBeanServer();
if (server.isRegistered(sname)) {
server.invoke(sname, "store", new Object[] { context }, new String[] { "java.lang.String" });
} else {
log.error(sm.getString("standardServer.storeConfig.notAvailable", sname));
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error(sm.getString("standardServer.storeConfig.contextError", context.getName()), t);
}
}
/**
* @return <code>true</code> if naming should be used.
*/
private boolean isUseNaming() {
boolean useNaming = true;
// Reading the "catalina.useNaming" environment variable
String useNamingProperty = System.getProperty("catalina.useNaming");
if (useNamingProperty != null && useNamingProperty.equals("false")) {
useNaming = false;
}
return useNaming;
}
@Override
protected void startInternal() throws LifecycleException {
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
setState(LifecycleState.STARTING);
// Initialize utility executor
synchronized (utilityExecutorLock) {
reconfigureUtilityExecutor(getUtilityThreadsInternal(utilityThreads));
register(utilityExecutor, "type=UtilityExecutor");
}
globalNamingResources.start();
// Start our defined Services
for (Service service : findServices()) {
service.start();
}
if (periodicEventDelay > 0) {
monitorFuture = getUtilityExecutor().scheduleWithFixedDelay(this::startPeriodicLifecycleEvent, 0, 60,
TimeUnit.SECONDS);
}
}
private void startPeriodicLifecycleEvent() {
if (periodicLifecycleEventFuture == null || periodicLifecycleEventFuture.isDone()) {
if (periodicLifecycleEventFuture != null && periodicLifecycleEventFuture.isDone()) {
// There was an error executing the scheduled task, get it and log it
try {
periodicLifecycleEventFuture.get();
} catch (InterruptedException | ExecutionException e) {
log.error(sm.getString("standardServer.periodicEventError"), e);
}
}
periodicLifecycleEventFuture =
getUtilityExecutor().scheduleAtFixedRate(() -> fireLifecycleEvent(PERIODIC_EVENT, null),
periodicEventDelay, periodicEventDelay, TimeUnit.SECONDS);
}
}
@Override
protected void stopInternal() throws LifecycleException {
setState(LifecycleState.STOPPING);
if (monitorFuture != null) {
monitorFuture.cancel(true);
monitorFuture = null;
}
if (periodicLifecycleEventFuture != null) {
periodicLifecycleEventFuture.cancel(false);
periodicLifecycleEventFuture = null;
}
fireLifecycleEvent(CONFIGURE_STOP_EVENT, null);
// Stop our defined Services
for (Service service : findServices()) {
service.stop();
}
synchronized (utilityExecutorLock) {
if (utilityExecutor != null) {
utilityExecutor.shutdownNow();
unregister("type=UtilityExecutor");
utilityExecutor = null;
}
}
globalNamingResources.stop();
stopAwait();
}
/**
* {@inheritDoc}
* <p>
* This is used to allow connectors to bind to restricted ports under Unix operating environments.
*/
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
// Register global String cache
// Note although the cache is global, if there are multiple Servers
// present in the JVM (may happen when embedding) then the same cache
// will be registered under multiple names
onameStringCache = register(new StringCache(), "type=StringCache");
// Register the MBeanFactory
MBeanFactory factory = new MBeanFactory();
factory.setContainer(this);
onameMBeanFactory = register(factory, "type=MBeanFactory");
// Register the naming resources
globalNamingResources.init();
// Initialize our defined Services
for (Service service : findServices()) {
service.init();
}
}
@Override
protected void destroyInternal() throws LifecycleException {
// Destroy our defined Services
for (Service service : findServices()) {
service.destroy();
}
globalNamingResources.destroy();
unregister(onameMBeanFactory);
unregister(onameStringCache);
super.destroyInternal();
}
@Override
public ClassLoader getParentClassLoader() {
if (parentClassLoader != null) {
return parentClassLoader;
}
if (catalina != null) {
return catalina.getParentClassLoader();
}
return ClassLoader.getSystemClassLoader();
}
@Override
public void setParentClassLoader(ClassLoader parent) {
ClassLoader oldParentClassLoader = this.parentClassLoader;
this.parentClassLoader = parent;
support.firePropertyChange("parentClassLoader", oldParentClassLoader, this.parentClassLoader);
}
private ObjectName onameStringCache;
private ObjectName onameMBeanFactory;
/**
* @return the MBean domain for this server. The domain is obtained using the following search order:
* <ol>
* <li>Name of first {@link org.apache.catalina.Engine}.</li>
* <li>Name of first {@link Service}.</li>
* </ol>
*/
@Override
protected String getDomainInternal() {
String domain = null;
Service[] services = findServices();
if (services.length > 0) {
Service service = services[0];
if (service != null) {
domain = service.getDomain();
}
}
return domain;
}
@Override
protected String getObjectNameKeyProperties() {
return "type=Server";
}
@Override
public ScheduledExecutorService getUtilityExecutor() {
return utilityExecutorWrapper;
}
}
|