从零开始手写Tomcat的教程14节----服务器组件(Server)和服务组件(Service)
Posted 大忽悠爱忽悠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从零开始手写Tomcat的教程14节----服务器组件(Server)和服务组件(Service)相关的知识,希望对你有一定的参考价值。
从零开始手写Tomcat的教程14节----服务器组件Server和服务组件Service
服务器组件
public interface Server
// ------------------------------------------------------------- Properties
/**
* Return descriptive information about this Server implementation and
* the corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo();
/**
* Return the global naming resources.
*/
public NamingResources getGlobalNamingResources();
/**
* Set the global naming resources.
*
* @param namingResources The new global naming resources
*/
public void setGlobalNamingResources
(NamingResources globalNamingResources);
/**
* Return the port number we listen to for shutdown commands.
*/
public int getPort();
/**
* Set the port number we listen to for shutdown commands.
*
* @param port The new port number
*/
public void setPort(int port);
/**
* Return the shutdown command string we are waiting for.
*/
public String getShutdown();
/**
* Set the shutdown command we are waiting for.
*
* @param shutdown The new shutdown command
*/
public void setShutdown(String shutdown);
// --------------------------------------------------------- Public Methods
/**
* Add a new Service to the set of defined Services.
*
* @param service The Service to be added
*/
public void addService(Service service);
/**
* Wait until a proper shutdown command is received, then return.
*/
public void await();
/**
* Return the specified Service (if it exists); otherwise return
* <code>null</code>.
*
* @param name Name of the Service to be returned
*/
public Service findService(String name);
/**
* Return the set of Services defined within this Server.
*/
public Service[] findServices();
/**
* Remove the specified Service from the set associated from this
* Server.
*
* @param service The Service to be removed
*/
public void removeService(Service service);
/**
* Invoke a pre-startup initialization. This is used to allow connectors
* to bind to restricted ports under Unix operating environments.
*
* @exception LifecycleException If this server was already initialized.
*/
public void initialize()
throws LifecycleException;
StandardServer类
initialize方法
/**
* Invoke a pre-startup initialization. This is used to allow connectors
* to bind to restricted ports under Unix operating environments.
*/
public void initialize()
throws LifecycleException
if (initialized)
throw new LifecycleException (
sm.getString("standardServer.initialize.initialized"));
initialized = true;
// Initialize our defined Services
for (int i = 0; i < services.length; i++)
services[i].initialize();
start方法
public void start() throws LifecycleException
// Validate and update our current component state
if (started)
throw new LifecycleException
(sm.getString("standardServer.start.started"));
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
// Start our defined Services
synchronized (services)
for (int i = 0; i < services.length; i++)
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).start();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
stop方法
public void stop() throws LifecycleException
// Validate and update our current component state
if (!started)
throw new LifecycleException
(sm.getString("standardServer.stop.notStarted"));
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
started = false;
// Stop our defined Services
for (int i = 0; i < services.length; i++)
if (services[i] instanceof Lifecycle)
((Lifecycle) services[i]).stop();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
await方法
public void await()
// Set up a server socket to wait on
ServerSocket serverSocket = null;
try
serverSocket =
new ServerSocket(port, 1,
InetAddress.getByName("127.0.0.1"));
catch (IOException e)
System.err.println("StandardServer.await: create[" + port
+ "]: " + e);
e.printStackTrace();
System.exit(1);
// Loop waiting for a connection and a valid command
while (true)
// Wait for the next connection
Socket socket = null;
InputStream stream = null;
try
socket = serverSocket.accept();
socket.setSoTimeout(10 * 1000); // Ten seconds
stream = socket.getInputStream();
catch (AccessControlException ace)
System.err.println("StandardServer.accept security exception: "
+ ace.getMessage());
continue;
catch (IOException e)
System.err.println("StandardServer.await: accept: " + e);
e.printStackTrace();
System.exit(1);
// Read a set of characters from the socket
StringBuffer command = new StringBuffer();
int expected = 1024; // Cut off to avoid DoS attack
while (expected < shutdown.length())
if (random == null)
random = new Random(System.currentTimeMillis());
expected += (random.nextInt() % 1024);
while (expected > 0)
int ch = -1;
try
ch = stream.read();
catch (IOException e)
System.err.println("StandardServer.await: read: " + e);
e.printStackTrace();
ch = -1;
if (ch < 32) // Control character or EOF terminates loop
break;
command.append((char) ch);
expected--;
// Close the socket now that we are done with it
try
socket.close();
catch (IOException e)
;
// Match against our command string
boolean match = command.toString().equals(shutdown);
if (match)
break;
else
System.err.println("StandardServer.await: Invalid command '" +
command.toString() + "' received");
// Close the server socket and return
try
serverSocket.close();
catch (IOException e)
;
Service接口
public interface Service
// ------------------------------------------------------------- Properties
/**
* Return the <code>Container</code> that handles requests for all
* <code>Connectors</code> associated with this Service.
*/
public Container getContainer();
/**
* Set the <code>Container</code> that handles requests for all
* <code>Connectors</code> associated with this Service.
*
* @param container The new Container
*/
public void setContainer(Container container);
/**
* Return descriptive information about this Service implementation and
* the corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo();
/**
* Return the name of this Service.
*/
public String getName();
/**
* Set the name of this Service.
*
* @param name The new service name
*/
public void setName(String name);
/**
* Return the <code>Server</code> with which we are associated (if any).
*/
public Server getServer();
/**
* Set the <code>Server</code> with which we are associated (if any).
*
* @param server The server that owns this Service
*/
public void setServer(Server server);
// --------------------------------------------------------- Public Methods
/**
* Add a new Connector to the set of defined Connectors, and associate it
* with this Service's Container.
*
* @param connector The Connector to be added
*/
public void addConnector(Connector connector);
/**
* Find and return the set of Connectors associated with this Service.
*/
public Connector[] findConnectors();
/**
* Remove the specified Connector from the set associated from this
* Service. The removed Connector will also be disassociated from our
* Container.
*
* @param connector The Connector to be removed
*/
public void removeConnector(Connector connector);
/**
* Invoke a pre-startup initialization. This is used to allow connectors
* to bind to restricted ports under Unix operating environments.
*
* @exception LifecycleException If this server was already initialized.
*/
public void initialize()
throws LifecycleException;
StandardService类
Connector和Container
public void setContainer(Container container)
Container oldContainer = this.container;
if ((oldContainer != null) && (oldContainer instanceof Engine))
((Engine) oldContainer).setService(null);
this.container = container;
if ((this.container != null) && (this.container instanceof Engine))
((Engine) this.container).setService(this);
if (started && (this.container != null) &&
(this.container instanceof Lifecycle))
try
((Lifecycle) this.container).start();
catch (LifecycleException e)
;
synchronized (connectors)
for (int i = 0; i < connectors.length; i++)
connectors[i].setContainer(this.container);
if (started && (oldContainer != null) &&
(oldContainer instanceof Lifecycle))
try
((Lifecycle) oldContainer).stop();
catch (LifecycleException e)
;
// Report this property change to interested listeners
support.firePropertyChange("container", oldContainer, this.container);
public void addConnector(Connector connector)
synchronized (connectors)
connector.setContainer(this.container);
connector.setService(this);
Connector results[] = new Connector[connectors.length + 1];
System.arraycopy(connectors, 0, results, 0, connectors.length);
results[connectors.length] = connector;
connectors = results;
if (initialized)
try
connector.initialize();
catch (LifecycleException e)
e.printStackTrace(System.err);
if (started && (connector instanceof Lifecycle))
try
((Lifecycle) connector).start();
catch (LifecycleException e)
;
// Report this property change to interested listeners
support.firePropertyChange("connector", null, connector);
public void removeConnector(Connector connector)
synchronized (connectors)
int j = -1;
for (int i = 0; i < connectors.length; i++)
if (connector == connectors[i])
j = i;
break;
if (j < 0)
return;
if (started && (connectors[j] instanceof Lifecycle))
try
((Lifecycle) connectors[j]).stop();
catch (LifecycleException e)
;
connectors[j].setContainer(null);
connector.setService(null);
int k = 0;
Connector results[] = new Connector[connectors.length - 1];
for (int i = 0; i < connectors.length; i++)
if (i != j)
results[k++] = connectors[i];
connectors = results;
// Report this property change to interested listeners
support.firePropertyChange("connector", connector, null);
与生命周期有关的方法
public void initialize()
throws LifecycleException
if (initialized)
throw new LifecycleException (
sm.getString("standardService.initialize.initialized"));
initialized = true;
// Initialize our defined Connectors
synchronized (connectors)
以上是关于从零开始手写Tomcat的教程14节----服务器组件(Server)和服务组件(Service)的主要内容,如果未能解决你的问题,请参考以下文章
从零开始手写Tomcat的教程11节----StandardWrapper