List of usage examples for java.nio.file.attribute FileTime toMillis
public long toMillis()
From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java
/** * Invoked for a file in a directory./*from ww w .j av a2 s . co m*/ * Unless overridden, this method returns {@link java.nio.file.FileVisitResult#CONTINUE * CONTINUE}. * * @param aPath Path instance. * @param aFileAttributes File attribute instance. */ @Override public FileVisitResult visitFile(Path aPath, BasicFileAttributes aFileAttributes) throws IOException { Logger appLogger = mAppMgr.getLogger(this, "visitFile"); String pathFileName = aPath.toAbsolutePath().toString(); if (mCrawlIgnore.isMatchedNormalized(pathFileName)) appLogger.debug(String.format("Ignoring File: %s", pathFileName)); else { File fsFile = aPath.toFile(); if ((fsFile.canRead()) && (mBag != null)) { String crawlType = mCrawlQueue.getCrawlType(); if (StringUtils.equals(crawlType, Connector.CRAWL_TYPE_INCREMENTAL)) { String docId = generateDocumentId(aPath); boolean docExistsInIndex = documentExistsInIndex(docId); if (docExistsInIndex) { Date incDate = mCrawlQueue.getCrawlLastModified(); FileTime lastModifiedTime = aFileAttributes.lastModifiedTime(); Date lmDate = new Date(lastModifiedTime.toMillis()); if (lmDate.after(incDate)) processFile(aPath, aFileAttributes); } else processFile(aPath, aFileAttributes); } else processFile(aPath, aFileAttributes); } else appLogger.warn(String.format("Access Failed: %s", pathFileName)); } if (mAppMgr.isAlive()) return FileVisitResult.CONTINUE; else return FileVisitResult.TERMINATE; }
From source file:com.nridge.connector.fs.con_fs.core.FileCrawler.java
private void processFile(Path aPath, BasicFileAttributes aFileAttributes) throws IOException { Logger appLogger = mAppMgr.getLogger(this, "processFile"); appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER); StopWatch stopWatch = new StopWatch(); stopWatch.start();//from www .j a va2 s.co m File fsFile = aPath.toFile(); String docId = generateDocumentId(aPath); String pathFileName = aPath.toAbsolutePath().toString(); appLogger.debug(String.format("Processing File (%s): %s", docId, pathFileName)); boolean isFileFlat = true; Document fsDocument = new Document(Constants.FS_DOCUMENT_TYPE, mBag); DataBag fileBag = fsDocument.getBag(); fileBag.resetValuesWithDefaults(); fileBag.setValueByName("nsd_id", docId); String fileName = fsFile.getName(); fileBag.setValueByName("nsd_url", fsFile.toURI().toURL().toString()); String viewURL = createViewURL(docId); fileBag.setValueByName("nsd_url_view", viewURL); fileBag.setValueByName("nsd_url_display", viewURL); fileBag.setValueByName("nsd_name", fileName); fileBag.setValueByName("nsd_file_name", fileName); fileBag.setValueByName("nsd_file_size", aFileAttributes.size()); FileTime creationTime = aFileAttributes.creationTime(); Date cDate = new Date(creationTime.toMillis()); fileBag.setValueByName("nsd_doc_created_ts", cDate); FileTime lastModifiedTime = aFileAttributes.lastModifiedTime(); Date lmDate = new Date(lastModifiedTime.toMillis()); fileBag.setValueByName("nsd_doc_modified_ts", lmDate); fileBag.setValueByName("nsd_crawl_type", mCrawlQueue.getCrawlType()); DataField dataField = fileBag.getFirstFieldByFeatureName(Field.FEATURE_IS_CONTENT); if (dataField != null) { ContentExtractor contentExtractor = new ContentExtractor(mAppMgr); contentExtractor.setCfgPropertyPrefix(Constants.CFG_PROPERTY_PREFIX + ".extract"); try { String mimeType = contentExtractor.detectType(fsFile); if (StringUtils.isNotEmpty(mimeType)) fileBag.setValueByName("nsd_mime_type", mimeType); if (isExpandableCSVFile(mimeType)) { isFileFlat = false; processCSVFile(aPath, aFileAttributes, viewURL); } else contentExtractor.process(pathFileName, dataField); } catch (NSException e) { String msgStr = String.format("%s: %s", pathFileName, e.getMessage()); appLogger.error(msgStr); } } if (isFileFlat) { fileBag.setValueByName("nsd_doc_hash", fsDocument.generateUniqueHash(false)); saveAddQueueDocument(fsDocument, stopWatch); } appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART); }
From source file:com.liferay.sync.engine.lan.server.file.LanFileServerHandler.java
protected void sendFile(final ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest, SyncFile syncFile) throws Exception { Path path = Paths.get(syncFile.getFilePathName()); if (Files.notExists(path)) { _syncTrafficShapingHandler.decrementConnectionsCount(); if (_logger.isTraceEnabled()) { Channel channel = channelHandlerContext.channel(); _logger.trace("Client {}: file not found {}", channel.remoteAddress(), path); }//from w ww . ja v a 2 s . c om _sendError(channelHandlerContext, NOT_FOUND); return; } if (_logger.isDebugEnabled()) { Channel channel = channelHandlerContext.channel(); _logger.debug("Client {}: sending file {}", channel.remoteAddress(), path); } long modifiedTime = syncFile.getModifiedTime(); long previousModifiedTime = syncFile.getPreviousModifiedTime(); if (OSDetector.isApple()) { modifiedTime = modifiedTime / 1000 * 1000; previousModifiedTime = previousModifiedTime / 1000 * 1000; } FileTime currentFileTime = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS); long currentTime = currentFileTime.toMillis(); if ((currentTime != modifiedTime) && (currentTime != previousModifiedTime)) { _syncTrafficShapingHandler.decrementConnectionsCount(); Channel channel = channelHandlerContext.channel(); _logger.error( "Client {}: file modified {}, currentTime {}, modifiedTime " + "{}, previousModifiedTime {}", channel.remoteAddress(), path, currentTime, modifiedTime, previousModifiedTime); _sendError(channelHandlerContext, NOT_FOUND); return; } HttpResponse httpResponse = new DefaultHttpResponse(HTTP_1_1, OK); long size = Files.size(path); HttpUtil.setContentLength(httpResponse, size); HttpHeaders httpHeaders = httpResponse.headers(); MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap(); httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, mimetypesFileTypeMap.getContentType(syncFile.getName())); if (HttpUtil.isKeepAlive(fullHttpRequest)) { httpHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } channelHandlerContext.write(httpResponse); SyncChunkedFile syncChunkedFile = new SyncChunkedFile(path, size, 4 * 1024 * 1024, currentTime); ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(new HttpChunkedInput(syncChunkedFile), channelHandlerContext.newProgressivePromise()); channelFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { _syncTrafficShapingHandler.decrementConnectionsCount(); if (channelFuture.isSuccess()) { return; } Throwable exception = channelFuture.cause(); Channel channel = channelHandlerContext.channel(); _logger.error("Client {}: {}", channel.remoteAddress(), exception.getMessage(), exception); channelHandlerContext.close(); } }); if (!HttpUtil.isKeepAlive(fullHttpRequest)) { channelFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.github.wellcomer.query3.core.Autocomplete.java
/** ?. ?, TreeSet ? ? ?. ? TreeSet . /*from www .jav a2 s . c om*/ @param queryList ?? ?. @param scanModifiedOnly ? ?. @param mergePrevious ?? ? ?? . */ public void autolearn(QueryList queryList, boolean scanModifiedOnly, boolean mergePrevious) throws IOException { FileTime timestamp; long modifiedSince = 0; Path timestampFilePath = Paths.get(filePath, ".timestamp"); if (scanModifiedOnly) { // try { // ? ? ? timestamp = Files.getLastModifiedTime(timestampFilePath); modifiedSince = timestamp.toMillis(); } catch (IOException e) { // ? ? Files.createFile(timestampFilePath); } } HashMap<String, TreeSet<String>> fields = new HashMap<>(); // - ? ?, - ? Iterator<Query> queryIterator = queryList.iterator(modifiedSince); // ? ?? ? ? ? String k, v; while (queryIterator.hasNext()) { Query query = queryIterator.next(); for (Map.Entry<String, String> entry : query.entrySet()) { k = entry.getKey().toLowerCase(); v = entry.getValue().trim(); if (v.length() < 2) continue; if (!fields.containsKey(k)) { TreeSet<String> treeSet = new TreeSet<>(); try { if (mergePrevious) { // ? ? List<String> lines = Files.readAllLines(Paths.get(filePath, k), charset); treeSet.addAll(lines); } } catch (IOException e) { e.printStackTrace(); } fields.put(k, treeSet); } TreeSet<String> treeSet = fields.get(k); treeSet.add(v); } } for (Map.Entry<String, TreeSet<String>> entry : fields.entrySet()) { k = entry.getKey(); ArrayList<String> lines = new ArrayList<>(fields.get(k)); FileWriter fileWriter = new FileWriter(Paths.get(filePath, k).toString()); fileWriter.write(StringUtils.join(lines, System.getProperty("line.separator"))); fileWriter.flush(); fileWriter.close(); } try { Files.setLastModifiedTime(timestampFilePath, FileTime.fromMillis(System.currentTimeMillis())); } catch (IOException e) { if (e.getClass().getSimpleName().equals("NoSuchFileException")) Files.createFile(timestampFilePath); e.printStackTrace(); } }
From source file:de.tiqsolutions.hdfs.BasicFileAttributeViewImpl.java
@Override public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException { if (lastModifiedTime == null || lastAccessTime == null) { BasicFileAttributes attributes = readAttributes(); if (lastModifiedTime == null) lastModifiedTime = attributes.lastModifiedTime(); if (lastAccessTime == null) lastAccessTime = attributes.lastAccessTime(); }/*from ww w . j a va 2 s . c o m*/ ((HadoopFileSystem) path.getFileSystem()).getFileContext().setTimes(((HadoopFileSystemPath) path).getPath(), lastModifiedTime.toMillis(), lastAccessTime.toMillis()); }
From source file:de.blizzy.backup.backup.BackupRun.java
private int backupFolder(IFolder folder, int parentFolderId, String overrideName) throws IOException { currentFileOrFolder.add(folder);//from www. j av a 2 s .c o m try { FileTime creationTime = folder.getCreationTime(); FileTime lastModificationTime = folder.getLastModificationTime(); database.factory().insertInto(Tables.ENTRIES) .set(Tables.ENTRIES.PARENT_ID, (parentFolderId > 0) ? Integer.valueOf(parentFolderId) : null) .set(Tables.ENTRIES.BACKUP_ID, Integer.valueOf(backupId)) .set(Tables.ENTRIES.TYPE, Byte.valueOf((byte) EntryType.FOLDER.getValue())) .set(Tables.ENTRIES.CREATION_TIME, (creationTime != null) ? new Timestamp(creationTime.toMillis()) : null) .set(Tables.ENTRIES.MODIFICATION_TIME, (lastModificationTime != null) ? new Timestamp(lastModificationTime.toMillis()) : null) .set(Tables.ENTRIES.HIDDEN, Boolean.valueOf(folder.isHidden())) .set(Tables.ENTRIES.NAME, StringUtils.isNotBlank(overrideName) ? overrideName : folder.getName()) .set(Tables.ENTRIES.NAME_LOWER, StringUtils.isNotBlank(overrideName) ? overrideName.toLowerCase() : folder.getName().toLowerCase()) .execute(); int id = database.factory().lastID().intValue(); List<IFileSystemEntry> entries = new ArrayList<>(folder.list()); Collections.sort(entries, new Comparator<IFileSystemEntry>() { @Override public int compare(IFileSystemEntry e1, IFileSystemEntry e2) { return e1.getName().compareTo(e2.getName()); } }); for (IFileSystemEntry entry : entries) { if (!running) { break; } doPause(); if (entry.isFolder()) { try { backupFolder((IFolder) entry, id, null); } catch (IOException e) { BackupPlugin.getDefault().logError("error while backing up folder: " + //$NON-NLS-1$ entry.getAbsolutePath(), e); fireBackupErrorOccurred(e, BackupErrorEvent.Severity.ERROR); } } else { try { backupFile((IFile) entry, id); } catch (IOException e) { BackupPlugin.getDefault().logError("error while backing up file: " + //$NON-NLS-1$ entry.getAbsolutePath(), e); fireBackupErrorOccurred(e, BackupErrorEvent.Severity.ERROR); } } } return id; } finally { currentFileOrFolder.remove(currentFileOrFolder.size() - 1); } }
From source file:de.blizzy.backup.backup.BackupRun.java
private void backupFile(IFile file, int parentFolderId) throws IOException { currentFileOrFolder.add(file);/*from w w w . ja v a 2 s.c o m*/ try { if ((numEntries % 50) == 0) { checkDiskSpaceAndRemoveOldBackups(); } fireBackupStatusChanged(new BackupStatus(file.getAbsolutePath(), numEntries, totalEntries)); FileTime creationTime = file.getCreationTime(); FileTime lastModificationTime = file.getLastModificationTime(); int fileId = -1; if (settings.isUseChecksums()) { String checksum = getChecksum(file); fileId = findOldFileViaChecksum(file, checksum); } else { fileId = findOldFileViaTimestamp(file); } EntryType type = EntryType.FILE; if (fileId <= 0) { try { String backupFilePath = Utils.createBackupFilePath(settings.getOutputFolder()); File backupFile = Utils.toBackupFile(backupFilePath, settings.getOutputFolder()); fileId = backupFileContents(file, backupFile, backupFilePath); } catch (IOException e) { BackupPlugin.getDefault().logError("error while backing up file: " + //$NON-NLS-1$ file.getAbsolutePath(), e); // file might be in use this time so only show a warning instead of an error fireBackupErrorOccurred(e, Severity.WARNING); type = EntryType.FAILED_FILE; } } database.factory().insertInto(Tables.ENTRIES) .set(Tables.ENTRIES.PARENT_ID, Integer.valueOf(parentFolderId)) .set(Tables.ENTRIES.BACKUP_ID, Integer.valueOf(backupId)) .set(Tables.ENTRIES.TYPE, Byte.valueOf((byte) type.getValue())) .set(Tables.ENTRIES.CREATION_TIME, (creationTime != null) ? new Timestamp(creationTime.toMillis()) : null) .set(Tables.ENTRIES.MODIFICATION_TIME, (lastModificationTime != null) ? new Timestamp(lastModificationTime.toMillis()) : null) .set(Tables.ENTRIES.HIDDEN, Boolean.valueOf(file.isHidden())) .set(Tables.ENTRIES.NAME, file.getName()) .set(Tables.ENTRIES.NAME_LOWER, file.getName().toLowerCase()) .set(Tables.ENTRIES.FILE_ID, (fileId > 0) ? Integer.valueOf(fileId) : null).execute(); numEntries++; } finally { currentFileOrFolder.remove(currentFileOrFolder.size() - 1); } }
From source file:de.blizzy.backup.backup.BackupRun.java
private int findOldFileViaTimestamp(IFile file) throws IOException { FileTime lastModificationTime = file.getLastModificationTime(); long length = file.getLength(); Cursor<Record> cursor = null; try {//w w w . j a v a 2 s . co m cursor = database.factory().select(Tables.BACKUPS.ID).from(Tables.BACKUPS) .where(Tables.BACKUPS.ID.notEqual(Integer.valueOf(backupId))) .orderBy(Tables.BACKUPS.RUN_TIME.desc()).fetchLazy(); while (cursor.hasNext()) { int backupId = cursor.fetchOne().getValue(Tables.BACKUPS.ID).intValue(); int entryId = findFileOrFolderEntryInBackup(file, backupId); if (entryId > 0) { Record record = database.factory() .select(Tables.ENTRIES.MODIFICATION_TIME, Tables.FILES.ID, Tables.FILES.LENGTH) .from(Tables.ENTRIES).join(Tables.FILES) .on(Tables.FILES.ID.equal(Tables.ENTRIES.FILE_ID)) .where(Tables.ENTRIES.ID.equal(Integer.valueOf(entryId)), Tables.ENTRIES.TYPE.equal(Byte.valueOf((byte) EntryType.FILE.getValue()))) .fetchAny(); if (record != null) { Timestamp entryModTime = record.getValue(Tables.ENTRIES.MODIFICATION_TIME); long entryModificationTime = (entryModTime != null) ? entryModTime.getTime() : -1; long entryLength = record.getValue(Tables.FILES.LENGTH).longValue(); if ((entryModificationTime > 0) && (lastModificationTime != null) && (entryModificationTime == lastModificationTime.toMillis()) && (entryLength == length)) { return record.getValue(Tables.FILES.ID).intValue(); } } } } } finally { database.closeQuietly(cursor); } return -1; }
From source file:org.eclipse.winery.repository.backend.BackendUtils.java
/** * This is not repository specific, but we leave it close to the only caller * //from w w w . j a va2 s . c o m * If the passed ref is newer than the modified date (or the modified date * is null), an OK response with an inputstream pointing to the path is * returned */ private static ResponseBuilder returnRefAsResponseBuilder(RepositoryFileReference ref, String modified) { if (!Repository.INSTANCE.exists(ref)) { return Response.status(Status.NOT_FOUND); } FileTime lastModified; try { lastModified = Repository.INSTANCE.getLastModifiedTime(ref); } catch (IOException e1) { BackendUtils.logger.debug("Could not get lastModifiedTime", e1); return Response.serverError(); } // do we really need to send the file or can send "not modified"? if (!BackendUtils.isFileNewerThanModifiedDate(lastModified.toMillis(), modified)) { return Response.status(Status.NOT_MODIFIED); } ResponseBuilder res; try { res = Response.ok(Repository.INSTANCE.newInputStream(ref)); } catch (IOException e) { BackendUtils.logger.debug("Could not open input stream", e); return Response.serverError(); } res = res.lastModified(new Date(lastModified.toMillis())); // vary:accept header is always set to be safe res = res.header(HttpHeaders.VARY, HttpHeaders.ACCEPT); // determine and set MIME content type try { res = res.header(HttpHeaders.CONTENT_TYPE, Repository.INSTANCE.getMimeType(ref)); } catch (IOException e) { BackendUtils.logger.debug("Could not determine mime type", e); return Response.serverError(); } // set filename ContentDisposition contentDisposition = ContentDisposition.type("attachment").fileName(ref.getFileName()) .modificationDate(new Date(lastModified.toMillis())).build(); res.header("Content-Disposition", contentDisposition); return res; }
From source file:org.eclipse.winery.repository.backend.filebased.FilebasedRepository.java
/** * @return null if an error occurred//from w ww .ja v a 2 s . c o m */ @Override public Date getLastUpdate(RepositoryFileReference ref) { Path path = this.ref2AbsolutePath(ref); Date res; if (Files.exists(path)) { FileTime lastModifiedTime; try { lastModifiedTime = Files.getLastModifiedTime(path); res = new Date(lastModifiedTime.toMillis()); } catch (IOException e) { FilebasedRepository.logger.debug(e.getMessage(), e); res = null; } } else { // this branch is taken if the resource directory exists, but the // configuration itself does not exist. // For instance, this happens if icons are manually put for a node // type, but no color configuration is made. res = Constants.LASTMODIFIEDDATE_FOR_404; } return res; }