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

📄 preverificationbuilder.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.preverification.builder;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.core.model.IStreamsProxy;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import de.schlichtherle.io.ArchiveException;
import de.schlichtherle.io.File;
import de.schlichtherle.io.FileOutputStream;
import eclipseme.core.BuildLoggingConfiguration;
import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.console.BuildConsoleProxy;
import eclipseme.core.console.IBuildConsoleProxy;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.PreferenceAccessor;
import eclipseme.core.internal.packaging.DeployedJADWriter;
import eclipseme.core.internal.packaging.ObfuscatorTool;
import eclipseme.core.internal.preverifier.PreverificationUtils;
import eclipseme.core.internal.utils.AbstractClasspathEntryVisitor;
import eclipseme.core.internal.utils.ColonDelimitedProperties;
import eclipseme.core.internal.utils.Utils;
import eclipseme.core.model.ApplicationDescriptor;
import eclipseme.core.model.IJADConstants;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.Version;
import eclipseme.core.model.ApplicationDescriptor.MidletDefinition;
import eclipseme.preverifier.results.PreverificationError;

/**
 * Provides an incremental project builder implementation
 * to do a J2ME preverification of classes.  It is imperative
 * that this builder follow the standard Java builder.  The
 * standard Java builder will generate the standard compiled
 * class.  The preverifier will then preverify that generated
 * class.
 * <p />
 * Copyright (c) 2003-2007 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.16 $
 * <br>
 * $Date: 2006/11/26 21:42:00 $
 * <br>
 * @author Craig Setera
 */
public class PreverificationBuilder extends IncrementalProjectBuilder {
	public static final String ARG_UPDATE_VERSION = "_update_version";
	public static final String ARG_DO_PACKAGE = "_do_package";
	public static final String ARG_DO_OBFUSCATION = "_do_obfuscation";
	
	private static BuildLoggingConfiguration buildLoggingConfig = BuildLoggingConfiguration.instance;
	
	// Tracks the TrueZip File instances that point to the deployed jar files
	private static Map runtimeJars = new HashMap(5);

