1. 程式人生 > >讓Eclipse的TomcatPlugin支援Tomcat 8.x

讓Eclipse的TomcatPlugin支援Tomcat 8.x

專案原因,近期要遷移到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,一起正常。

何為管理?做人、管人、理事,最重要的是能為兄弟們解決問題。好幾個月沒寫程式碼了,寫兩句感覺挺好的。

改後的原始碼如下:

package org.apache.catalina.loader;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import org.apache.catalina.LifecycleException;

public class DevLoader extends WebappLoader {
	private static final String info = "org.apache.catalina.loader.DevLoader/1.0";
	private String webClassPathFile = ".#webclasspath";
	private String tomcatPluginFile = ".tomcatplugin";

	public DevLoader() {
	}

	public DevLoader(ClassLoader parent) {
		super(parent);
	}

	public void startInternal() throws LifecycleException {
		log("Starting DevLoader");

		super.startInternal();

		ClassLoader cl = super.getClassLoader();
		if (!(cl instanceof WebappClassLoader)) {
			logError("Unable to install WebappClassLoader !");
			return;
		}
		WebappClassLoader devCl = (WebappClassLoader) cl;

		List webClassPathEntries = readWebClassPathEntries();
		StringBuffer classpath = new StringBuffer();
		for (Iterator it = webClassPathEntries.iterator(); it.hasNext();) {
			String entry = (String) it.next();
			File f = new File(entry);
			if (f.exists()) {
				if ((f.isDirectory()) && (!(entry.endsWith("/"))))
					f = new File(entry + "/");
				try {
					URL url = f.toURL();

					// devCl.addRepository(url.toString());
					devCl.addURL(url);
					classpath.append(f.toString() + File.pathSeparatorChar);
					log("added " + url.toString());
				} catch (MalformedURLException e) {
					logError(entry + " invalid (MalformedURL)");
				}
			} else {
				logError(entry + " does not exist !");
			}
		}

		String cp = (String) getServletContext().getAttribute("org.apache.catalina.jsp_classpath");
		StringTokenizer tokenizer = new StringTokenizer(cp, File.pathSeparatorChar + "");
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();

			if ((token.charAt(0) == '/') && (token.charAt(2) == ':')) {
				token = token.substring(1);
			}
			classpath.append(token + File.pathSeparatorChar);
		}

		getServletContext().setAttribute("org.apache.catalina.jsp_classpath", classpath.toString());
		log("JSPCompiler Classpath = " + classpath);
	}

	protected void log(String msg) {
		System.out.println("[DevLoader] " + msg);
	}

	protected void logError(String msg) {
		System.err.println("[DevLoader] Error: " + msg);
	}

	protected List readWebClassPathEntries() {
		List rc = null;

		File prjDir = getProjectRootDir();
		if (prjDir == null)
			return new ArrayList();

		log("projectdir=" + prjDir.getAbsolutePath());

		rc = loadWebClassPathFile(prjDir);

		if (rc == null)
			rc = new ArrayList();
		return rc;
	}

	protected File getProjectRootDir() {
		File rootDir = getWebappDir();
		FileFilter filter = new FileFilter() {
			public boolean accept(File file) {
				return (file.getName().equalsIgnoreCase(webClassPathFile) || file.getName().equalsIgnoreCase(tomcatPluginFile));
			}
		};
		while (rootDir != null) {
			File[] files = rootDir.listFiles(filter);
			if ((files != null) && (files.length >= 1))
				return files[0].getParentFile();

			rootDir = rootDir.getParentFile();
		}
		return null;
	}

	protected List loadWebClassPathFile(File prjDir) {
		File cpFile = new File(prjDir, this.webClassPathFile);
		if (cpFile.exists()) {
			FileReader reader = null;
			try {
				List rc = new ArrayList();
				reader = new FileReader(cpFile);
				LineNumberReader lr = new LineNumberReader(reader);
				String line = null;
				while ((line = lr.readLine()) != null) {
					line = line.replace('\\', '/');
					rc.add(line);
				}
				return rc;
			} catch (IOException ioEx) {
				if (reader != null)
					;
				return null;
			}
		}
		return null;
	}

	protected ServletContext getServletContext() {
		// return ((Context) getContainer()).getServletContext();
		return this.getContext().getServletContext();
	}

	protected File getWebappDir() {
		File webAppDir = new File(getServletContext().getRealPath("/"));
		return webAppDir;
	}
}

最後,附DevloaderTomcat8.jar的下載地址:http://download.csdn.net/download/matrix_designer/8244253