让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

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值