List of usage examples for org.apache.commons.io FileUtils moveDirectory
public static void moveDirectory(File srcDir, File destDir) throws IOException
From source file:org.fuin.owndeb.pkg.base.AbstractDownloadTarGzPackage.java
private static void renameOriginalToPackageDir(final File srcDir, final File packageDir) { if (packageDir.exists()) { LOG.info("Delete existing package directory: {}", packageDir); FileUtils.deleteQuietly(packageDir); }//from ww w . j a v a 2 s. c o m LOG.info("Rename original directory '{}' to: {}", srcDir, packageDir); try { FileUtils.moveDirectory(srcDir, packageDir); } catch (final IOException ex) { throw new RuntimeException("Error moving " + srcDir + " to: " + packageDir, ex); } }
From source file:org.geoserver.util.IOUtils.java
/** * Renames a file./*from ww w . j a v a2 s .com*/ * * @param source The file to rename. * @param dest The file to rename to. */ public static void rename(File source, File dest) throws IOException { // same path? Do nothing if (source.getCanonicalPath().equalsIgnoreCase(dest.getCanonicalPath())) return; // windows needs special treatment, we cannot rename onto an existing file boolean win = System.getProperty("os.name").startsWith("Windows"); if (win && dest.exists()) { // windows does not do atomic renames, and can not rename a file if the dest file // exists if (!dest.delete()) { throw new IOException("Could not delete: " + dest.getCanonicalPath()); } } // make sure the rename actually succeeds if (!source.renameTo(dest)) { FileUtils.deleteQuietly(dest); if (source.isDirectory()) { FileUtils.moveDirectory(source, dest); } else { FileUtils.moveFile(source, dest); } } }
From source file:org.giavacms.cache.listener.CacheListener.java
@Override public void contextInitialized(ServletContextEvent sce) { StringBuffer physicalDir = new StringBuffer(); String docBaseProperty = sce.getServletContext().getInitParameter("baseFolderSystemProperty"); logger.info("baseFolderSystemProperty: " + docBaseProperty); if (docBaseProperty != null && !docBaseProperty.trim().isEmpty()) { // se inizia per $ una var di sistema if (docBaseProperty.trim().startsWith("$")) { String docBase = System.getenv(docBaseProperty.trim().substring(1)); if (docBase != null && !docBase.isEmpty()) physicalDir.append(docBase); } else {/*from w ww . j a v a 2 s . co m*/ // altrimenti la suo cosi com' physicalDir.append(docBaseProperty); } } String pagesPath = sce.getServletContext().getInitParameter(MappingFilter.PAGES_PATH_PARAM_NAME); physicalDir.append(pagesPath); backupDir = new File(physicalDir.toString()); boolean backupRestored = false; if (backupDir.exists() && backupDir.isDirectory()) { try { cacheDir = cacheService.getAbsolutePath(pagesPath); if (cacheDir.exists()) { cacheDir.delete(); } FileUtils.moveDirectory(backupDir, cacheDir); logger.info("Pages backup restored from " + backupDir.getAbsolutePath() + " --> " + cacheDir.getAbsolutePath()); backupRestored = true; } catch (Exception e) { logger.error("Pages backup NOT restored from " + backupDir.getAbsolutePath()); logger.error(e.getMessage(), e); } } if (!backupRestored) { cacheService.writeAll(pagesPath); logger.warn("Pages backup NOT restored; brand new pages cache saved to " + pagesPath); } try { cacheDir = cacheService.getAbsolutePath(pagesPath); } catch (Exception e) { } }
From source file:org.giavacms.cache.listener.CacheListener.java
@PreDestroy public void preDestroy() { logger.warn("preDestroy"); if (backupDir == null || cacheDir == null) { return;//from www . jav a 2s. c o m } if (!cacheDir.exists()) { return; } if (backupDir.exists()) { backupDir.delete(); } try { FileUtils.moveDirectory(cacheDir, backupDir); logger.info("Pages backup saved from " + cacheDir.getAbsolutePath() + " --> " + backupDir.getAbsolutePath()); } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:org.goobi.io.SafeFile.java
public void moveDirectory(SafeFile destDir) throws IOException { FileUtils.moveDirectory(delegate, destDir.delegate); }
From source file:org.goobi.io.SafeFile.java
public void moveDirectory(String destDir) throws IOException { FileUtils.moveDirectory(delegate, new File(destDir)); }
From source file:org.gradle.api.changedetection.state.DefaultDirectoryStateChangeDetecter.java
public void detectChanges(ChangeProcessor changeProcessor) { Clock c = new Clock(); try {/*from w w w . j a v a2s . c o m*/ int lowestLevel = 0; try { lowestLevel = directoryListFileCreator.createDirectoryListFiles(directoryToProcess); } catch (IOException e) { throw new GradleException("failed to create directory list files", e); } // Calculate the digests of files and directories Map<String, DirectoryState> previousLevelDirectoryStates = Collections .unmodifiableMap(new HashMap<String, DirectoryState>()); Map<String, DirectoryState> currentLevelDirectoryStates = new ConcurrentHashMap<String, DirectoryState>(); for (int levelIndex = lowestLevel; levelIndex >= 0; levelIndex--) { final File directoryLevelListFile = stateFileUtil.getDirsListFile(levelIndex); threadPool = ThreadUtils.newFixedThreadPool(4); if (directoryLevelListFile.exists()) { final File stateFile = stateFileUtil .getNewDirsStateFile(stateFileUtil.getDirsStateFilename(levelIndex)); final StateFileWriter newDirectoriesStateFileWriter = new StateFileWriter(ioFactory, stateFile); BufferedReader directoryListFileReader = null; try { directoryListFileReader = new BufferedReader(new FileReader(directoryLevelListFile)); String absoluteDirectoryPath = null; while ((absoluteDirectoryPath = directoryListFileReader.readLine()) != null) { final DirectoryState directoryState = directoryStateBuilder .directory(new File(absoluteDirectoryPath)).getDirectoryState(); final DirectoryStateDigestCalculator digestCalculator = new DirectoryStateDigestCalculator( directoryState, digesterCache, digesterUtil, this, currentLevelDirectoryStates, previousLevelDirectoryStates, ioFactory); threadPool.submit(digestCalculator); } // each directory level has to be processed before continueing with the next(higher) level ThreadUtils.shutdown(threadPool); final List<DirectoryState> currentLevelDirectoryStatesList = new ArrayList<DirectoryState>( currentLevelDirectoryStates.values()); Collections.sort(currentLevelDirectoryStatesList); // if one of the directory state processors failed -> change detection fails // This is a simple not so efficient way, we could fail earlier but that will make everything more complicated for (final DirectoryState directoryState : currentLevelDirectoryStatesList) { final Throwable failureCause = directoryState.getFailureCause(); if (failureCause != null) throw new GradleException("Failed to detect changes", failureCause); else { newDirectoriesStateFileWriter.addDigest(directoryState.getRelativePath(), directoryState.getDigest()); } } } catch (IOException e) { throw new GradleException("failed to detect changes (dirs." + newDirectoriesStateFileWriter.getStateFile().getAbsolutePath() + ".state write failed)", e); } finally { IOUtils.closeQuietly(directoryListFileReader); FileUtils.deleteQuietly(directoryLevelListFile); newDirectoriesStateFileWriter.close(); } } previousLevelDirectoryStates = Collections .unmodifiableMap(new HashMap<String, DirectoryState>(currentLevelDirectoryStates)); currentLevelDirectoryStates = new ConcurrentHashMap<String, DirectoryState>(); } // Compare new and old directory state + notify DirectoryStateChangeDetecterListener try { boolean keepComparing = true; int currentLevel = 0; final StateChangeEventDispatcher stateChangeEventDispatcher = new StateChangeEventDispatcher( stateChangeEventQueue, 100L, TimeUnit.MILLISECONDS, changeProcessor); final Thread changeProcessorEventThread = new Thread(stateChangeEventDispatcher); changeProcessorEventThread.start(); threadPool = ThreadUtils.newFixedThreadPool(4); while (keepComparing && currentLevel <= lowestLevel) { keepComparing = stateComparator.compareState(this, currentLevel); currentLevel++; } ThreadUtils.shutdown(threadPool); while (!stateChangeEventQueue.isEmpty()) { Thread.yield(); } stateChangeEventDispatcher.stopConsuming(); ThreadUtils.join(changeProcessorEventThread); for (DirectoryStateDigestComparator directoryStateDigestComparator : directoryStateDigestComparators) { final Throwable failureCause = directoryStateDigestComparator.getFailureCause(); if (failureCause != null) throw new GradleException("failed to compare directory state", failureCause); } } catch (IOException e) { throw new GradleException("failed to compare new and old state", e); } // Remove old directory state try { FileUtils.deleteDirectory(stateFileUtil.getOldDirectoryStateDir()); } catch (IOException e) { throw new GradleException("failed to clean old state", e); } // Move new to old directory state try { FileUtils.moveDirectory(stateFileUtil.getNewDirectoryStateDir(), stateFileUtil.getOldDirectoryStateDir()); } catch (IOException e) { throw new GradleException("failed to transfer current state to old state", e); } } finally { System.out.println(c.getTime()); } }
From source file:org.gradle.api.internal.filestore.PathKeyFileStore.java
public void moveFilestore(File destination) { if (baseDir.exists()) { try {/*www.j a va2 s .c om*/ FileUtils.moveDirectory(baseDir, destination); } catch (IOException e) { throw new UncheckedException(e); } } baseDir = destination; }
From source file:org.gradle.util.GFileUtils.java
public static void moveDirectory(File source, File destination) { try {/* w w w .j a v a 2 s . c o m*/ FileUtils.moveDirectory(source, destination); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.interreg.docexplore.PresentationImporter.java
void doImport(Component comp, String title, String desc, File bookFile) throws Exception { progress = 0;/*w ww . j ava2 s. c om*/ File indexFile = new File(exportDir, "index.xml"); if (!indexFile.exists()) { ErrorHandler.defaultHandler.submit(new Exception("Invalid server resource directory")); } try { int bookNum = 0; for (String filename : exportDir.list()) if (filename.startsWith("book") && filename.endsWith(".xml")) { String numString = filename.substring(4, filename.length() - 4); try { int num = Integer.parseInt(numString); if (num >= bookNum) bookNum = num + 1; } catch (Exception e) { } } String bookFileName = "book" + bookNum + ".xml"; String xml = StringUtils.readFile(indexFile, "UTF-8"); //look for duplicate int curIndex = 0; while (curIndex >= 0) { int startIndex = xml.indexOf("<Book", curIndex); if (startIndex < 0) break; int index = xml.indexOf("title=\"", startIndex); if (index < 0) break; index += "title=\"".length(); int endIndex = xml.indexOf("\"", index); String testTitle = xml.substring(index, endIndex); if (testTitle.equals(title)) { int res = JOptionPane.showConfirmDialog(comp, XMLResourceBundle.getString("authoring-lrb", "exportDuplicateMessage"), "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION); if (res == JOptionPane.CANCEL_OPTION) return; if (res == JOptionPane.YES_OPTION) { endIndex = xml.indexOf("</Book>", startIndex); xml = xml.substring(0, startIndex) + xml.substring(endIndex + "</Book>".length(), xml.length()); } else { title = JOptionPane.showInputDialog(comp, XMLResourceBundle.getString("authoring-lrb", "collectionAddBookMessage"), title); if (title != null) doImport(comp, title, desc, bookFile); return; } break; } curIndex = endIndex; } int insertIndex = xml.indexOf("</Index>"); if (insertIndex < 0) throw new Exception("Invalid index file"); xml = xml.substring(0, insertIndex) + "\t<Book title=\"" + title + "\" src=\"" + bookFileName + "\">\n\t\t" + desc + "\n\t</Book>\n" + xml.substring(insertIndex); FileOutputStream indexOutput = new FileOutputStream(indexFile); indexOutput.write(xml.getBytes(Charset.forName("UTF-8"))); indexOutput.close(); File bookDir = new File(exportDir, "book" + bookNum); String bookSpec = StringUtils.readFile(bookFile, "UTF-8"); int start = bookSpec.indexOf("path=\"") + 6; int end = bookSpec.indexOf("\"", start); bookSpec = bookSpec.substring(0, start) + bookDir.getName() + "/" + bookSpec.substring(end); FileOutputStream bookOutput = new FileOutputStream(new File(exportDir, bookFileName)); bookOutput.write(bookSpec.toString().getBytes(Charset.forName("UTF-8"))); bookOutput.close(); File contentDir = new File(bookFile.getParentFile(), bookFile.getName().substring(0, bookFile.getName().length() - 4)); FileUtils.moveDirectory(contentDir, bookDir); } catch (Exception e) { ErrorHandler.defaultHandler.submit(e); } }