Example usage for org.apache.commons.io FileUtils forceDelete

List of usage examples for org.apache.commons.io FileUtils forceDelete

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils forceDelete.

Prototype

public static void forceDelete(File file) throws IOException 

Source Link

Document

Deletes a file.

Usage

From source file:info.donsun.cache.filecache.FileCache.java

@Override
public void remove(Object key) throws CacheException {
    synchronized (LOCK) {
        // ??// ww  w  .j a va2 s.c  o  m
        for (String fileName : listFileNames()) {
            if (isCacheFile(key, fileName)) {
                File file = FileUtils.getFile(config.getDir(), fileName);
                try {
                    FileUtils.forceDelete(file);
                    break;
                } catch (IOException e) {
                    throw new CacheException("Delete file " + config.getDir() + " fail.", e);
                }
            }
        }
    }
}

From source file:de.pksoftware.springstrap.fs.service.LocalFileSystemService.java

/**
 * //w  w w  .  j  av  a  2 s  . c o  m
 */
@Override
public void deleteItem(ItemVisibility visibility, FileSystemBucket bucket, IFileSystemPath path)
        throws NoSuchItemException, FSIOException {
    String pathString = FileSystemUtils.validateScopedPath(path);

    File itemToDelete = new File(getBucketRoot(visibility, bucket), pathString);

    if (!itemToDelete.exists()) {
        throw new NoSuchItemException(bucket, path);
    }

    try {
        FileUtils.forceDelete(itemToDelete);
    } catch (IOException e) {
        throw new FSIOException(bucket, path, "deleteItem", e);
    }
}

From source file:gobblin.scheduler.JobConfigFileMonitorTest.java

@AfterClass
public void tearDown() throws TimeoutException, IOException {
    if (jobConfigDir != null) {
        FileUtils.forceDelete(new File(jobConfigDir));
    }/*from  w w  w.j a va 2s .  c om*/
    this.serviceManager.stopAsync().awaitStopped(30, TimeUnit.SECONDS);
}

From source file:com.qq.tars.maven.gensrc.TarsBuildMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("Start run build...");
    try {//www .jav  a  2 s  .  c  om
        if (platformName == null) {
            String osName = System.getProperty("os.name");
            if (osName.toLowerCase().contains(Platform.WINDOWS_NAME)) {
                platformName = Platform.WINDOWS_NAME;
            } else {
                platformName = Platform.UNIX_NAME;
            }
        }

        File targetDir = new File(project.getBuild().getDirectory());
        File tarsDir = new File(targetDir, "tars");
        if (tarsDir.exists()) {
            FileUtils.forceDelete(tarsDir);
        }
        tarsDir.mkdir();

        File binDir = new File(tarsDir, "bin");
        binDir.mkdir();

        File confDir = new File(tarsDir, "conf");
        confDir.mkdir();

        File dataDir = new File(tarsDir, "data");
        dataDir.mkdir();

        File binAppsDir = new File(binDir, "apps" + File.separator + "ROOT");
        binAppsDir.mkdir();
        FileUtils.copyDirectory(war, binAppsDir);

        File binConfDir = new File(binDir, "conf");
        binConfDir.mkdir();

        File binLogDir = new File(binDir, "log");
        binLogDir.mkdir();

        File libDir = new File(binDir, "lib");
        libDir.mkdir();

        String app = getApp();
        String server = getServer();

        if (app == null || server == null) {
            throw new MojoExecutionException("Failed to build app server is null.");
        }

        String configFileName = String.format("%s.%s.%s", app, server, "config.conf");
        File configFile = new File(confDir, configFileName);
        getLog().info(String.format("Create config file %s ", configFile.getCanonicalPath()));
        createConfigScript(binDir, dataDir, binLogDir, configFile);

        installLibDependency(libDir);

        getLog().info(String.format("Create platform %s start script...", platformName));
        Platform platform = Platform.getInstance(platformName);
        String binscript = createBinScript(platform, libDir, binDir, configFile);
        getLog().info("Start Script in " + binscript);
    } catch (Exception e) {
        getLog().error(e);
        throw new MojoExecutionException("Failed to tars build", e);
    }
}

From source file:com.facebook.presto.accumulo.AccumuloQueryRunner.java

/**
 * Creates and starts an instance of MiniAccumuloCluster, returning the new instance.
 *
 * @return New MiniAccumuloCluster/*from   w  w  w. j a va2s. c o  m*/
 */
