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:com.orange.mmp.dao.flf.ModuleDaoFlfImpl.java

public void delete(Module module) throws MMPDaoException {
    try {//from w  w w.  j av  a2s. co m
        Module[] moduleList = this.find(module);
        this.lock.lock();
        for (Module currentModule : moduleList) {
            File toDelete = new File(currentModule.getLocation());
            if (!toDelete.delete()) {
                throw new MMPDaoException("failed to delete module " + currentModule.getId());
            }
        }

        FileUtils.touch(new File(this.path));
    } catch (IOException ioe) {
        throw new MMPDaoException("failed to delete module : " + ioe.getMessage());
    } finally {
        this.lock.unlock();
    }
}

From source file:com.orange.mmp.dao.flf.ServiceDaoFlfImpl.java

public void delete(Service service) throws MMPDaoException {
    if (service == null) {
        throw new MMPDaoException("missing or bad data access object");
    }//from w  ww .  j av a2 s  . com

    Service[] services = this.find(service);

    for (Service foundService : services) {
        this.lock.lock();

        if (foundService.getIsDefault()) {
            Service aService = new Service();
            Service[] allServices = this.find(aService);
            if (allServices.length < 2) {
                // If it is the only service, don't remove it
                throw new MMPDaoException("cannot remove last service");
            } else {
                for (int i = 0; i < allServices.length; i++) {
                    if (!allServices[i].getId().equals(foundService.getId())) {
                        // Another service must become the default service
                        allServices[i].setIsDefault(true);
                        this.createOrUdpdate(allServices[i]);
                        break;
                    }
                }
            }
        }

        try {
            File serviceFile = new File(
                    this.path.concat("/").concat(filenameFormat.format(new String[] { foundService.getId() })));
            if (!serviceFile.delete()) {
                throw new MMPDaoException("failed to delete service");
            }

            try {
                FileUtils.touch(new File(this.path));
            } catch (IOException ioe) {
                //Nop
            }
        } finally {
            this.lock.unlock();
        }
    }
}

From source file:de.tor.tribes.types.UserProfile.java

public boolean delete() {
    boolean success = false;
    try {/*from w  w w.j a  va2s  . c o m*/
        FileUtils.deleteDirectory(new File(getProfileDirectory()));
        success = true;
    } catch (IOException ioe) {
        try {
            logger.info("Failed to delete profile. Added 'deleted' marker for next startup");
            FileUtils.touch(new File(getProfileDirectory() + File.separator + "deleted"));
            success = true;
        } catch (IOException ignored) {
        }
    }
    ProfileManager.getSingleton().loadProfiles();
    return success;
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetLoader.java

private void fetch(File aTarget, DataPackage... aPackages) throws IOException {
    // First validate if local copies are still up-to-date
    boolean reload = false;
    packageValidationLoop: for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());
        if (!cachedFile.exists()) {
            continue;
        }/*  ww w. j  a  v  a 2  s .c  o  m*/

        if (pack.getSha1() != null) {
            String actual = getDigest(cachedFile, "SHA1");
            if (!pack.getSha1().equals(actual)) {
                LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + pack.getSha1()
                        + "] - actual [" + actual + "]");
                reload = true;
                break packageValidationLoop;
            } else {
                LOG.info("Local SHA1 hash verified on [" + cachedFile + "] - [" + actual + "]");
            }
        }

        if (pack.getMd5() != null) {
            String actual = getDigest(cachedFile, "MD5");
            if (!pack.getMd5().equals(actual)) {
                LOG.info("Local MD5 hash mismatch on [" + cachedFile + "] - expected [" + pack.getMd5()
                        + "] - actual [" + actual + "]");
                reload = true;
                break packageValidationLoop;
            } else {
                LOG.info("Local MD5 hash verified on [" + cachedFile + "] - [" + actual + "]");
            }
        }

    }

    // If any of the packages are outdated, clear the cache and download again
    if (reload) {
        LOG.info("Clearing local cache for [" + aTarget + "]");
        FileUtils.deleteQuietly(aTarget);
    }

    for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());

        if (cachedFile.exists()) {
            continue;
        }

        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(e);
        }

        MessageDigest sha1;
        try {
            sha1 = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(e);
        }

        cachedFile.getParentFile().mkdirs();
        URL source = new URL(pack.getUrl());

        LOG.info("Fetching [" + cachedFile + "]");

        URLConnection connection = source.openConnection();
        connection.setRequestProperty("User-Agent", "Java");

        try (InputStream is = connection.getInputStream()) {
            DigestInputStream md5Filter = new DigestInputStream(is, md5);
            DigestInputStream sha1Filter = new DigestInputStream(md5Filter, sha1);
            FileUtils.copyInputStreamToFile(sha1Filter, cachedFile);

            if (pack.getMd5() != null) {
                String md5Hex = new String(Hex.encodeHex(md5Filter.getMessageDigest().digest()));
                if (!pack.getMd5().equals(md5Hex)) {
                    String message = "MD5 mismatch. Expected [" + pack.getMd5() + "] but got [" + md5Hex + "].";
                    LOG.error(message);
                    throw new IOException(message);
                }
            }

            if (pack.getSha1() != null) {
                String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest()));
                if (!pack.getSha1().equals(sha1Hex)) {
                    String message = "SHA1 mismatch. Expected [" + pack.getSha1() + "] but got [" + sha1Hex
                            + "].";
                    LOG.error(message);
                    throw new IOException(message);
                }
            }
        }
    }

    // Perform a post-fetch action such as unpacking
    for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());
        File postActionCompleteMarker = new File(cachedFile.getPath() + ".postComplete");
        if (pack.getPostAction() != null && !postActionCompleteMarker.exists()) {
            try {
                pack.getPostAction().run(pack);
                FileUtils.touch(postActionCompleteMarker);
            } catch (IOException e) {
                throw e;
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
    }
}

