Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0

 

  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第一个开始查找,找到则加载

 

  这里看完我们就可以再看加载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.jar
ajax.json__ezmorph-1.0.4.jar-2.2.jar
ajax.json__json-lib-2.2-jdk15.jar-2.2.jar
apache.activemq-5.3.0-patch.jar
apache.geronimo.j2ee-management-1.0.1.jar
apache.geronimo.jms-1.1.1.jar
apache.hadoop.zookeeper-3.3.3.jar
authz.core-1.0.0-20120801.064744-6.jar
authz.member-1.0.0-20120802.004050-7.jar
authz.webx3-1.0.0-20120718.012318-8.jar
base.common-1.0.9.6.jar
base.dal-1.0.9.6.jar
base.privilege-1.0.9.6.jar
base.resource-1.0.9.6.jar
bouncycastle.provider-1.38-jdk15.jar
brand_shared.api-1.0-SNAPSHOT.jar
buc.sso.client-0.1.9.jar
buc.sso.common-0.1.9.jar
cat.client-2.0.0.jar
caucho.hessian-3.2.0.jar
cmshollywood.dispatcher-1.0.0-20111227.062653-5.jar
codehaus.groovy-1.6.3.jar
codehaus.jackson.core-1.5.4.jar
codehaus.jackson.mapper-1.5.4.jar
codehaus.mvel-2.0.6.jar
concurrent-0.0.0.jar
dragoon-common-2.7.0.jar
druid-0.2.1.jar
dubbo-1.0.99-dummy.jar
dubbo.all-2.0-20120731.012520-32.jar
dubbo.core.api-1.0.99-dummy.jar
dubbo.core.common-1.0.99-dummy.jar
dubbo.core.registry.default-1.0.99-dummy.jar
dubbo.core.rpc-1.0.99-dummy.jar
dubbo.core.rpc.dbo-1.0.99-dummy.jar
dubbo.core.rpc.http-1.0.99-dummy.jar
dubbo.core.service-1.0.99-dummy.jar
dubbo.core.service.server-1.0.99-dummy.jar
dubbo.hessian.lite-3.2.1.osgi_03.jar
dubbo.remoting-2.3.7.jar
dubbo.rpc-2.3.7.jar
eclipse.aspectj-1.5.4.jar
eclipse.aspectj.weaver-1.6.5.jar
fastjson-1.1.9.jar
fasttext-css-1.3.22.jar
fasttext-html-1.3.5.jar
fasttext-psoriasis-1.3.22.jar
fasttext-sec-1.3.22.jar
fasttext-segment-1.3.22.jar
fasttext-utils-1.3.22.jar
fasttext.all-1.3-20120502.125625-30.jar
gene_shared.api-1.0-20120724.041644-61.jar
gene_shared.biz.common-1.0-20120724.041541-64.jar
headquarters.biz.cache-1.3-20120614.020233-9544.jar
headquarters.biz.common-1.1-20110426.054945-9.jar
headquarters.biz.viewcache-1.2-20120711.041231-16.jar
headquarters.biz.viewcachebase-1.2-20120711.041154-19.jar
headquarters.capsule-2.5-20120711.041905-10.jar
headquarters.commons.fasttext-1.2-20120614.020242-10138.jar
headquarters.jdbc.proxy-1.1-20120711.041307-7.jar
industry.biz.apiimp-1.0-SNAPSHOT.jar
industry.biz.brand-1.0-SNAPSHOT.jar
industry.common.config-1.0-SNAPSHOT.jar
industry.dal-1.0-SNAPSHOT.jar
industry.web.brand-1.0-SNAPSHOT.jar
industry.web.brandbops-1.0-SNAPSHOT.jar
jakarta.ant-1.7.1.jar
jakarta.ant.launcher-1.7.1.jar
jakarta.commons.beanutils-1.7.0.jar
jakarta.commons.codec-1.3.jar
jakarta.commons.collections-3.2.1.jar
jakarta.commons.configuration-1.5a.jar
jakarta.commons.dbcp-1.4.jar
jakarta.commons.digester-1.8.jar
jakarta.commons.fileupload-1.2.1.jar
jakarta.commons.httpclient-3.1.jar
jakarta.commons.io-1.4.jar
jakarta.commons.jelly-1.0-RC1.jar
jakarta.commons.jexl-1.1.jar
jakarta.commons.lang-2.4.jar
jakarta.commons.logging-1.1.jar
jakarta.commons.pool-1.5.4.jar
jakarta.ecs-1.4.2.jar
jakarta.log4j-1.2.16.jar
jakarta.oro-0.0.0.jar
jakarta.taglib.jstl-0.0.0.jar
jakarta.taglib.jstl.support-0.0.0.jar
jakarta.velocity-1.6.4.jar
java.jce.cryptix-0.0.0.jar
jboss.hornetq-core-client-2.2.13.jar
jboss.jboss-netty-3.2.5.Final.jar
jdbc.mysql.mysql-connector-5.0.4.jar
jdbc.oracle-11.1.0.7.jar
libra_shared.biz-1.0-20120503.051641-5.jar
libra_shared.dal-1.0-20120315.051425-2.jar
libra_shared.service-1.0-20120315.051506-2.jar
member.service.api-1.0.0-20120801.064645-19.jar
member.service.model-1.0.0-20120801.064625-22.jar
misc.aspectj__aspectjrt-1.6.5.jar-1.6.5.jar
misc.aspectj__aspectjweaver-1.6.5.jar-1.6.5.jar
misc.htmlparser-0.0.0.jar
misc.javassist-3.9.0.GA.jar
misc.javasvn-1.3.0.jar
misc.javasvn__jna.jar-1.3.0.jar
misc.javasvn__svnkit-1.3.0.jar-1.3.0.jar
misc.javasvn__svnkit-cli-1.3.0.jar-1.3.0.jar
misc.javasvn__svnkit-javahl-1.3.0.jar-1.3.0.jar
misc.javasvn__trilead.jar-1.3.0.jar
misc.jgroup-2.4.1.1.jar
napoli.client-1.5.6.jar
napoli.common-1.5.6.jar
napoli.dragoon-1.5.6.jar
napoli.normandy-1.5.6.jar
network.core-1.2.9.jar
nonda.core-0.1.2.jar
nonda.databind-0.1.2.jar
nonda.databind-webx3-0.1.2.jar
nonda.validation-0.1.2.jar
nonda.validation-webx3-0.1.2.jar
normandy.client-1.0.0.jar
normandy.common-1.0.0.jar
objectweb.asm-0.0.0.jar
objectweb.asm.attrs-0.0.0.jar
offer.api.commons-1.0.0-20120801.063531-24.jar
offer.api.core-1.0.0-20120801.063619-53.jar
offer.api.query-1.0.0-20120717.042556-53.jar
offer.enums-1.0.0-20120717.042329-48.jar
offer.shared.codes-1.0.0-20120717.042349-38.jar
opensymphony.oscache-0.0.0.jar
opensymphony.quartz-1.6.1.jar
oracle.berkeleydb-3.2.76-patch.jar
org.slf4j.jcl-over-slf4j-1.5.6.jar
org.slf4j.jcl104-over-slf4j-1.5.6.jar
org.slf4j.slf4j-api-1.5.6.jar
org.slf4j.slf4j-log4j12-1.5.2.jar
pc2.client.agent-1.2.5.jar
pc2.common-1.2.5.jar
pivot.dal.offer-1.2-20120730.110022-70.jar
searchad.searchengine-2.5-20120717.060401-38.jar
server.jetty.jetty-core-6.1.21.jar
server.jetty.jetty-util-6.1.21.jar
sourceforge.c3p0-0.9.1.jar
sourceforge.c3p0.oracle-0.9.1.jar
sourceforge.cglib-nodep-2.1_3.jar
sourceforge.freemarker-2.3.15.jar
sourceforge.ibatis-2.3.0.jar
sourceforge.jaxen-1.1.jar
sourceforge.jline-0.9.94.jar
sourceforge.pinyin4j-2.5.0.jar
sourceforge.saxpath-1.0.jar
sourceforge.spring-2.5.6.jar
sourceforge.spring.modules.beans-2.5.6.SEC02.jar
sourceforge.spring.modules.core-2.5.6.SEC02.jar
sourceforge.spring.modules.webmvc-2.5.6.SEC02.jar
sourceforge.spring.weaving.aspects-2.5.4.jar
tagclient-1.0.0-20120712.041856-2.jar
tagcommon-1.0.0-20120712.041809-2.jar
test.junit-3.8.1.jar
test.objenesis-1.0.jar
toolkit.biz.command-1.0.jar
toolkit.common.collection-1.0.jar
toolkit.common.configuration-1.0.jar
toolkit.common.convert-1.0.jar
toolkit.common.expression-1.0.jar
toolkit.common.lang-1.0.jar
toolkit.common.logging-1.0.jar
toolkit.common.regexp-1.0.jar
toolkit.common.resourcebundle-1.0.jar
toolkit.sandbox.shy2-0.1.jar
toolkit.service.form-1.0.jar
toolkit.service.framework-1.0.jar
toolkit.service.freemarker-1.0.jar
toolkit.service.jsp-1.0.jar
toolkit.service.localization-1.0.jar
toolkit.service.mail-1.0.jar
toolkit.service.mimetype-1.0.jar
toolkit.service.naming-1.0.jar
toolkit.service.pipeline-1.0.jar
toolkit.service.pool-1.0.jar
toolkit.service.pull-1.0.jar
toolkit.service.resource-1.0.jar
toolkit.service.rundata-1.0.jar
toolkit.service.spring-1.0.jar
toolkit.service.template-1.0.jar
toolkit.service.threadcontext-1.0.jar
toolkit.service.upload-1.0.jar
toolkit.service.uribroker-1.0.jar
toolkit.service.velocity-1.0.jar
toolkit.webx.framework-2.0.jar
toolkit.webx.request-2.0.jar
toolkit.webx.turbine-2.0.jar
toolkit.webx.turbine.tld-2.0.jar
webx2-2.0.1.jar
webx3.compat-3.0.6.jar
webx3.core-3.0.6.jar
webx3.extension.migration.china-1.0-20120627.081453-1.jar
webx3.extension.rpc-0.2.6.jar
whalin.memcached-0.0.0.jar
work_shared.common-1.0-20120528.090917-5.jar
work_shared.dal-1.0-20120531.040958-10.jar
work_shared.service-1.0-20120528.091004-9.jar
xml.apis-1.0.b2.jar
xml.apis.css-1.3.jar
xml.commons-resolver-0.0.0.jar
xml.dom4j-1.6.1.jar
xml.jox-0.0.0.jar
xml.jox__dtdparser-1.13.jar-0.0.0.jar
xml.jox__jox-1.16.jar-0.0.0.jar
xml.nekohtml-1.9.9.jar
xml.xerces-0.0.0.jar
xml.xmlgraphics-1.7.jar
xml.xmlgraphics__batik-css-1.7.jar-1.7.jar
xml.xmlgraphics__batik-util-1.6-1.jar-1.7.jar
xml.xpp3-1.1.4c.jar
xml.xstream-1.3.1.jar
zk.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;
    }

 

 

作者:zhongweijian 发表于2013-1-28 22:54:23 原文链接
阅读:44 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>