/** * Copies a file to a directory preserving the file date. This method copies the contents of the specified source file to a file of the same name in the * specified destination directory. The destination directory is created if it does not exist. If the destination file exists, then this method will * overwrite it. * * @param srcFile an existing file to copy, must not be <code>null</code> * @param destDir the directory to place the copy in, must not be <code>null</code> * @throws Fabric3Exception if an IO error occurs during copying */ public static void copyFileToDirectory(File srcFile, File destDir) throws Fabric3Exception { copyFileToDirectory(srcFile, destDir, true); }
/** * Copies a file to a new location preserving the file date. This method copies the contents of the specified source file to the specified destination * file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it. * * @param srcFile an existing file to copy, must not be <code>null</code> * @param destFile the new file, must not be <code>null</code> * @throws Fabric3Exception if an IO error occurs during copying */ public static void copyFile(File srcFile, File destFile) throws Fabric3Exception { copyFile(srcFile, destFile, true); }
/** * Copies a whole directory to a new location preserving the file dates. This method copies the specified directory and all its child directories and files * to the specified destination. The destination is the new location and name of the directory. The destination directory is created if it does not exist. * If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence. * * @param srcDir an existing directory to copy, must not be <code>null</code> * @param destDir the new directory, must not be <code>null</code> * @throws Fabric3Exception if an IO error occurs during copying * @since Commons IO 1.1 */ public static void copyDirectory(File srcDir, File destDir) throws Fabric3Exception { copyDirectory(srcDir, destDir, true); }
public void afterInvoke(File file, Object[] payload) { if (payload.length != 1) { throw new AssertionError("Invalid payload length: " + payload.length); } if (!(payload[0] instanceof Closeable)) { throw new AssertionError("Invalid payload type: " + payload[0]); } IOHelper.closeQuietly((Closeable) payload[0]); }
@SuppressWarnings("ResultOfMethodCallIgnored") private void createDirectories() throws FabricException { // clear out the tmp directory if (tempDirectory.exists()) { FileHelper.cleanDirectory(tempDirectory); } tempDirectory.mkdirs(); extensionsDirectory.mkdirs(); dataDirectory.mkdirs(); }
/** * Creates a new read-write runtime directory * * @param sourceConfigDir the configuration directory to use as a template * @param targetDir the target runtime directory */ public static void cloneRuntimeImage(File sourceConfigDir, File targetDir) { File targetConfigDir = new File(targetDir, "config"); FileHelper.forceMkdir(targetConfigDir); FileHelper.copyDirectory(sourceConfigDir, targetConfigDir); FileHelper.forceMkdir(new File(targetDir, "data")); File targetDeployDir = new File(targetDir, "deploy"); FileHelper.forceMkdir(targetDeployDir); File sourceDeployDir = new File(targetConfigDir.getParent(), "deploy"); FileHelper.copyDirectory(sourceDeployDir, targetDeployDir); File repository = new File(targetDir, "repository"); FileHelper.forceMkdir(repository); FileHelper.forceMkdir(new File(repository, "runtime")); FileHelper.forceMkdir(new File(repository, "user")); FileHelper.forceMkdir(new File(targetDir, "tmp")); }
@Init public void init() throws IOException { if (tempDir.exists()) { FileHelper.deleteDirectory(tempDir); } tempDir.mkdirs(); }
/** * Returns the current {@link OperatingSystem}. * * @return the current OS */ public static OperatingSystem getOperatingSystem() { String name = System.getProperty("os.name"); String processor = OSHelper.parseProcessor(System.getProperty("os.arch")); String versionStr = OSHelper.parseVersion(System.getProperty("os.version")); Version version = Version.parseVersion(versionStr); return new OperatingSystem(name, processor, version); }
/** * Returns the index of the last extension separator character, which is a dot. This method also checks that there is no directory separator after the last * dot. To do this it uses {@link #indexOfLastSeparator(String)} which will handle a file in either Unix or Windows format. The output will be the same * irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; }
/** * Recursively schedule directory for deletion on JVM exit. * * @param directory directory to delete, must not be <code>null</code> * @throws NullPointerException if the directory is <code>null</code> * @throws Fabric3Exception in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(File directory) throws Fabric3Exception { if (!directory.exists()) { return; } cleanDirectoryOnExit(directory); directory.deleteOnExit(); }
/** * Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories. * * @param file file or directory to delete, must not be <code>null</code> * @throws NullPointerException if the file is <code>null</code> * @throws Fabric3Exception in case deletion is unsuccessful */ public static void forceDeleteOnExit(File file) throws Fabric3Exception { if (file.isDirectory()) { deleteDirectoryOnExit(file); } else { file.deleteOnExit(); } }
@SuppressWarnings("unchecked") public E nextElement() { if (!hasMoreElements()) { throw new NoSuchElementException(); } return (E) enumerations[index].nextElement(); }
public Enumeration<URL> getResources(String name) throws IOException { Enumeration[] resources = new Enumeration[2]; resources[0] = getParent().getResources(name); resources[1] = findResources(name); return new CompositeEnumeration<>(resources); }
private void releaseLock(File lockFile, FileLock lock, FileChannel lockChannel) { if (lock != null) { try { lock.release(); IOHelper.closeQuietly(lockChannel); if (lockFile.exists()) { lockFile.delete(); } } catch (IOException e) { // ignore } } }
public void archive(File file, File archiveDirectory) throws IOException { File destFile = new File(archiveDirectory, file.getName()); FileHelper.copyFile(file, destFile); file.delete(); } }
/** * Recursively delete a directory. * * @param directory directory to delete * @throws Fabric3Exception in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws Fabric3Exception { if (!directory.exists()) { return; } cleanDirectory(directory); if (!directory.delete()) { String message = "Unable to delete directory " + directory + "."; throw new Fabric3Exception(message); } }
/** * Gets the name minus the path from a full filename. This method will handle a file in either Unix or Windows format. The text after the last forward or * backslash is returned. * <pre> * a/b/c.txt --> c.txt * a.txt --> a.txt * a/b/c --> c * a/b/c/ --> "" * </pre> * The output will be the same irrespective of the machine that the code is running on. * * @param fileName the filename to query, null returns null * @return the name of the file without the path, or an empty string if none exists */ public static String getName(String fileName) { if (fileName == null) { return null; } int index = indexOfLastSeparator(fileName); return fileName.substring(index + 1); }
public void afterInvoke(File file, Object[] payload) { if (payload.length != 1) { throw new AssertionError("Invalid payload length: " + payload.length); } if (!(payload[0] instanceof DataHandler)) { throw new AssertionError("Invalid payload type: " + payload[0]); } DataHandler dataHandler = (DataHandler) payload[0]; try { IOHelper.closeQuietly(dataHandler.getInputStream()); } catch (IOException e) { // ignore as this will not happen } }
public void error(File file, File errorDirectory, Exception e) throws IOException { FileHelper.copyFile(file, new File(errorDirectory, file.getName())); file.delete(); }
public Object[] beforeInvoke(File file) throws InvalidDataException { FileInputStream fileStream = null; try { fileStream = new FileInputStream(file); return new Object[]{new BufferedInputStream(fileStream)}; } catch (FileNotFoundException e) { IOHelper.closeQuietly(fileStream); throw new InvalidDataException(e); } }