From source file:com.mirth.connect.connectors.file.filesystems.test.FileConnectionTest.java

@Test
public void testMove() {
    File originalFile = new File(someFolder, "TestFile_" + System.currentTimeMillis() + "_.dat");
    File renamedFile = new File(someFolder, "TestRenamedFile_" + System.currentTimeMillis() + "_.dat");

    // Touch the file
    try {/*from  w ww.ja  v  a  2  s  . c  o m*/
        FileUtils.touch(originalFile);
    } catch (Exception e) {
        fail("We could not make the file using JavaIO");
    }

    try {
        fc.move(originalFile.getName(), someFolder.getAbsolutePath(), renamedFile.getName(),
                someFolder.getAbsolutePath());
    } catch (FileConnectorException e) {
        fail("FileConnection.move threw a FileConnectorException");
    }

    // The original file should be gone
    assertFalse(originalFile.exists());

    // the new file should exist
    assertTrue(renamedFile.exists());
}

From source file:com.thoughtworks.go.domain.materials.mercurial.HgCommandTest.java

private void addLockTo(File hgRepoRootDir) throws IOException {
    File lock = new File(hgRepoRootDir, ".hg/store/lock");
    FileUtils.touch(lock);
}

From source file:gov.redhawk.sca.efs.tests.ScaFileStoreTest.java

/**
 * Test method for {@link gov.redhawk.efs.sca.internal.ScaFileStore#delete(int, org.eclipse.core.runtime.IProgressMonitor)}.
 * @throws Exception /*  ww w . ja  va2 s  .  co  m*/
 */
@Test
public void testDeleteIntIProgressMonitor() throws Exception {
    this.deleteFile = new File(ScaFileStoreTest.session.getRootFile(), "test");
    FileUtils.touch(this.deleteFile);
    final IFileStore child = this.rootFileStore.getChild("test");
    child.delete(0, null);
    Assert.assertTrue(!this.deleteFile.exists());
}

From source file:edu.ur.file.db.FileSystemManager.java

/**
 * Add a file to the parent folder.  This creates an empty
 * file with the specified name.  This is good for creating 
 * an empty file that has a size of 0./*from   ww  w.j  av a  2  s  .  c om*/
 *
 * @param parent folder to add the file to
 * @param uniqueName name in the file system to save the file to.
 * 
 * @return the information of the created file.
 * @throws IllegalFileSystemNameException 
 */
static DefaultFileInfo addFile(TreeFolderInfo parent, String uniqueName) throws IllegalFileSystemNameException {
    DefaultFileInfo fileInfo = null;

    File newFile = new File(parent.getFullPath() + uniqueName);

    try {
        fileInfo = new DefaultFileInfo();
        fileInfo.setFolderInfo(parent);
        fileInfo.setName(uniqueName);
        fileInfo.setModifiedDate(new Timestamp(new Date().getTime()));
        fileInfo.setCreatedDate(new Timestamp(new Date().getTime()));
        FileUtils.touch(newFile);
        fileInfo.setExists(true);
        fileInfo.setSize(newFile.length());

    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    return fileInfo;
}

From source file:com.norconex.jef4.status.FileJobStatusStore.java

@Override
public long touch(String suiteName, String jobId) throws IOException {
    File file = getStatusFile(suiteName, jobId);
    FileUtils.touch(file);
    return file.lastModified();
}

From source file:com.netflix.genie.core.jobs.workflow.impl.JobKickoffTask.java

private boolean canExecute(final String runScriptFile) {
    try {//from w ww . j  a  va  2  s.co  m
        return retryTemplate.execute(c -> {
            FileUtils.touch(new File(runScriptFile));
            return true;
        });
    } catch (Exception e) {
        registry.counter("genie.jobs.tasks.jobKickoffTask.run.failure").increment();
        log.warn("Failed touching the run script file", e);
    }
    return false;
}