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

http://blog.csdn.net/zhongweijian/article/details/8550801

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的加载顺序又是怎么样的呢?

   

[java] view plain copy
  1. @Override  
  2.    public void configure(WebAppContext context) throws Exception  
  3.    {  
  4.        //cannot configure if the context is already started  
  5.        if (context.isStarted())  
  6.        {  
  7.            if (Log.isDebugEnabled()){Log.debug("Cannot configure webapp "+context+" after it is started");}  
  8.            return;  
  9.        }  
  10.   
  11.        Resource web_inf = context.getWebInf();  
  12.   
  13.        // Add WEB-INF classes and lib classpaths  
  14.        if (web_inf != null && web_inf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)  
  15.        {  
  16.            // Look for classes directory  
  17.            Resource classes= web_inf.addPath("classes/");  
  18.            if (classes.exists())  
  19.                ((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);  
  20.   
  21.            // Look for jars  
  22.            Resource lib= web_inf.addPath("lib/");  
  23.            if (lib.exists() || lib.isDirectory())  
  24.                ((WebAppClassLoader)context.getClassLoader()).addJars(lib);  
  25.        }  
  26.          
  27.        // Look for extra resource  
  28.        List<Resource> resources = (List<Resource>)context.getAttribute(RESOURCE_URLS);  
  29.        if (resources!=null)  
  30.        {  
  31.            Resource[] collection=new Resource[resources.size()+1];  
  32.            int i=0;  
  33.            collection[i++]=context.getBaseResource();  
  34.            for (Resource resource : resources)  
  35.                collection[i++]=resource;  
  36.            context.setBaseResource(new ResourceCollection(collection));  
  37.        }  
  38.    }  

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

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

 

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

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

   

[java] view plain copy
  1. /* ------------------------------------------------------------ */  
  2. /** Add elements to the class path for the context from the jar and zip files found 
  3.  *  in the specified resource. 
  4.  * @param lib the resource that contains the jar and/or zip files. 
  5.  */  
  6. public void addJars(Resource lib)  
  7. {  
  8.     if (lib.exists() && lib.isDirectory())  
  9.     {  
  10.         String[] files=lib.list();  
  11.         for (int f=0;files!=null && f<files.length;f++)  
  12.         {  
  13.             try   
  14.             {  
  15.                 Resource fn=lib.addPath(files[f]);  
  16.                 String fnlc=fn.getName().toLowerCase();  
  17.                 if (!fn.isDirectory() && isFileSupported(fnlc))  
  18.                 {  
  19.                     String jar=fn.toString();  
  20.                     jar=StringUtil.replace(jar, ",""%2C");  
  21.                     jar=StringUtil.replace(jar, ";""%3B");  
  22.                     addClassPath(jar);  
  23.                 }  
  24.             }  
  25.             catch (Exception ex)  
  26.             {  
  27.                 Log.warn(Log.EXCEPTION,ex);  
  28.             }  
  29.         }  
  30.     }  
  31. }  

 

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

[java] view plain copy
  1. /* --------------------------------------------------------- */  
  2. /** 
  3.  * Returns a list of resources contained in the given resource 
  4.  */  
  5. @Override  
  6. public String[] list()  
  7. {  
  8.     String[] list =_file.list();  
  9.     if (list==null)  
  10.         return null;  
  11.     for (int i=list.length;i-->0;)  
  12.     {  
  13.         if (new File(_file,list[i]).isDirectory() &&  
  14.             !list[i].endsWith("/"))  
  15.             list[i]+="/";  
  16.     }  
  17.     return list;  
  18. }  

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

[java] view plain copy
  1. import java.io.File;  
  2.   
  3. public class FileListTest {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         String[] files = new File("E:\\project\\20120801_161305_1_industry\\bundle\\war\\target\\industry.bundle.war-1.0-SNAPSHOT\\WEB-INF\\lib").list();  
  10.         for(String str: files){  
  11.             System.out.println(str);  
  12.         }  
  13.     }  
  14.   
  15. }  

 