private static MiniAccumuloCluster createMiniAccumuloCluster() throws IOException, InterruptedException {
    // Create MAC directory
    File macDir = Files.createTempDirectory("mac-").toFile();
    LOG.info("MAC is enabled, starting MiniAccumuloCluster at %s", macDir);

    // Start MAC and connect to it
    MiniAccumuloCluster accumulo = new MiniAccumuloCluster(macDir, MAC_PASSWORD);
    accumulo.start();

    // Add shutdown hook to stop MAC and cleanup temporary files
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            LOG.info("Shutting down MAC");
            accumulo.stop();
        } catch (IOException | InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new PrestoException(MINI_ACCUMULO, "Failed to shut down MAC instance", e);
        }

        try {
            LOG.info("Cleaning up MAC directory");
            FileUtils.forceDelete(macDir);
        } catch (IOException e) {
            throw new PrestoException(MINI_ACCUMULO, "Failed to clean up MAC directory", e);
        }
    }));

    return accumulo;
}

From source file:com.magnet.plugin.generator.Generator.java

public void makeFilePerformance(ProgressIndicator progressIndicator) {
    progressIndicator.setFraction(0.1);//from  w ww  . j  ava  2 s .c o  m
    try {
        File cachedSourceFolder = cacheManager.getControllerSourceFolder();

        // copy test files
        File generatedTestFiles = new File(cachedSourceFolder, CacheManager.RELATIVE_TEST_DIR);
        File targetTestFolder = ProjectManager.getTestSourceFolderFile(project);
        if (generatedTestFiles.exists()) {
            if (targetTestFolder != null && targetTestFolder.exists()) {
                // if test filename exists, copy the test into a fileName_latest.java test
                List<String> testFiles = FileHelper.getCommonTestFiles(project, controllerName, packageName);
                String fileName = testFiles == null || testFiles.size() == 0 ? null : testFiles.get(0); // assuming one test class
                if (fileName == null || !fileName.endsWith(".java")) {
                    FileUtils.copyDirectory(generatedTestFiles, targetTestFolder);
                } else {
                    String newFileName = fileName + ".latest";
                    String header = Rest2MobileMessages.getMessage(Rest2MobileMessages.LATEST_TEST_CLASS_HEADER,
                            fileName.substring(fileName.lastIndexOf('/') + 1));
                    File newFile = new File(generatedTestFiles, newFileName);
                    File oldFile = new File(generatedTestFiles, fileName);
                    String content = FileUtils.readFileToString(oldFile);
                    FileUtils.write(newFile, header);
                    FileUtils.write(newFile, content, true);
                    FileUtils.forceDelete(oldFile);
                    FileUtils.copyDirectory(generatedTestFiles, targetTestFolder);
                }
            }
            // TODO: the src directory can be confusing, it should be called test,or tests as with ios.
            File rootTestDirectory = new File(cachedSourceFolder, "src");
            FileUtils.deleteDirectory(rootTestDirectory);
        }

        // copy others
        FileUtils.copyDirectory(cachedSourceFolder, ProjectManager.getSourceFolderFile(project));
        ControllerHistoryManager.saveController(project, cacheManager.getControllerFolder().getName());
        File controllerFile = ProjectManager.getControllerFile(project, packageName, controllerName);
        if (null != controllerFile) {
            showControllerFile(controllerFile);
        }
        //displayIndicatorMessage(progressIndicator, "Completed generation", 100);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.magnet.plugin.r2m.generator.Generator.java

public void makeFilePerformance(ProgressIndicator progressIndicator) {
    progressIndicator.setFraction(0.1);/*from  w ww .  j av  a2  s  .c  o m*/
    try {
        File cachedSourceFolder = cacheManager.getControllerSourceFolder();

        // copy test files
        File generatedTestFiles = new File(cachedSourceFolder, CacheManager.RELATIVE_TEST_DIR);
        File targetTestFolder = ProjectManager.getTestSourceFolderFile(project);
        if (generatedTestFiles.exists()) {
            if (targetTestFolder != null && targetTestFolder.exists()) {
                // if test filename exists, copy the test into a fileName_latest.java test
                List<String> testFiles = FileHelper.getCommonTestFiles(project, controllerName, packageName);
                String fileName = testFiles == null || testFiles.size() == 0 ? null : testFiles.get(0); // assuming one test class
                if (fileName == null || !fileName.endsWith(".java")) {
                    FileUtils.copyDirectory(generatedTestFiles, targetTestFolder);
                } else {
                    String newFileName = fileName + ".latest";
                    String header = R2MMessages.getMessage("LATEST_TEST_CLASS_HEADER",
                            fileName.substring(fileName.lastIndexOf('/') + 1));
                    File newFile = new File(generatedTestFiles, newFileName);
                    File oldFile = new File(generatedTestFiles, fileName);
                    String content = FileUtils.readFileToString(oldFile);
                    FileUtils.write(newFile, header);
                    FileUtils.write(newFile, content, true);
                    FileUtils.forceDelete(oldFile);
                    FileUtils.copyDirectory(generatedTestFiles, targetTestFolder);
                }
            }
            // TODO: the src directory can be confusing, it should be called test,or tests as with ios.
            File rootTestDirectory = new File(cachedSourceFolder, "src");
            FileUtils.deleteDirectory(rootTestDirectory);
        }

        // copy others
        FileUtils.copyDirectory(cachedSourceFolder, ProjectManager.getSourceFolderFile(project));
        ControllerHistoryManager.saveController(project, cacheManager.getControllerFolder().getName());
        File controllerFile = ProjectManager.getControllerFile(project, packageName, controllerName);
        if (null != controllerFile) {
            showControllerFile(controllerFile);
        }
        //displayIndicatorMessage(progressIndicator, "Completed generation", 100);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.github.jrh3k5.flume.mojo.plugin.AbstractFlumePluginMojo.java

/**
 * Build a Flume plugin.//from   ww w  . j a va  2s.  co m
 * 
 * @param pluginLibrary
 *            A {@link File} representing the library that is to copied into the {@code lib/} directory of the plugin.
 * @param mavenProject
 *            A {@link MavenProject} representing the project from which dependency information should be read.
 * @throws MojoExecutionException
 *             If any errors occur during the bundling of the plugin archive.
 */
protected void buildFlumePluginArchive(File pluginLibrary, MavenProject mavenProject)
        throws MojoExecutionException {
    final String pluginName = getPluginName();
    // Create the directory into which the libraries will be copied
    final File pluginStagingDirectory = new File(pluginsStagingDirectory,
            String.format("%s-staging", pluginName));
    final File stagingDirectory = new File(pluginStagingDirectory, pluginName);
    try {
        FileUtils.forceMkdir(stagingDirectory);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to create directory: " + stagingDirectory.getAbsolutePath(),
                e);
    }

    final File libDirectory = new File(stagingDirectory, "lib");
    // Copy the primary library
    try {
        FileUtils.copyFile(pluginLibrary, new File(libDirectory, pluginLibrary.getName()));
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Failed to copy primary artifact to staging lib directory: " + libDirectory.getAbsolutePath(),
                e);
    }

    // Copy the dependencies of the plugin into the libext directory
    final File libExtDirectory = new File(stagingDirectory, "libext");
    final AndArtifactFilter joinFilter = new AndArtifactFilter();
    joinFilter.add(providedArtifactFilter);
    joinFilter.add(new ExclusionArtifactFilter(exclusions));
    for (DependencyNode resolvedDependency : resolveDependencies(mavenProject, joinFilter)) {
        copyPluginDependency(resolvedDependency, libExtDirectory);
    }

    // Because of the way that Maven represents dependency trees, the above logic may copy the given plugin library into libext - remove it if so
    final File pluginLibraryLibExt = new File(libExtDirectory, pluginLibrary.getName());
    if (pluginLibraryLibExt.exists()) {
        try {
            FileUtils.forceDelete(pluginLibraryLibExt);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to delete file: " + pluginLibraryLibExt.getAbsolutePath(),
                    e);
        }
    }

    String classifier = null;
    // If the plugin name is the same as the artifact, then don't bother over-complicating the classifier
    if (project.getArtifactId().equals(pluginName)) {
        classifier = classifierSuffix;
    } else {
        classifier = String.format("%s-%s", pluginName, classifierSuffix);
    }
    final ArchiveUtils archiveUtils = ArchiveUtils.getInstance(new MojoLogger(getLog(), getClass()));
    // Create the TAR
    final File tarFile = new File(pluginStagingDirectory,
            String.format("%s-%s-%s.tar", project.getArtifactId(), project.getVersion(), classifier));
    try {
        archiveUtils.tarDirectory(pluginStagingDirectory, tarFile);
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Failed to TAR directory %s to file %s",
                stagingDirectory.getAbsolutePath(), tarFile.getAbsolutePath()), e);
    }

    // GZIP the TAR file
    final File gzipFile = new File(outputDirectory, String.format("%s.gz", tarFile.getName()));
    try {
        archiveUtils.gzipFile(tarFile, gzipFile);
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Failed to gzip TAR file %s to %s",
                tarFile.getAbsolutePath(), gzipFile.getAbsolutePath()), e);
    }

    // Attach the artifact, if configured to do so
    if (attach) {
        projectHelper.attachArtifact(project, "tar.gz", classifier, gzipFile);
    }
}

