⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tomcatproject.java

📁 一个学习eclipse插件开发的绝好入门程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.sysdeo.eclipse.tomcat;

/*
 * (c) Copyright Sysdeo SA 2001, 2002.
 * All Rights Reserved.
 */
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import com.sysdeo.eclipse.tomcat.editors.ProjectListElement;




public class TomcatProject extends PlatformObject implements IProjectNature  {

	// Persistence properties of projects
	private static final String PROPERTIES_FILENAME = ".tomcatplugin";
	private static final String KEY_WEBPATH = "webPath";
	private static final String KEY_UPDATEXML = "updateXml";
	private static final String KEY_EXPORTSOURCE = "exportSource";
	private static final String KEY_RELOADABLE="reloadable";
	private static final String KEY_REDIRECTLOGGER="redirectLogger";	
	private static final String KEY_WARLOCATION = "warLocation";
	private static final String KEY_ROOTDIR = "rootDir";
	private static final String KEY_EXTRAINFO = "extraInfo";
	private static final String KEY_WEBCLASSPATH = WebClassPathEntries.TAG_NAME;
	private static final String extraBeginTag = "<!-- Extra info begin -->";
	private static final String extraEndTag = "<!-- Extra info end -->";	

//	private static final QualifiedName QN_WEBPATH = new QualifiedName("TomcatProject", KEY_WEBPATH);
//	private static final QualifiedName QN_UPDATEXML = new QualifiedName("TomcatProject", KEY_UPDATEXML);
//	private static final QualifiedName QN_EXPORTSOURCE = new QualifiedName("TomcatProject", KEY_EXPORTSOURCE);
//	private static final QualifiedName QN_RELOADABLE = new QualifiedName("TomcatProject", KEY_RELOADABLE);
//	private static final QualifiedName QN_REDIRECTLOGGER = new QualifiedName("TomcatProject", KEY_REDIRECTLOGGER);
//	private static final QualifiedName QN_WARLOCATION = new QualifiedName("TomcatProject", KEY_WARLOCATION);
//	private static final QualifiedName QN_ROOTDIR = new QualifiedName("TomcatProject", KEY_ROOTDIR);


	/**
	 * The platform project this <code>TomcatProject</code> is based on
	 */
	protected IProject project;
	protected IJavaProject javaProject;

	protected String webPath = "";
	protected String warLocation = "";
	protected String rootDir = "";
	protected String extraInfo = "";
	protected boolean updateXml;
	protected boolean exportSource;
	protected boolean reloadable = true;
	protected boolean redirectLogger = false;
	protected WebClassPathEntries webClassPathEntries;
	
	protected IFolder rootDirFolder;

	/**
	 * Gets the project.
	 * @return Returns a IProject
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * Sets the project.
	 * @param project The project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}
	
	/*
	 * @see IProjectNature#configure()
	 */
	public void configure() throws CoreException {
	}

	/*
	 * @see IProjectNature#deconfigure()
	 */
	public void deconfigure() throws CoreException {
	}

	/*
	 * @see IProjectNature#getProject()
	 */
	public IJavaProject getJavaProject() {
		return javaProject;
	}

	/*
	 * @see IProjectNature#setProject(IProject)
	 */
	public void setJavaProject(IJavaProject javaProject) {
		this.javaProject = javaProject;
		this.setProject(javaProject.getProject());
	}


	static public void addTomcatNature(IJavaProject project) {
		try {
			JDTUtil.addNatureToProject(project.getProject(), TomcatLauncherPlugin.NATURE_ID); 	
		} catch(CoreException ex) {
			TomcatLauncherPlugin.log(ex.getMessage());
		}
	}

	static public void removeTomcatNature(IJavaProject project) {
		try {
			JDTUtil.removeNatureToProject(project.getProject(), TomcatLauncherPlugin.NATURE_ID); 	
		} catch(CoreException ex) {
			TomcatLauncherPlugin.log(ex.getMessage());
		}
	}