	/**
	 * Clean the output of the specified project.
	 * @param project
	 * @param cleanDeployed Whether or not to clean the deployed directory too.
	 * @param monitor
	 * 
	 * @throws JavaModelException
	 * @throws CoreException
	 */
	public static void cleanProject(IProject project, boolean cleanDeployed, IProgressMonitor monitor)
		throws JavaModelException, CoreException 
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.cleanProject project = " + project);
		}
		
		IJavaProject javaProject = JavaCore.create(project);
		BuildInfo buildInfo = 
			new BuildInfo(IncrementalProjectBuilder.CLEAN_BUILD, new HashMap(), javaProject);
		
		// Clear and remove the old verified directory and runtime folders
		IFolder oldVerifiedFolder = project.getFolder(EclipseMECorePlugin.getVerifiedOutputDirectoryName());
		if (oldVerifiedFolder.exists()) {
			Utils.clearContainer(oldVerifiedFolder, monitor);
			oldVerifiedFolder.delete(true, monitor);
		}
		IFolder oldRuntimeFolder = project.getFolder(IEclipseMECoreConstants.TEMP_FOLDER_NAME).getFolder("runtime");
		if (oldRuntimeFolder.exists()) {
			Utils.clearContainer(oldRuntimeFolder, monitor);
			oldRuntimeFolder.delete(true, monitor);
		}
		
		// Clear the classes and libraries
		IFolder classesPreverifyFolder = buildInfo.getVerifiedClassesFolder(monitor);
		Utils.clearContainer(classesPreverifyFolder, monitor);
		
		IFolder libsPreverifyFolder = buildInfo.getVerifiedLibsFolder(monitor);
		Utils.clearContainer(libsPreverifyFolder, monitor);

		// Delete the runtime JAR file
		IFolder runtimeFolder = buildInfo.getRuntimeFolder(monitor);
		if (runtimeFolder.exists()) {
			deleteRuntimeJar(project, monitor);
			Utils.clearContainer(runtimeFolder, monitor);
		}
		
		// We also delete the deployed folder
		if (cleanDeployed) {
			IFolder deploymentFolder = getDeploymentFolder(project, monitor); 
			if (deploymentFolder.exists()) {
				Utils.clearContainer(deploymentFolder, monitor);
			}
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.cleanProject project = " + project);
		}
	}

	/**
	 * Return the File instance for the runtime jar file in the specified project.
	 * 
	 * @param project
	 * @return
	 */
	public static File getRuntimeJar(IProject project, IProgressMonitor monitor)
		throws CoreException
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.getRuntimeJar project = " + project);
		}

		File runtimeJar = (File) runtimeJars.get(project);
		if (runtimeJar == null) {
			if (buildLoggingConfig.isPreverifierTraceEnabled()) {
				BuildConsoleProxy.instance.traceln("- PreverificationBuilder.getRuntimeJar JAR file not in cache");
			}

			IFolder tempFolder = project.getFolder(IEclipseMECoreConstants.TEMP_FOLDER_NAME);
			IFolder runtimeFolder = tempFolder.getFolder(IEclipseMECoreConstants.EMULATION_FOLDER_NAME);
			createFolders(runtimeFolder, monitor);
			
			IJavaProject javaProject = JavaCore.create(project);
			IMidletSuiteProject midletSuite = 
				MidletSuiteFactory.getMidletSuiteProject(javaProject);
			
			runtimeJar = new File(runtimeFolder.getLocation().toFile(), midletSuite.getJarFilename());
			runtimeJar.mkdir();
			runtimeFolder.refreshLocal(IResource.DEPTH_ONE, monitor);
			
			runtimeJars.put(project, runtimeJar);
		}
		
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.getRuntimeJar project = " + project);
		}
		return runtimeJar;
	}

	/**
	 * Delete the deployed jar file instance being cached (if found).
	 * 
	 * @param project
	 * @param monitor
	 * @throws CoreException
	 */
	private static void deleteRuntimeJar(IProject project, IProgressMonitor monitor)
		throws CoreException
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.deleteRuntimeJar project = " + project);
		}

		File runtimeJar = (File) runtimeJars.remove(project);
		if ((runtimeJar != null) && (runtimeJar.exists())) {
			try {
				if (buildLoggingConfig.isPreverifierTraceEnabled()) {
					BuildConsoleProxy.instance.traceln("- PreverificationBuilder.deleteRuntimeJar jar = " + runtimeJar);
				}

				File.umount(runtimeJar, true, true, true, true);
				
				IFolder tempFolder = project.getFolder(IEclipseMECoreConstants.TEMP_FOLDER_NAME);
				IFolder runtimeFolder = tempFolder.getFolder(IEclipseMECoreConstants.EMULATION_FOLDER_NAME);
				IFile file = runtimeFolder.getFile(runtimeJar.getName());
				file.delete(true, monitor);

			} catch (CoreException e) {
				e.printStackTrace();
				throw e;
			} catch (ArchiveException e) {
				e.printStackTrace();
				EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
			}
		}

		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.deleteRuntimeJar project = " + project);
		}
	}

	/**
	 * Get the folder into which the jad and jar will be written.
	 * 
	 * @param project
	 * @param monitor
	 * @return
	 * @throws CoreException
	 */
	private static IFolder getDeploymentFolder(IProject project, IProgressMonitor monitor) 
		throws CoreException
	{
		String deploymentDirectoryName = EclipseMECorePlugin.getDeploymentDirectoryName();
		IFolder deploymentFolder = project.getFolder(deploymentDirectoryName);
		if (!deploymentFolder.exists()) {
			deploymentFolder.create(false, true, monitor);
		}
		
		return deploymentFolder;
	}
	
	/**
	 * Implementation of the IClasspathEntryVisitor interface
	 * for collecting the set of required projects
	 */
	private class RequiredProjectsCPEntryVisitor
		extends AbstractClasspathEntryVisitor
	{
		private ArrayList requiredProjects;
		
		/** Construct a new instance. */
		private RequiredProjectsCPEntryVisitor() {
			requiredProjects = new ArrayList();
		}
		
		/**
		 * @return Returns the requiredProjects.
		 */
		public ArrayList getRequiredProjects() {
			return requiredProjects;
		}
		
		/**
		 * @see eclipseme.core.internal.utils.IClasspathEntryVisitor#visitProject(IClasspathEntry, org.eclipse.jdt.core.IJavaProject, org.eclipse.jdt.core.IJavaProject, org.eclipse.core.runtime.IProgressMonitor)
		 */
		public boolean visitProject(
			IClasspathEntry entry,
			IJavaProject javaProject, 
			IJavaProject classpathProject, 
			IProgressMonitor monitor)
				throws CoreException 
		{
			boolean continueVisitation = entry.isExported();
			
			if (continueVisitation) {
				requiredProjects.add(classpathProject);
			}
			
			return continueVisitation;
		}
	}

	/**
	 * Create the specified folder and all parent folders as necessary.
	 * 
	 * @param folder
	 * @param monitor
	 * @throws CoreException
	 */
	static void createFolders(IFolder folder, IProgressMonitor monitor) 
		throws CoreException
	{
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.createFolders folder = " + folder);
		}

		while (!folder.exists()) {
			if (folder.getParent().getType() == IResource.FOLDER) {
				createFolders((IFolder) folder.getParent(), monitor);
			}
			
			folder.create(true, true, monitor);
			folder.setDerived(true);
		}

		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("< PreverificationBuilder.createFolders folder = " + folder);
		}
	}
	
	/**
	 * Generate a MANIFEST.MF file into the deployed folder based
	 * on the current information in the JAD file.
	 * 
	 * @param midletSuite
	 * @param monitor
	 * @throws CoreException 
	 */
	static void generateDeployedManifest(IMidletSuiteProject midletSuite, IProgressMonitor monitor) 
		throws CoreException 
	{

		IProject project = midletSuite.getProject();
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("> PreverificationBuilder.generateDeployedManifest project = " + project);
		}

		ApplicationDescriptor applicationDescriptor = 
			midletSuite.getApplicationDescriptor();
		Properties manifestProperties = applicationDescriptor.getManifestProperties();
		
		// Filter out manifest attributes
		if (buildLoggingConfig.isPreverifierTraceEnabled()) {
			BuildConsoleProxy.instance.traceln("- PreverificationBuilder.generateDeployedManifest filtering excluded properties");
		}

		String[] excluded = PreferenceAccessor.instance.getExcludedManifestProperties(project);		
		for (int i = 0; i < excluded.length; i++) {
			String excludedName = excluded[i];
			if (manifestProperties.containsKey(excludedName)) {
				manifestProperties.remove(excludedName);
			}

⌨️ 快捷键说明

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