让Eclipse的TomcatPlugin支持Tomcat 8.x

来源:互联网 发布:宏盟媒体集团 知乎 编辑:程序博客网 时间:2024/05/16 21:04

项目原因,近期要迁移到Eclipse上开发。重新架构,自然打算都用新的版本。发现一个问题:TomcatPlugin已经支持最新的Eclipse 4.4,但Tomcat的版本却只支持到7.x。纠结啊,Tomcat 8.x已经出来许久,用不了岂不是很痛心。于是乎打算深入处理一下。

1,直接用DevloaderTomcat7.jar放到tomcat8.x中运行,提示什么getContainer方法找不到。看来Tomcat8.x的源码改动有点大。貌似只有重新构建DevLoader了。

2,解压DevloaderTomcat7.jar,里面已经包含了工程信息,直接导入Eclipse,修改依赖Jar包目录,可惜没有DevLoader类的源码。没事反编译就行了。

3,修改编译报错的地方。

4,有些地方反编译有些问题,还好Devloader.jar的源码是有的,结合着改了下,OK。

5,重新打JAR包,命名DevloaderTomcat8.jar。

放入Tomcat8.x,一起正常。

何为管理?做人、管人、理事,最重要的是能为兄弟们解决问题。好几个月没写代码了,写两句感觉挺好的。

改后的源码如下:

[java] view plain copy
  1. package org.apache.catalina.loader;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileFilter;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.LineNumberReader;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.util.ArrayList;  
  11. import java.util.Iterator;  
  12. import java.util.List;  
  13. import java.util.StringTokenizer;  
  14. import javax.servlet.ServletContext;  
  15. import org.apache.catalina.LifecycleException;  
  16.   
  17. public class DevLoader extends WebappLoader {  
  18.     private static final String info = "org.apache.catalina.loader.DevLoader/1.0";  
  19.     private String webClassPathFile = ".#webclasspath";  
  20.     private String tomcatPluginFile = ".tomcatplugin";  
  21.   
  22.     public DevLoader() {  
  23.     }  
  24.   
  25.     public DevLoader(ClassLoader parent) {  
  26.         super(parent);  
  27.     }  
  28.   
  29.     public void startInternal() throws LifecycleException {  
  30.         log("Starting DevLoader");  
  31.   
  32.         super.startInternal();  
  33.   
  34.         ClassLoader cl = super.getClassLoader();  
  35.         if (!(cl instanceof WebappClassLoader)) {  
  36.             logError("Unable to install WebappClassLoader !");  
  37.             return;  
  38.         }  
  39.         WebappClassLoader devCl = (WebappClassLoader) cl;  
  40.   
  41.         List webClassPathEntries = readWebClassPathEntries();  
  42.         StringBuffer classpath = new StringBuffer();  
  43.         for (Iterator it = webClassPathEntries.iterator(); it.hasNext();) {  
  44.             String entry = (String) it.next();  
  45.             File f = new File(entry);  
  46.             if (f.exists()) {  
  47.                 if ((f.isDirectory()) && (!(entry.endsWith("/"))))  
  48.                     f = new File(entry + "/");  
  49.                 try {  
  50.                     URL url = f.toURL();  
  51.   
  52.                     // devCl.addRepository(url.toString());  
  53.                     devCl.addURL(url);  
  54.                     classpath.append(f.toString() + File.pathSeparatorChar);  
  55.                     log("added " + url.toString());  
  56.                 } catch (MalformedURLException e) {  
  57.                     logError(entry + " invalid (MalformedURL)");  
  58.                 }  
  59.             } else {  
  60.                 logError(entry + " does not exist !");  
  61.             }  
  62.         }  
  63.   
  64.         String cp = (String) getServletContext().getAttribute("org.apache.catalina.jsp_classpath");  
  65.         StringTokenizer tokenizer = new StringTokenizer(cp, File.pathSeparatorChar + "");  
  66.         while (tokenizer.hasMoreTokens()) {  
  67.             String token = tokenizer.nextToken();  
  68.   
  69.             if ((token.charAt(0) == '/') && (token.charAt(2) == ':')) {  
  70.                 token = token.substring(1);  
  71.             }  
  72.             classpath.append(token + File.pathSeparatorChar);  
  73.         }  
  74.   
  75.         getServletContext().setAttribute("org.apache.catalina.jsp_classpath", classpath.toString());  
  76.         log("JSPCompiler Classpath = " + classpath);  
  77.     }  
  78.   
  79.     protected void log(String msg) {  
  80.         System.out.println("[DevLoader] " + msg);  
  81.     }  
  82.   
  83.     protected void logError(String msg) {  
  84.         System.err.println("[DevLoader] Error: " + msg);  
  85.     }  
  86.   
  87.     protected List readWebClassPathEntries() {  
  88.         List rc = null;  
  89.   
  90.         File prjDir = getProjectRootDir();  
  91.         if (prjDir == null)  
  92.             return new ArrayList();  
  93.   
  94.         log("projectdir=" + prjDir.getAbsolutePath());  
  95.   
  96.         rc = loadWebClassPathFile(prjDir);  
  97.   
  98.         if (rc == null)  
  99.             rc = new ArrayList();  
  100.         return rc;  
  101.     }  
  102.   
  103.     protected File getProjectRootDir() {  
  104.         File rootDir = getWebappDir();  
  105.         FileFilter filter = new FileFilter() {  
  106.             public boolean accept(File file) {  
  107.                 return (file.getName().equalsIgnoreCase(webClassPathFile) || file.getName().equalsIgnoreCase(tomcatPluginFile));  
  108.             }  
  109.         };  
  110.         while (rootDir != null) {  
  111.             File[] files = rootDir.listFiles(filter);  
  112.             if ((files != null) && (files.length >= 1))  
  113.                 return files[0].getParentFile();  
  114.   
  115.             rootDir = rootDir.getParentFile();  
  116.         }  
  117.         return null;  
  118.     }  
  119.   
  120.     protected List loadWebClassPathFile(File prjDir) {  
  121.         File cpFile = new File(prjDir, this.webClassPathFile);  
  122.         if (cpFile.exists()) {  
  123.             FileReader reader = null;  
  124.             try {  
  125.                 List rc = new ArrayList();  
  126.                 reader = new FileReader(cpFile);  
  127.                 LineNumberReader lr = new LineNumberReader(reader);  
  128.                 String line = null;  
  129.                 while ((line = lr.readLine()) != null) {  
  130.                     line = line.replace('\\', '/');  
  131.                     rc.add(line);  
  132.                 }  
  133.                 return rc;  
  134.             } catch (IOException ioEx) {  
  135.                 if (reader != null)  
  136.                     ;  
  137.                 return null;  
  138.             }  
  139.         }  
  140.         return null;  
  141.     }  
  142.   
  143.     protected ServletContext getServletContext() {  
  144.         // return ((Context) getContainer()).getServletContext();  
  145.         return this.getContext().getServletContext();  
  146.     }  
  147.   
  148.     protected File getWebappDir() {  
  149.         File webAppDir = new File(getServletContext().getRealPath("/"));  
  150.         return webAppDir;  
  151.     }  
  152. }  

最后,附DevloaderTomcat8.jar的下载地址:http://download.csdn.net/download/matrix_designer/8244253
0 0
原创粉丝点击