List of usage examples for org.apache.commons.io FileUtils isFileOlder
public static boolean isFileOlder(File file, long timeMillis)
File
is older than the specified time reference. From source file:info.fetter.logstashforwarder.util.LastModifiedFileFilter.java
@Override public boolean accept(File file) { long timeMillis = System.currentTimeMillis() - cutoff; if (after) {/* w w w . j a va 2s. c om*/ return FileUtils.isFileNewer(file, timeMillis); } else { return FileUtils.isFileOlder(file, timeMillis); } }
From source file:eu.openanalytics.rsb.si.MinimumAgeFileListFilter.java
@Override protected boolean accept(final File file) { return FileUtils.isFileOlder(file, System.currentTimeMillis() - minimumAge); }
From source file:com.mmounirou.spotirss.spotify.tracks.TrackCache.java
public TrackCache() throws IOException { File cacheDir = new File(FileUtils.getTempDirectory(), "spotify"); cacheDir.mkdirs();//from www .j a v a 2 s . c om File cacheFile = new File(cacheDir, "hrefCache"); if (FileUtils.isFileOlder(cacheFile, DateUtils.addDays(new Date(), -1))) { cacheFile.delete(); System.out.println("Delete cache ..." + cacheFile.getAbsolutePath()); } if (cacheFile.exists()) { System.out.println("load cache ..." + cacheFile.getAbsolutePath()); List<String> strExtendedTracks = Files.readLines(cacheFile, Charsets.UTF_8); for (String strExtendedTrack : strExtendedTracks) { String[] extendedTrackSplitted = strExtendedTrack.split(","); String m_artist = extendedTrackSplitted[0]; String m_song = extendedTrackSplitted[1]; String m_href = extendedTrackSplitted[2]; Track track = new Track(Sets.newHashSet(m_artist.split("&")), m_song); m_cache.put(track, m_href); } } stream = Files.newWriterSupplier(cacheFile, Charsets.UTF_8, true).getOutput(); }
From source file:edu.coeia.reports.IndexUtil.java
public static List<String> getAllFilesBetweenDates(final CaseFacade caseFacade, final Date from, final Date to) throws IOException { List<String> files = new ArrayList<String>(); for (String fileName : getAllFilePaths(caseFacade)) { File file = new File(fileName); if (FileUtils.isFileNewer(file, from) && FileUtils.isFileOlder(file, to)) { files.add(fileName);//from ww w .jav a 2 s . c o m } } return files; }
From source file:com.aionemu.commons.log4j.appenders.TruncateToZipFileAppender.java
/** * This method creates archive with file instead of deleting it. * * @param file file to truncate/* ww w. j a va 2s.c o m*/ */ protected void truncate(File file) { LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started."); // Linux systems doesn't provide file creation time, so we have to hope // that log files // were not modified manually after server starup // We can use here Windowns-only solution but that suck :( if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) { File backupRoot = new File(getBackupDir()); if (!backupRoot.exists() && !backupRoot.mkdirs()) { throw new AppenderInitializationError("Can't create backup dir for backup storage"); } SimpleDateFormat df; try { df = new SimpleDateFormat(getBackupDateFormat()); } catch (Exception e) { throw new AppenderInitializationError( "Invalid date formate for backup files: " + getBackupDateFormat(), e); } String date = df.format(new Date(file.lastModified())); File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip"); ZipOutputStream zos = null; FileInputStream fis = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = new ZipEntry(file.getName()); entry.setMethod(ZipEntry.DEFLATED); entry.setCrc(FileUtils.checksumCRC32(file)); zos.putNextEntry(entry); fis = FileUtils.openInputStream(file); byte[] buffer = new byte[1024]; int readed; while ((readed = fis.read(buffer)) != -1) { zos.write(buffer, 0, readed); } } catch (Exception e) { throw new AppenderInitializationError("Can't create zip file", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { // not critical error LogLog.warn("Can't close zip file", e); } } if (fis != null) { try { // not critical error fis.close(); } catch (IOException e) { LogLog.warn("Can't close zipped file", e); } } } if (!file.delete()) { throw new AppenderInitializationError("Can't delete old log file " + file.getAbsolutePath()); } } }
From source file:com.l2jserver.service.core.logging.TruncateToZipFileAppender.java
/** * This method creates archive with file instead of deleting it. * //from ww w . ja v a2 s . c om * @param file * file to truncate */ protected void truncate(File file) { LogLog.debug("Compression of file: " + file.getAbsolutePath() + " started."); // Linux systems doesn't provide file creation time, so we have to hope // that log files // were not modified manually after server starup // We can use here Windowns-only solution but that suck :( if (FileUtils.isFileOlder(file, ManagementFactory.getRuntimeMXBean().getStartTime())) { File backupRoot = new File(getBackupDir()); if (!backupRoot.exists() && !backupRoot.mkdirs()) { throw new Error("Can't create backup dir for backup storage"); } SimpleDateFormat df; try { df = new SimpleDateFormat(getBackupDateFormat()); } catch (Exception e) { throw new Error("Invalid date formate for backup files: " + getBackupDateFormat(), e); } String date = df.format(new Date(file.lastModified())); File zipFile = new File(backupRoot, file.getName() + "." + date + ".zip"); ZipOutputStream zos = null; FileInputStream fis = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = new ZipEntry(file.getName()); entry.setMethod(ZipEntry.DEFLATED); entry.setCrc(FileUtils.checksumCRC32(file)); zos.putNextEntry(entry); fis = FileUtils.openInputStream(file); byte[] buffer = new byte[1024]; int readed; while ((readed = fis.read(buffer)) != -1) { zos.write(buffer, 0, readed); } } catch (Exception e) { throw new Error("Can't create zip file", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { // not critical error LogLog.warn("Can't close zip file", e); } } if (fis != null) { try { // not critical error fis.close(); } catch (IOException e) { LogLog.warn("Can't close zipped file", e); } } } if (!file.delete()) { throw new Error("Can't delete old log file " + file.getAbsolutePath()); } } }
From source file:com.alibaba.otter.node.etl.common.pipe.impl.http.AbstractHttpPipe.java
public void afterPropertiesSet() throws Exception { Assert.notNull(remoteUrlBuilder);//from w w w .j a v a2 s.c o m Assert.notNull(htdocsDir); NioUtils.create(new File(htdocsDir), false, 3); if (StringUtils.isEmpty(downloadDir)) { downloadDir = htdocsDir; } else { NioUtils.create(new File(downloadDir), false, 3); } // ?? schedulor.scheduleAtFixedRate(new Runnable() { public void run() { try { long threshold = System.currentTimeMillis() - timeout; File htdocsDirFile = new File(htdocsDir); File[] htdocsFiles = htdocsDirFile.listFiles(); Set<File> files = Sets.newHashSet(); for (File htdocsFile : htdocsFiles) { files.add(htdocsFile); } if (downloadDir.equals(htdocsDir) == false) { File downloadDirFile = new File(downloadDir); File[] downloadFiles = downloadDirFile.listFiles(); for (File downloadFile : downloadFiles) { files.add(downloadFile); } } for (File file : files) { boolean isOld = FileUtils.isFileOlder(file, threshold); if (isOld) { NioUtils.delete(file, 3); } } } catch (Exception e) { logger.error("old_file_clean_error", e); } } }, DEFAULT_PERIOD, DEFAULT_PERIOD, TimeUnit.MILLISECONDS); }
From source file:com.ning.arecibo.util.timeline.persistent.Replayer.java
public void purgeOldFiles(final DateTime purgeIfOlderDate) { final Collection<File> files = FileUtils.listFiles(new File(path), new String[] { "bin" }, false); for (final File file : files) { if (FileUtils.isFileOlder(file, new Date(purgeIfOlderDate.getMillis()))) { if (!file.delete()) { log.warn("Unable to delete file: {}", file.getAbsolutePath()); }/*from w w w . ja v a 2 s. c o m*/ } } }
From source file:ch.entwine.weblounge.contentrepository.impl.PreviewGeneratorWorker.java
/** * {@inheritDoc}// w ww .j a va 2s .c o m * * @see java.lang.Runnable#run() */ public void run() { ResourceURI resourceURI = resource.getURI(); String resourceType = resourceURI.getType(); try { // Find the resource serializer ResourceSerializer<?, ?> serializer = contentRepository.getSerializerByType(resourceType); if (serializer == null) { logger.warn("Unable to index resources of type '{}': no resource serializer found", resourceType); return; } // Does the serializer come with a preview generator? PreviewGenerator previewGenerator = serializer.getPreviewGenerator(resource); if (previewGenerator == null) { logger.debug("Resource type '{}' does not support previews", resourceType); return; } // Create the scaled images String mimeType = "image/" + format; ResourceSerializer<?, ?> s = contentRepository.getSerializerByMimeType(mimeType); if (s == null) { logger.warn("No resource serializer is capable of dealing with resources of format '{}'", mimeType); return; } else if (!(s instanceof ImageResourceSerializer)) { logger.warn("Resource serializer lookup for format '{}' returned {}", format, s.getClass()); return; } // Find us an image serializer ImageResourceSerializer irs = (ImageResourceSerializer) s; ImagePreviewGenerator imagePreviewGenerator = (ImagePreviewGenerator) irs.getPreviewGenerator(format); if (imagePreviewGenerator == null) { logger.warn("Image resource serializer {} does not provide support for '{}'", irs, format); return; } // Now scale the original preview according to the existing styles for (Language l : languages) { if (!resource.supportsContentLanguage(l)) continue; // Have we been told to stop doing work in the meantime? if (canceled) return; // Create the original preview image for every language File originalPreview = null; if (!resource.supportsContentLanguage(l)) continue; originalPreview = createPreview(resource, null, l, previewGenerator, format); if (originalPreview == null || !originalPreview.exists() || originalPreview.length() == 0) { logger.warn("Preview generation for {} failed", resource); return; } long resourceLastModified = ResourceUtils.getModificationDate(resource, l).getTime(); // Create the remaining styles for (ImageStyle style : styles) { // Have we been told to stop doing work in the meantime? if (canceled) return; // The original has been produced already if (ImageScalingMode.None.equals(style.getScalingMode())) continue; FileInputStream fis = null; FileOutputStream fos = null; try { File scaledFile = ImageStyleUtils.createScaledFile(resource, l, style); // Create the file if it doesn't exist or if it is out dated. Note // that the last modified date of a file has a precision of seconds if (!scaledFile.isFile() || FileUtils.isFileOlder(scaledFile, new Date(resourceLastModified))) { logger.info("Creating preview at {}", scaledFile.getAbsolutePath()); fis = new FileInputStream(originalPreview); fos = new FileOutputStream(scaledFile); imagePreviewGenerator.createPreview(originalPreview, environment, l, style, format, fis, fos); scaledFile.setLastModified(Math.max(new Date().getTime(), resourceLastModified)); // Store the style definition used while creating the preview File baseDir = ImageStyleUtils.getDirectory(resource.getURI().getSite(), style); File definitionFile = new File(baseDir, "style.xml"); if (!definitionFile.isFile()) { logger.debug("Storing style definition at {}", definitionFile); definitionFile.createNewFile(); FileUtils.copyInputStreamToFile(IOUtils.toInputStream(style.toXml(), "UTF-8"), definitionFile); } } else { logger.debug("Skipping creation of existing '{}' preview of {}", style, resource); } } catch (Throwable t) { logger.error("Error scaling {}: {}", originalPreview, t.getMessage()); continue; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } } } } finally { if (canceled) logger.debug("Preview operation for {} has been canceled", resource.getIdentifier()); contentRepository.previewCreated(resource); } }
From source file:cec.easyshop.storefront.filters.AcceleratorAddOnFilter.java
/** * Copies file @param sourceAddOnFileName to @param targetWebAddOnFileName if it is older. Creates a directory * structure if needed.//from ww w. j a v a 2 s . c o m * * @param sourceAddOnFileName * @param targetWebAddOnFileName * @throws IOException */ protected void copyFileInternalIfNeeded(final String sourceAddOnFileName, final String targetWebAddOnFileName) throws IOException { final File sourceAddOnFile = new File(sourceAddOnFileName); final File targetAddOnFile = new File(targetWebAddOnFileName); if (!sourceAddOnFile.exists()) { LOG.warn("Add-on source file [" + sourceAddOnFileName + "] should exists "); return; } if (!targetAddOnFile.exists()) { try { FileUtils.forceMkdir(targetAddOnFile.getParentFile()); } catch (final IOException e) { LOG.info("Unable to create addon folder for resource " + targetAddOnFile.getParent() + " please rebuild platform for relocating add-ons"); if (LOG.isDebugEnabled()) { LOG.debug(e); } } FileUtils.copyFile(sourceAddOnFile, targetAddOnFile); } else { if (FileUtils.isFileOlder(targetAddOnFile, sourceAddOnFile)) { LOG.info("Copying <<" + sourceAddOnFile.getAbsolutePath() + ">> to <<" + targetAddOnFile.getAbsolutePath() + ">>."); FileUtils.copyFile(sourceAddOnFile, targetAddOnFile); } } }