From source file:com.simpligility.maven.plugins.android.phase09package.AarMojo.java

/**
 * @return AAR file.//from   w ww  .  ja v  a 2  s .  c  om
 * @throws MojoExecutionException
 */
protected File createAarLibraryFile(File classesJar) throws MojoExecutionException {
    final File aarLibrary = new File(targetDirectory, finalName + "." + AAR);
    FileUtils.deleteQuietly(aarLibrary);

    try {
        final ZipArchiver zipArchiver = new ZipArchiver();
        zipArchiver.setDestFile(aarLibrary);

        zipArchiver.addFile(destinationManifestFile, "AndroidManifest.xml");
        addDirectory(zipArchiver, assetsDirectory, "assets");
        addDirectory(zipArchiver, resourceDirectory, "res");
        zipArchiver.addFile(classesJar, SdkConstants.FN_CLASSES_JAR);

        final File[] overlayDirectories = getResourceOverlayDirectories();
        for (final File resOverlayDir : overlayDirectories) {
            if (resOverlayDir != null && resOverlayDir.exists()) {
                addDirectory(zipArchiver, resOverlayDir, "res");
            }
        }

        if (consumerProguardFiles != null) {
            final File mergedConsumerProguardFile = new File(targetDirectory, "consumer-proguard.txt");
            if (mergedConsumerProguardFile.exists()) {
                FileUtils.forceDelete(mergedConsumerProguardFile);
            }
            mergedConsumerProguardFile.createNewFile();
            StringBuilder mergedConsumerProguardFileBuilder = new StringBuilder();
            for (File consumerProguardFile : consumerProguardFiles) {
                if (consumerProguardFile.exists()) {
                    getLog().info("Adding consumer proguard file " + consumerProguardFile);
                    FileInputStream consumerProguardFileInputStream = null;
                    try {
                        consumerProguardFileInputStream = new FileInputStream(consumerProguardFile);
                        mergedConsumerProguardFileBuilder
                                .append(IOUtils.toString(consumerProguardFileInputStream));
                        mergedConsumerProguardFileBuilder.append(SystemUtils.LINE_SEPARATOR);
                    } catch (IOException e) {
                        throw new MojoExecutionException("Error writing consumer proguard file ", e);
                    } finally {
                        IOUtils.closeQuietly(consumerProguardFileInputStream);
                    }
                }
            }
            FileOutputStream mergedConsumerProguardFileOutputStream = null;
            try {
                mergedConsumerProguardFileOutputStream = new FileOutputStream(mergedConsumerProguardFile);
                IOUtils.write(mergedConsumerProguardFileBuilder, mergedConsumerProguardFileOutputStream);
            } catch (IOException e) {
                throw new MojoExecutionException("Error writing consumer proguard file ", e);
            } finally {
                IOUtils.closeQuietly(mergedConsumerProguardFileOutputStream);
            }

            zipArchiver.addFile(mergedConsumerProguardFile, "proguard.txt");
        }

        addR(zipArchiver);

        // Lastly, add any native libraries
        addNativeLibraries(zipArchiver);

        zipArchiver.createArchive();
    } catch (ArchiverException e) {
        throw new MojoExecutionException("ArchiverException while creating ." + AAR + " file.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("IOException while creating ." + AAR + " file.", e);
    }

    return aarLibrary;
}

From source file:io.prestosql.plugin.accumulo.AccumuloQueryRunner.java

/**
 * Creates and starts an instance of MiniAccumuloCluster, returning the new instance.
 *
 * @return New MiniAccumuloCluster//  w ww  . java2  s  .c om
 */
private static MiniAccumuloCluster createMiniAccumuloCluster() throws IOException, InterruptedException {
    // Create MAC directory
    File macDir = Files.createTempDirectory("mac-").toFile();
    LOG.info("MAC is enabled, starting MiniAccumuloCluster at %s", macDir);

    // Start MAC and connect to it
    MiniAccumuloCluster accumulo = new MiniAccumuloCluster(macDir, MAC_PASSWORD);
    accumulo.getConfig().setDefaultMemory(512, MEGABYTE);
    setConfigClassPath(accumulo.getConfig());
    accumulo.start();

    // Add shutdown hook to stop MAC and cleanup temporary files
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            LOG.info("Shutting down MAC");
            accumulo.stop();
        } catch (IOException | InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new PrestoException(MINI_ACCUMULO, "Failed to shut down MAC instance", e);
        }

        try {
            LOG.info("Cleaning up MAC directory");
            FileUtils.forceDelete(macDir);
        } catch (IOException e) {
            throw new PrestoException(MINI_ACCUMULO, "Failed to clean up MAC directory", e);
        }
    }));

    return accumulo;
}