java jetty 嵌入式_JETTY嵌入式Web容器的开发(一)---基础开发

package org.jetty.demo;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import org.eclipse.jetty.server.Server;

import

org.eclipse.jetty.server.handler.ContextHandlerCollection;

import org.eclipse.jetty.webapp.WebAppContext;

import org.eclipse.jetty.xml.XmlConfiguration;

import org.xml.sax.SAXException;

public class JettyCustomServer extends Server {

private String xmlConfigPath;

private String contextPath;

private String warPath;

private String resourceBase = "./webRoot";

private String webXmlPath = "./webRoot/WEB-INF/web.xml";

public JettyCustomServer(String xmlConfigPath,String

contextPath,String resourceBase,String webXmlPath) {

this(xmlConfigPath,contextPath,resourceBase,webXmlPath,null);

}

public JettyCustomServer(String xmlConfigPath,String contextPath)

{

this(xmlConfigPath,contextPath,null,null,null);

}

public JettyCustomServer(String xmlConfigPath,String

contextPath,String warPath) {

this(xmlConfigPath,contextPath,null,null,warPath);

}

public JettyCustomServer(String xmlConfigPath,String

contextPath,String resourceBase,String webXmlPath,String warPath)

{

super();

if(StringUtils.isNotBlank(xmlConfigPath)){

this.xmlConfigPath = xmlConfigPath;

readXmlConfig();

}

if(StringUtils.isNotBlank(warPath)){

this.warPath = warPath;

if(StringUtils.isNotBlank(contextPath)){

this.contextPath = contextPath;

applyHandle(true);

}

}else{

if(StringUtils.isNotBlank(resourceBase))

this.resourceBase = resourceBase;

if(StringUtils.isNotBlank(webXmlPath))

this.webXmlPath = webXmlPath;

if(StringUtils.isNotBlank(contextPath)){

this.contextPath = contextPath;

applyHandle(false);

}

}

}

private void readXmlConfig(){

try {

XmlConfiguration configuration = new

XmlConfiguration(new FileInputStream(this.xmlConfigPath));

configuration.configure(this);

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (SAXException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

public void applyHandle(Boolean warDeployFlag){

ContextHandlerCollectionhandler = new

ContextHandlerCollection();

WebAppContext webapp = new

WebAppContext();

webapp.setContextPath(contextPath);

webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");

if(!warDeployFlag){

webapp.setResourceBase(resourceBase);

webapp.setDescriptor(webXmlPath);

}else{

webapp.setWar(warPath);

}

handler.addHandler(webapp);

super.setHandler(handler);

}

public void startServer(){

try {

super.start();

System.out.println("current

thread:"+super.getThreadPool().getThreads()+"| idle

thread:"+super.getThreadPool().getIdleThreads());

super.join();

} catch (Exception e) {

e.printStackTrace();

}

}

public String getXmlConfigPath() {

return xmlConfigPath;

}

public void setXmlConfigPath(String xmlConfigPath) {

this.xmlConfigPath = xmlConfigPath;

}

public String getContextPath() {

return contextPath;

}

public void setContextPath(String contextPath) {

this.contextPath = contextPath;

}

public String getResourceBase() {

return resourceBase;

}

public void setResourceBase(String resourceBase) {

this.resourceBase = resourceBase;

}

public String getWebXmlPath() {

return webXmlPath;

}

public void setWebXmlPath(String webXmlPath) {

this.webXmlPath = webXmlPath;

}

public String getWarPath() {

return warPath;

}

public void setWarPath(String warPath) {

this.warPath = warPath;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值