[jboss-cvs] jbosside/core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util ...

Robert Stryker rob.stryker at jboss.com
Mon Apr 16 13:34:43 EDT 2007


  User: rawb    
  Date: 07/04/16 13:34:43

  Added:       core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util    
                        ModelUtil.java PackagesExport.java TrueZipUtil.java
                        ModelTruezipBridge.java
  Log:
  Complete rewrite of the the two plugins leading to a cleaner, leaner project. 
  
  Revision  Changes    Path
  1.1      date: 2007/04/16 17:34:43;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util/ModelUtil.java
  
  Index: ModelUtil.java
  ===================================================================
  package org.jboss.ide.eclipse.packages.core.util;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  
  import org.eclipse.core.runtime.IPath;
  import org.jboss.ide.eclipse.packages.core.model.IPackageFileSet;
  import org.jboss.ide.eclipse.packages.core.model.IPackageNode;
  import org.jboss.ide.eclipse.packages.core.model.IPackageNodeVisitor;
  import org.jboss.ide.eclipse.packages.core.model.internal.PackagesModel;
  
  public class ModelUtil {
  	public static IPackageFileSet[] getMatchingFilesets(final IPath path) {
  		final ArrayList rets = new ArrayList();
  		PackagesModel.instance().accept(new IPackageNodeVisitor() {
  			public boolean visit(IPackageNode node) {
  				if( node.getNodeType() == IPackageNode.TYPE_PACKAGE_FILESET && 
  						((IPackageFileSet)node).matchesPath(path)) {
  					rets.add((IPackageFileSet)node);
  				}
  				return true;
  			} 
  		});
  		return (IPackageFileSet[]) rets.toArray(new IPackageFileSet[rets.size()]);
  	}
  
  	public static IPackageFileSet[] findAllDescendentFilesets(IPackageNode node) {
  		if( node.getNodeType() == IPackageNode.TYPE_PACKAGE_FILESET ) 
  			return new IPackageFileSet[] {(IPackageFileSet)node};
  		
  		final ArrayList filesets = new ArrayList();
  		node.accept(new IPackageNodeVisitor() {
  			public boolean visit(IPackageNode node) {
  				if( node.getNodeType() == IPackageNode.TYPE_PACKAGE_FILESET)
  					filesets.add(node);
  				return true;
  			} 
  		});
  		return (IPackageFileSet[]) filesets.toArray(new IPackageFileSet[filesets.size()]);
  	}
  
  
  	public static boolean otherFilesetMatchesPath(IPackageFileSet fileset, IPath path) {
  		IPackageFileSet[] filesets = ModelUtil.getMatchingFilesets(path);
  		if( filesets.length == 0 || (filesets.length == 1 && Arrays.asList(filesets).contains(fileset))) {
  			return false;
  		} else {
  			// other filesets DO match... but are they at the same location in the archive?
  			for( int i = 0; i < filesets.length; i++ ) {
  				if( fileset.equals(filesets[i])) continue;
  				if( fileset.getRootPackageRelativePath(path).equals(filesets[i].getRootPackageRelativePath(path))) {
  					// the two put the file in the same spot! It's a match!
  					return true;
  				}
  			}
  		}
  		return false;
  	}
  
  }
  
  
  
  1.3       +40 -5     jbosside/core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util/PackagesExport.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PackagesExport.java
  ===================================================================
  RCS file: PackagesExport.java
  diff -N PackagesExport.java
  --- /dev/null	1 Jan 1970 00:00:00 -0000
  +++ PackagesExport.java	16 Apr 2007 17:34:43 -0000	1.3
  @@ -0,0 +1,184 @@
  +package org.jboss.ide.eclipse.packages.core.util;
  +
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.util.ArrayList;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.Map;
  +
  +import javax.xml.transform.Source;
  +import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerConfigurationException;
  +import javax.xml.transform.TransformerException;
  +import javax.xml.transform.TransformerFactory;
  +import javax.xml.transform.TransformerFactoryConfigurationError;
  +import javax.xml.transform.stream.StreamResult;
  +import javax.xml.transform.stream.StreamSource;
  +
  +import org.eclipse.core.resources.IFile;
  +import org.eclipse.core.resources.IProject;
  +import org.eclipse.core.runtime.CoreException;
  +import org.eclipse.core.runtime.IPath;
  +import org.eclipse.core.runtime.IProgressMonitor;
  +import org.eclipse.core.runtime.Path;
  +import org.eclipse.core.runtime.QualifiedName;
  +import org.jboss.ide.eclipse.packages.core.PackagesCorePlugin;
  +import org.jboss.ide.eclipse.packages.core.model.IPackage;
  +import org.jboss.ide.eclipse.packages.core.model.IPackageFileSet;
  +import org.jboss.ide.eclipse.packages.core.model.IPackageNode;
  +import org.jboss.ide.eclipse.packages.core.model.IPackageNodeVisitor;
  +import org.jboss.ide.eclipse.packages.core.model.internal.PackagesModel;
  +import org.jboss.ide.eclipse.packages.core.model.internal.xb.XbPackages;
  +
  +public class PackagesExport {
  +	public static final QualifiedName PROPERTY_ANT_SCRIPT_PATH =
  +		new QualifiedName("org.jboss.ide.eclipse.packages.core", "antScriptPath");
  +
  +	public static final IPath DEFAULT_ANT_SCRIPT_PATH = new Path("buildPackages.xml");
  +	public static final String XSL_PATH = "xml/packages2ant.xsl";
  +	
  +	private static String escapeProjectName(IProject project)
  +	{
  +		String name = project.getName();
  +		name = name.replaceAll(" ", "_");
  +		return name;
  +	}
  +	
  +	public static List findReferencedProjects (IProject project, IPackage pkg)
  +	{
  +		final ArrayList referencedProjects = new ArrayList();
  +		referencedProjects.add(project);
  +		
  +		pkg.accept(new IPackageNodeVisitor () {
  +			public boolean visit (IPackageNode node)
  +			{
  +				if (node.getNodeType() == IPackageNode.TYPE_PACKAGE_FILESET)
  +				{
  +					IPackageFileSet fileset = (IPackageFileSet)node;
  +					IProject project = fileset.getProject();
  +					if (!project.equals(project) && !referencedProjects.contains(project))
  +					{
  +						referencedProjects.add(project);
  +					}
  +				}
  +				return true;
  +			}
  +		});
  +		return referencedProjects;
  +	}
  +		
  +	public static void exportAntScript (IProject project, Map args, IProgressMonitor monitor)
  +	{
  +		monitor.beginTask("Exporting ant script...", 2);
  +	
  +		XbPackages projectPackagesElement = PackagesModel.instance().getXbPackages(project);
  +		IPackage packages[] = PackagesModel.instance().getProjectPackages(project);
  +		ArrayList referencedProjects = new ArrayList();
  +		
  +		monitor.beginTask("Finding referenced projects...", packages.length);
  +		for (int i = 0; i < packages.length; i++)
  +		{
  +			referencedProjects.addAll(findReferencedProjects(project, packages[i]));
  +			monitor.worked(1);
  +		}
  +		monitor.done();
  +		
  +		for (Iterator iter = referencedProjects.iterator(); iter.hasNext(); )
  +		{
  +			IProject referencedProject = (IProject)iter.next();
  +			String propertyName = null;
  +			
  +			if (referencedProject.equals(project))
  +			{
  +				propertyName = "project-root";
  +			}
  +			else {
  +				propertyName = "project-root-" + escapeProjectName(referencedProject);
  +			}
  +			
  +			IPath location = referencedProject.getLocation();
  +			if (location != null)
  +				projectPackagesElement.getProperties().getProperties().setProperty(propertyName, location.toString());
  +		}
  +		
  +		try {
  +			InputStream configIn = project.getFile(PackagesModel.PROJECT_PACKAGES_FILE).getContents();
  +			InputStream xslIn = PackagesCorePlugin.getDefault().getBundle().getEntry(XSL_PATH).openStream();
  +			ByteArrayOutputStream out = new ByteArrayOutputStream();
  +			IFile antFile = project.getFile(getPathToPackagesScript(project));
  +			
  +			Source stylesheet = new StreamSource(xslIn);
  +			Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesheet);
  +			transformer.setOutputProperty("indent", "yes");
  +			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  +			transformer.transform(new StreamSource(configIn), new StreamResult(out));
  +			
  +			ByteArrayInputStream fileStream = new ByteArrayInputStream(out.toByteArray());
  +			if (!antFile.exists()) {
  +				antFile.create(fileStream, true, monitor);
  +			}
  +			else {
  +				antFile.setContents(fileStream, IFile.KEEP_HISTORY, monitor);
  +			}
  +			
  +			xslIn.close();
  +			out.close();
  +			
  +			monitor.worked(1);
  +			monitor.done();
  +		} catch (TransformerConfigurationException e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		} catch (IllegalArgumentException e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		} catch (CoreException e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		} catch (IOException e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		} catch (TransformerFactoryConfigurationError e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		} catch (TransformerException e) {
  +			// TODO Auto-generated catch block
  +			e.printStackTrace();
  +		}
  +	}
  +	
  +	public static IPath getPathToPackagesScript (IProject project)
  +	{
  +		try {
  +			String scriptPath = project.getPersistentProperty(PROPERTY_ANT_SCRIPT_PATH);
  +			if (scriptPath == null) {
  +				project.setPersistentProperty(PROPERTY_ANT_SCRIPT_PATH, DEFAULT_ANT_SCRIPT_PATH.toString());
  +				return DEFAULT_ANT_SCRIPT_PATH;
  +			}
  +			else
  +				return new Path(scriptPath);
  +		} catch (CoreException e) {
  +			e.printStackTrace();
  +		}
  +		
  +		return DEFAULT_ANT_SCRIPT_PATH;
  +	}
  +	
  +	public static IPath getRawPathToPackagesScript (IProject project)
  +	{
  +		IPath scriptPath = getPathToPackagesScript(project);
  +		IFile script = project.getFile(scriptPath);
  +		if (script != null)
  +		{
  +			if (script.getLocation() == null)
  +				return script.getRawLocation();
  +			return script.getLocation();
  +		}
  +		
  +		return scriptPath;
  +	}
  +
  +}
  
  
  
  1.1      date: 2007/04/16 17:34:43;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util/TrueZipUtil.java
  
  Index: TrueZipUtil.java
  ===================================================================
  package org.jboss.ide.eclipse.packages.core.util;
  
  import java.io.FileNotFoundException;
  import java.io.IOException;
  
  import org.eclipse.core.runtime.IPath;
  
  import de.schlichtherle.io.ArchiveDetector;
  import de.schlichtherle.io.ArchiveException;
  
  public class TrueZipUtil {
  	
  	public static de.schlichtherle.io.File getFile(IPath path) {
  		return getFile(path, ArchiveDetector.DEFAULT);
  	}
  	public static de.schlichtherle.io.File getFile(IPath path, ArchiveDetector detector) {
  		return new de.schlichtherle.io.File(path.toOSString(), detector);
  	}
  	
  	public static boolean pathExists(IPath path) {
  		return pathExists( getFile(path));
  	}
  	public static boolean pathExists( de.schlichtherle.io.File file) {
  		return file.exists();
  	}
  	
  
  	public static long getTimestamp(IPath path) {
  		return getTimestamp( getFile(path));
  	}
  
  	public static long getTimestamp(de.schlichtherle.io.File file) {
  		return file.lastModified();
  	}
  	
  	
  	public static void copyFile(String source, IPath dest) throws IOException {
  		copyFile(source, getFile(dest));
  	}
  	
  	public static void copyFile(String source, de.schlichtherle.io.File file) {
  		try {
  			// make parent folders
  			if( !file.getParentFile().exists() )
  				file.getParentFile().mkdirs();
  			
  			de.schlichtherle.io.FileOutputStream fos = new de.schlichtherle.io.FileOutputStream(file);
  			java.io.FileInputStream fis = new java.io.FileInputStream(source);
  			
  			byte[] buf = new byte[1024];
  			int i = 0;
  			while((i=fis.read(buf))!=-1) {
  			  fos.write(buf, 0, i);
  			  }
  			fis.close();
  			fos.close();
  		} catch (FileNotFoundException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		} catch (IOException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  	    
  	    updateParentTimestamps(file);
  	}
  	
  	public static void touchFile(IPath path) {
  		de.schlichtherle.io.File f = getFile(path);
  		f.setLastModified(System.currentTimeMillis());
  	    updateParentTimestamps(path);
  	}
  	
  	
  	// Delete methods
  	public static void deleteAll(IPath path, String fileName) {
  		deleteAll(path.append(fileName));
  	}
  	public static void deleteAll(IPath path) {
  		deleteAll(getFile(path));
  	}
  	public static void deleteAll(de.schlichtherle.io.File file) {
  		file.deleteAll();
  	}
  	
  	public static void deleteEmptyFolders(java.io.File file ) {
  		if( file.isDirectory() ) {
  			java.io.File[] children = file.listFiles();
  			for( int i = 0; i < children.length; i++ )
  				deleteEmptyFolders(children[i]);
  			if( file.listFiles().length == 0 )
  				file.delete();
  		}
  	}
  	
  	
  	public static void createFolder(IPath parent, String folderName) {
  		createFolder(parent.append(folderName));
  	}
  	public static void createFolder(IPath path) {
  		getFile(path, ArchiveDetector.NULL).mkdirs();
  	    updateParentTimestamps(path);
  	}
  	public static void createArchive(IPath parent, String folderName) {
  		createArchive(parent.append(folderName));
  	}
  	public static void createArchive(IPath path) {
  		getFile(path).mkdirs();
  	    updateParentTimestamps(path);
  	}
  	public static void umount() {
  		try {
  			de.schlichtherle.io.File.umount();
  		} catch( ArchiveException ae ) {
  		}
  	}
  	
  	/**
  	 * Sync's with file system after executing
  	 * @param run
  	 */
  	public static void syncExec(Runnable run) {
  		try {
  			run.run();
  		} catch (Exception e ) {}
  		umount();
  	}
  	
  	public static void updateParentTimestamps(IPath path) {
  		updateParentTimestamps(getFile(path));
  	}
  	public static void updateParentTimestamps(de.schlichtherle.io.File file) {
  		long time = System.currentTimeMillis();
  		de.schlichtherle.io.File parent = file.getEnclArchive();
  		while( parent != null ) {
  			parent.setLastModified(time);
  			parent = parent.getEnclArchive();
  		}
  
  	}
  }
  
  
  
  1.1      date: 2007/04/16 17:34:43;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.packages.core/src/main/org/jboss/ide/eclipse/packages/core/util/ModelTruezipBridge.java
  
  Index: ModelTruezipBridge.java
  ===================================================================
  package org.jboss.ide.eclipse.packages.core.util;
  
  import org.eclipse.core.runtime.IPath;
  import org.jboss.ide.eclipse.packages.core.model.IPackage;
  import org.jboss.ide.eclipse.packages.core.model.IPackageFileSet;
  import org.jboss.ide.eclipse.packages.core.model.IPackageFolder;
  import org.jboss.ide.eclipse.packages.core.model.IPackageNode;
  
  import de.schlichtherle.io.ArchiveDetector;
  import de.schlichtherle.io.File;
  
  /**
   * This class is meant to bridge between the model
   * and the raw true-zip utility class. It is a higher level
   * API which deals with filesets and packages instead of 
   * raw Strings and paths.
   * 
   * It will also make sure that a de.schlichtherle.io.File is
   * created with the proper ArchiveDetector for each and every
   * level, rather than the TrueZipUtil class, which not accurately
   * create the proper File type for exploded archives.
   * 
   * @author rstryker
   *
   */
  public class ModelTruezipBridge {
  	public static void deletePackage(IPackage pkg) {
  		final File file = getFile(pkg);
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				file.deleteAll();
  			}
  		});
  	}
  	
  	public static void createContainer(IPackageNode node) {
  		File f = getFile(node);
  		f.mkdirs();
  	}
  	
  	public static void deleteFolder(IPackageFolder folder) {
  		IPackageFileSet[] filesets = ModelUtil.findAllDescendentFilesets(folder);
  		deleteFolder(folder,filesets);
  	}
  	
  	public static void deleteFolder(IPackageFolder folder, IPackageFileSet[] filesets) {
  		// remove all files from filesets
  		for( int i = 0; i < filesets.length; i++ )
  			fullFilesetRemove(filesets[i]);
  		
  		final File file = getFile(folder);
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				TrueZipUtil.deleteEmptyFolders(file);
  			}
  		});
  	}
  	
  	public static void fullFilesetBuild(IPackageFileSet fileset) {
  		fileset.resetScanner();
  		IPath[] paths = fileset.findMatchingPaths();
  		ModelTruezipBridge.copyFiles(fileset, paths);
  	}
  		
  	public static void fullFilesetRemove(IPackageFileSet fileset) {
  		IPath[] paths = fileset.findMatchingPaths();
  		for( int i = 0; i < paths.length; i++ ) {
  			if( !ModelUtil.otherFilesetMatchesPath(fileset, paths[i])) {
  				// remove
  				ModelTruezipBridge.deleteFiles(fileset, new IPath[] {paths[i]});
  			}
  		}
  	}
  
  	
  	public static void copyFiles(IPackageFileSet[] filesets, IPath[] paths) {
  		for( int i = 0; i < filesets.length; i++ ) {
  			copyFiles(filesets[i], paths);
  		}
  	}
  	
  	public static void copyFiles(IPackageFileSet fileset, final IPath[] paths) {
  		final File[] destFiles = getFiles(paths, fileset);
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				for( int i = 0; i < paths.length; i++ ) {
  					TrueZipUtil.copyFile(paths[i].toOSString(), destFiles[i]);
  				}
  			}
  		});
  	}
  	
  	public static void deleteFiles(IPackageFileSet[] filesets, IPath[] paths ) {
  		for( int i = 0; i < filesets.length; i++ ) {
  			deleteFiles(filesets[i], paths);
  		}
  	}
  	public static void deleteFiles(IPackageFileSet fileset, final IPath[] paths ) {
  		final File[] destFiles = getFiles(paths, fileset);
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				for( int i = 0; i < paths.length; i++ ) {
  					TrueZipUtil.deleteAll(destFiles[i]);
  				}
  			}
  		});
  	}
  	
  	/**
  	 * Gets all properly-created de.sch files for a fileset
  	 * @param inputFiles
  	 * @param fs
  	 * @return
  	 */
  	private static File[] getFiles(IPath[] inputFiles, IPackageFileSet fs ) {
  		String filesetRelative;
  		File fsFile = getFile(fs);
  		File[] returnFiles = new File[inputFiles.length];
  		int fsLength = fs.getSourcePath().toOSString().length()+1;
  		for( int i = 0; i < inputFiles.length; i++ ) {
  			filesetRelative = inputFiles[i].toOSString().substring(fsLength);
  			returnFiles[i] = new File(fsFile, filesetRelative, ArchiveDetector.NULL);
  		}
  		return returnFiles;
  	}
  	
  	
  	/**
  	 * This should go through the tree and create a file that is 
  	 * correctly perceived at each step of the way. 
  	 * 
  	 * To just create a new File would let the Archive Detector have too 
  	 * much control, and *ALL* war's and jars, including exploded ones, 
  	 * would be treated as archives instead of folders. 
  	 * @param node
  	 * @return
  	 */
  	private static File getFile(IPackageNode node) {
  		if( node == null ) return null;
  		
  		if( node.getNodeType() == IPackageNode.TYPE_MODEL ) return null;
  		
  		if( node.getNodeType() == IPackageNode.TYPE_PACKAGE_FILESET)
  			return getFile(node.getParent());
  		
  		File parentFile = getFile(node.getParent());
  		if( node.getNodeType() == IPackageNode.TYPE_PACKAGE ) {
  			IPackage node2 = ((IPackage)node);
  			boolean exploded = ((IPackage)node).isExploded();
  			ArchiveDetector detector = exploded ? ArchiveDetector.NULL : ArchiveDetector.DEFAULT;
  			if( parentFile == null ) 
  				return new File(node2.getDestinationPath().append(node2.getName()).toOSString(), detector);
  			return new File(parentFile, node2.getName(), detector);
  		}
  		if( node.getNodeType() == IPackageNode.TYPE_PACKAGE_FOLDER ) {
  			return new File(parentFile, ((IPackageFolder)node).getName(), ArchiveDetector.NULL);
  		}
  		return null;
  	}
  	
  	
  	/**
  	 * Creates the file, folder, or archive represented by the node.
  	 * Does nothing for filesets
  	 * @param node
  	 */
  	public static void createFile(final IPackageNode node) {
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				File f = getFile(node);
  				if( f != null ) {
  					f.mkdirs();
  				}
  			}
  		});
  	}
  	
  }
  
  
  



More information about the jboss-cvs-commits mailing list