public void parseLogToThingWorx(File file) { DaiCollectionParser parser = new DaiCollectionParser(); Map<String, String> fileMeta = new HashMap<String, String>(); List<DaiMeterCollection> collections = null; DateTime time = new DateTime(); String timezone = time.getZone().toString(); fileMeta.put("olson_timezone_id", timezone); fileMeta.put("current_system_time", String.valueOf(System.currentTimeMillis())); String fileCreated = ""; BasicFileAttributes attributes = null; try { attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class); fileCreated = String.valueOf(attributes.creationTime().toMillis()); } catch (IOException e) { logger.warn("Unable to get File Create Time: ", e.getMessage()); } fileMeta.put("file_create_time", fileCreated); try { collections = parser.parse(file, fileMeta); } catch (Exception e) { logger.warn("Failed to parse file: ", e.getMessage()); } sendToThingWorx(collections); }
private ZonedDateTime getLastModifiedDate(Path resourcePath) { if (!Files.isReadable(resourcePath)) { throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' is not readable."); } BasicFileAttributes fileAttributes; try { fileAttributes = Files.readAttributes(resourcePath, BasicFileAttributes.class); } catch (NoSuchFileException | FileNotFoundException e) { // This shouldn't be happening because we checked the file's readability before. But just in // case. throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' does not exists.", e); } catch (Exception e) { // UnsupportedOperationException, IOException or any other Exception that might occur. throw new FileOperationException( "Cannot read file attributes from static resource file '" + resourcePath + "'.", e); } if (fileAttributes.isRegularFile()) { return ZonedDateTime.ofInstant(fileAttributes.lastModifiedTime().toInstant(), GMT_TIME_ZONE); } else { /* * From book "OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide" page 478: * Java defines a regular file as one that contains content, as opposed to a symbolic link, * directory, resource (e.g. port, pipe), or other non-regular files that may be present in some * operating systems. [...] It is possible for isRegularFile() to return true for a symbolic link, * as long as the link resolves to a regular file. * Hence, checking 'isRegularFile' of a file is enough to determine its existence and not being a directory. */ throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' is not a regular file."); } }
public static void main(String args[]) { try { aServer asr = new aServer(); // file channel. FileInputStream is = new FileInputStream(""); is.read(); FileChannel cha = is.getChannel(); ByteBuffer bf = ByteBuffer.allocate(1024); bf.flip(); cha.read(bf); // Path Paths Path pth = Paths.get("", ""); // Files some static operation. Files.newByteChannel(pth); Files.copy(pth, pth); // file attribute, other different class for dos and posix system. BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class); bas.size(); } catch (Exception e) { System.err.println(e); } System.out.println("hello "); }
public static synchronized void deleteExpired() { Date expiryDate; long freeSpace = getFreeUserSpace(); if (freeSpace < Globals.DISK_FREE_THRESHOLD) expiryDate = new Date(new Date().getTime() - Globals.FILE_KEEP_IN_CACHE_DURATION_MS_MINIMAL); else expiryDate = new Date(new Date().getTime() - Globals.FILE_KEEP_IN_CACHE_DURATION_MS); Log.warn( "running Delete Expired job", "expiryDate=" + expiryDate.toString() + " , freeSpace=" + freeSpace); File folder = new File(Config.cachePath); for (final File file : folder.listFiles()) { if (file.isFile() && file.getName().endsWith(Globals.FILE_EXT_DATA)) { BasicFileAttributes attrs; try { attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); Date fileLastAccessTime = new Date(attrs.lastAccessTime().toMillis()); if (fileLastAccessTime.before(expiryDate)) deleteDataAndHeaderFiles(file); } catch (Exception e) { Log.error("Cannot read file attributes", e.getCause() + " : " + e.getMessage()); e.printStackTrace(); } } } }
// copy source to target with verification static void copyAndVerify(Path source, Path target, CopyOption... options) throws IOException { Path result = copy(source, target, options); assertTrue(result == target); // get attributes of source and target file to verify copy boolean followLinks = true; LinkOption[] linkOptions = new LinkOption[0]; boolean copyAttributes = false; for (CopyOption opt : options) { if (opt == NOFOLLOW_LINKS) { followLinks = false; linkOptions = new LinkOption[] {NOFOLLOW_LINKS}; } if (opt == COPY_ATTRIBUTES) copyAttributes = true; } BasicFileAttributes basicAttributes = readAttributes(source, BasicFileAttributes.class, linkOptions); // check hash if regular file if (basicAttributes.isRegularFile()) assertTrue(computeHash(source) == computeHash(target)); // check link target if symbolic link if (basicAttributes.isSymbolicLink()) assert (readSymbolicLink(source).equals(readSymbolicLink(target))); // check that attributes are copied if (copyAttributes && followLinks) { checkBasicAttributes( basicAttributes, readAttributes(source, BasicFileAttributes.class, linkOptions)); // verify other attributes when same provider if (source.getFileSystem().provider() == target.getFileSystem().provider()) { // check POSIX attributes are copied String os = System.getProperty("os.name"); if ((os.equals("SunOS") || os.equals("Linux")) && testPosixAttributes) { checkPosixAttributes( readAttributes(source, PosixFileAttributes.class, linkOptions), readAttributes(target, PosixFileAttributes.class, linkOptions)); } // check DOS attributes are copied if (os.startsWith("Windows")) { checkDosAttributes( readAttributes(source, DosFileAttributes.class, linkOptions), readAttributes(target, DosFileAttributes.class, linkOptions)); } // check named attributes are copied if (followLinks && getFileStore(source).supportsFileAttributeView("xattr") && getFileStore(target).supportsFileAttributeView("xattr")) { checkUserDefinedFileAttributes( readUserDefinedFileAttributes(source), readUserDefinedFileAttributes(target)); } } } }
public final FileTime lastModified() throws IOException { if (exists()) { BasicFileAttributes attrs = Files.readAttributes(getPath(), BasicFileAttributes.class, options); return attrs.lastModifiedTime(); } else { return null; } }
@Override public void initialize(URL location, ResourceBundle resources) { // TODO Auto-generated method stub this.tbViewColData.setCellValueFactory(cellData -> cellData.getValue().DataProperty()); this.tbViewColArquivo.setCellValueFactory(cellData -> cellData.getValue().FileNameProperty()); File file = new File("./relatorios/"); if (file.exists()) { File[] m = file.listFiles(); for (File f : m) { Path path = FileSystems.getDefault().getPath("./relatorios"); BasicFileAttributes attributes; try { attributes = Files.readAttributes(path, BasicFileAttributes.class); FileTime creationTime = attributes.creationTime(); data.add(new TableHistoricoData(creationTime.toString(), f.getPath())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.tbViewHistorico.getItems().addAll(data); } this.tbViewHistorico.setRowFactory( tv -> { TableRow<TableHistoricoData> row = new TableRow<>(); row.setOnMouseClicked( event -> { if (event.getClickCount() == 2 && (!row.isEmpty())) { TableHistoricoData rowData = row.getItem(); try { Thread t = new Thread() { public void run() { if (Desktop.isDesktopSupported()) if (Desktop.getDesktop().isSupported(Action.OPEN)) { Desktop op = Desktop.getDesktop(); if (new File(rowData.getFileName()).exists()) { try { op.open(new File(rowData.getFileName())); } catch (IOException e) { e.printStackTrace(); } } } } }; t.start(); } catch (Exception e) { e.printStackTrace(); } } }); return row; }); }
public FsDir(String aMountPoint, File aFile) { super(aMountPoint, aFile); try { BasicFileAttributes attr = Files.readAttributes(aFile.toPath(), BasicFileAttributes.class); creationDate = attr.creationTime(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { if (attr.isSymbolicLink()) { System.out.format("Symbolic link: %s ", file); } else if (attr.isRegularFile()) { System.out.format("Regular file: %s ", file); } else { System.out.format("Other: %s ", file); } System.out.println("(" + attr.size() + "bytes)"); return CONTINUE; }
private boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException { BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class); long modTime = attr.lastModifiedTime().toMillis(); long age = System.currentTimeMillis() - modTime; if (age > durationToExpireMs) { return true; } return false; }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(file.getFileName())) { if (attrs.isSymbolicLink()) { System.out.format("Symbolic link: %s ", file); } else if (attrs.isRegularFile()) { System.out.format("Regular file: %s ", file); } else { System.out.format("Other: %s ", file); } System.out.println("(" + attrs.size() + "bytes)"); } return FileVisitResult.CONTINUE; }
@Override protected Long getDefaultPositionPerFile(File file) throws IOException { if (this.defaultTimestampIsFileUpdated) { BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); BasicFileAttributes readAttributes = fileAttributeView.readAttributes(); return AbsoluteTimeGranularityUtil.asPosition( new Date(readAttributes.lastModifiedTime().toMillis())); } else if (this.defaultDate != null) { return this.defaultDate.getTime(); } else { return super.getDefaultPositionPerFile(file); } }
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { System.out.println("find dir:" + dir.toFile().getName()); System.out.println("attrs:" + attrs); System.out.println(attrs.creationTime().toString()); return FileVisitResult.CONTINUE; }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("find file:" + file.toFile().getName()); System.out.println("attrs:" + attrs); System.out.println(attrs.creationTime().toString()); return FileVisitResult.CONTINUE; }
@Override public StorageMetadata getContainerMetadata(String container) { MutableStorageMetadata metadata = new MutableStorageMetadataImpl(); metadata.setName(container); metadata.setType(StorageType.CONTAINER); metadata.setLocation(getLocation(container)); Path path = new File(buildPathStartingFromBaseDir(container)).toPath(); BasicFileAttributes attr; try { attr = readAttributes(path, BasicFileAttributes.class); } catch (IOException e) { throw Throwables.propagate(e); } metadata.setCreationDate(new Date(attr.creationTime().toMillis())); return metadata; }
private static boolean shouldVisit(Path file, BasicFileAttributes attrs) { return !file.toString().endsWith(".tmp") && attrs.isRegularFile() && !file.endsWith("files") && !file.endsWith(INSTANCE_IDENTIFIER_FILE) && !file.toString().endsWith(METADATA_SUFFIX); }
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isRegularFile()) { if (m != null) m.reset(root.relativize(file).toString()); if (m == null || m.matches()) futures.add(exec.submit(new FileLoader(root, file, blocSize))); } return FileVisitResult.CONTINUE; }
public final void update(File[] xml) { if (xml != null || xml.length != 0) { data = new String[xml.length][columnNames.length]; try { for (int i = 0; i < xml.length; i++) { if (xml[i].isFile()) { BasicFileAttributes attrs = Files.readAttributes(xml[i].toPath(), BasicFileAttributes.class); data[i][0] = xml[i].getName(); data[i][1] = String.format("%d B", attrs.size()); data[i][2] = String.format("%1$tY/%1$tm/%1$td", new Date(attrs.creationTime().toMillis())); data[i][3] = String.format("%1$tY/%1$tm/%1$td", new Date(attrs.lastModifiedTime().toMillis())); } } rows = xml.length; fireTableStructureChanged(); } catch (IOException ex) { System.err.println(ex.getMessage()); } } else JOptionPane.showMessageDialog(null, "No existen facturas de consumidor final"); }
public static String getFileKey(Path filePath) { if (!Files.exists(filePath)) { return ""; } try { if (OSDetector.isWindows()) { UserDefinedFileAttributeView userDefinedFileAttributeView = Files.getFileAttributeView(filePath, UserDefinedFileAttributeView.class); List<String> list = userDefinedFileAttributeView.list(); if (!list.contains("fileKey")) { return ""; } ByteBuffer byteBuffer = ByteBuffer.allocate(userDefinedFileAttributeView.size("fileKey")); userDefinedFileAttributeView.read("fileKey", byteBuffer); CharBuffer charBuffer = _CHARSET.decode((ByteBuffer) byteBuffer.flip()); return charBuffer.toString(); } else { BasicFileAttributes basicFileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class); Object fileKey = basicFileAttributes.fileKey(); return fileKey.toString(); } } catch (Exception e) { _logger.error(e.getMessage(), e); return ""; } }
public FileAttrs getAttrs() throws IOException { FileAttrs result; BasicFileAttributes attr; DosFileAttributes dosAttr; PosixFileAttributes posixAttr; result = new FileAttrs(); attr = Files.readAttributes(this.path.toPath(), BasicFileAttributes.class); result.setCtime(attr.creationTime().toMillis()); result.setMtime(attr.lastModifiedTime().toMillis()); // result.append("symlink", attr.isSymbolicLink()); //Redundant result.setSize(attr.size()); if (System.getProperty("os.name").startsWith("Windows")) { dosAttr = Files.readAttributes(this.path.toPath(), DosFileAttributes.class); result.setDosArchive(dosAttr.isArchive()); result.setDosHidden(dosAttr.isHidden()); result.setDosReadonly(dosAttr.isReadOnly()); result.setDosSystem(dosAttr.isSystem()); } else { posixAttr = Files.readAttributes(this.path.toPath(), PosixFileAttributes.class); result.setPosixSymlink(this.isSymlink()); if (result.getPosixSymlink()) { result.setLinkTo(Files.readSymbolicLink(this.path.toPath()).toString()); } result.setPosixOwner(posixAttr.owner().getName()); result.setPosixGroup(posixAttr.group().getName()); result.setPosixPermission(PosixFilePermissions.toString(posixAttr.permissions())); } return result; }
public static synchronized FileInfo getFileInfo(String name) { int version = getCurrentVersion(name, null); Path dataFilePath = Paths.get(Config.cachePath, getDataFileName(name, version)); Path headerFilePath = Paths.get(Config.cachePath, getHeadersFileName(name, version)); Date fileLastModifiedTime; BasicFileAttributes attrs; try { attrs = Files.readAttributes(dataFilePath, BasicFileAttributes.class); fileLastModifiedTime = new Date(attrs.lastModifiedTime().toMillis()); } catch (Exception e) { Log.error("Cannot read file attributes", e.getCause() + ": " + e.getMessage()); e.printStackTrace(); return null; } // read headers ArrayList<String> headers = loadHeaders(headerFilePath.toFile()); String url = null; String etag = null; for (String header : headers) { if (header.startsWith(HttpConstants.HEADER_FILE_URL + ":")) { url = header.split(":", 2)[1].trim(); } else if (header.startsWith(HttpConstants.HEADER_ETAG + ":")) { etag = header.split(":", 2)[1].trim(); } } if (url != null) return new FileInfo(url, fileLastModifiedTime, etag); else return null; }
/** * Delete older logs from disk. * * @param time The minimum age of the logs. */ public void recycle(long time) { List<Path> toRecycle = new ArrayList<>(); // Check if the log directory exists. If it doesn't // then there isn't anything to recycle. Path logDir = Paths.get(db.getLogPath()).toAbsolutePath(); if (!Files.exists(logDir)) { return; } try { DirectoryStream<Path> stream = Files.newDirectoryStream(logDir, "log_*"); for (Path entry : stream) { // System.out.printf("investigating log %s\n", entry); // Do not include the current block. if (logPath == null || !Files.isSameFile(entry, logPath)) { BasicFileAttributes view = Files.getFileAttributeView(entry, BasicFileAttributeView.class).readAttributes(); if (view.lastModifiedTime().toMillis() < time) { // We no longer need this entry. Add to our // recycling list. toRecycle.add(entry); } } } // Now delete all the values. for (Path p : toRecycle) { Files.delete(p); } } catch (IOException e) { e.printStackTrace(); } }
/* * (non-Javadoc) * * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes) */ @Override public boolean accept( final Path basePath, final Path relativePath, final BasicFileAttributes attrs) { accumulatedSize += attrs.size(); final boolean result = accumulatedSize > thresholdBytes; final String match = result ? ">" : "<="; final String accept = result ? "ACCEPTED" : "REJECTED"; LOGGER.trace( "IfAccumulatedFileSize {}: {} accumulated size '{}' {} thresholdBytes '{}'", accept, relativePath, accumulatedSize, match, thresholdBytes); if (result) { return IfAll.accept(nestedConditions, basePath, relativePath, attrs); } return result; }
/** * Read the account requests from the .xml files that are stored in the * LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER) and stored those requests into * this.requests * * <p>1). read each .xml file 2). get the responsible staff for that request from Support Tracker * DB 3). create a HashMap object to store this request 4). add this request (HashMap object) into * the list that response by this staff 5). add responsileStaff as the key of the requests object * and the list of the requests that this staff responsible as the value of the requests object * * @return a map object, where: + its key is the name of the staff that responsible for the * requests + its value is the list of the request that this staff responsible for => each * request in this list is the Map object, where key is the attribute of the user, and value * is the value of that attribute @Note: if you want to see, how requests object look like, * please uncomment the print lines in this method. * @throws IOException if it failed to create DocumentBuilder object or failed to parse the * DocumentBuilder contents into a DOM object or failed to read the account requests from the * account requests storage folder */ private void extractRequests() throws IOException { logger.debug( "About to read the request *.xml file from the file system: " + LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER)); requests = new TreeMap<String, List<Map<String, String>>>(); try { // building DocumentBuilder DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); File outFolder = new File(LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER)); // outFolder doesn't exist because of the incorrect configured in ldap.properties // or it failed to read from that folder if (!outFolder.exists()) { String foldername = LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER); logger.error(ErrorConstants.FAIL_READING_ACCT_REQUEST + foldername); throw new IOException(ErrorConstants.FAIL_READING_ACCT_REQUEST + foldername); } // we need to order the request based on the date it created. // thats why, we convert the array of files to TreeSet object. TreeSet<File> xmlFiles = new TreeSet<File>(Arrays.asList(outFolder.listFiles())); // ADDITIONAL CODE - SPT-446 // Handle null case by creating empty array if (xmlFiles == null) { xmlFiles = new TreeSet<File>(); } for (File file : xmlFiles) { // read xml file, put the content into DocumentBuilder object // File file = xmlFiles[i]; if (file.getName().endsWith(".xml")) { // parse docBuilder contents into a DOM object (doc) Document doc = docBuilder.parse(file); NodeList fields = doc.getElementsByTagName("field"); logger.debug("Reading a file called: " + file.getName()); // get the data from DOM and store into HashMap String responsibleStaff = null; HashMap<String, String> maps = new HashMap<String, String>(); for (int j = 0; j < fields.getLength(); j++) { String key = fields.item(j).getAttributes().item(0).getTextContent(); maps.put(key, fields.item(j).getTextContent()); // looking for the company field // get the company name from the company field // find the staff who responsible for this request if (key.equalsIgnoreCase("company")) { try { responsibleStaff = SupportTrackerJDBC.getResponsibleStaff(fields.item(j).getTextContent()); } catch (SQLException e) { responsibleStaff = null; } } logger.debug( fields.item(j).getAttributes().item(0).getTextContent() + "|" + fields.item(j).getTextContent()); } String createdDate = null; try { createdDate = file.getName().replace(".xml", ""); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); Date date = sdf.parse(createdDate); sdf = new SimpleDateFormat("yyyy-MM-dd"); createdDate = sdf.format(date); } catch (Exception e) { BasicFileAttributes attrs = Files.readAttributes(Paths.get(file.getPath()), BasicFileAttributes.class); createdDate = attrs.creationTime().toString(); createdDate = createdDate.substring(0, createdDate.indexOf('T')); } maps.put("createdDate", createdDate); maps.put("filename", file.getName()); String countryCode = maps.get("c"); if (!countryCode.equals("") && countryCode != null) { maps.put("co", CountryCode.getCountryByCode(countryCode)); } // if there's no resposibleStaff stored in support tracker DB (when // responsibleStaff==null) // set responsibleStaff as "Others" if (responsibleStaff == null) responsibleStaff = "Others"; // add this request into its corresponding list ArrayList<Map<String, String>> requestResponsibleByThisStaff = null; if (requests.containsKey(responsibleStaff)) { requestResponsibleByThisStaff = (ArrayList<Map<String, String>>) requests.get(responsibleStaff); } else { requestResponsibleByThisStaff = new ArrayList<Map<String, String>>(); } requestResponsibleByThisStaff.add(maps); requests.put(responsibleStaff, requestResponsibleByThisStaff); } } // Note: if you want to see, how requests object look like, pls uncomment these lines // for(Map.Entry<String, List<Map<String, String>>> rqls : requests.entrySet()){ // String responsibleStaff = rqls.getKey(); // System.out.println(responsibleStaff); // List<Map<String, String>> requestList = rqls.getValue(); // int i = 1; // for(Map<String, String> request : requestList){ // System.out.println("\t" + i++ + ")."); // for(Map.Entry<String, String> attr_value : request.entrySet()){ // System.out.println("\t" + attr_value.getKey() + " : " + attr_value.getValue()); // } // } // } logger.debug("Finished reading the request *.xml."); } catch (ParserConfigurationException e) { String foldername = LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER); logger.error("Failed to create DocumentBuilder", e); new IOException(ErrorConstants.FAIL_READING_ACCT_REQUEST + foldername); } catch (SAXException e) { String foldername = LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER); logger.error("Failed to parse docBuilder contents into DOM object", e); new IOException(ErrorConstants.FAIL_READING_ACCT_REQUEST + foldername); } catch (IOException e) { String foldername = LdapProperty.getProperty(LdapConstants.OUTPUT_FOLDER); logger.error("Failed to parse docBuilder contents into DOM object", e); new IOException(ErrorConstants.FAIL_READING_ACCT_REQUEST + foldername); } }
protected List<PluginInfo<T>> scan( LogSource logger, String hosterpath, final List<? extends LazyPlugin<T>> pluginCache, final AtomicLong lastFolderModification) throws Exception { DirectoryStream<Path> stream = null; final ArrayList<PluginInfo<T>> ret = new ArrayList<PluginInfo<T>>(); final long timeStamp = System.currentTimeMillis(); try { long lastModified = lastFolderModification != null ? lastFolderModification.get() : -1; final Path folder = Application.getRootByClass(jd.SecondLevelLaunch.class, hosterpath).toPath(); final long lastFolderModified = Files.readAttributes(folder, BasicFileAttributes.class).lastModifiedTime().toMillis(); if (lastModified > 0 && lastFolderModified == lastModified && pluginCache != null && pluginCache.size() > 0) { for (final LazyPlugin<T> lazyPlugin : pluginCache) { final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPlugin.getLazyPluginClass(), null); pluginInfo.setLazyPlugin(lazyPlugin); ret.add(pluginInfo); } return ret; } PluginClassLoaderChild cl = null; MessageDigest md = null; final String pkg = hosterpath.replace("/", "."); final byte[] mdCache = new byte[32767]; final HashMap<String, List<LazyPlugin<T>>> lazyPluginClassMap; if (pluginCache != null && pluginCache.size() > 0) { lazyPluginClassMap = new HashMap<String, List<LazyPlugin<T>>>(); for (final LazyPlugin<T> lazyPlugin : pluginCache) { List<LazyPlugin<T>> list = lazyPluginClassMap.get(lazyPlugin.getLazyPluginClass().getClassName()); if (list == null) { list = new ArrayList<LazyPlugin<T>>(); lazyPluginClassMap.put(lazyPlugin.getLazyPluginClass().getClassName(), list); } list.add(lazyPlugin); } } else { lazyPluginClassMap = null; } if (lastFolderModification != null) { lastFolderModification.set(lastFolderModified); } stream = Files.newDirectoryStream(folder, "*.class"); for (final Path path : stream) { try { final String pathFileName = path.getFileName().toString(); final String className = pathFileName.substring(0, pathFileName.length() - 6); if (className.indexOf("$") < 0 && !PluginController.IGNORELIST.contains(className)) { byte[] sha256 = null; final BasicFileAttributes pathAttr = Files.readAttributes(path, BasicFileAttributes.class); if (lazyPluginClassMap != null) { final List<LazyPlugin<T>> lazyPlugins = lazyPluginClassMap.get(className); if (lazyPlugins != null && lazyPlugins.size() > 0) { final LazyPluginClass lazyPluginClass = lazyPlugins.get(0).getLazyPluginClass(); if (lazyPluginClass != null && (lazyPluginClass.getLastModified() == pathAttr.lastModifiedTime().toMillis() || ((md = MessageDigest.getInstance("SHA-256")) != null && (sha256 = PluginController.getFileHashBytes(path.toFile(), md, mdCache)) != null && Arrays.equals(sha256, lazyPluginClass.getSha256())))) { for (final LazyPlugin<T> lazyPlugin : lazyPlugins) { // logger.finer("Cached: " + className + "|" + lazyPlugin.getDisplayName() + "|" // + // lazyPluginClass.getRevision()); final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPluginClass, null); pluginInfo.setLazyPlugin(lazyPlugin); ret.add(pluginInfo); } continue; } } } Class<T> pluginClass = null; long[] infos = null; try { if (cl == null) { cl = PluginClassLoader.getInstance().getChild(); } if (md == null) { md = MessageDigest.getInstance("SHA-256"); } pluginClass = (Class<T>) cl.loadClass(pkg + "." + className); if (!Modifier.isAbstract(pluginClass.getModifiers()) && Plugin.class.isAssignableFrom(pluginClass)) { infos = getPluginController().getInfos(pluginClass); if (infos == null) { continue; } } else { continue; } } catch (final Throwable e) { logger.finer("Failed: " + className); logger.log(e); continue; } if (sha256 == null) { sha256 = PluginController.getFileHashBytes(path.toFile(), md, mdCache); } // final LazyPluginClass lazyPluginClass = new LazyPluginClass( className, sha256, pathAttr.lastModifiedTime().toMillis(), (int) infos[0], infos[1]); final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPluginClass, pluginClass); // logger.finer("Scaned: " + className + "|" + lazyPluginClass.getRevision()); ret.add(pluginInfo); } } catch (Throwable e) { logger.finer("Failed: " + path); logger.log(e); } } return ret; } finally { logger.info( "@PluginController(NIO): scan took " + (System.currentTimeMillis() - timeStamp) + "ms for " + ret.size()); if (stream != null) { stream.close(); } } }
static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time long time1 = attrs1.lastModifiedTime().toMillis(); long time2 = attrs2.lastModifiedTime().toMillis(); assertTrue(time1 == time2); // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }
// move source to target with verification static void moveAndVerify(Path source, Path target, CopyOption... options) throws IOException { // read attributes before file is moved BasicFileAttributes basicAttributes = null; PosixFileAttributes posixAttributes = null; DosFileAttributes dosAttributes = null; Map<String, ByteBuffer> namedAttributes = null; // get file attributes of source file String os = System.getProperty("os.name"); if (os.startsWith("Windows")) { dosAttributes = readAttributes(source, DosFileAttributes.class, NOFOLLOW_LINKS); basicAttributes = dosAttributes; } else { posixAttributes = readAttributes(source, PosixFileAttributes.class, NOFOLLOW_LINKS); basicAttributes = posixAttributes; } if (basicAttributes == null) basicAttributes = readAttributes(source, BasicFileAttributes.class, NOFOLLOW_LINKS); // hash file contents if regular file int hash = (basicAttributes.isRegularFile()) ? computeHash(source) : 0; // record link target if symbolic link Path linkTarget = null; if (basicAttributes.isSymbolicLink()) linkTarget = readSymbolicLink(source); // read named attributes if available (and file is not a sym link) if (!basicAttributes.isSymbolicLink() && getFileStore(source).supportsFileAttributeView("xattr")) { namedAttributes = readUserDefinedFileAttributes(source); } // move file Path result = move(source, target, options); assertTrue(result == target); // verify source does not exist assertTrue(notExists(source)); // verify file contents if (basicAttributes.isRegularFile()) { if (computeHash(target) != hash) throw new RuntimeException("Failed to verify move of regular file"); } // verify link target if (basicAttributes.isSymbolicLink()) { if (!readSymbolicLink(target).equals(linkTarget)) throw new RuntimeException("Failed to verify move of symbolic link"); } // verify basic attributes checkBasicAttributes( basicAttributes, readAttributes(target, BasicFileAttributes.class, NOFOLLOW_LINKS)); // verify other attributes when same provider if (source.getFileSystem().provider() == target.getFileSystem().provider()) { // verify POSIX attributes if (posixAttributes != null && !basicAttributes.isSymbolicLink() && testPosixAttributes) { checkPosixAttributes( posixAttributes, readAttributes(target, PosixFileAttributes.class, NOFOLLOW_LINKS)); } // verify DOS attributes if (dosAttributes != null && !basicAttributes.isSymbolicLink()) { DosFileAttributes attrs = readAttributes(target, DosFileAttributes.class, NOFOLLOW_LINKS); checkDosAttributes(dosAttributes, attrs); } // verify named attributes if (namedAttributes != null && getFileStore(target).supportsFileAttributeView("xattr")) { checkUserDefinedFileAttributes(namedAttributes, readUserDefinedFileAttributes(target)); } } }
static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time if not a symbolic link if (!attrs1.isSymbolicLink()) { long time1 = attrs1.lastModifiedTime().to(TimeUnit.SECONDS); long time2 = attrs2.lastModifiedTime().to(TimeUnit.SECONDS); if (time1 != time2) { System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime()); System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime()); assertTrue(false); } } // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relFilePath = m_srcRoot.relativize(file); if (isSrcIgnored(relFilePath)) { m_progress.reportProgress(CloneProgressReporter.EventType.FILE_SKIP_IGNORE, file, null, null); return FileVisitResult.CONTINUE; } Path newPathCandidate = m_destRoot.resolve(relFilePath); Iterator<FileHandler> it = m_fileHandlers.iterator(); while (it.hasNext()) { FileHandler handler = it.next(); try { Path newPath = handler.canHandleFile(file, attrs, newPathCandidate); if (newPath != null) { // record the visited file even if we don't process it recordVisitedFile(file, newPath); // does the new path exist if (Files.exists(newPath, LinkOption.NOFOLLOW_LINKS)) { // if it's the same file, then skip if (Files.isSameFile(file, newPath)) { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_SKIP_SAME, file, newPath, null); } else { // process file BasicFileAttributes targetAttrs = Files.readAttributes( newPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (targetAttrs.lastModifiedTime().compareTo(attrs.lastModifiedTime()) < 0) { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_BEGIN, file, newPath, null); handler.processFile(file, attrs, newPathCandidate, m_runType); m_progress.reportProgress( CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_END, file, newPath, null); } else { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_SKIP_NEWER, file, newPath, null); } } } else { // otherwise process it m_progress.reportProgress( CloneProgressReporter.EventType.FILE_CREATE_NEW_BEGIN, file, newPath, null); handler.processFile(file, attrs, newPathCandidate, m_runType); m_progress.reportProgress( CloneProgressReporter.EventType.FILE_CREATE_NEW_END, file, newPath, null); } // file has been handled break; } } catch (FileCloningError e) { m_progress.reportProgress(CloneProgressReporter.EventType.ERROR, file, null, e); // TODO: should we continue and try next handler in list? } } return FileVisitResult.CONTINUE; }