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

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

Introduction

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

Prototype

public static void touch(File file) throws IOException 

Source Link

Document

Implements the same behaviour as the "touch" utility on Unix.

Usage

From source file:org.exoplatform.services.jcr.ext.artifact.ArtifactManagingServiceImpl.java

public void importArtifacts(SessionProvider sp, InputStream in)
        throws RepositoryException, FileNotFoundException {
    LOG.info("Extract repository to temporary folder");
    String path = System.getProperty("java.io.tmpdir") + File.separator + "maven2";
    File temporaryFolder = new File(getUniqueFilename(path));
    if (!temporaryFolder.mkdir()) {
        throw new FileNotFoundException("Cannot create temporary folder");
    }/* www  . j  a  v a 2s. c  o  m*/
    ZipEntry entry;
    ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(in));
    try {
        while ((entry = zipIn.getNextEntry()) != null) {

            if (!(entry.isDirectory()
                    && new File(temporaryFolder + File.separator + entry.getName()).mkdir())) {
                int count;
                byte data[] = new byte[BUFFER];
                File file = new File(temporaryFolder + File.separator + entry.getName());

                FileUtils.touch(file);

                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zipIn.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }

        }
    } catch (IOException e) {
        LOG.error("Cannot get zip entry from stream", e);
    } finally {
        IOUtils.closeQuietly(zipIn);
        IOUtils.closeQuietly(in);
    }

    // main part - copy to JCR from temp folder
    // use uploading artifacts from local folder.

    importArtifacts(sp, temporaryFolder);
    try {
        FileUtils.deleteDirectory(temporaryFolder);
    } catch (IOException e) {
        LOG.error("Cannot remove temporary folder", e);
    }

}