[java] view plain copy
  1. ajax.jsonlib-2.2.jar  
  2. ajax.json__ezmorph-1.0.4.jar-2.2.jar  
  3. ajax.json__json-lib-2.2-jdk15.jar-2.2.jar  
  4. apache.activemq-5.3.0-patch.jar  
  5. apache.geronimo.j2ee-management-1.0.1.jar  
  6. apache.geronimo.jms-1.1.1.jar  
  7. apache.hadoop.zookeeper-3.3.3.jar  
  8. authz.core-1.0.0-20120801.064744-6.jar  
  9. authz.member-1.0.0-20120802.004050-7.jar  
  10. authz.webx3-1.0.0-20120718.012318-8.jar  
  11. base.common-1.0.9.6.jar  
  12. base.dal-1.0.9.6.jar  
  13. base.privilege-1.0.9.6.jar  
  14. base.resource-1.0.9.6.jar  
  15. bouncycastle.provider-1.38-jdk15.jar  
  16. brand_shared.api-1.0-SNAPSHOT.jar  
  17. buc.sso.client-0.1.9.jar  
  18. buc.sso.common-0.1.9.jar  
  19. cat.client-2.0.0.jar  
  20. caucho.hessian-3.2.0.jar  
  21. cmshollywood.dispatcher-1.0.0-20111227.062653-5.jar  
  22. codehaus.groovy-1.6.3.jar  
  23. codehaus.jackson.core-1.5.4.jar  
  24. codehaus.jackson.mapper-1.5.4.jar  
  25. codehaus.mvel-2.0.6.jar  
  26. concurrent-0.0.0.jar  
  27. dragoon-common-2.7.0.jar  
  28. druid-0.2.1.jar  
  29. dubbo-1.0.99-dummy.jar  
  30. dubbo.all-2.0-20120731.012520-32.jar  
  31. dubbo.core.api-1.0.99-dummy.jar  
  32. dubbo.core.common-1.0.99-dummy.jar  
  33. dubbo.core.registry.default-1.0.99-dummy.jar  
  34. dubbo.core.rpc-1.0.99-dummy.jar  
  35. dubbo.core.rpc.dbo-1.0.99-dummy.jar  
  36. dubbo.core.rpc.http-1.0.99-dummy.jar  
  37. dubbo.core.service-1.0.99-dummy.jar  
  38. dubbo.core.service.server-1.0.99-dummy.jar  
  39. dubbo.hessian.lite-3.2.1.osgi_03.jar  
  40. dubbo.remoting-2.3.7.jar  
  41. dubbo.rpc-2.3.7.jar  
  42. eclipse.aspectj-1.5.4.jar  
  43. eclipse.aspectj.weaver-1.6.5.jar  
  44. fastjson-1.1.9.jar  
  45. fasttext-css-1.3.22.jar  
  46. fasttext-html-1.3.5.jar  
  47. fasttext-psoriasis-1.3.22.jar  
  48. fasttext-sec-1.3.22.jar  
  49. fasttext-segment-1.3.22.jar  
  50. fasttext-utils-1.3.22.jar  
  51. fasttext.all-1.3-20120502.125625-30.jar  
  52. gene_shared.api-1.0-20120724.041644-61.jar  
  53. gene_shared.biz.common-1.0-20120724.041541-64.jar  
  54. headquarters.biz.cache-1.3-20120614.020233-9544.jar  
  55. headquarters.biz.common-1.1-20110426.054945-9.jar  
  56. headquarters.biz.viewcache-1.2-20120711.041231-16.jar  
  57. headquarters.biz.viewcachebase-1.2-20120711.041154-19.jar  
  58. headquarters.capsule-2.5-20120711.041905-10.jar  
  59. headquarters.commons.fasttext-1.2-20120614.020242-10138.jar  
  60. headquarters.jdbc.proxy-1.1-20120711.041307-7.jar  
  61. industry.biz.apiimp-1.0-SNAPSHOT.jar  
  62. industry.biz.brand-1.0-SNAPSHOT.jar  
  63. industry.common.config-1.0-SNAPSHOT.jar  
  64. industry.dal-1.0-SNAPSHOT.jar  
  65. industry.web.brand-1.0-SNAPSHOT.jar  
  66. industry.web.brandbops-1.0-SNAPSHOT.jar  
  67. jakarta.ant-1.7.1.jar  
  68. jakarta.ant.launcher-1.7.1.jar  
  69. jakarta.commons.beanutils-1.7.0.jar  
  70. jakarta.commons.codec-1.3.jar  
  71. jakarta.commons.collections-3.2.1.jar  
  72. jakarta.commons.configuration-1.5a.jar  
  73. jakarta.commons.dbcp-1.4.jar  
  74. jakarta.commons.digester-1.8.jar  
  75. jakarta.commons.fileupload-1.2.1.jar  
  76. jakarta.commons.httpclient-3.1.jar  
  77. jakarta.commons.io-1.4.jar  
  78. jakarta.commons.jelly-1.0-RC1.jar  
  79. jakarta.commons.jexl-1.1.jar  
  80. jakarta.commons.lang-2.4.jar  
  81. jakarta.commons.logging-1.1.jar  
  82. jakarta.commons.pool-1.5.4.jar  
  83. jakarta.ecs-1.4.2.jar  
  84. jakarta.log4j-1.2.16.jar  
  85. jakarta.oro-0.0.0.jar  
  86. jakarta.taglib.jstl-0.0.0.jar  
  87. jakarta.taglib.jstl.support-0.0.0.jar  
  88. jakarta.velocity-1.6.4.jar  
  89. java.jce.cryptix-0.0.0.jar  
  90. jboss.hornetq-core-client-2.2.13.jar  
  91. jboss.jboss-netty-3.2.5.Final.jar  
  92. jdbc.mysql.mysql-connector-5.0.4.jar  
  93. jdbc.oracle-11.1.0.7.jar  
  94. libra_shared.biz-1.0-20120503.051641-5.jar  
  95. libra_shared.dal-1.0-20120315.051425-2.jar  
  96. libra_shared.service-1.0-20120315.051506-2.jar  
  97. member.service.api-1.0.0-20120801.064645-19.jar  
  98. member.service.model-1.0.0-20120801.064625-22.jar  
  99. misc.aspectj__aspectjrt-1.6.5.jar-1.6.5.jar  
  100. misc.aspectj__aspectjweaver-1.6.5.jar-1.6.5.jar  
  101. misc.htmlparser-0.0.0.jar  
  102. misc.javassist-3.9.0.GA.jar  
  103. misc.javasvn-1.3.0.jar  
  104. misc.javasvn__jna.jar-1.3.0.jar  
  105. misc.javasvn__svnkit-1.3.0.jar-1.3.0.jar  
  106. misc.javasvn__svnkit-cli-1.3.0.jar-1.3.0.jar  
  107. misc.javasvn__svnkit-javahl-1.3.0.jar-1.3.0.jar  
  108. misc.javasvn__trilead.jar-1.3.0.jar  
  109. misc.jgroup-2.4.1.1.jar  
  110. napoli.client-1.5.6.jar  
  111. napoli.common-1.5.6.jar  
  112. napoli.dragoon-1.5.6.jar  
  113. napoli.normandy-1.5.6.jar  
  114. network.core-1.2.9.jar  
  115. nonda.core-0.1.2.jar  
  116. nonda.databind-0.1.2.jar  
  117. nonda.databind-webx3-0.1.2.jar  
  118. nonda.validation-0.1.2.jar  
  119. nonda.validation-webx3-0.1.2.jar  
  120. normandy.client-1.0.0.jar  
  121. normandy.common-1.0.0.jar  
  122. objectweb.asm-0.0.0.jar  
  123. objectweb.asm.attrs-0.0.0.jar  
  124. offer.api.commons-1.0.0-20120801.063531-24.jar  
  125. offer.api.core-1.0.0-20120801.063619-53.jar  
  126. offer.api.query-1.0.0-20120717.042556-53.jar  
  127. offer.enums-1.0.0-20120717.042329-48.jar  
  128. offer.shared.codes-1.0.0-20120717.042349-38.jar  
  129. opensymphony.oscache-0.0.0.jar  
  130. opensymphony.quartz-1.6.1.jar  
  131. oracle.berkeleydb-3.2.76-patch.jar  
  132. org.slf4j.jcl-over-slf4j-1.5.6.jar  
  133. org.slf4j.jcl104-over-slf4j-1.5.6.jar  
  134. org.slf4j.slf4j-api-1.5.6.jar  
  135. org.slf4j.slf4j-log4j12-1.5.2.jar  
  136. pc2.client.agent-1.2.5.jar  
  137. pc2.common-1.2.5.jar  
  138. pivot.dal.offer-1.2-20120730.110022-70.jar  
  139. searchad.searchengine-2.5-20120717.060401-38.jar  
  140. server.jetty.jetty-core-6.1.21.jar  
  141. server.jetty.jetty-util-6.1.21.jar  
  142. sourceforge.c3p0-0.9.1.jar  
  143. sourceforge.c3p0.oracle-0.9.1.jar  
  144. sourceforge.cglib-nodep-2.1_3.jar  
  145. sourceforge.freemarker-2.3.15.jar  
  146. sourceforge.ibatis-2.3.0.jar  
  147. sourceforge.jaxen-1.1.jar  
  148. sourceforge.jline-0.9.94.jar  
  149. sourceforge.pinyin4j-2.5.0.jar  
  150. sourceforge.saxpath-1.0.jar  
  151. sourceforge.spring-2.5.6.jar  
  152. sourceforge.spring.modules.beans-2.5.6.SEC02.jar  
  153. sourceforge.spring.modules.core-2.5.6.SEC02.jar  
  154. sourceforge.spring.modules.webmvc-2.5.6.SEC02.jar  
  155. sourceforge.spring.weaving.aspects-2.5.4.jar  
  156. tagclient-1.0.0-20120712.041856-2.jar  
  157. tagcommon-1.0.0-20120712.041809-2.jar  
  158. test.junit-3.8.1.jar  
  159. test.objenesis-1.0.jar  
  160. toolkit.biz.command-1.0.jar  
  161. toolkit.common.collection-1.0.jar  
  162. toolkit.common.configuration-1.0.jar  
  163. toolkit.common.convert-1.0.jar  
  164. toolkit.common.expression-1.0.jar  
  165. toolkit.common.lang-1.0.jar  
  166. toolkit.common.logging-1.0.jar  
  167. toolkit.common.regexp-1.0.jar  
  168. toolkit.common.resourcebundle-1.0.jar  
  169. toolkit.sandbox.shy2-0.1.jar  
  170. toolkit.service.form-1.0.jar  
  171. toolkit.service.framework-1.0.jar  
  172. toolkit.service.freemarker-1.0.jar  
  173. toolkit.service.jsp-1.0.jar  
  174. toolkit.service.localization-1.0.jar  
  175. toolkit.service.mail-1.0.jar  
  176. toolkit.service.mimetype-1.0.jar  
  177. toolkit.service.naming-1.0.jar  
  178. toolkit.service.pipeline-1.0.jar  
  179. toolkit.service.pool-1.0.jar  
  180. toolkit.service.pull-1.0.jar  
  181. toolkit.service.resource-1.0.jar  
  182. toolkit.service.rundata-1.0.jar  
  183. toolkit.service.spring-1.0.jar  
  184. toolkit.service.template-1.0.jar  
  185. toolkit.service.threadcontext-1.0.jar  
  186. toolkit.service.upload-1.0.jar  
  187. toolkit.service.uribroker-1.0.jar  
  188. toolkit.service.velocity-1.0.jar  
  189. toolkit.webx.framework-2.0.jar  
  190. toolkit.webx.request-2.0.jar  
  191. toolkit.webx.turbine-2.0.jar  
  192. toolkit.webx.turbine.tld-2.0.jar  
  193. webx2-2.0.1.jar  
  194. webx3.compat-3.0.6.jar  
  195. webx3.core-3.0.6.jar  
  196. webx3.extension.migration.china-1.0-20120627.081453-1.jar  
  197. webx3.extension.rpc-0.2.6.jar  
  198. whalin.memcached-0.0.0.jar  
  199. work_shared.common-1.0-20120528.090917-5.jar  
  200. work_shared.dal-1.0-20120531.040958-10.jar  
  201. work_shared.service-1.0-20120528.091004-9.jar  
  202. xml.apis-1.0.b2.jar  
  203. xml.apis.css-1.3.jar  
  204. xml.commons-resolver-0.0.0.jar  
  205. xml.dom4j-1.6.1.jar  
  206. xml.jox-0.0.0.jar  
  207. xml.jox__dtdparser-1.13.jar-0.0.0.jar  
  208. xml.jox__jox-1.16.jar-0.0.0.jar  
  209. xml.nekohtml-1.9.9.jar  
  210. xml.xerces-0.0.0.jar  
  211. xml.xmlgraphics-1.7.jar  
  212. xml.xmlgraphics__batik-css-1.7.jar-1.7.jar  
  213. xml.xmlgraphics__batik-util-1.6-1.jar-1.7.jar  
  214. xml.xpp3-1.1.4c.jar  
  215. xml.xstream-1.3.1.jar  
  216. zk.zkclient-0.1.0.jar  

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

 

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

 

