Tomcat中的 Server
- Server 是一个接口
- Server 的实现类是 StandardServer,我们直接看 StandardServer 类
- Server 接口继承了 Lifecycle 接口,而 StandardServer 继承了 LifecycleBase 类,意味着 Lifecycle 接口中的方法是在 LifecycleBase 中实现的
StandardServer 中的属性
private int port = 8005;
private String address = "localhost";
private Service services[] = new Service[0];
private final Object servicesLock = new Object();
private String shutdown = "SHUTDOWN";
private Catalina catalina = null;
private volatile ServerSocket awaitSocket = null;
StandardServer 的构造函数
public StandardServer() {
super();
globalNamingResources = new NamingResourcesImpl();
globalNamingResources.setContainer(this);
if (isUseNaming()) {
namingContextListener = new NamingContextListener();
addLifecycleListener(namingContextListener);
} else {
namingContextListener = null;
}
}
StandardServer 的 initInternal 方法
protected void initInternal() throws LifecycleException {
super.initInternal();
onameStringCache = register(new StringCache(), "type=StringCache");
MBeanFactory factory = new MBeanFactory();
factory.setContainer(this);
onameMBeanFactory = register(factory, "type=MBeanFactory");
globalNamingResources.init();
if (getCatalina() != null) {
ClassLoader cl = getCatalina().getParentClassLoader();
while (cl != null && cl != ClassLoader.getSystemClassLoader()) {
if (cl instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) cl).getURLs();
for (URL url : urls) {
if (url.getProtocol().equals("file")) {
try {
File f = new File (url.toURI());
if (f.isFile() &&
f.getName().endsWith(".jar")) {
ExtensionValidator.addSystemResource(f);
}
} catch (URISyntaxException e) {
} catch (IOException e) {
}
}
}
}
cl = cl.getParent();
}
}
for (int i = 0; i < services.length; i++) {
services[i].init();
}
}
StandardServer 的 startInternal 方法
protected void startInternal() throws LifecycleException {
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
setState(LifecycleState.STARTING);
globalNamingResources.start();
synchronized (servicesLock) {
for (int i = 0; i < services.length; i++) {
services[i].start();
}
}
}
StandardServer 的 stopInternal 方法
protected void stopInternal() throws LifecycleException {
setState(LifecycleState.STOPPING);
fireLifecycleEvent(CONFIGURE_STOP_EVENT, null);
for (int i = 0; i < services.length; i++) {
services[i].stop();
}
globalNamingResources.stop();
stopAwait();
}
StandardServer 的 destroyInternal 方法
protected void destroyInternal() throws LifecycleException {
for (int i = 0; i < services.length; i++) {
services[i].destroy();
}
globalNamingResources.destroy();
unregister(onameMBeanFactory);
unregister(onameStringCache);
super.destroyInternal();
}
StandardServer 的 addService 方法
public void addService(Service service) {
service.setServer(this);
synchronized (servicesLock) {
Service results[] = new Service[services.length + 1];
System.arraycopy(services, 0, results, 0, services.length);
results[services.length] = service;
services = results;
if (getState().isAvailable()) {
try {
service.start();
} catch (LifecycleException e) {
}
}
support.firePropertyChange("service", null, service);
}
}
StandardServer 的 findService 方法
public Service findService(String name) {
if (name == null) {
return null;
}
synchronized (servicesLock) {
for (int i = 0; i < services.length; i++) {
if (name.equals(services[i].getName())) {
return services[i];
}
}
}
return null;
}
StandardServer 的 await 方法
public void await() {
if (port == -2) {
return;
}
if (port==-1) {
try {
awaitThread = Thread.currentThread();
while(!stopAwait) {
try {
Thread.sleep( 10000 );
} catch( InterruptedException ex ) {
}
}
} finally {
awaitThread = null;
}
return;
}
try {
awaitSocket = new ServerSocket(port, 1,
InetAddress.getByName(address));
} catch (IOException e) {
log.error("StandardServer.await: create[" + address
+ ":" + port
+ "]: ", e);
return;
}
try {
awaitThread = Thread.currentThread();
while (!stopAwait) {
ServerSocket serverSocket = awaitSocket;
if (serverSocket == null) {
break;
}
Socket socket = null;
StringBuilder command = new StringBuilder();
try {
InputStream stream;
long acceptStartTime = System.currentTimeMillis();
try {
socket = serverSocket.accept();
socket.setSoTimeout(10 * 1000);
stream = socket.getInputStream();
} catch (SocketTimeoutException ste) {
log.warn(sm.getString("standardServer.accept.timeout",
Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
continue;
} catch (AccessControlException ace) {
log.warn(sm.getString("standardServer.accept.security"), ace);
continue;
} catch (IOException e) {
if (stopAwait) {
break;
}
log.error(sm.getString("standardServer.accept.error"), e);
break;
}
int expected = 1024;
while (expected < shutdown.length()) {
if (random == null)
random = new Random();
expected += (random.nextInt() % 1024);
}
while (expected > 0) {
int ch = -1;
try {
ch = stream.read();
} catch (IOException e) {
log.warn(sm.getString("standardServer.accept.readError"), e);
ch = -1;
}
if (ch < 32 || ch == 127) {
break;
}
command.append((char) ch);
expected--;
}
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
}
}
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;
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
}
}
}
}
StandardServer 的 stopAwait 方法
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) {
}
}
t.interrupt();
try {
t.join(1000);
} catch (InterruptedException e) {
}
}
}
小结
- StandardServer 主要是操作内部的 Service 数组
- StandardServer 维护了一个关闭服务 ServerSocket