SimpleHttpServer的学习之UML

如何分析一个稍微大点的源码呢?

静态分析

除了看代码,就是 uml图,UML虽然在书本类与类之间的关系很复杂,可能要一本书,但是最核心的其实很简单;

(1)继承 extends

(2)实现接口 implements

以上两个没啥说的,很easy.

(3)关联和依赖,这两者我一块说,不区分。简单来说,就是当前类直接使用哪些类。我们看项目中的主类依赖关系,从主类别开始逐层深入不断分析。SimpleHttpServer的学习之UML

代码和和图一结合,很简单吧

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.simpleHTTPServer; import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger; /**
*
* @author romain
*/
public class SimpleHTTPServer { //默认端口
public static final int DEFAULT_PORT = 8000;
//默认目录
public static final File DEFAULT_FILE = new File(".");
//自定义端口
private final int port;
//自定义目录
private final File rootDir;
//最大线程数
private int maxThreads = 10;
//连接超时时间
private int clientTimeoutInMillis = 1000;
//依赖的一个类,多线程的为用户服务
private ServerMultiThreadedWorkers server;
private boolean started = false; public SimpleHTTPServer() {
this(DEFAULT_PORT, DEFAULT_FILE);
} public SimpleHTTPServer(int port, File rootDir) {
this.port = port;
this.rootDir = rootDir;
} public void start() {
if (!started) {
System.out.println("Serving HTTP on 0.0.0.0 port 8000 ...");
//z请求处理类,此处又直接依赖RequestHandlerFactory类,和RequestHandlerStaticSiteFactory
RequestHandlerFactory requestHandlerFactory = new RequestHandlerStaticSiteFactory(rootDir);
server = new ServerMultiThreadedWorkers(port, clientTimeoutInMillis, maxThreads, requestHandlerFactory);
server.start();
started = true;
} else {
throw new RuntimeException("Server already started (HTTP port=" + port + ", rootDir=" + rootDir.getAbsolutePath() + ")"); }
} public void stop() {
if (started) {
server.terminate();
try {
//直接使用了Thread类
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(SimpleHTTPServer.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.out.println("Server not started (HTTP port=" + port + ", rootDir=" + rootDir.getAbsolutePath() + ")");
}
} /**
* @param maxThreads the maxThreads to set
*/
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
} /**
* @param clientTimeoutInMillis the clientTimeoutInMillis to set
*/
public void setClientTimeoutInMillis(int clientTimeoutInMillis) {
this.clientTimeoutInMillis = clientTimeoutInMillis;
} /**
*
* @param args. First arg is the port number.
*/
public static void main(String[] args) {
int port = DEFAULT_PORT;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
SimpleHTTPServer server = new SimpleHTTPServer(port, DEFAULT_FILE);
server.start(); //Use Ctrl + C to stop.
}
}
 
上一篇:用Redis存储Tomcat集群的Session实现session共享


下一篇:iOS开发 - RunLoop理解