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

Robert Stryker rob.stryker at jboss.com
Wed Apr 18 17:07:52 EDT 2007


  User: rawb    
  Date: 07/04/18 17:07:52

  Added:       core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/util    
                        ModelTruezipBridge.java TrueZipUtil.java
                        PackagesExport.java ModelUtil.java
  Log:
  Given it's own module under the new name
  
  Revision  Changes    Path
  1.1      date: 2007/04/18 21:07:52;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/util/ModelTruezipBridge.java
  
  Index: ModelTruezipBridge.java
  ===================================================================
  package org.jboss.ide.eclipse.archives.core.util;
  
  import org.eclipse.core.runtime.IPath;
  import org.jboss.ide.eclipse.archives.core.model.IArchive;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveFileSet;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveFolder;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveNode;
  
  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 deleteArchive(IArchive archive) {
  		final File file = getFile(archive);
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				file.deleteAll();
  			}
  		});
  	}
  	
  	public static void createContainer(IArchiveNode node) {
  		File f = getFile(node);
  		f.mkdirs();
  	}
  	
  	public static void deleteFolder(IArchiveFolder folder) {
  		IArchiveFileSet[] filesets = ModelUtil.findAllDescendentFilesets(folder);
  		deleteFolder(folder,filesets);
  	}
  	
  	public static void deleteFolder(IArchiveFolder folder, IArchiveFileSet[] 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(IArchiveFileSet fileset) {
  		fileset.resetScanner();
  		IPath[] paths = fileset.findMatchingPaths();
  		ModelTruezipBridge.copyFiles(fileset, paths);
  	}
  		
  	public static void fullFilesetRemove(IArchiveFileSet 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(IArchiveFileSet[] filesets, IPath[] paths) {
  		for( int i = 0; i < filesets.length; i++ ) {
  			copyFiles(filesets[i], paths);
  		}
  	}
  	
  	public static void copyFiles(IArchiveFileSet 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(IArchiveFileSet[] filesets, IPath[] paths ) {
  		for( int i = 0; i < filesets.length; i++ ) {
  			deleteFiles(filesets[i], paths);
  		}
  	}
  	public static void deleteFiles(IArchiveFileSet 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, IArchiveFileSet fs ) {
  		String filesetRelative;
  		File fsFile = getFile(fs);
  		File[] returnFiles = new File[inputFiles.length];
  		int fsLength = fs.getGlobalSourcePath().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(IArchiveNode node) {
  		if( node == null ) return null;
  		
  		if( node.getNodeType() == IArchiveNode.TYPE_MODEL ) return null;
  		
  		if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET)
  			return getFile(node.getParent());
  		
  		File parentFile = getFile(node.getParent());
  		if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE ) {
  			IArchive node2 = ((IArchive)node);
  			boolean exploded = ((IArchive)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() == IArchiveNode.TYPE_ARCHIVE_FOLDER ) {
  			return new File(parentFile, ((IArchiveFolder)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 IArchiveNode node) {
  		TrueZipUtil.syncExec(new Runnable() {
  			public void run() {
  				File f = getFile(node);
  				if( f != null ) {
  					f.mkdirs();
  				}
  			}
  		});
  	}
  	
  }
  
  
  
  1.1      date: 2007/04/18 21:07:52;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/util/TrueZipUtil.java
  
  Index: TrueZipUtil.java
  ===================================================================
  package org.jboss.ide.eclipse.archives.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/18 21:07:52;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/util/PackagesExport.java
  
  Index: PackagesExport.java
  ===================================================================
  package org.jboss.ide.eclipse.archives.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.archives.core.ArchivesCorePlugin;
  import org.jboss.ide.eclipse.archives.core.model.IArchive;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveFileSet;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveNode;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveNodeVisitor;
  import org.jboss.ide.eclipse.archives.core.model.internal.ArchivesModel;
  import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
  
  public class PackagesExport {
  	public static final QualifiedName PROPERTY_ANT_SCRIPT_PATH =
  		new QualifiedName("org.jboss.ide.eclipse.archives.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, IArchive pkg)
  	{
  		final ArrayList referencedProjects = new ArrayList();
  		referencedProjects.add(project);
  		
  		pkg.accept(new IArchiveNodeVisitor () {
  			public boolean visit (IArchiveNode node)
  			{
  				if (node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET)
  				{
  					IArchiveFileSet fileset = (IArchiveFileSet)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 = ArchivesModel.instance().getXbPackages(project);
  		IArchive packages[] = ArchivesModel.instance().getProjectArchives(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(ArchivesModel.PROJECT_PACKAGES_FILE).getContents();
  			InputStream xslIn = ArchivesCorePlugin.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/18 21:07:52;  author: rawb;  state: Exp;jbosside/core/plugins/org.jboss.ide.eclipse.archives.core/src/main/org/jboss/ide/eclipse/archives/core/util/ModelUtil.java
  
  Index: ModelUtil.java
  ===================================================================
  package org.jboss.ide.eclipse.archives.core.util;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.resources.IWorkspaceRoot;
  import org.eclipse.core.resources.ResourcesPlugin;
  import org.eclipse.core.runtime.IPath;
  import org.jboss.ide.eclipse.archives.core.model.IArchive;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveFileSet;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveFolder;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveNode;
  import org.jboss.ide.eclipse.archives.core.model.IArchiveNodeVisitor;
  import org.jboss.ide.eclipse.archives.core.model.internal.ArchivesModel;
  
  public class ModelUtil {
  	public static IArchiveFileSet[] getMatchingFilesets(final IPath path) {
  		final ArrayList rets = new ArrayList();
  		ArchivesModel.instance().accept(new IArchiveNodeVisitor() {
  			public boolean visit(IArchiveNode node) {
  				if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET && 
  						((IArchiveFileSet)node).matchesPath(path)) {
  					rets.add((IArchiveFileSet)node);
  				}
  				return true;
  			} 
  		});
  		return (IArchiveFileSet[]) rets.toArray(new IArchiveFileSet[rets.size()]);
  	}
  
  	public static IArchiveFileSet[] findAllDescendentFilesets(IArchiveNode node) {
  		if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET ) 
  			return new IArchiveFileSet[] {(IArchiveFileSet)node};
  		
  		final ArrayList filesets = new ArrayList();
  		node.accept(new IArchiveNodeVisitor() {
  			public boolean visit(IArchiveNode node) {
  				if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET)
  					filesets.add(node);
  				return true;
  			} 
  		});
  		return (IArchiveFileSet[]) filesets.toArray(new IArchiveFileSet[filesets.size()]);
  	}
  
  
  	public static boolean otherFilesetMatchesPath(IArchiveFileSet fileset, IPath path) {
  		IArchiveFileSet[] 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.getRootArchiveRelativePath(path).equals(filesets[i].getRootArchiveRelativePath(path))) {
  					// the two put the file in the same spot! It's a match!
  					return true;
  				}
  			}
  		}
  		return false;
  	}
  
  	public static IResource getResource(IArchiveNode node) {
  		IPath dest = null;
  		if( node.getRootArchive().isDestinationInWorkspace() ) {
  			// our root is somewhere in workspace... good sign
  			if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE)
  				dest = ((IArchive)node).getDestinationPath();
  			else if( node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FOLDER) {
  				dest = node.getRootArchive().getDestinationPath();
  				dest = dest.append(((IArchiveFolder)node).getRootArchiveRelativePath());
  			}
  			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
  			IResource res = root.getContainerForLocation(dest);
  			return res;
  		}
  		return null;
  	}
  }
  
  
  



More information about the jboss-cvs-commits mailing list