	/**
	 * Return a TomcatProject if this javaProject has the tomcat nature
	 * Return null if Project has not tomcat nature
	 */
	static public TomcatProject create(IJavaProject javaProject) {
		TomcatProject result = null;
		try {
			result = (TomcatProject)javaProject.getProject().getNature(TomcatLauncherPlugin.NATURE_ID);
			if(result != null)
				result.setJavaProject(javaProject);
		} catch(CoreException ex) {
			TomcatLauncherPlugin.log(ex.getMessage());
		}
		return result;
	}

	/**
	 * Return a TomcatProject if this Project has the tomcat nature
	 * Return null if Project has not tomcat nature
	 */
	static public TomcatProject create(IProject project) {

		IJavaProject javaProject = JavaCore.create(project);
		if(javaProject != null) {
			return TomcatProject.create(javaProject);
		} else {
			return null;
		}
	}	

	private File getPropertiesFile() {
		return (this.getProject().getLocation().append(PROPERTIES_FILENAME).toFile());
	}
	
	private String readProperty(String key) {
		String result = null;
		try {
			result = FileUtil.readPropertyInXMLFile(getPropertiesFile(), key);
		} catch (IOException e) {
			try {
				result = getJavaProject().getCorrespondingResource().getPersistentProperty(new QualifiedName("TomcatProject", key));
			} catch (Exception e2) {
				TomcatLauncherPlugin.log(e2);
			}
		}
		
		if(result == null) {
			result = "";
		}
			
		return result;
	}
	
	/**
	 * Gets the rootDir.
	 * @return Returns a String
	 */
	public String getRootDir() {
		return this.readProperty(KEY_ROOTDIR);
	}	

	/**
	 * Sets the rootDir.
	 * @param rootDir The rootDir to set
	 */
	public void setRootDir(String rd) {
		this.rootDir = rd;
		this.rootDirFolder = null;
	}
		
	/**
	 * Gets the webpath.
	 * @return Returns a String
	 */
	public String getWebPath() {
		return this.readProperty(KEY_WEBPATH);
	}	

	/**
	 * Sets the webpath.
	 * @param webpath The webpath to set
	 */
	public void setWebPath(String wp) {
		this.webPath = wp;
	}

	public void updateWebPath(String newWebPath) throws Exception {
		setWebPath(newWebPath);	
		if(!newWebPath.equals(this.getWebPath())) {
			if(getUpdateXml()) {
				removeContext();
			}
		}	
	}
		
	/**
	 * Gets the warfile.
	 * @return Returns a String
	 */
	public String getWarLocation() {
		return this.readProperty(KEY_WARLOCATION);
	}

	/**
	 * Sets the warfile
	 * @param warfile The warfile to set
	 */
	public void setWarLocation(String wl) {
		this.warLocation = wl;
	}
	
	/**
	 * Gets the updateXml.
	 * @return Returns a boolean
	 */
	public boolean getUpdateXml() {
		return new Boolean(this.readProperty(KEY_UPDATEXML)).booleanValue();
	}

	/**
	 * Sets the updateXml.
	 * @param updateXml The updateXml to set
	 */
	public void setUpdateXml(boolean updateXml) {
		this.updateXml = updateXml;
	}

	/**
	 * Gets the updateXml.
	 * @return Returns a boolean
	 */
	public boolean getExportSource() {
		return new Boolean(this.readProperty(KEY_EXPORTSOURCE)).booleanValue();
	}

	/**
	 * Sets the exportSource.
	 * @param exportSource The exportSource to set
	 */
	public void setExportSource(boolean exportSource) {
		this.exportSource = exportSource;
	}

	/**
	 * Gets the reloadable
	 * @return Returns a boolean
	 */
	public boolean getReloadable(){
		String reloadableProperty = this.readProperty(KEY_RELOADABLE);
		// Set default value to true
		if(reloadableProperty.equals("")) {
			reloadableProperty = "true";	
		}
		return new Boolean(reloadableProperty).booleanValue();
	}
	
