|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.vcs.profiles.commands; import java.io.*; import java.util.*; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.netbeans.modules.vcscore.VcsFileSystem; import org.netbeans.modules.vcscore.VcsAction; import org.netbeans.modules.vcscore.cache.CacheFile; import org.netbeans.modules.vcscore.cache.CacheDir; import org.netbeans.modules.vcscore.cache.CacheHandler; import org.netbeans.modules.vcscore.cache.FileSystemCache; import org.netbeans.modules.vcscore.caching.VcsCacheDir; import org.netbeans.modules.vcscore.cmdline.ExecuteCommand; import org.netbeans.modules.vcscore.commands.*; import org.netbeans.modules.vcscore.util.*; //import org.netbeans.modules.vcscore.cmdline.exec.*; import org.netbeans.modules.vcscore.cmdline.VcsAdditionalCommand; import org.netbeans.modules.vcscore.cmdline.UserCommandTask; /** * This class works as a wrapper for recursive commands which needs to perform * some tasks in each subdirectory of a current directory. * Due to problems with filesystem.attributes in PVCS, the command is called explicitly * for EACH file in the directory excluding filesystem.attributes * @author Martin Entlicher */ public class RecursiveFolderCommand extends Object implements VcsAdditionalCommand { private static final String NBATTRS = ".nbattrs"; // NOI18N private static final String NBINTDB = ".nbintdb"; // NOI18N public static final String LOCAL_DIR_ONLY = "-l"; // NOI18N public static final String PRINT_OUTPUT = "-o"; // NOI18N public static final String PRINT_DEBUG = "-d"; // NOI18N public static final String PRINT_FILE_PATH_A = "-fa"; // NOI18N public static final String PRINT_FILE_PATH_R = "-fr"; // NOI18N public static final String PRINT_FILE_PATH_W = "-fw"; // NOI18N public static final String PRINT_FILE_PATH_C = "-fc"; // NOI18N private VcsFileSystem fileSystem = null; private FileSystemCache cache; private CommandOutputListener stdoutNRListener; private CommandOutputListener stderrNRListener; private CommandDataOutputListener stdoutListener; private CommandDataOutputListener stderrListener; private String dataRegex; private String errorRegex; private Collection filteredFilesOut = null; private boolean filteredFilesOutCaseInsensitive = false; private boolean localOnly; private boolean printOutput; private boolean printDebug; private boolean findFileResource; private boolean printFilesToProcess; private String filesToProcessSubstractPath; private String rootDir = ""; //private Set lockedFileObjects = new HashSet(); /** Creates new RecursiveFolderCommand */ public RecursiveFolderCommand() { } public void setFileSystem(VcsFileSystem fileSystem) { this.fileSystem = fileSystem; } private boolean runCommandRecursively(final FileObject dir, final Hashtable vars, final VcsCommand cmd) throws InterruptedException { /* String relWork; if (dir.length() > rootDir.length()) { relWork = dir.substring(rootDir.length() + 1); // the folder relative to the root directory. } else { relWork = ""; // the folder relative to the root directory. } while(relWork.length() > 0 && relWork.charAt(0) == File.separatorChar) relWork = relWork.substring(1); vars.put("RELWORK", relWork); // the folder relative to the root directory. //System.out.println("RELWORK = '"+relWork+"'"); File dirFile = new File(dir); final File[] subfiles = dirFile.listFiles(new FileFilter() { public boolean accept(File file) { return (file.isFile() && !filteredFilesOut.contains((filteredFilesOutCaseInsensitive) ? file.getName().toUpperCase() : file.getName())); } }); */ FileObject[] children = dir.getChildren(); ArrayList childrenFiles = new ArrayList(); ArrayList childrenFolders = new ArrayList(); for (int i = 0; i < children.length; i++) { FileObject fo = children[i]; if (fo.isFolder()) childrenFolders.add(fo); else childrenFiles.add(fo); } Table files = new Table(); boolean processAll = VcsCommandIO.getBooleanPropertyAssumeDefault(cmd, VcsCommand.PROPERTY_PROCESS_ALL_FILES) || fileSystem.isProcessUnimportantFiles(); VcsAction.addImportantFiles(childrenFiles, files, processAll, fileSystem); VcsCommandExecutor[] commands = VcsAction.doCommand(files, cmd, vars, fileSystem, stdoutNRListener, stderrNRListener, stdoutListener, stderrListener); boolean success = true; if (!localOnly) { for (Iterator it = childrenFolders.iterator(); it.hasNext(); ) { FileObject fo = (FileObject) it.next(); Hashtable newVars = new Hashtable(vars); boolean recSuccess = runCommandRecursively(fo, newVars, cmd); success = success && recSuccess; } } for (int i = 0; i < commands.length; i++) { VcsCommandExecutor vce = commands[i]; try { fileSystem.getCommandsPool().waitToFinish(vce); } catch (InterruptedException iexc) { for (int j = i; j < commands.length; j++) { fileSystem.getCommandsPool().kill(commands[j]); } throw iexc; } success = success && (vce.getExitStatus() == VcsCommandExecutor.SUCCEEDED); } return success; } private void waitToLoad(VcsCacheDir dir) throws InterruptedException { //System.out.println("waitToLoad("+dir+")"); if (!dir.isIgnoreListSet() && fileSystem.getIgnoreListSupport() != null) { dir.setIgnoreList(VcsUtilities.createIgnoreList(dir, dir.getFSPath(), fileSystem.getIgnoreListSupport())); } //System.out.println(" isLocal() = "+dir.isLocal()+", isLoaded() = "+dir.isLoaded()+", isIgnoreListSet() = "+dir.isIgnoreListSet()); if (!dir.isLocal()) { //System.out.println(" isLocal() = "+dir.isLocal()+", isLoaded() = "+dir.isLoaded()+", isIgnoreListSet() = "+dir.isIgnoreListSet()); dir.waitToLoad(); } } private void waitToLoad(VcsCacheDir dir, boolean recursively) throws InterruptedException { if (!dir.isLoaded()) { waitToLoad((VcsCacheDir) dir); if (recursively) { CacheDir[] subDirs = dir.getSubDirs(); for (int i = 0; i < subDirs.length; i++) { waitToLoad((VcsCacheDir) subDirs[i], recursively); } } } } private String getFSPath(CacheFile file) { return getFSPath(file.getAbsolutePath()); } private String getFSPath(String absolutePath) { String path = absolutePath; String fsRoot = fileSystem.getFile("").getAbsolutePath(); path = path.substring(fsRoot.length()); path = path.replace(File.separatorChar, '/'); while (path.startsWith("/")) path = path.substring(1); return path; } private boolean canProcessFile(String filePath) { if (fileSystem.isImportant(filePath)) return true; return fileSystem.isProcessUnimportantFiles(); } /** @deprecated use fillDirFilesWithTurbo */ private void fillDirFiles(Table files, CacheDir dir, CommandInfo info, boolean recursive) { String path; if (dir instanceof VcsCacheDir) { path = ((VcsCacheDir) dir).getFSPath(); } else { path = getFSPath(dir); } if (printDebug) stdoutListener.outputData(new String[] { "Collecting files for command "+info.cmd.getName()+" in folder '"+path+"'" }); FilenameFilter fsFilter = fileSystem.getFileFilter(); File dirFile = new File(dir.getAbsolutePath()); if (info.canRunOnFolders) { if ((info.canRunOnRoot || !(path.length() == 0 || ".".equals(path))) && info.canRunOnStatus(dir.getStatus())) { if (printDebug) stdoutListener.outputData(new String[] { " Processing folder = "+path }); files.put(path, (findFileResource) ? fileSystem.findResource(path) : null); } } if (path.length() > 0) path += "/"; if (info.canRunOnFiles) { CacheFile[] subFiles = dir.getFiles(); for (int i = 0; i < subFiles.length; i++) { if (info.canRunOnStatus(subFiles[i].getStatus()) && !dir.isIgnored(subFiles[i].getName()) && fsFilter.accept(dirFile, subFiles[i].getName())) { String filePath = path + subFiles[i].getName(); if (printDebug) stdoutListener.outputData(new String[] { " Processing file = "+filePath }); files.put(filePath, (findFileResource) ? fileSystem.findResource(filePath) : null); } } // Add local files. Local files are not part of the cache. if (info.canRunOnStatus(fileSystem.getStatusProvider().getLocalFileStatus())) { final Set cachedFilesSet = new HashSet(); for (int i = 0; i < subFiles.length; i++) { cachedFilesSet.add(subFiles[i].getName()); } String[] localSubFiles = dirFile.list(new FilenameFilter() { public boolean accept(File dir, String name) { return !new File(dir, name).isDirectory() && !cachedFilesSet.contains(name); } }); if (localSubFiles != null) { for (int i = 0; i < localSubFiles.length; i++) { if (!dir.isIgnored(localSubFiles[i]) && fsFilter.accept(dirFile, localSubFiles[i]) && !NBATTRS.equals(localSubFiles[i]) && !NBINTDB.equals(localSubFiles[i])) { String filePath = path + localSubFiles[i]; if (!files.containsKey(filePath) && canProcessFile(filePath)) { files.put(filePath, (findFileResource) ? fileSystem.findResource(filePath) : null); } } } } } } if (recursive) { CacheDir[] subDirs = dir.getSubDirs(); List localDirs = null; if (info.canRunOnStatus(fileSystem.getStatusProvider().getLocalFileStatus())) { String[] localSubFiles = dirFile.list(new FilenameFilter() { public boolean accept(File dir, String name) { return new File(dir, name).isDirectory(); } }); if (localSubFiles != null) { localDirs = new ArrayList(Arrays.asList(localSubFiles)); } } for (int i = 0; i < subDirs.length; i++) { if (!dir.isIgnored(subDirs[i].getName()) && fsFilter.accept(dirFile, subDirs[i].getName()) && canProcessFile(getFSPath(subDirs[i].getAbsolutePath()))) { fillDirFiles(files, subDirs[i], info, recursive); if (localDirs != null) localDirs.remove(subDirs[i].getName()); } } if (localDirs != null) { for (Iterator it = localDirs.iterator(); it.hasNext(); ) { String subDirName = (String) it.next(); if (!dir.isIgnored(subDirName) && fsFilter.accept(dirFile, subDirName) && canProcessFile(path + subDirName)) { fillLocalDirFiles(files, dir, new File(dirFile, subDirName), info, recursive); } } } } } private void fillLocalDirFiles(Table files, CacheDir dir, File dirFile, CommandInfo info, boolean recursive) { String path = getFSPath(dirFile.getAbsolutePath()); if (printDebug) stdoutListener.outputData(new String[] { "Collecting files for command "+info.cmd.getName()+" in folder '"+path+"'" }); FilenameFilter fsFilter = fileSystem.getFileFilter(); if (info.canRunOnFolders && (info.canRunOnRoot || !(path.length() == 0 || ".".equals(path)))) { if (printDebug) stdoutListener.outputData(new String[] { " Processing folder = "+path }); files.put(path, (findFileResource) ? fileSystem.findResource(path) : null); } if (path.length() > 0) path += "/"; if (info.canRunOnFiles) { String[] localSubFiles = dirFile.list(new FilenameFilter() { public boolean accept(File dir, String name) { return !new File(dir, name).isDirectory(); } }); if (localSubFiles != null) { for (int i = 0; i < localSubFiles.length; i++) { if (!dir.isIgnored(localSubFiles[i]) && fsFilter.accept(dirFile, localSubFiles[i]) && !NBATTRS.equals(localSubFiles[i]) && !NBINTDB.equals(localSubFiles[i])) { String filePath = path + localSubFiles[i]; if (canProcessFile(filePath)) { files.put(filePath, (findFileResource) ? fileSystem.findResource(filePath) : null); } } } } } if (recursive) { String[] localSubFiles = dirFile.list(new FilenameFilter() { public boolean accept(File dir, String name) { return new File(dir, name).isDirectory(); } }); if (localSubFiles == null) return ; List localDirs = Arrays.asList(localSubFiles); if (localDirs != null) { for (Iterator it = localDirs.iterator(); it.hasNext(); ) { String subDirName = (String) it.next(); if (!dir.isIgnored(subDirName) && fsFilter.accept(dirFile, subDirName)) { fillLocalDirFiles(files, dir, new File(dirFile, subDirName), info, recursive); } } } } } /** @deprecated use FileObject */ private boolean runCommandsRecursively(VcsCacheDir dir, Collection cmdInfos) throws InterruptedException { ArrayList realRecursiveCommands = new ArrayList(); ArrayList somewhatRecursiveCommands = new ArrayList(); if (printFilesToProcess) { return printFilesRecursively(dir, cmdInfos); } for (Iterator it = cmdInfos.iterator(); it.hasNext(); ) { CommandInfo info = (CommandInfo) it.next(); if (!localOnly && info.canRunOnMultipleFiles && !info.canRunOnMultipleFilesInFolder) { realRecursiveCommands.add(info); } else { somewhatRecursiveCommands.add(info); } } //System.out.println("runCommandsRecursively("+dir.getName()+", "+cmdInfos.size()+"): realRecursiveCommands = "+realRecursiveCommands+", somewhatRecursiveCommands = "+somewhatRecursiveCommands); boolean status = true; if (realRecursiveCommands.size() > 0) { status = runCommandsReallyRecursively(dir, realRecursiveCommands); } if (somewhatRecursiveCommands.size() > 0) { status &= runCommandsSomewhatRecursively(dir, somewhatRecursiveCommands); } return status; } /** @deprecated use runCommandsReallyRecursivelyWithTurbo */ private boolean runCommandsReallyRecursively(VcsCacheDir dir, Collection cmdInfos) throws InterruptedException { waitToLoad(dir, true); //String path = dir.getAbsolutePath().substring(fileSystem.getFile("").getAbsolutePath()); //while (path.startsWith("/")) path = path.substring(1); //FileObject fo = fileSystem.findResource(path); //files.put(path, fo); boolean status = true; CommandsPool cPool = fileSystem.getCommandsPool(); for (Iterator it = cmdInfos.iterator(); it.hasNext(); ) { CommandInfo info = (CommandInfo) it.next(); Table files = new Table(); fillDirFiles(files, dir, info, true); VcsCommandExecutor[] executors; if (printOutput) { executors = VcsAction.doCommand(files, info.cmd, info.vars, fileSystem, stdoutNRListener, stderrNRListener, null, null); } else { executors = VcsAction.doCommand(files, info.cmd, info.vars, fileSystem); } for (int i = 0; i < executors.length; i++) { try { cPool.waitToFinish(executors[i]); } catch (InterruptedException iexc) { for (int j = i; j < executors.length; j++) { cPool.kill(executors[j]); } throw iexc; } status &= (executors[i].getExitStatus() == VcsCommandExecutor.SUCCEEDED); } } return status; } private boolean runCommandsSomewhatRecursively(VcsCacheDir dir, Collection cmdInfos) throws InterruptedException { return runCommandsSomewhatRecursively(dir, null, cmdInfos); } private boolean runCommandsSomewhatRecursively(VcsCacheDir dir, File dirFile, Collection cmdInfos) throws InterruptedException { //System.out.println("runCommandsSomewhatRecursively("+dir+"), localOnly = "+localOnly); if (dirFile == null) waitToLoad(dir, false); CommandsPool cPool = fileSystem.getCommandsPool(); FilenameFilter fsFilter = fileSystem.getFileFilter(); boolean status = true; for (Iterator it = cmdInfos.iterator(); it.hasNext(); ) { CommandInfo info = (CommandInfo) it.next(); Table files = new Table(); if (dirFile == null) { fillDirFiles(files, dir, info, false); } else { fillLocalDirFiles(files, dir, dirFile, info, false); } VcsCommandExecutor[] executors; if (files.size() > 0) { if (printOutput) { executors = VcsAction.doCommand(files, info.cmd, info.vars, fileSystem, stdoutNRListener, stderrNRListener, null, null); } else { executors = VcsAction.doCommand(files, info.cmd, info.vars, fileSystem); } //System.out.println("doCommand("+files+", "+info.cmd.getName()); } else { executors = new VcsCommandExecutor[0]; //System.out.println("do no Command("+files+", "+info.cmd.getName()); } if (!localOnly) { CacheDir[] subDirs = null; if (dirFile == null) { dirFile = dir.getFile(); subDirs = dir.getSubDirs(); } List localDirs = null; if (info.canRunOnStatus(fileSystem.getStatusProvider().getLocalFileStatus())) { String[] localSubFiles = dirFile.list(new FilenameFilter() { public boolean accept(File dir, String name) { return new File(dir, name).isDirectory(); } }); if (localSubFiles != null) { localDirs = new ArrayList(Arrays.asList(localSubFiles)); } } if (subDirs != null) { for (int i = 0; i < subDirs.length; i++) { if (!dir.isIgnored(subDirs[i].getName()) && fsFilter.accept(dirFile, subDirs[i].getName()) && canProcessFile(getFSPath(subDirs[i].getAbsolutePath()))) { status &= runCommandsSomewhatRecursively((VcsCacheDir) subDirs[i], cmdInfos); if (localDirs != null) localDirs.remove(subDirs[i].getName()); } } } if (localDirs != null) { for (Iterator ldit = localDirs.iterator(); ldit.hasNext(); ) { String subDirName = (String) ldit.next(); String path = getFSPath(dirFile.getAbsolutePath()); if (path.length() > 0) path += "/"; if (!dir.isIgnored(subDirName) && fsFilter.accept(dirFile, subDirName) && canProcessFile(path + subDirName)) { status &= runCommandsSomewhatRecursively(dir, // recursion new File(dirFile, subDirName), cmdInfos); } } } } for (int i = 0; i < executors.length; i++) { try { cPool.waitToFinish(executors[i]); } catch (InterruptedException iexc) { for (int j = i; j < executors.length; j++) { fileSystem.getCommandsPool().kill(executors[j]); } throw iexc; } status &= (executors[i].getExitStatus() == VcsCommandExecutor.SUCCEEDED); } } return status; } private boolean printFilesRecursively(VcsCacheDir dir, Collection cmdInfos) { try { waitToLoad(dir, !localOnly); } catch (InterruptedException iexc) { return false; } CommandInfo info; if (cmdInfos.size() > 0) info = (CommandInfo) cmdInfos.iterator().next(); else info = new CommandInfo(new org.netbeans.modules.vcscore.cmdline.UserCommand(), null); Table files = new Table(); fillDirFiles(files, dir, info, !localOnly); File root = fileSystem.getFile(""); for (Iterator it = files.keySet().iterator(); it.hasNext(); ) { String fsPath = (String) it.next(); String filePath = new File(root, fsPath).getAbsolutePath(); stdoutListener.outputData(new String[] { filePath}); } /* BufferedWriter w = null; boolean status = true; try { w = new BufferedWriter(new FileWriter(fileToSaveFiles)); for (Iterator it = files.keySet().iterator(); it.hasNext(); ) { String fsPath = (String) it.next(); String filePath = new File(root, fsPath).getAbsolutePath() + "\n"; w.write(filePath); } } catch (IOException ioex) { status = false; } finally { if (w != null) { try { w.close(); } catch (IOException cioex) { status = false; } } } return status; */ return true; } private boolean runCommandsRecursively(FileObject dir, Collection cmdInfos) throws InterruptedException { boolean status = true; for (Iterator it = cmdInfos.iterator(); it.hasNext(); ) { CommandInfo info = (CommandInfo) it.next(); status &= runCommandRecursively(dir, info.vars, info.cmd); } return status; } private boolean runCommandsRecursively(String path, Collection cmdInfos) throws InterruptedException { //System.out.println("runCommandsRecursively("+path+", "+cmdInfos.size()); cache = CacheHandler.getInstance().getCache(fileSystem.getCacheIdStr()); if (cache != null) { Object locker = new Object(); //System.out.println("getting cache file ("+path+") = "+fileSystem.getFile(path)); cache.getCacheFile(new File(fileSystem.getFile(path), "testing"), CacheHandler.STRAT_DISK_OR_REFRESH_RECURS, locker); VcsCacheDir cacheDir = (VcsCacheDir) cache.getCacheFile(fileSystem.getFile(path), CacheHandler.STRAT_DISK, locker); //System.out.println("cacheDir = "+cacheDir); if (cacheDir == null) return true; boolean status = runCommandsRecursively(cacheDir, cmdInfos); locker = null; return status; } else { FileObject dir = fileSystem.findResource(path); return runCommandsRecursively(dir, cmdInfos); } } /** * This method is used to execute the command. * The provided command is executed recursively for all subfolders. * The command is executed only on non-empty folders. * * @param vars the variables that can be passed to the command * @param args the command line parametres passed to it in properties * @param stdoutNRListener listener of the standard output of the command * @param stderrNRListener listener of the error output of the command * @param stdoutListener listener of the standard output of the command which * satisfies regex |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.