分享

JETTY嵌入式Web容器的开发(一)

 知识存储馆 2014-09-05

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){

 

        ContextHandlerCollection handler = 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;

         }

 

        

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多