	/**
	 * Sets the reloadable
	 * @param reloadable The reloadable to set
	 */
	public void setReloadable(boolean reloadable){
		this.reloadable = reloadable;
	}

	/**
	 * Gets the reloadable
	 * @return Returns a boolean
	 */
	public boolean getRedirectLogger(){
		String redirectLoggerProperty = this.readProperty(KEY_REDIRECTLOGGER);
		// Set default value to false
		if(redirectLoggerProperty.equals("")) {
			redirectLoggerProperty = "false";	
		}
		return new Boolean(redirectLoggerProperty).booleanValue();
	}
	
	/**
	 * Sets the reloadable
	 * @param reloadable The reloadable to set
	 */
	public void setRedirectLogger(boolean redirectLogger){
		this.redirectLogger = redirectLogger;
	}

	/**
	 * Gets the warfile.
	 * @return Returns a String
	 */
	public String getExtraInfo() {
		return URLDecoder.decode(this.readProperty(KEY_EXTRAINFO));
	}

	/**
	 * Sets the warfile
	 * @param warfile The warfile to set
	 */
	public void setExtraInfo(String extra) {
		this.extraInfo = extra;
	}
		
	/**
	 * set the classpath entries which shall be loaded by the webclassloader
	 * @param entries List of WebClasspathEntry objects
	 */
	public void setWebClassPathEntries(WebClassPathEntries entries) {
		webClassPathEntries = entries;
	}
	
	/** 
	 * return the webclasspath entries
	 */
	public WebClassPathEntries getWebClassPathEntries() {
		try {
			return WebClassPathEntries.xmlUnmarshal(FileUtil.readTextFile(getPropertiesFile()));
		} catch(IOException ioEx) {
			return null;
		}
	}

	/*
	 * Store exportSource in project persistent properties
	 */
	public void saveProperties() {
		try {
			StringBuffer fileContent = new StringBuffer();
			fileContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			fileContent.append("<tomcatProjectProperties>\n");
			fileContent.append("    <rootDir>" + rootDir + "</rootDir>\n");
			fileContent.append("    <exportSource>" + exportSource + "</exportSource>\n");
			fileContent.append("    <reloadable>" + reloadable + "</reloadable>\n");
			fileContent.append("    <redirectLogger>" + redirectLogger + "</redirectLogger>\n");
			fileContent.append("    <updateXml>" + updateXml + "</updateXml>\n");
			fileContent.append("    <warLocation>" + warLocation + "</warLocation>\n");
			fileContent.append("    <extraInfo>" + URLEncoder.encode(extraInfo) + "</extraInfo>\n");			
			fileContent.append("    <webPath>" + webPath + "</webPath>\n");		
			if (webClassPathEntries != null) {
				fileContent.append(webClassPathEntries.xmlMarshal(4));
			}
			fileContent.append("</tomcatProjectProperties>\n");
			FileUtil.toTextFile(getPropertiesFile(), fileContent.toString());
			// refresh the project files. 
			project.refreshLocal(IResource.DEPTH_ONE, null); 
			
			//Notification for VCM (Team plug-in)
			IFile propertiesIFile = this.getProject().getFile(PROPERTIES_FILENAME);
			ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{propertiesIFile}, null);
			
			
		} catch (Exception ex) {
			TomcatLauncherPlugin.log(ex.getMessage());	
		}	
	}

	/**
	 * Run all the steps to configure a JavaProject as a TomcatProject
	 */
	public void fullConfiguration() throws CoreException, IOException {
		if(!rootDir.equals("")) {
			this.initRootDirFolder(true);	
		}

		this.addProjectToSourcePathPref();	
		this.createWEBINFFolder();
		this.createWEBINFSrcFolder();
		this.createWorkFolder();
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -