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

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

Introduction

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

Prototype

public static boolean isFileNewer(File file, long timeMillis) 

Source Link

Document

Tests if the specified File is newer than the specified time reference.

Usage

From source file:org.opennms.netmgt.provision.persist.FusedForeignSourceRepository.java

/**
 * <p>save</p>/*w  w  w.j a  va2 s  .co  m*/
 *
 * @param requisition a {@link org.opennms.netmgt.provision.persist.requisition.Requisition} object.
 * @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.
 */
public synchronized void save(final Requisition requisition) throws ForeignSourceRepositoryException {
    final String foreignSource = requisition.getForeignSource();

    final URL pendingUrl = m_pendingForeignSourceRepository.getRequisitionURL(foreignSource);
    final File pendingFile = pendingUrl == null ? null : new File(pendingUrl.getFile());

    /*
    final URL deployedUrl = m_deployedForeignSourceRepository.getRequisitionURL(foreignSource);
    final File deployedFile = deployedUrl == null? null : new File(deployedUrl.getFile());
    */

    final List<File> pendingSnapshots = RequisitionFileUtils.findSnapshots(m_pendingForeignSourceRepository,
            foreignSource);

    /* determine whether to delete the pending requisition */
    boolean deletePendingRequisition = true;
    if (pendingSnapshots.size() > 0) {
        for (final File snap : pendingSnapshots) {
            if (FileUtils.isFileNewer(pendingFile, snap)) {
                // the pending file is newer than an in-process snapshot, don't delete it
                deletePendingRequisition = false;
                break;
            } else if (snap.lastModified() == pendingFile.lastModified()
                    && snap.length() != pendingFile.length()) {
                // if the dates are the same, but they're different lengths, err on the side of caution and leave the pending file
                deletePendingRequisition = false;
                break;
            }
        }
    }
    if (deletePendingRequisition) {
        m_pendingForeignSourceRepository.delete(requisition);
    }

    /* determine whether this requisition was imported from a snapshot, and if so, delete its snapshot file */
    RequisitionFileUtils.deleteResourceIfSnapshot(requisition);

    m_deployedForeignSourceRepository.save(requisition);
}

From source file:org.sakuli.starter.helper.SahiProxy.java

private boolean isNewer(Path source, Path target) {
    return !Files.exists(target) || FileUtils.isFileNewer(source.toFile(), target.toFile());
}

From source file:org.scriptbox.util.common.io.Tailer.java

/**
 * Follows changes in the file, calling the TailerListener's handle method for each new line.
 *///from  w w w  .j  a  v  a2s .  c  o  m
public void run() {
    boolean notFoundSent = false;
    boolean rotatedSent = false;

    RandomAccessFile reader = null;
    try {
        long last = 0; // The last time the file was checked for changes
        long position = 0; // position within the file
        // Open the file
        while (run && reader == null) {
            try {
                reader = new RandomAccessFile(file, RAF_MODE);
                notFoundSent = false;
            } catch (FileNotFoundException e) {
                if (!notFoundSent) {
                    listener.fileNotFound();
                    notFoundSent = true;
                }
            }

            if (reader == null) {
                pause();
            } else {
                // The current position in the file
                position = end ? file.length() : 0;
                last = System.currentTimeMillis();
                reader.seek(position);
            }
        }

        while (run) {

            boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first

            // Check the file length to see if it was rotated
            long length = file.length();

            if (length < position) {

                // File was rotated
                if (!rotatedSent) {
                    listener.fileRotated();
                    rotatedSent = true;
                }

                // Reopen the reader after rotation
                try {
                    // Ensure that the old file is closed iff we re-open it successfully
                    RandomAccessFile save = reader;
                    reader = new RandomAccessFile(file, RAF_MODE);
                    position = 0;
                    // close old file explicitly rather than relying on GC picking up previous RAF
                    IOUtils.closeQuietly(save);
                    notFoundSent = false;
                } catch (FileNotFoundException e) {
                    // in this case we continue to use the previous reader and position values
                    if (!notFoundSent) {
                        listener.fileNotFound();
                        notFoundSent = true;
                    }
                    pause();
                }
                continue;
            } else {
                rotatedSent = false;
                // File was not rotated

                // See if the file needs to be read again
                if (length > position) {

                    // The file has more content than it did last time
                    position = readLines(reader);
                    last = System.currentTimeMillis();

                } else if (newer) {
                    if (listener.newer(file)) {
                        /*
                         * This can happen if the file is truncated or overwritten with the exact same length of
                         * information. In cases like this, the file position needs to be reset
                         */
                        position = 0;
                        reader.seek(position); // cannot be null here

                        // Now we can read new lines
                        position = readLines(reader);
                    }
                    last = System.currentTimeMillis();
                }
            }
            if (reOpen) {
                IOUtils.closeQuietly(reader);
            }
            pause();
            if (run && reOpen) {
                reader = new RandomAccessFile(file, RAF_MODE);
                reader.seek(position);
            }
        }

    } catch (Exception e) {

        listener.handle(e);

    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * @see FileUtils//from w ww  . jav  a  2s .c  o  m
 */
protected boolean isFileNewer(final FileBasePath basePath, final File file, final File reference) {
    verify(basePath, file);
    verify(basePath, reference);
    return FileUtils.isFileNewer(getExistingFile(basePath, file), getExistingFile(basePath, reference));
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * @see FileUtils//from  w  ww  .  j  a  va  2 s.  c om
 */
protected boolean isFileNewer(final FileBasePath basePath, final File file, final Date date) {
    verify(basePath, file);
    return FileUtils.isFileNewer(getExistingFile(basePath, file), date);
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * @see FileUtils/*from  w w  w. j ava2s  .  co m*/
 */
protected boolean isFileNewer(final FileBasePath basePath, final File file, final long timeMillis) {
    verify(basePath, file);
    return FileUtils.isFileNewer(getExistingFile(basePath, file), timeMillis);
}

From source file:org.sonar.plugins.cutoff.CutoffFilter.java

public boolean accept(File file) {
    return cutoffDate == null || FileUtils.isFileNewer(file, cutoffDate);
}

From source file:org.sonar.plugins.pitest.ReportFinder.java

public File findReport(File reportDirectory) {
    if (reportDirectory == null || !reportDirectory.exists() || !reportDirectory.isDirectory()) {
        return null;
    }/*from   w  w  w .j  ava2s  .  c  o m*/
    Collection<File> reports = FileUtils.listFiles(reportDirectory, new String[] { "xml" }, true);
    File latestReport = null;
    for (File report : reports) {
        if (latestReport == null || FileUtils.isFileNewer(report, latestReport)) {
            latestReport = report;
        }
    }
    return latestReport;
}

From source file:org.sonatype.flexmojos.compiler.AbstractCompilerMojo.java

@SuppressWarnings("unchecked")
private boolean isCompilationRequired() throws MojoExecutionException {
    if (!quick) {
        // not running at quick mode
        return true;
    }/* ww  w.ja v a 2s. c  o  m*/

    Artifact artifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(),
            project.getVersion(), null, project.getPackaging());
    try {
        resolver.resolve(artifact, remoteRepositories, localRepository);
    } catch (AbstractArtifactResolutionException e) {
        // Not available at repository
        return true;
    }

    File artifactFile = artifact.getFile();
    if (artifactFile == null || !artifactFile.exists()) {
        // Recompile, file doesn't exists
        getLog().warn("Can't find any older instaled version.");
        return true;
    }
    try {
        FileUtils.copyFile(artifactFile, getOutput());
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to copy instaled version to target folder.", e);
    }
    long lastCompiledArtifact = artifactFile.lastModified();

    Set<Artifact> dependencies = getDependencyArtifacts();
    for (Artifact dependency : dependencies) {
        if (FileUtils.isFileNewer(dependency.getFile(), lastCompiledArtifact)) {
            // a dependency is newer, recompile
            getLog().warn("Found a updated dependency: " + dependency);
            return true;
        }
    }

    List<File> paths = new ArrayList<File>(Arrays.asList(sourcePaths));
    paths.remove(new File(resourceBundlePath));

    addLocalesPath(paths, compiledLocales);
    addLocalesPath(paths, runtimeLocales);

    for (File sourcePath : paths) {
        Collection<File> files = FileUtils.listFiles(sourcePath, new AgeFileFilter(lastCompiledArtifact, false),
                TrueFileFilter.INSTANCE);

        // If has any newer file
        if (files.size() > 0) {
            getLog().warn("Found some updated files.");
            return true;
        }
    }

    // nothing new was found.
    return false;
}

From source file:org.toobsframework.mojo.XsdRelease.java

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
    int count = 0;
    int of = 0;//from  w  w  w  .jav  a  2  s.  c  om

    try {
        if (verbose) {
            getLog().info("release-xsd will attempt copy versions of xsds from directory " + source.getPath()
                    + " to directory " + target.getPath() + " with version " + version);
        }

        if (!source.exists()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + source.getPath() + " since the directory does not exists");
        }
        if (!source.isDirectory()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + source.getPath() + " since it is not a directory");
        }

        if (verbose) {
            getLog().info(source.getPath() + " directory was located");
        }

        if (!target.exists()) {
            if (!target.mkdirs()) {
                throw new MojoExecutionException("Cannot create the targetdirectory " + target.getPath());
            }
            if (verbose) {
                getLog().info(target.getPath() + " directory was created");
            }
        } else if (!target.isDirectory()) {
            throw new MojoExecutionException(
                    "Cannot copy xsds from " + target.getPath() + " since it is not a directory");
        } else {
            if (verbose) {
                getLog().info(target.getPath() + " directory was located");
            }
        }

        File[] sourceFiles = source.listFiles((FileFilter) new SuffixFileFilter(".xsd"));
        for (File inputFile : sourceFiles) {
            of++;

            String fileName = replace(inputFile.getName(), ".xsd", "-" + version + ".xsd");
            File outputFile = new File(target, fileName);

            if (outputFile.exists() && FileUtils.isFileNewer(outputFile, inputFile)) {
                if (verbose) {
                    getLog().info("File " + inputFile.getPath() + " is newer than " + outputFile.getPath()
                            + ".  Skipped.");
                }
                continue;
            }

            if (verbose) {
                getLog().info("copying file " + inputFile.getPath() + " to " + outputFile.getPath());
            }

            InputStream inputStream = new FileInputStream(inputFile);
            List<String> inputLines;
            try {
                inputLines = IOUtils.readLines(inputStream);
            } finally {
                inputStream.close();
            }

            List<String> outputLines = new ArrayList<String>(inputLines.size());
            for (String inputLine : inputLines) {
                String line = fixupLine(inputLine);
                outputLines.add(line);
                /*getLog().info(">>" + line);*/
            }

            OutputStream outputStream = new FileOutputStream(outputFile, false);
            try {
                IOUtils.writeLines(outputLines, "\n", outputStream);
            } finally {
                outputStream.close();
            }
            count++;
        }

        if (count == 0) {
            getLog().info("No xsds released - " + of + " are up to date");
        } else {
            getLog().info(count + " xsd(s) out of " + of + " were sucessfully updated");
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error occurred when trying to release xsds: " + e.getMessage(), e);
    }
}