[java] view plain copy
  1. /* ------------------------------------------------------------ */  
  2.   @Override  
  3.   public synchronized String[] list()  
  4.   {  
  5.         
  6.       if(isDirectory() && _list==null)  
  7.       {  
  8.           ArrayList list = new ArrayList(32);  
  9.   
  10.           checkConnection();  
  11.             
  12.           JarFile jarFile=_jarFile;  
  13.           if(jarFile==null)  
  14.           {  
  15.               try  
  16.               {  
  17.                   JarURLConnection jc=(JarURLConnection)((new URL(_jarUrl)).openConnection());  
  18.                   jc.setUseCaches(getUseCaches());  
  19.                   jarFile=jc.getJarFile();  
  20.               }  
  21.               catch(Exception e)  
  22.               {  
  23.                    Log.ignore(e);  
  24.               }  
  25.           }  
  26.             
  27.           Enumeration e=jarFile.entries();  
  28.           String dir=_urlString.substring(_urlString.indexOf("!/")+2);  
  29.           while(e.hasMoreElements())  
  30.           {  
  31.                 
  32.               JarEntry entry = (JarEntry) e.nextElement();                 
  33.               String name=entry.getName().replace('\\','/');                 
  34.               if(!name.startsWith(dir) || name.length()==dir.length())  
  35.               {  
  36.                   continue;  
  37.               }  
  38.               String listName=name.substring(dir.length());                 
  39.               int dash=listName.indexOf('/');  
  40.               if (dash>=0)  
  41.               {  
  42.                   //when listing jar:file urls, you get back one  
  43.                   //entry for the dir itself, which we ignore  
  44.                   if (dash==0 && listName.length()==1)  
  45.                       continue;  
  46.                   //when listing jar:file urls, all files and  
  47.                   //subdirs have a leading /, which we remove  
  48.                   if (dash==0)  
  49.                       listName=listName.substring(dash+1, listName.length());  
  50.                   else  
  51.                       listName=listName.substring(0,dash+1);  
  52.                     
  53.                   if (list.contains(listName))  
  54.                       continue;  
  55.               }  
  56.                 
  57.               list.add(listName);  
  58.           }  
  59.             
  60.           _list=new String[list.size()];  
  61.           list.toArray(_list);  
  62.       }  
  63.       return _list;  
  64.   }  

展开阅读全文

没有更多推荐了,返回首页