当前位置:编程学习 > JAVA >>

jetty的servlet请求路由与ContextHandlerColleection的实现

我们一般会在tomcat下面部署多个应用,每个应用都对应着一个自己的context,那么就需要一个collection将他们管理起来,而且需要对http请求进行路由,将http请求交个相应的应用来处理。。。。
这件事情在jetty中就是ContextHanlerCollection干的事情,它维护者当前jetty中部署的应用。。。将http请求交给匹配的context,然后context再转由内部的servlet来处理。。。。
好了。。还是先来看看ContextHanlerCollection的继承体系吧:
 
本身继承还是很简单的,前面已经说过了,整个jetty在这方面都还算比较简单一些。。。
其实从名字就能够知道这是一个collection,用于存储handler,其实这里的handler就是前面的文章分析过的contextHandler。。。。
 
这里我们先来看看HandlerCollection的定义吧。。。这里我们就来看看一些比较重要的内容就好了。。。。
[java]  
//添加一个handler,其实是要将其添加到最后面去,说白了就是建立一个数组  
public void addHandler(Handler handler) {  
    setHandlers((Handler[])LazyList.addToArray(getHandlers(), handler, Handler.class));  
}  
 
这个是添加handler的方法,其实也就是建立一个数组,新添加的handler将会在这个数组的最后面。。。
[java]  
//其实就是启动保存的所有的handler  
protected void doStart() throws Exception {  
    MultiException mex=new MultiException();  
    if (_handlers!=null)  
    {  
        for (int i=0;i<_handlers.length;i++)  
            try{_handlers[i].start();}catch(Throwable e){mex.add(e);}  
    }  
    super.doStart();  
    mex.ifExceptionThrow();  
}  
 
启动,其实也没啥意思,主要就是启动当前所包含的所有的handler
另外handlerCollection本身也有handle方法,不过这个方法的实现比较的扯淡,对于http请求,它会遍历所有的handler,然后直到找到一个handler将这个http请求处理了为止。。。它的实现将会在ContextHandlerCollection中被覆盖。。。
 
好了,接下来我们来看看ContextHandlerCollection的定义吧,先来看两个比较重要的属性的定义:
[java] 
private PathMap _contextMap;  //用于context的map,将contextPath与contextHandler对应起来,而且还有lazy匹配,一些前缀,后缀匹配什么的  
private Class _contextClass = ContextHandler.class;    //这里是用到的handler的类型  
 
首先前面的contextMap就是用于维护contextPath与contextHandler的对应关系,PathMap是jetty自己定义的类型,它继承自HashMap,但是对其进行了一些扩展,加上了前缀匹配,后缀匹配。。。lazy匹配什么的。。。
至于PathMap的分析留给以后吧,,,其实还算是比较有意思的东西,毕竟所有的http请求都要先到这里经由它来匹配出相应的contextHandler,还算是比较重的。。。。而且在contextHandler中又要用到它来处理path与servlet的对应关系。。。
我们来看看它的doStart方法吧:
[java]  
//启动当前的组件  
protected void doStart() throws Exception {  
    mapContexts();  //对当前所有的app的context进行map  
    super.doStart();  //父类的启动组要是启动当前所有的handler  
}  
 
这里第一个方法的执行是比较重要的,它用于创建pathMap,创建当前所有的handler与path之间的对应关系。。。然后在执行父类的doStart方法,前面已经说过,在父类中主要是进行handler的启动工作。。。
好饿了。。那么我们来看看这个重要的mapContexts方法吧:
[java] 
//用于map请求的path,这里会创建PathMap,用于path来索引相应的contextHandler  
public void mapContexts() {  
    PathMap contextMap = new PathMap();  //创建一个pathmap  
    Handler[] branches = getHandlers();  //获取所有的handler  
      
    //遍历当前的所有的handler  
    for (int b=0;branches!=null && b<branches.length;b++)  {  
        Handler[] handlers=null;  
          
        if (branches[b] instanceof ContextHandler) {   //如果是contextHandler需要新创建爱你一个数组  。。?为啥。。?   
            handlers = new Handler[]{ branches[b] };  
        } else if (branches[b] instanceof HandlerContainer) {  
            handlers = ((HandlerContainer)branches[b]).getChildHandlersByClass(ContextHandler.class);  
        } else {  
            continue;  
        }  
        //遍历当前的contextHandler  
        for (int i=0;i<handlers.length;i++) {  
            ContextHandler handler=(ContextHandler)handlers[i];  
  
            String contextPath=handler.getContextPath();  //获取当前这个handler的context的path  
            //判断不合规的contextPath  
            if (contextPath==null || contextPath.indexOf(',')>=0 || contextPath.startsWith("*"))  
                throw new IllegalArgumentException ("Illegal context spec:"+contextPath);  
  
            if(!contextPath.startsWith("/"))  //如果不是/开头的,那么给它加上  
                contextPath='/'+contextPath;  
  
            if (contextPath.length()>1) {  
                if (contextPath.endsWith("/"))  //表示一个路径下的所有,那么就为其添加*  
                    contextPath+="*";  
                else if (!contextPath.endsWith("/*"))  
                    contextPath+="/*";   //反正都是要变成/*  
            }  
            //也就是最后的contextPath都要变成类似于:/manager/*  类型的  
  
            Object contexts=contextMap.get(contextPath);     //这个path对应的所有contexts,这里可以理解为其实一个数组  
            String[] vhosts=handler.getVirtualHosts();  //获取虚拟主机名  
  
              
            if (vhosts!=null && vhosts.length>0) {  //如果有虚拟主机名字  
                Map hosts;  
  
                if (contexts instanceof Map) {  
                    hosts=(Map)contexts;  
  &nbs
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,