首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

jetty对于包的加载顺序的处置

2013-02-05 
jetty对于包的加载顺序的处理1.问题今天在本地和测试环境用jetty跑应用发现本地环境跑起来有问题,而测试环

jetty对于包的加载顺序的处理

 

  1.问题

     今天在本地和测试环境用jetty跑应用发现本地环境跑起来有问题,而测试环境却没有问题,出现的是war包的WEB-INF的lib目录下有两个jar包a.jar和b.jar,但是这两个jar包邮两个包名和类目一样,但是功能却不一样的类,那么jetty启动加载这个类的时候用的是a.jar里面的类还是b.jar里面的类呢,出了问题只能查了。

  2.本地环境jetty加载目录里面是b.jar排在a.jar前面,而测试环境是a.jar排在b.jar前面,而记载a.jar里面的类是正确的。我们看看jetty怎么处理:

   首先jetty加载一个APP war的过程中WebInfConfiguration.java类里面加载类的顺序,优先加载的是WEB-INF/classes,然后再加载WEB-INF/lib里面的jar包,而jar的加载顺序又是怎么样的呢?

   

 @Override    public void configure(WebAppContext context) throws Exception    {        //cannot configure if the context is already started        if (context.isStarted())        {            if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp "+context+" after it is started");}            return;        }        Resource web_inf = context.getWebInf();        // Add WEB-INF classes and lib classpaths        if (web_inf != null && web_inf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)        {            // Look for classes directory            Resource classes= web_inf.addPath("classes/");            if (classes.exists())                ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);            // Look for jars            Resource lib= web_inf.addPath("lib/");            if (lib.exists() || lib.isDirectory())                ((WebAppClassLoader)context.getClassLoader()).addJars(lib);        }                // Look for extra resource        List<Resource> resources = (List<Resource>)context.getAttribute(RESOURCE_URLS);        if (resources!=null)        {            Resource[] collection=new Resource[resources.size()+1];            int i=0;            collection[i++]=context.getBaseResource();            for (Resource resource : resources)                collection[i++]=resource;            context.setBaseResource(new ResourceCollection(collection));        }    }

((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);   这是加载每个class类的过程

   addURL(url);调用jdk包的URLClassPath把每个class文件放到list的后续,每次加载类的时候从这个list第一个开始查找,找到则加载
jetty对于包的加载顺序的处置
 

  这里看完我们就可以再看加载lib下面的jar包的过程了

addJars是对lib下面所有jar包用jdk的list方法获取所有jar包,然后对每个jar进行处理。

   

    /* ------------------------ */    /** Add elements to the class path for the context from the jar and zip files found     *  in the specified resource.     * @param lib the resource that contains the jar and/or zip files.     */    public void addJars(Resource lib)    {        if (lib.exists() && lib.isDirectory())        {            String[] files=lib.list();            for (int f=0;files!=null && f<files.length;f++)            {                try                 {                    Resource fn=lib.addPath(files[f]);                    String fnlc=fn.getName().toLowerCase();                    if (!fn.isDirectory() && isFileSupported(fnlc))                    {                        String jar=fn.toString();                        jar=StringUtil.replace(jar, ",", "%2C");                        jar=StringUtil.replace(jar, ";", "%3B");                        addClassPath(jar);                    }                }                catch (Exception ex)                {                    Log.warn(Log.EXCEPTION,ex);                }            }        }    }

 

 String[] files=lib.list(); 这一行就是关系jar包的加载顺序关键点,这里列出来的顺序就是jar包的加载顺序,

    /* --------------------- */    /**     * Returns a list of resources contained in the given resource     */    @Override    public String[] list()    {        String[] list =_file.list();        if (list==null)            return null;        for (int i=list.length;i-->0;)        {            if (new File(_file,list[i]).isDirectory() &&                !list[i].endsWith("/"))                list[i]+="/";        }        return list;    }

 最终 String[] list =_file.list();就是java.io.File的list方法,而这个方法的顺序是怎么样,只能看jdk的代码了。我们写个程序简单测试下

import java.io.File;public class FileListTest {/** * @param args */public static void main(String[] args) {String[] files = new File("E:\\project\\20120801_161305_1_industry\\bundle\\war\\target\\industry.bundle.war-1.0-SNAPSHOT\\WEB-INF\\lib").list();for(String str: files){System.out.println(str);}}}

 

ajax.jsonlib-2.2.jarajax.json__ezmorph-1.0.4.jar-2.2.jarajax.json__json-lib-2.2-jdk15.jar-2.2.jarapache.activemq-5.3.0-patch.jarapache.geronimo.j2ee-management-1.0.1.jarapache.geronimo.jms-1.1.1.jarapache.hadoop.zookeeper-3.3.3.jarauthz.core-1.0.0-20120801.064744-6.jarauthz.member-1.0.0-20120802.004050-7.jarauthz.webx3-1.0.0-20120718.012318-8.jarbase.common-1.0.9.6.jarbase.dal-1.0.9.6.jarbase.privilege-1.0.9.6.jarbase.resource-1.0.9.6.jarbouncycastle.provider-1.38-jdk15.jarbrand_shared.api-1.0-SNAPSHOT.jarbuc.sso.client-0.1.9.jarbuc.sso.common-0.1.9.jarcat.client-2.0.0.jarcaucho.hessian-3.2.0.jarcmshollywood.dispatcher-1.0.0-20111227.062653-5.jarcodehaus.groovy-1.6.3.jarcodehaus.jackson.core-1.5.4.jarcodehaus.jackson.mapper-1.5.4.jarcodehaus.mvel-2.0.6.jarconcurrent-0.0.0.jardragoon-common-2.7.0.jardruid-0.2.1.jardubbo-1.0.99-dummy.jardubbo.all-2.0-20120731.012520-32.jardubbo.core.api-1.0.99-dummy.jardubbo.core.common-1.0.99-dummy.jardubbo.core.registry.default-1.0.99-dummy.jardubbo.core.rpc-1.0.99-dummy.jardubbo.core.rpc.dbo-1.0.99-dummy.jardubbo.core.rpc.http-1.0.99-dummy.jardubbo.core.service-1.0.99-dummy.jardubbo.core.service.server-1.0.99-dummy.jardubbo.hessian.lite-3.2.1.osgi_03.jardubbo.remoting-2.3.7.jardubbo.rpc-2.3.7.jareclipse.aspectj-1.5.4.jareclipse.aspectj.weaver-1.6.5.jarfastjson-1.1.9.jarfasttext-css-1.3.22.jarfasttext-html-1.3.5.jarfasttext-psoriasis-1.3.22.jarfasttext-sec-1.3.22.jarfasttext-segment-1.3.22.jarfasttext-utils-1.3.22.jarfasttext.all-1.3-20120502.125625-30.jargene_shared.api-1.0-20120724.041644-61.jargene_shared.biz.common-1.0-20120724.041541-64.jarheadquarters.biz.cache-1.3-20120614.020233-9544.jarheadquarters.biz.common-1.1-20110426.054945-9.jarheadquarters.biz.viewcache-1.2-20120711.041231-16.jarheadquarters.biz.viewcachebase-1.2-20120711.041154-19.jarheadquarters.capsule-2.5-20120711.041905-10.jarheadquarters.commons.fasttext-1.2-20120614.020242-10138.jarheadquarters.jdbc.proxy-1.1-20120711.041307-7.jarindustry.biz.apiimp-1.0-SNAPSHOT.jarindustry.biz.brand-1.0-SNAPSHOT.jarindustry.common.config-1.0-SNAPSHOT.jarindustry.dal-1.0-SNAPSHOT.jarindustry.web.brand-1.0-SNAPSHOT.jarindustry.web.brandbops-1.0-SNAPSHOT.jarjakarta.ant-1.7.1.jarjakarta.ant.launcher-1.7.1.jarjakarta.commons.beanutils-1.7.0.jarjakarta.commons.codec-1.3.jarjakarta.commons.collections-3.2.1.jarjakarta.commons.configuration-1.5a.jarjakarta.commons.dbcp-1.4.jarjakarta.commons.digester-1.8.jarjakarta.commons.fileupload-1.2.1.jarjakarta.commons.httpclient-3.1.jarjakarta.commons.io-1.4.jarjakarta.commons.jelly-1.0-RC1.jarjakarta.commons.jexl-1.1.jarjakarta.commons.lang-2.4.jarjakarta.commons.logging-1.1.jarjakarta.commons.pool-1.5.4.jarjakarta.ecs-1.4.2.jarjakarta.log4j-1.2.16.jarjakarta.oro-0.0.0.jarjakarta.taglib.jstl-0.0.0.jarjakarta.taglib.jstl.support-0.0.0.jarjakarta.velocity-1.6.4.jarjava.jce.cryptix-0.0.0.jarjboss.hornetq-core-client-2.2.13.jarjboss.jboss-netty-3.2.5.Final.jarjdbc.mysql.mysql-connector-5.0.4.jarjdbc.oracle-11.1.0.7.jarlibra_shared.biz-1.0-20120503.051641-5.jarlibra_shared.dal-1.0-20120315.051425-2.jarlibra_shared.service-1.0-20120315.051506-2.jarmember.service.api-1.0.0-20120801.064645-19.jarmember.service.model-1.0.0-20120801.064625-22.jarmisc.aspectj__aspectjrt-1.6.5.jar-1.6.5.jarmisc.aspectj__aspectjweaver-1.6.5.jar-1.6.5.jarmisc.htmlparser-0.0.0.jarmisc.javassist-3.9.0.GA.jarmisc.javasvn-1.3.0.jarmisc.javasvn__jna.jar-1.3.0.jarmisc.javasvn__svnkit-1.3.0.jar-1.3.0.jarmisc.javasvn__svnkit-cli-1.3.0.jar-1.3.0.jarmisc.javasvn__svnkit-javahl-1.3.0.jar-1.3.0.jarmisc.javasvn__trilead.jar-1.3.0.jarmisc.jgroup-2.4.1.1.jarnapoli.client-1.5.6.jarnapoli.common-1.5.6.jarnapoli.dragoon-1.5.6.jarnapoli.normandy-1.5.6.jarnetwork.core-1.2.9.jarnonda.core-0.1.2.jarnonda.databind-0.1.2.jarnonda.databind-webx3-0.1.2.jarnonda.validation-0.1.2.jarnonda.validation-webx3-0.1.2.jarnormandy.client-1.0.0.jarnormandy.common-1.0.0.jarobjectweb.asm-0.0.0.jarobjectweb.asm.attrs-0.0.0.jaroffer.api.commons-1.0.0-20120801.063531-24.jaroffer.api.core-1.0.0-20120801.063619-53.jaroffer.api.query-1.0.0-20120717.042556-53.jaroffer.enums-1.0.0-20120717.042329-48.jaroffer.shared.codes-1.0.0-20120717.042349-38.jaropensymphony.oscache-0.0.0.jaropensymphony.quartz-1.6.1.jaroracle.berkeleydb-3.2.76-patch.jarorg.slf4j.jcl-over-slf4j-1.5.6.jarorg.slf4j.jcl104-over-slf4j-1.5.6.jarorg.slf4j.slf4j-api-1.5.6.jarorg.slf4j.slf4j-log4j12-1.5.2.jarpc2.client.agent-1.2.5.jarpc2.common-1.2.5.jarpivot.dal.offer-1.2-20120730.110022-70.jarsearchad.searchengine-2.5-20120717.060401-38.jarserver.jetty.jetty-core-6.1.21.jarserver.jetty.jetty-util-6.1.21.jarsourceforge.c3p0-0.9.1.jarsourceforge.c3p0.oracle-0.9.1.jarsourceforge.cglib-nodep-2.1_3.jarsourceforge.freemarker-2.3.15.jarsourceforge.ibatis-2.3.0.jarsourceforge.jaxen-1.1.jarsourceforge.jline-0.9.94.jarsourceforge.pinyin4j-2.5.0.jarsourceforge.saxpath-1.0.jarsourceforge.spring-2.5.6.jarsourceforge.spring.modules.beans-2.5.6.SEC02.jarsourceforge.spring.modules.core-2.5.6.SEC02.jarsourceforge.spring.modules.webmvc-2.5.6.SEC02.jarsourceforge.spring.weaving.aspects-2.5.4.jartagclient-1.0.0-20120712.041856-2.jartagcommon-1.0.0-20120712.041809-2.jartest.junit-3.8.1.jartest.objenesis-1.0.jartoolkit.biz.command-1.0.jartoolkit.common.collection-1.0.jartoolkit.common.configuration-1.0.jartoolkit.common.convert-1.0.jartoolkit.common.expression-1.0.jartoolkit.common.lang-1.0.jartoolkit.common.logging-1.0.jartoolkit.common.regexp-1.0.jartoolkit.common.resourcebundle-1.0.jartoolkit.sandbox.shy2-0.1.jartoolkit.service.form-1.0.jartoolkit.service.framework-1.0.jartoolkit.service.freemarker-1.0.jartoolkit.service.jsp-1.0.jartoolkit.service.localization-1.0.jartoolkit.service.mail-1.0.jartoolkit.service.mimetype-1.0.jartoolkit.service.naming-1.0.jartoolkit.service.pipeline-1.0.jartoolkit.service.pool-1.0.jartoolkit.service.pull-1.0.jartoolkit.service.resource-1.0.jartoolkit.service.rundata-1.0.jartoolkit.service.spring-1.0.jartoolkit.service.template-1.0.jartoolkit.service.threadcontext-1.0.jartoolkit.service.upload-1.0.jartoolkit.service.uribroker-1.0.jartoolkit.service.velocity-1.0.jartoolkit.webx.framework-2.0.jartoolkit.webx.request-2.0.jartoolkit.webx.turbine-2.0.jartoolkit.webx.turbine.tld-2.0.jarwebx2-2.0.1.jarwebx3.compat-3.0.6.jarwebx3.core-3.0.6.jarwebx3.extension.migration.china-1.0-20120627.081453-1.jarwebx3.extension.rpc-0.2.6.jarwhalin.memcached-0.0.0.jarwork_shared.common-1.0-20120528.090917-5.jarwork_shared.dal-1.0-20120531.040958-10.jarwork_shared.service-1.0-20120528.091004-9.jarxml.apis-1.0.b2.jarxml.apis.css-1.3.jarxml.commons-resolver-0.0.0.jarxml.dom4j-1.6.1.jarxml.jox-0.0.0.jarxml.jox__dtdparser-1.13.jar-0.0.0.jarxml.jox__jox-1.16.jar-0.0.0.jarxml.nekohtml-1.9.9.jarxml.xerces-0.0.0.jarxml.xmlgraphics-1.7.jarxml.xmlgraphics__batik-css-1.7.jar-1.7.jarxml.xmlgraphics__batik-util-1.6-1.jar-1.7.jarxml.xpp3-1.1.4c.jarxml.xstream-1.3.1.jarzk.zkclient-0.1.0.jar

 从这里看出来就是jar包名称的字母序的顺序。

 

从jar里面解析出class类看下:JarFileResource

 

  /* ------------------------ */    @Override    public synchronized String[] list()    {                if(isDirectory() && _list==null)        {            ArrayList list = new ArrayList(32);            checkConnection();                        JarFile jarFile=_jarFile;            if(jarFile==null)            {                try                {                    JarURLConnection jc=(JarURLConnection)((new URL(_jarUrl)).openConnection());                    jc.setUseCaches(getUseCaches());                    jarFile=jc.getJarFile();                }                catch(Exception e)                {                     Log.ignore(e);                }            }                        Enumeration e=jarFile.entries();            String dir=_urlString.substring(_urlString.indexOf("!/")+2);            while(e.hasMoreElements())            {                                JarEntry entry = (JarEntry) e.nextElement();                               String name=entry.getName().replace('\\','/');                               if(!name.startsWith(dir) || name.length()==dir.length())                {                    continue;                }                String listName=name.substring(dir.length());                               int dash=listName.indexOf('/');                if (dash>=0)                {                    //when listing jar:file urls, you get back one                    //entry for the dir itself, which we ignore                    if (dash==0 && listName.length()==1)                        continue;                    //when listing jar:file urls, all files and                    //subdirs have a leading /, which we remove                    if (dash==0)                        listName=listName.substring(dash+1, listName.length());                    else                        listName=listName.substring(0,dash+1);                                        if (list.contains(listName))                        continue;                }                                list.add(listName);            }                        _list=new String[list.size()];            list.toArray(_list);        }        return _list;    }

 

 

热点排行