From source file:org.fuin.vfs2.filter.AgeFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {
    testDir = getTestDir(AgeFileFilterTest.class.getName());

    // Set the file's time stamp two days back
    oldFile = new File(testDir, "old.txt");
    FileUtils.touch(oldFile);
    oldFile.setLastModified(TWO_DAYS_AGO);
    oldFileInfo = createFSI(oldFile);//from w  ww.  j  a va  2s  .  c om

    // Reference file
    currentFile = new File(testDir, "current.txt");
    FileUtils.touch(currentFile);
    currentFile.setLastModified(NOW);
    currentFileInfo = createFSI(currentFile);

    // Set the file's time stamp two days into the future
    newFile = new File(testDir, "new.txt");
    FileUtils.touch(newFile);
    newFile.setLastModified(TWO_DAYS_LATER);
    newFileInfo = createFSI(newFile);

    // Zip the test directory
    zipFile = new File(getTempDir(), AgeFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.fuin.vfs2.filter.CanReadFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {

    testDir = getTestDir(CanReadFileFilterTest.class.getName());

    writeableFile = new File(testDir, WRITEABLE);
    writeableFileInfo = createFSI(writeableFile);
    FileUtils.touch(writeableFile);

    readOnlyFile = new File(testDir, READONLY);
    readOnlyFileInfo = createFSI(readOnlyFile);
    FileUtils.touch(readOnlyFile);// ww  w. j ava2s  .c  o  m
    readOnlyFile.setReadable(true);
    readOnlyFile.setWritable(false);

    notExistingFile = new File(testDir, "not-existing-file.txt");
    notExistingFileInfo = createFSI(notExistingFile);

    zipFile = new File(getTempDir(), CanReadFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.fuin.vfs2.filter.CanWriteFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {

    testDir = getTestDir(CanWriteFileFilterTest.class.getName());

    writeableFile = new File(testDir, WRITEABLE);
    writeableFileInfo = createFSI(writeableFile);
    FileUtils.touch(writeableFile);

    readOnlyFile = new File(testDir, READONLY);
    readOnlyFileInfo = createFSI(readOnlyFile);
    FileUtils.touch(readOnlyFile);/*from w ww  .ja  v  a 2s.c o m*/
    readOnlyFile.setReadable(true);
    readOnlyFile.setWritable(false);

    notExistingFile = new File(testDir, "not-existing-file.txt");
    notExistingFileInfo = createFSI(notExistingFile);

    zipFile = new File(getTempDir(), CanWriteFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.fuin.vfs2.filter.DirectoryAndFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {

    testDir = getTestDir(DirectoryAndFileFilterTest.class.getName());
    testDir.mkdir();/*ww w  . j  a  v a2 s  . co m*/

    dir = new File(testDir, DIR);
    dir.mkdir();
    dirInfo = createFSI(dir);

    file = new File(dir, FILE);
    FileUtils.touch(file);
    fileInfo = createFSI(file);

    notExistingFile = new File(testDir, "not-existing-file.txt");
    notExistingFileInfo = createFSI(notExistingFile);

    zipFile = new File(getTempDir(), DirectoryAndFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.fuin.vfs2.filter.EmptyFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {
    testDir = getTestDir(EmptyFileFilterTest.class.getName());
    testDir.mkdir();//  w ww. j  a v  a 2s.  c  om

    notEmptyFile = new File(testDir, "full.txt");
    FileUtils.write(notEmptyFile, "whatever");
    notEmptyFileInfo = createFSI(notEmptyFile);

    emptyFile = new File(testDir, "empty.txt");
    FileUtils.touch(emptyFile);
    emptyFileInfo = createFSI(emptyFile);

    notEmptyDir = new File(testDir, "full-dir");
    notEmptyDir.mkdir();
    notEmptyDirInfo = createFSI(notEmptyDir);
    FileUtils.touch(new File(notEmptyDir, "foobar.txt"));

    emptyDir = new File(testDir, "empty-dir");
    emptyDir.mkdir();
    emptyDirInfo = createFSI(emptyDir);

    notExistingFile = new File(testDir, "not-existing-file.txt");
    notExistingFileInfo = createFSI(notExistingFile);

    // Zip the test directory
    zipFile = new File(getTempDir(), EmptyFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.fuin.vfs2.filter.HiddenFileFilterTest.java

@BeforeClass
public static void beforeClass() throws IOException {
    testDir = getTestDir(HiddenFileFilterTest.class.getName());
    testDir.mkdir();/*from w  w w . j a va 2 s.  c  o  m*/

    visibleFile = new File(testDir, "visible.txt");
    FileUtils.touch(visibleFile);
    visibleFileInfo = createFSI(visibleFile);

    hiddenFile = new File(testDir, "hidden.txt");
    // TODO xxx In Java 6 there is no way to hide a file
    // hiddenFile.setVisible(false);
    hiddenFileInfo = createFSI(hiddenFile);

    notExistingFile = new File(testDir, "not-existing-file.txt");
    notExistingFileInfo = createFSI(notExistingFile);

    // Zip the test directory
    zipFile = new File(getTempDir(), HiddenFileFilterTest.class.getName() + ".zip");
    Utils4J.zipDir(testDir, "", zipFile);
    zipFileObj = getZipFileObject(zipFile);

}

From source file:org.geoserver.backuprestore.Backup.java

/**
 * @return//from   w w w .j a v a 2  s.  c  o  m
 * @throws IOException
 * 
 */
public BackupExecutionAdapter runBackupAsync(final Resource archiveFile, final boolean overwrite,
        final Filter filter, final Hints params) throws IOException {
    // Check if archiveFile exists
    if (archiveFile.file().exists()) {
        if (!overwrite && FileUtils.sizeOf(archiveFile.file()) > 0) {
            // Unless the user explicitly wants to overwrite the archiveFile, throw an exception whenever it already exists
            throw new IOException(
                    "The target archive file already exists. Use 'overwrite=TRUE' if you want to overwrite it.");
        } else {
            FileUtils.forceDelete(archiveFile.file());
        }
    } else {
        // Make sure the parent path exists
        if (!archiveFile.file().getParentFile().exists()) {
            try {
                archiveFile.file().getParentFile().mkdirs();
            } finally {
                if (!archiveFile.file().getParentFile().exists()) {
                    throw new IOException("The path to target archive file is unreachable.");
                }
            }
        }
    }

    // Initialize ZIP
    FileUtils.touch(archiveFile.file());

    // Write flat files into a temporary folder
    Resource tmpDir = BackupUtils.geoServerTmpDir(getGeoServerDataDirectory());

    // Fill Job Parameters
    JobParametersBuilder paramsBuilder = new JobParametersBuilder();

    if (filter != null) {
        paramsBuilder.addString("filter", ECQL.toCQL(filter));
    }

    paramsBuilder.addString(PARAM_JOB_NAME, BACKUP_JOB_NAME)
            .addString(PARAM_OUTPUT_FILE_PATH, BackupUtils.getArchiveURLProtocol(tmpDir) + tmpDir.path())
            .addLong(PARAM_TIME, System.currentTimeMillis());

    parseParams(params, paramsBuilder);

    JobParameters jobParameters = paramsBuilder.toJobParameters();

    // Send Execution Signal
    BackupExecutionAdapter backupExecution;
    try {
        if (getRestoreRunningExecutions().isEmpty() && getBackupRunningExecutions().isEmpty()) {
            synchronized (jobOperator) {
                // Start a new Job
                JobExecution jobExecution = jobLauncher.run(backupJob, jobParameters);
                backupExecution = new BackupExecutionAdapter(jobExecution, totalNumberOfBackupSteps);
                backupExecutions.put(backupExecution.getId(), backupExecution);

                backupExecution.setArchiveFile(archiveFile);
                backupExecution.setOverwrite(overwrite);
                backupExecution.setFilter(filter);

                backupExecution.getOptions().add("OVERWRITE=" + overwrite);
                for (Entry jobParam : jobParameters.toProperties().entrySet()) {
                    if (!PARAM_OUTPUT_FILE_PATH.equals(jobParam.getKey())
                            && !PARAM_INPUT_FILE_PATH.equals(jobParam.getKey())
                            && !PARAM_TIME.equals(jobParam.getKey())) {
                        backupExecution.getOptions().add(jobParam.getKey() + "=" + jobParam.getValue());
                    }
                }

                return backupExecution;
            }
        } else {
            throw new IOException(
                    "Could not start a new Backup Job Execution since there are currently Running jobs.");
        }
    } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
            | JobParametersInvalidException e) {
        throw new IOException("Could not start a new Backup Job Execution: ", e);
    } finally {
    }
}

From source file:org.geoserver.importer.DataFormatTest.java

public void testLookupKML() throws Exception {
    File kmlFile = new File(tmpDir(), "foo.kml");
    FileUtils.touch(kmlFile);

    DataFormat format = DataFormat.lookup(kmlFile);
    assertNotNull("No format found for kml files", format);
    String name = format.getName();
    assertEquals("KML format not found", "KML", name);
}

From source file:org.geoserver.importer.DirectoryTest.java

public void testMultipleSpatialASpatialFile() throws Exception {
    File dir = unpack("shape/archsites_epsg_prj.zip");
    unpack("shape/bugsites_esri_prj.tar.gz", dir);
    FileUtils.touch(new File(dir, "foo.txt")); //TODO: don't rely on alphabetical order 

    Directory d = new Directory(dir);
    d.prepare();/*from   w  w  w. j a v  a  2s  .co m*/

    assertEquals(3, d.getFiles().size());
    assertTrue(d.getFiles().get(0) instanceof SpatialFile);
    assertTrue(d.getFiles().get(1) instanceof SpatialFile);
    assertTrue(d.getFiles().get(2) instanceof ASpatialFile);
}