步骤 1 : 拓扑图点亮 步骤 2 : 静态部署 步骤 3 : Host 步骤 4 : TestTomcat 步骤 5 : 比较可运行项目,快速定位问题
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.catalina;
import cn.how2j.diytomcat.util.Constant;
import cn.how2j.diytomcat.util.ServerXMLUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.LogFactory;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Host {
private String name;
private Map<String, Context> contextMap;
private Engine engine;
public Host(String name, Engine engine){
this.contextMap = new HashMap<>();
this.name = name;
this.engine = engine;
scanContextsOnWebAppsFolder();
scanContextsInServerXML();
scanWarOnWebAppsFolder();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void scanContextsInServerXML() {
List<Context> contexts = ServerXMLUtil.getContexts(this);
for (Context context : contexts) {
contextMap.put(context.getPath(), context);
}
}
private void scanContextsOnWebAppsFolder() {
File[] folders = Constant.webappsFolder.listFiles();
for (File folder : folders) {
if (!folder.isDirectory())
continue;
loadContext(folder);
}
}
private void loadContext(File folder) {
String path = folder.getName();
if ("ROOT".equals(path))
path = "/";
else
path = "/" + path;
String docBase = folder.getAbsolutePath();
Context context = new Context(path,docBase,this, true);
contextMap.put(context.getPath(), context);
}
public Context getContext(String path) {
return contextMap.get(path);
}
public void reload(Context context) {
LogFactory.get().info("Reloading Context with name [{}] has started", context.getPath());
String path = context.getPath();
String docBase = context.getDocBase();
boolean reloadable = context.isReloadable();
// stop
context.stop();
// remove
contextMap.remove(path);
// allocate new context
Context newContext = new Context(path, docBase, this, reloadable);
// assign it to map
contextMap.put(newContext.getPath(), newContext);
LogFactory.get().info("Reloading Context with name [{}] has completed", context.getPath());
}
public void load(File folder) {
String path = folder.getName();
if ("ROOT".equals(path))
path = "/";
else
path = "/" + path;
String docBase = folder.getAbsolutePath();
Context context = new Context(path, docBase, this, false);
contextMap.put(context.getPath(), context);
}
public void loadWar(File warFile) {
String fileName =warFile.getName();
String folderName = StrUtil.subBefore(fileName,".",true);
//看看是否已经有对应的 Context了
Context context= getContext("/"+folderName);
if(null!=context)
return;
//先看是否已经有对应的文件夹
File folder = new File(Constant.webappsFolder,folderName);
if(folder.exists())
return;
//移动war文件,因为jar 命令只支持解压到当前目录下
File tempWarFile = FileUtil.file(Constant.webappsFolder, folderName, fileName);
File contextFolder = tempWarFile.getParentFile();
contextFolder.mkdir();
FileUtil.copyFile(warFile, tempWarFile);
//解压
String command = "jar xvf " + fileName;
// System.out.println(command);
Process p = RuntimeUtil.exec(null, contextFolder, command);
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
//解压之后删除临时war
tempWarFile.delete();
//然后创建新的 Context
load(contextFolder);
}
private void scanWarOnWebAppsFolder() {
File folder = FileUtil.file(Constant.webappsFolder);
File[] files = folder.listFiles();
for (File file : files) {
if(!file.getName().toLowerCase().endsWith(".war"))
continue;
loadWar(file);
}
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.test;
import cn.how2j.diytomcat.util.MiniBrowser;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpUtil;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import sun.net.www.content.text.plain;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestTomcat {
private static int port = 18080;
private static String ip = "127.0.0.1";
@BeforeClass
public static void beforeClass() {
//所有测试开始前看diy tomcat 是否已经启动了
if(NetUtil.isUsableLocalPort(port)) {
System.err.println("请先启动 位于端口: " +port+ " 的diy tomcat,否则无法进行单元测试");
System.exit(1);
}
else {
System.out.println("检测到 diy tomcat已经启动,开始进行单元测试");
}
}
@Test
public void testHelloTomcat() {
String html = getContentString("/");
Assert.assertEquals(html,"Hello DIY Tomcat from how2j.cn");
}
@Test
public void testTimeConsumeHtml() throws InterruptedException {
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 20, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(10));
TimeInterval timeInterval = DateUtil.timer();
for(int i = 0; i<3; i++){
threadPool.execute(new Runnable(){
public void run() {
getContentString("/timeConsume.html");
}
});
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.HOURS);
long duration = timeInterval.intervalMs();
Assert.assertTrue(duration < 3000);
}
@Test
public void testaIndex() {
String html = getContentString("/a");
Assert.assertEquals(html,"Hello DIY Tomcat from index.html@a");
}
@Test
public void testbIndex() {
String html = getContentString("/b/");
Assert.assertEquals(html,"Hello DIY Tomcat from index.html@b");
}
@Test
public void test404() {
String response = getHttpString("/not_exist.html");
containAssert(response, "HTTP/1.1 404 Not Found");
}
@Test
public void test500() {
String response = getHttpString("/500.html");
containAssert(response, "HTTP/1.1 500 Internal Server Error");
}
@Test
public void testaTxt() {
String response = getHttpString("/a.txt");
containAssert(response, "Content-Type: text/plain");
}
@Test
public void testPNG() {
byte[] bytes = getContentBytes("/logo.png");
int pngFileLength = 1672;
Assert.assertEquals(pngFileLength, bytes.length);
}
@Test
public void testPDF() {
byte[] bytes = getContentBytes("/etf.pdf");
int pngFileLength = 3590775;
Assert.assertEquals(pngFileLength, bytes.length);
}
@Test
public void testhello() {
String html = getContentString("/j2ee/hello");
Assert.assertEquals(html,"Hello DIY Tomcat from HelloServlet");
}
@Test
public void testJavawebHello() {
String html = getContentString("/javaweb/hello");
containAssert(html,"Hello DIY Tomcat from HelloServlet@javaweb");
}
@Test
public void testJavawebHelloSingleton() {
String html1 = getContentString("/javaweb/hello");
String html2 = getContentString("/javaweb/hello");
Assert.assertEquals(html1,html2);
}
@Test
public void testgetParam() {
String uri = "/javaweb/param";
String url = StrUtil.format("http://{}:{}{}", ip,port,uri);
Map<String,Object> params = new HashMap<>();
params.put("name","meepo");
String html = MiniBrowser.getContentString(url, params, true);
Assert.assertEquals(html,"get name:meepo");
}
@Test
public void testpostParam() {
String uri = "/javaweb/param";
String url = StrUtil.format("http://{}:{}{}", ip,port,uri);
Map<String,Object> params = new HashMap<>();
params.put("name","meepo");
String html = MiniBrowser.getContentString(url, params, false);
Assert.assertEquals(html,"post name:meepo");
}
@Test
public void testheader() {
String html = getContentString("/javaweb/header");
Assert.assertEquals(html,"how2j mini brower / java1.8");
}
@Test
public void testsetCookie() {
String http = getHttpString("/javaweb/setCookie");
containAssert(http,"Set-Cookie: name=Gareen(cookie); Expires=");
}
@Test
public void testgetCookie() throws IOException {
String url = StrUtil.format("http://{}:{}{}", ip,port,"/javaweb/getCookie");
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestProperty("Cookie","name=Gareen(cookie)");
conn.connect();
InputStream is = conn.getInputStream();
String html = IoUtil.read(is, "utf-8");
containAssert(html,"name:Gareen(cookie)");
}
@Test
public void testSession() throws IOException {
String jsessionid = getContentString("/javaweb/setSession");
if(null!=jsessionid)
jsessionid = jsessionid.trim();
String url = StrUtil.format("http://{}:{}{}", ip,port,"/javaweb/getSession");
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestProperty("Cookie","JSESSIONID="+jsessionid);
conn.connect();
InputStream is = conn.getInputStream();
String html = IoUtil.read(is, "utf-8");
containAssert(html,"Gareen(session)");
}
@Test
public void testGzip() {
byte[] gzipContent = getContentBytes("/",true);
byte[] unGzipContent = ZipUtil.unGzip(gzipContent);
String html = new String(unGzipContent);
Assert.assertEquals(html, "Hello DIY Tomcat from how2j.cn");
}
@Test
public void testJsp() {
String html = getContentString("/javaweb/");
Assert.assertEquals(html, "hello jsp@javaweb");
}
@Test
public void testClientJump(){
String http_servlet = getHttpString("/javaweb/jump1");
containAssert(http_servlet,"HTTP/1.1 302 Found");
String http_jsp = getHttpString("/javaweb/jump1.jsp");
containAssert(http_jsp,"HTTP/1.1 302 Found");
}
@Test
public void testServerJump(){
String http_servlet = getHttpString("/javaweb/jump2");
containAssert(http_servlet,"Hello DIY Tomcat from HelloServlet");
}
@Test
public void testServerJumpWithAttributes(){
String http_servlet = getHttpString("/javaweb/jump2");
containAssert(http_servlet,"Hello DIY Tomcat from HelloServlet@javaweb, the name is gareen");
}
@Test
public void testJavaweb0Hello() {
String html = getContentString("/javaweb0/hello");
containAssert(html,"Hello DIY Tomcat from HelloServlet@javaweb");
}
private byte[] getContentBytes(String uri) {
return getContentBytes(uri,false);
}
private byte[] getContentBytes(String uri,boolean gzip) {
String url = StrUtil.format("http://{}:{}{}", ip,port,uri);
return MiniBrowser.getContentBytes(url,gzip);
}
private String getContentString(String uri) {
String url = StrUtil.format("http://{}:{}{}", ip,port,uri);
String content = MiniBrowser.getContentString(url);
return content;
}
private String getHttpString(String uri) {
String url = StrUtil.format("http://{}:{}{}", ip,port,uri);
String http = MiniBrowser.getHttpString(url);
return http;
}
private void containAssert(String html, String string) {
boolean match = StrUtil.containsAny(html, string);
Assert.assertTrue(match);
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2020-06-19
站长,这样是不是简洁一点...
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|