List of usage examples for java.nio.file Files newDirectoryStream
public static DirectoryStream<Path> newDirectoryStream(Path dir) throws IOException
From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.io.HbsRenderableUpdater.java
private void run() { while (!isWatchServiceStopped) { WatchKey watchKey;//from w ww.j a v a 2 s.c o m try { watchKey = watcher.take(); } catch (ClosedWatchServiceException e) { log.debug("File watch service is closed."); return; } catch (InterruptedException e) { log.debug("File watch service interrupted."); return; } for (WatchEvent<?> event : watchKey.pollEvents()) { if (event.kind() != StandardWatchEventKinds.ENTRY_MODIFY) { continue; // We only watch file modify events. } Path updatedDirectory = (Path) watchKey.watchable(); @SuppressWarnings("unchecked") Path updatedFileName = ((WatchEvent<Path>) event).context(); Path updatedFileAbsolutePath = updatedDirectory.resolve(updatedFileName); try (DirectoryStream<Path> stream = Files.newDirectoryStream(updatedFileAbsolutePath.getParent())) { for (Path entry : stream) { if (Files.isDirectory(entry)) { continue; } MutableHbsRenderable mutableRenderable = watchingRenderables.get(entry); if (mutableRenderable != null) { // Updated file is a MutableHbsRenderable try { mutableRenderable.reload(new StringTemplateSource( mutableRenderable.getComponentPath(), readFileContent(entry))); log.info("Handlebars template '" + entry + "' reloaded successfully."); } catch (IOException e) { log.error("An error occurred while reloading Handlebars template '" + entry + "'.", e); } continue; } MutableExecutable mutableExecutable = watchingExecutables.get(entry); if (mutableExecutable != null) { // Updated file is a MutableExecutable try { mutableExecutable.reload(readFileContent(entry)); log.info("JavaScript file '" + entry + "' reloaded successfully."); } catch (IOException e) { log.error("An error occurred while reloading JavaScript file '" + entry + "'.", e); } } } } catch (IOException e) { log.error("An error occurred while reloading modified files '" + updatedFileAbsolutePath + "'.", e); } } boolean valid = watchKey.reset(); if (!valid) { // Watch key cannot not be reset because watch service is already closed. break; } } }
From source file:uk.ac.sanger.cgp.wwdocker.actions.Utils.java
public static List<File> getGnosKeys(BaseConfiguration config) { List<File> gnosKeys = new ArrayList(); Path dir = Paths.get(config.getString("gnosKeys")); try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { for (Path item : stream) { File file = item.toFile(); if (file.isFile() && !file.isHidden()) { gnosKeys.add(file);/* w ww. j av a 2 s. co m*/ } } } catch (IOException | DirectoryIteratorException e) { throw new RuntimeException(e.getMessage(), e); } return gnosKeys; }
From source file:cn.edu.zjnu.acm.judge.core.Judger.java
private boolean runProcess(RunRecord runRecord) throws IOException { Path dataPath = runRecord.getDataPath(); Objects.requireNonNull(dataPath, "dataPath"); Path specialFile = dataPath.resolve(JudgeConfiguration.VALIDATE_FILE_NAME); boolean isspecial = Files.exists(specialFile); if (!Files.isDirectory(dataPath)) { log.error("{} not exists", runRecord.getDataPath()); return false; }/*w ww .j a v a 2 s. c o m*/ List<Path[]> files = new ArrayList<>(20); try (DirectoryStream<Path> listFiles = Files.newDirectoryStream(dataPath)) { log.debug("dataPath = {}", dataPath); for (Path inFile : listFiles) { String inFileName = inFile.getFileName().toString(); if (!inFileName.toLowerCase().endsWith(".in")) { continue; } Path outFile = dataPath.resolve(inFileName.substring(0, inFileName.length() - 3) + ".out"); if (!Files.exists(outFile)) { continue; } files.add(new Path[] { inFile, outFile });//, } } int casenum = files.size(); log.debug("casenum = {}", casenum); if (casenum == 0) { return false; } int accept = 0; //? ArrayList<String> details = new ArrayList<>(casenum << 2); long time = 0; // long memory = 0; // String command = runRecord.getLanguage().getExecuteCommand(); Path work = judgeConfiguration.getWorkDirectory(runRecord.getSubmissionId()); // command = !StringUtils.isEmptyOrWhitespace(command) ? command : work.resolve("Main." + runRecord.getLanguage().getExecutableExtension()).toString(); long extTime = runRecord.getLanguage().getExtTime(); long castTimeLimit = runRecord.getTimeLimit() * runRecord.getLanguage().getTimeFactor() + extTime; long extraMemory = runRecord.getLanguage().getExtMemory(); // long caseMemoryLimit = (runRecord.getMemoryLimit() + extraMemory) * 1024; Options[] optionses = new Options[casenum]; for (int cas = 0; cas < casenum; cas++) { Path[] entry = files.get(cas); Path in = entry[0]; Path standard = entry[1]; Path progOutput = work.resolve(standard.getFileName()); optionses[cas] = Options.builder().timeLimit(castTimeLimit) // time limit .memoryLimit(caseMemoryLimit) // memory in bytes .outputLimit(16 * 1024 * 1024) // 16M .command(command).workDirectory(work).inputFile(in).outputFile(progOutput) .standardOutput(standard).errFile(getNull(work)).build(); } String detailMessageStr = null; String scorePerCase = new DecimalFormat("0.#").format(100.0 / casenum); final Validator validator = isspecial ? new SpecialValidator(specialFile.toString(), work) : new SimpleValidator(); try { ExecuteResult[] ers = JudgeBridge.INSTANCE.judge(optionses, false, validator); for (ExecuteResult er : ers) { long tim1 = er.getTime() - extTime; tim1 = Math.max(0, tim1); long mem1 = er.getMemory() / 1024 - extraMemory; mem1 = Math.max(0, mem1); String message = er.getMessage(); int caseResult = getResultFromExecuteResult(er); time = Math.max(time, tim1); memory = Math.max(memory, mem1); log.debug("message = {}, time = {}, memory = {}", message, time, memory); details.add(String.valueOf(caseResult)); if (caseResult == 0) { details.add(scorePerCase); } else { details.add("0"); } details.add(String.valueOf(tim1)); details.add(String.valueOf(mem1)); if (caseResult == 0) { ++accept; } } } catch (JudgeException | RuntimeException | Error ex) { log.error("", ex); accept = ResultType.SYSTEM_ERROR; detailMessageStr = ex.getMessage(); } log.debug("{}", details); int score = accept >= 0 ? (int) Math.round(accept * 100.0 / casenum) : accept; if (score == 0 && accept != 0) { ++score; } else if (score == 100 && accept != casenum) { --score; } submissionMapper.updateResult(runRecord.getSubmissionId(), score, time, memory); submissionMapper.saveDetail(runRecord.getSubmissionId(), detailMessageStr != null ? detailMessageStr : details.stream().map(String::valueOf).collect(Collectors.joining(","))); updateSubmissionStatus(runRecord); return score == 100; }
From source file:org.tinymediamanager.core.tvshow.TvShowExporter.java
/** * exports movie list according to template file. * /* w w w .ja v a 2 s .c o m*/ * @param tvShowsToExport * list of movies * @param exportDir * the path to export * @throws Exception * the exception */ @Override public <T extends MediaEntity> void export(List<T> tvShowsToExport, Path exportDir) throws Exception { LOGGER.info("preparing tv show export; using " + properties.getProperty("name")); // register own renderers engine.registerNamedRenderer(new NamedDateRenderer()); engine.registerNamedRenderer(new TvShowFilenameRenderer()); engine.registerNamedRenderer(new ArtworkCopyRenderer(exportDir)); // prepare export destination if (!Files.exists(exportDir)) { try { Files.createDirectories(exportDir); } catch (Exception e) { throw new Exception("error creating export directory"); } } // prepare listfile Path listExportFile = null; if (fileExtension.equalsIgnoreCase("html")) { listExportFile = exportDir.resolve("index.html"); } if (fileExtension.equalsIgnoreCase("xml")) { listExportFile = exportDir.resolve("tvshows.xml"); } if (fileExtension.equalsIgnoreCase("csv")) { listExportFile = exportDir.resolve("tvshows.csv"); } if (listExportFile == null) { throw new Exception("error creating tv show list file"); } // load episode template String episodeTemplateFile = properties.getProperty("episode"); String episodeTemplate = ""; if (StringUtils.isNotBlank(episodeTemplateFile)) { episodeTemplate = Utils.readFileToString(templateDir.resolve(episodeTemplateFile)); } // create the list LOGGER.info("generating tv show list"); Utils.deleteFileSafely(listExportFile); Map<String, Object> root = new HashMap<>(); root.put("tvShows", new ArrayList<>(tvShowsToExport)); String output = engine.transform(listTemplate, root); Utils.writeStringToFile(listExportFile, output); LOGGER.info("movie list generated: " + listExportFile); if (StringUtils.isNotBlank(detailTemplate)) { for (MediaEntity me : tvShowsToExport) { TvShow show = (TvShow) me; // create a TV show dir Path showDir = exportDir.resolve(getFilename(show)); if (Files.isDirectory(showDir)) { Utils.deleteDirectoryRecursive(showDir); } Files.createDirectory(showDir); Path detailsExportFile = showDir.resolve("tvshow." + fileExtension); root = new HashMap<>(); root.put("tvShow", show); output = engine.transform(detailTemplate, root); Utils.writeStringToFile(detailsExportFile, output); if (StringUtils.isNotBlank(episodeTemplate)) { for (TvShowEpisode episode : show.getEpisodes()) { List<MediaFile> mfs = episode.getMediaFiles(MediaFileType.VIDEO); if (!mfs.isEmpty()) { Path seasonDir = showDir.resolve(TvShowRenamer.generateSeasonDir("", episode)); if (!Files.isDirectory(seasonDir)) { Files.createDirectory(seasonDir); } String episodeFileName = getFilename(episode) + "." + fileExtension; Path episodeExportFile = seasonDir.resolve(episodeFileName); root = new HashMap<>(); root.put("episode", episode); output = engine.transform(episodeTemplate, root); Utils.writeStringToFile(episodeExportFile, output); } } } } } // copy all non .jtme/template.conf files to destination dir try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(templateDir)) { for (Path path : directoryStream) { if (Utils.isRegularFile(path)) { if (path.getFileName().toString().endsWith(".jmte") || path.getFileName().toString().endsWith("template.conf")) { continue; } Files.copy(path, exportDir.resolve(path.getFileName()), StandardCopyOption.REPLACE_EXISTING); } else if (Files.isDirectory(path)) { Utils.copyDirectoryRecursive(path, exportDir.resolve(path.getFileName())); } } } catch (IOException ex) { LOGGER.error("could not copy resources: ", ex); } }
From source file:org.nuxeo.transientstore.AbstractTransientStore.java
public void doGC() { File dir = getCachingDirectory(); long newSize = 0; try {// www . j av a 2 s .c om try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir.getAbsolutePath()))) { for (Path entry : stream) { String key = getKeyCachingDirName(entry.getFileName().toString()); try { // XXX should not get entry since it can mess the LRU if (getL1Cache().hasEntry(key)) { newSize += getSize(entry); continue; } if (getL2Cache().hasEntry(key)) { newSize += getSize(entry); continue; } FileUtils.deleteDirectory(entry.toFile()); } catch (IOException e) { log.error("Error while performing GC", e); } } } } catch (IOException e) { log.error("Error while performing GC", e); } setStorageSize(newSize); }
From source file:org.roda.core.plugins.plugins.characterization.ExifToolPlugin.java
private void processAIP(IndexService index, ModelService model, StorageService storage, Report report, SimpleJobPluginInfo jobPluginInfo, Job job, AIP aip) { LOGGER.debug("Processing AIP {}", aip.getId()); boolean inotify = false; Report reportItem = PluginHelper.initPluginReportItem(this, aip.getId(), AIP.class, AIPState.INGEST_PROCESSING); PluginHelper.updatePartialJobReport(this, model, reportItem, false, job); PluginState reportState = PluginState.SUCCESS; ValidationReport validationReport = new ValidationReport(); List<LinkingIdentifier> sources = new ArrayList<>(); for (Representation representation : aip.getRepresentations()) { LOGGER.debug("Processing representation {} from AIP {}", representation.getId(), aip.getId()); DirectResourceAccess directAccess = null; try {// w w w. j a v a 2 s.c o m StoragePath representationDataPath = ModelUtils.getRepresentationDataStoragePath(aip.getId(), representation.getId()); directAccess = storage.getDirectAccess(representationDataPath); sources.add(PluginHelper.getLinkingIdentifier(aip.getId(), representation.getId(), RodaConstants.PRESERVATION_LINKING_OBJECT_SOURCE)); CloseableIterable<OptionalWithCause<org.roda.core.data.v2.ip.File>> allFiles = model .listFilesUnder(aip.getId(), representation.getId(), true); if (!CloseableIterables.isEmpty(allFiles)) { Path metadata = Files.createTempDirectory("metadata"); ExifToolPluginUtils.runExifToolOnPath(directAccess.getPath(), metadata); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(metadata)) { for (Path path : directoryStream) { ContentPayload payload = new FSPathContentPayload(path); List<String> fileDirectoryPath = new ArrayList<>(); Path relativePath = metadata.relativize(path); for (int i = 0; i < relativePath.getNameCount() - 2; i++) { fileDirectoryPath.add(relativePath.getName(i).toString()); } String fileId = path.getFileName().toString(); model.createOrUpdateOtherMetadata(aip.getId(), representation.getId(), fileDirectoryPath, fileId, ".xml", RodaConstants.OTHER_METADATA_TYPE_EXIFTOOL, payload, inotify); } } FSUtils.deletePath(metadata); } } catch (RODAException | IOException e) { LOGGER.error("Error processing AIP {}: {}", aip.getId(), e.getMessage()); reportState = PluginState.FAILURE; validationReport.addIssue(new ValidationIssue(e.getMessage())); } finally { IOUtils.closeQuietly(directAccess); } } try { model.notifyAipUpdated(aip.getId()); } catch (RequestNotValidException | GenericException | NotFoundException | AuthorizationDeniedException e) { LOGGER.error("Error notifying of AIP update", e); } if (reportState.equals(PluginState.SUCCESS)) { jobPluginInfo.incrementObjectsProcessedWithSuccess(); reportItem.setPluginState(PluginState.SUCCESS); } else { jobPluginInfo.incrementObjectsProcessedWithFailure(); reportItem.setHtmlPluginDetails(true).setPluginState(PluginState.FAILURE); reportItem.setPluginDetails(validationReport.toHtml(false, false, false, "Error list")); } try { PluginHelper.createPluginEvent(this, aip.getId(), model, index, sources, null, reportItem.getPluginState(), "", true); } catch (ValidationException | RequestNotValidException | NotFoundException | GenericException | AuthorizationDeniedException | AlreadyExistsException e) { LOGGER.error("Error creating event: {}", e.getMessage(), e); } report.addReport(reportItem); PluginHelper.updatePartialJobReport(this, model, reportItem, true, job); }
From source file:org.apache.karaf.tooling.ArchiveMojo.java
public File archive(File source, File dest, Artifact artifact) throws //ArchiverException, IOException {/* ww w . j a va2 s. c o m*/ String serverName = null; if (targetFile != null) { serverName = targetFile.getName(); } else { serverName = artifact.getArtifactId() + "-" + artifact.getVersion(); } dest = new File(dest, serverName + "." + artifact.getType()); String prefix = ""; if (usePathPrefix) { prefix = pathPrefix.trim(); if (prefix.length() > 0 && !prefix.endsWith("/")) { prefix += "/"; } } if ("tar.gz".equals(artifact.getType())) { try (OutputStream fOut = Files.newOutputStream(dest.toPath()); OutputStream bOut = new BufferedOutputStream(fOut); OutputStream gzOut = new GzipCompressorOutputStream(bOut); TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut); DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath()) ) { tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); for (Path child : children) { addFileToTarGz(tOut, child, prefix); } } } else if ("zip".equals(artifact.getType())) { try (OutputStream fOut = Files.newOutputStream(dest.toPath()); OutputStream bOut = new BufferedOutputStream(fOut); ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut); DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath()) ) { for (Path child : children) { addFileToZip(tOut, child, prefix); } } } else { throw new IllegalArgumentException("Unknown target type: " + artifact.getType()); } return dest; }
From source file:uk.ac.sanger.cgp.wwdocker.actions.Utils.java
public static List<File> getWorkInis(BaseConfiguration config) { List<File> iniFiles = new ArrayList(); Path dir = Paths.get(config.getString("wfl_inis")); try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { for (Path item : stream) { File file = item.toFile(); if (file.isFile() && file.canRead() && !file.isHidden()) { if (!file.getName().endsWith(".ini")) { continue; }/* w w w . jav a2 s . co m*/ iniFiles.add(file); } } } catch (IOException | DirectoryIteratorException e) { throw new RuntimeException(e.getMessage(), e); } return iniFiles; }
From source file:uk.co.unclealex.executable.impl.MakeLinksCommandRunnerTest.java
protected void checkDirectoriesEqual(Path expectedDir, Path actualDir) throws IOException { Assert.assertTrue("Path " + expectedDir + "is not a directory.", Files.isDirectory(expectedDir, LinkOption.NOFOLLOW_LINKS)); Assert.assertTrue("Path " + actualDir + "is not a directory.", Files.isDirectory(actualDir, LinkOption.NOFOLLOW_LINKS)); Assert.assertArrayEquals("Directory " + actualDir + " has the wrong file entries.", filenamesIn(expectedDir), filenamesIn(actualDir)); DirectoryStream<Path> directoryStream = Files.newDirectoryStream(actualDir); for (Path expectedChild : directoryStream) { Path actualChild = expectedDir.resolve(expectedChild.getFileName()); if (Files.isDirectory(expectedChild, LinkOption.NOFOLLOW_LINKS)) { Assert.assertTrue("Path " + actualChild + " is not a directory.", Files.isDirectory(actualChild, LinkOption.NOFOLLOW_LINKS)); checkDirectoriesEqual(expectedChild, actualChild); } else if (Files.isSymbolicLink(expectedChild)) { Assert.assertTrue("Path " + actualChild + " is not a symbolic link", Files.isSymbolicLink(actualChild)); Assert.assertEquals("Symbolic link " + actualChild + " points to the wrong place.", Files.readSymbolicLink(expectedChild), Files.readSymbolicLink(actualChild)); } else {//from w w w. j av a 2s . c o m Assert.assertTrue("Path " + actualChild + " has the wrong content.", FileUtils.contentEquals(expectedChild.toFile(), actualChild.toFile())); } } }
From source file:org.jenkinsci.plugins.ssegateway.EventHistoryStore.java
static int getChannelEventCount(@Nonnull String channelName) throws IOException { Path dirPath = Paths.get(getChannelDir(channelName).toURI()); int count = 0; try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(dirPath)) { for (final Path entry : dirStream) { count++;/*from w w w .j a v a2s .co m*/ } } return count; }