List of usage examples for java.nio.file Path subpath
Path subpath(int beginIndex, int endIndex);
From source file:api.wiki.WikiGenerator.java
private Path copyToReleaseDirectory(Path file, Path releaseDirectory) { try {/*from www . ja v a 2 s.c o m*/ Path target = releaseDirectory .resolve(file.subpath(wikiDirectory().getNameCount(), file.getNameCount())); Files.createDirectories(target.subpath(1, target.getNameCount() - 1)); return Files.copy(file, target); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:fr.lille1.car.burihabwa.rest.utils.FTPAdapterImpl.java
@Override public String getParentDirectory(final String path) { if (path == null) { return ""; }/*from w w w .j a v a 2 s .c om*/ Path file = Paths.get(path); int nameCount = file.getNameCount(); String parentDirectory = ""; if (nameCount > 1) { parentDirectory = file.subpath(0, nameCount - 1).toString(); } return parentDirectory; }
From source file:com.mkl.websuites.internal.tests.ScenarioFolderTest.java
private void processRecursivelyFolder(Path folderPath, TestSuite parentSuite) { TestSuite currentFolderSuite;/*from w ww.j a v a2 s . c o m*/ if (isTopTest) { currentFolderSuite = new TestSuite(folderPath.toString()); isTopTest = false; } else { currentFolderSuite = new TestSuite( folderPath.subpath(folderPath.getNameCount() - 1, folderPath.getNameCount()).toString()); } List<Test> testsInCurrentFolder = processScenarioFilesInFolder(folderPath.toString()); for (Test test : testsInCurrentFolder) { currentFolderSuite.addTest(test); } parentSuite.addTest(currentFolderSuite); if (ignoreSubfolders) { return; } File folder = new File(folderPath.toString()); File[] nestedFolders = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { return file.isDirectory(); } }); if (nestedFolders == null) { throw new WebServiceException(String.format("Error while traversing through folder " + "structure starting from path '%s'. Probably there is something wrong " + "in the path string.", folderPath)); } sort(nestedFolders); for (File nested : nestedFolders) { processRecursivelyFolder(Paths.get(nested.toURI()), currentFolderSuite); } }
From source file:de.alexkamp.sandbox.ChrootSandbox.java
@Override public void walkDirectoryTree(String basePath, final DirectoryWalker walker) throws IOException { final int baseNameCount = data.getBaseDir().toPath().getNameCount(); File base = new File(data.getBaseDir(), basePath); Files.walkFileTree(base.toPath(), new FileVisitor<Path>() { @Override/*from w w w . ja v a2s . co m*/ public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes) throws IOException { if (walker.visitDirectory(calcSubpath(path))) { return FileVisitResult.CONTINUE; } return FileVisitResult.SKIP_SUBTREE; } private String calcSubpath(Path path) { if (path.getNameCount() == baseNameCount) { return "/"; } return "/" + path.subpath(baseNameCount, path.getNameCount()).toString(); } @Override public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException { String subpath = calcSubpath(path); if (walker.visitFile(subpath)) { try (InputStream is = Files.newInputStream(path)) { walker.visitFileContent(subpath, is); } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException { if (walker.failed(e)) { return FileVisitResult.CONTINUE; } else { return FileVisitResult.TERMINATE; } } @Override public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException { return FileVisitResult.CONTINUE; } }); }
From source file:com.spd.ukraine.lucenewebsearch1.web.IndexingController.java
@PostConstruct public void init() { if (IS_DIRECTORY_IN_DISK) { String userDirectory = System.getProperty("user.dir");// + "/lucene"; System.out.println("userDirectory " + userDirectory); Path userPath = Paths.get(userDirectory); Path rootPath = userPath.getRoot(); String workingDirectory = rootPath.toString() .concat(System.getProperty("file.separator").equals("/") ? userPath.subpath(0, 2).toString() + "/" : "\\Users\\sf\\") .concat("luceneindex"); System.out.println("workingDirectory " + workingDirectory); indexDir = new File(workingDirectory); try {/*from w ww . j ava 2s .c o m*/ Files.createDirectory(Paths.get(workingDirectory)); } catch (FileAlreadyExistsException ex) { System.out.println("FileAlreadyExistsException"); } catch (IOException ex) { // System.out.println("IOException: " + ex.getMessage()); ex.printStackTrace(); } if (null == indexDir) { return; } try { directory = FSDirectory.open(indexDir); } catch (IOException ex) { System.out.println("IOException: " + ex.getMessage()); } } else { directory = new RAMDirectory(); } analyzer = new StandardAnalyzer(Version.LUCENE_43);//new StandardAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, analyzer); try { indexWriter = new IndexWriter(directory, config); } catch (IOException ex) { // ex.printStackTrace(); // return; } }
From source file:com.thinkbiganalytics.metadata.modeshape.support.JcrPath.java
@Override public Path relativize(Path other) { if (other.isAbsolute() != isAbsolute() || other.getNameCount() < getNameCount()) { return other; } else if (other.getNameCount() == 0) { return this; } else {// w w w . j a v a2 s . c o m int idx = 0; for (; idx < getNameCount(); idx++) { if (!other.getName(idx).equals(getName(idx))) { return other; } } return other.subpath(idx - 1, other.getNameCount()); } }
From source file:com.github.podd.example.ExamplePoddClient.java
public Map<Path, String> uploadToStorage(final List<Path> bagsToUpload, final String sshServerFingerprint, final String sshHost, final int portNo, final String username, final Path pathToPublicKey, final Path localRootPath, final Path remoteRootPath, final PasswordFinder keyExtractor) throws PoddClientException, NoSuchAlgorithmException, IOException { final Map<Path, String> results = new ConcurrentHashMap<>(); final ConcurrentMap<Path, ConcurrentMap<PoddDigestUtils.Algorithm, String>> digests = PoddDigestUtils .getDigests(bagsToUpload);//from www .j a v a2s .c o m try (SSHClient sshClient = new SSHClient(ExamplePoddClient.DEFAULT_CONFIG);) { sshClient.useCompression(); sshClient.addHostKeyVerifier(sshServerFingerprint); sshClient.connect(sshHost, portNo); if (!Files.exists(pathToPublicKey)) { throw new PoddClientException("Could not find public key: " + pathToPublicKey); } if (!SecurityUtils.isBouncyCastleRegistered()) { throw new PoddClientException("Bouncy castle needed"); } final FileKeyProvider rsa = new PKCS8KeyFile(); rsa.init(pathToPublicKey.toFile(), keyExtractor); sshClient.authPublickey(username, rsa); // Session session = sshClient.startSession(); try (SFTPClient sftp = sshClient.newSFTPClient();) { for (final Path nextBag : bagsToUpload) { // Check to make sure that the bag was under the local root path final Path localPath = nextBag.toAbsolutePath(); if (!localPath.startsWith(localRootPath)) { this.log.error( "Local bag path was not a direct descendant of the local root path: {} {} {}", localRootPath, nextBag, localPath); throw new PoddClientException( "Local bag path was not a direct descendant of the local root path: " + localPath + " " + localRootPath); } // Take the local root path out to get the subpath to use on the remote final Path remoteSubPath = localPath.subpath(localRootPath.getNameCount(), nextBag.getNameCount() - 1); this.log.info("Remote sub path: {}", remoteSubPath); final Path remoteDirPath = remoteRootPath.resolve(remoteSubPath); this.log.info("Remote dir path: {}", remoteDirPath); final Path remoteBagPath = remoteDirPath.resolve(nextBag.getFileName()); this.log.info("Remote bag path: {}", remoteBagPath); boolean fileFound = false; boolean sizeCorrect = false; try { // check details of a remote bag final FileAttributes attribs = sftp.lstat(remoteBagPath.toAbsolutePath().toString()); final long localSize = Files.size(nextBag); final long remoteSize = attribs.getSize(); if (localSize <= 0) { this.log.error("Local bag was empty: {}", nextBag); sizeCorrect = false; fileFound = false; } else if (remoteSize <= 0) { this.log.warn("Remote bag was empty: {} {}", nextBag, attribs); sizeCorrect = false; fileFound = false; } else if (localSize == remoteSize) { this.log.info("Found file on remote already with same size as local: {} {}", nextBag, remoteBagPath); sizeCorrect = true; fileFound = true; } else { sizeCorrect = false; fileFound = true; // We always assume that a non-zero local file is correct // The bags contain time-stamps that will be modified when they are // regenerated, likely changing the file-size, and hopefully changing // the digest checksums // throw new PoddClientException( // "Could not automatically compare file sizes (need manual intervention to delete one) : " // + nextBag + " " + remoteBagPath + " localSize=" + localSize // + " remoteSize=" + remoteSize); } } catch (final IOException e) { // lstat() throws an IOException if the file does not exist // Ignore sizeCorrect = false; fileFound = false; } final ConcurrentMap<Algorithm, String> bagDigests = digests.get(nextBag); if (bagDigests.isEmpty()) { this.log.error("No bag digests were generated for bag: {}", nextBag); } for (final Entry<Algorithm, String> entry : bagDigests.entrySet()) { final Path localDigestPath = localPath .resolveSibling(localPath.getFileName() + entry.getKey().getExtension()); // Create the local digest file Files.copy( new ReaderInputStream(new StringReader(entry.getValue()), StandardCharsets.UTF_8), localDigestPath); final Path remoteDigestPath = remoteBagPath .resolveSibling(remoteBagPath.getFileName() + entry.getKey().getExtension()); boolean nextDigestFileFound = false; boolean nextDigestCorrect = false; try { final Path tempFile = Files.createTempFile("podd-digest-", entry.getKey().getExtension()); final SFTPFileTransfer sftpFileTransfer = new SFTPFileTransfer(sftp.getSFTPEngine()); sftpFileTransfer.download(remoteBagPath.toAbsolutePath().toString(), tempFile.toAbsolutePath().toString()); nextDigestFileFound = true; final List<String> allLines = Files.readAllLines(tempFile, StandardCharsets.UTF_8); if (allLines.isEmpty()) { nextDigestCorrect = false; } else if (allLines.size() > 1) { nextDigestCorrect = false; } // Check if the digests match exactly else if (allLines.get(0).equals(entry.getValue())) { nextDigestCorrect = true; } else { nextDigestCorrect = false; } } catch (final IOException e) { nextDigestFileFound = false; nextDigestCorrect = false; } if (nextDigestFileFound && nextDigestCorrect) { this.log.info( "Not copying digest to remote as it exists and contains the same content as the local digest"); } else if (nextDigestFileFound && !nextDigestCorrect) { this.log.error("Found remote digest but content was not correct: {} {}", localDigestPath, remoteDigestPath); sftp.rm(remoteDigestPath.toString()); this.log.info("Copying digest to remote: {}", remoteDigestPath); sftp.put(new FileSystemFile(localDigestPath.toString()), remoteDigestPath.toString()); } else if (!nextDigestFileFound) { this.log.info("About to make directories on remote: {}", remoteDirPath); sftp.mkdirs(remoteDirPath.toString()); this.log.info("Copying digest to remote: {}", remoteDigestPath); sftp.put(new FileSystemFile(localDigestPath.toString()), remoteDigestPath.toString()); } } if (fileFound && sizeCorrect) { this.log.info("Not copying bag to remote as it exists and is the same size as local bag"); } else if (fileFound && !sizeCorrect) { this.log.error("Found remote bag but size was not correct: {} {}", nextBag, remoteBagPath); sftp.rm(remoteBagPath.toString()); this.log.info("Copying bag to remote: {}", remoteBagPath); sftp.put(new FileSystemFile(localPath.toString()), remoteBagPath.toString()); } else if (!fileFound) { this.log.info("About to make directories on remote: {}", remoteDirPath); sftp.mkdirs(remoteDirPath.toString()); this.log.info("Copying bag to remote: {}", remoteBagPath); sftp.put(new FileSystemFile(localPath.toString()), remoteBagPath.toString()); } } } } catch (final IOException e) { throw new PoddClientException("Could not copy a bag to the remote location", e); } return results; }
From source file:org.apache.openaz.xacml.pdp.test.TestBase.java
/** * This processes a response. Saves the response out to disk. If there is a corresponding response file * for the request located in the "responses" sub-directory, then this method will compare that response * file with what the engine returned to see if it matched. * * @param requestFile/*from www . ja v a 2 s. com*/ * @param request * @param response * @param group * @param count * @throws Exception */ protected void processResponse(Path requestFile, Request request, Response response, String group, int count) throws Exception { // // Construct the output filename // Path responseFile = null; Path resultFile = null; int num = requestFile.getNameCount(); if (num < 2) { logger.error("Too few dir's in request filename."); throw new Exception( "Too few dir's in request filename. Format should be Request.[0-9]+.{Permit|Deny|NA|Indeterminate}.{json|xml}"); } String filename = requestFile.getFileName().toString(); if (group.equals("Generate")) { // // Using count variable, construct a filename // // i.e. Response.03.Generate.{count}.json // filename = "Response" + filename.substring(filename.indexOf('.'), filename.lastIndexOf('.')) + String.format("%03d", count) + filename.substring(filename.lastIndexOf('.')); } else { // // Construct filename // filename = "Response" + filename.substring(filename.indexOf('.')); } // // Determine equivalent response file path // responseFile = Paths.get(requestFile.subpath(0, num - 2).toString(), "responses"); if (Files.notExists(responseFile)) { // // Create it // logger.warn(responseFile.toString() + " does NOT exist, creating..."); try { Files.createDirectories(responseFile); } catch (IOException e) { logger.error(e); throw new Exception("Cannot proceed without an output directory."); } } responseFile = Paths.get(responseFile.toString(), filename); // // Determine path to write result file // if (this.output != null) { // // User specified an output path // resultFile = this.output; } else { // // Default path // resultFile = Paths.get(requestFile.subpath(0, num - 2).toString(), "results"); } // // Check if the path exists // if (Files.notExists(resultFile)) { // // Create it // logger.warn(resultFile.toString() + " does NOT exist, creating..."); try { Files.createDirectories(resultFile); } catch (IOException e) { logger.error(e); throw new Exception("Cannot proceed without an output directory."); } } // // Add the filename to the path // resultFile = Paths.get(resultFile.toString(), filename); // // Check if there is an equivalent response in the response // directory. If so, compare our response result with that one. // boolean succeeded = true; if (responseFile != null && Files.exists(responseFile)) { // // Do comparison // Response expectedResponse = null; if (TestBase.isJSON(responseFile)) { expectedResponse = JSONResponse.load(responseFile); } else if (TestBase.isXML(responseFile)) { expectedResponse = DOMResponse.load(responseFile); } if (expectedResponse != null) { // // Do the compare // if (response == null) { logger.error("NULL response returned."); this.responseNotMatches++; succeeded = false; } else { if (response.equals(expectedResponse)) { logger.info("Response matches expected response."); this.responseMatches++; } else { logger.error("Response does not match expected response."); logger.error("Expected: "); logger.error(expectedResponse.toString()); this.responseNotMatches++; succeeded = false; } } } } // // Write the response to the result file // logger.info("Request: " + requestFile.getFileName() + " response is: " + (response == null ? "null" : response.toString())); if (resultFile != null && response != null) { if (TestBase.isJSON(resultFile)) { Files.write(resultFile, JSONResponse.toString(response, true).getBytes()); } else if (TestBase.isXML(resultFile)) { Files.write(resultFile, DOMResponse.toString(response, true).getBytes()); } } // // Stats // if (group.equals("Permit")) { this.expectedPermits++; } else if (group.equals("Deny")) { this.expectedDenies++; } else if (group.equals("NA")) { this.expectedNotApplicables++; } else if (group.equals("Indeterminate")) { this.expectedIndeterminates++; } if (response != null) { for (Result result : response.getResults()) { Decision decision = result.getDecision(); if (group.equals("Generate")) { if (decision.equals(Decision.PERMIT)) { this.generatedpermits++; } else if (decision.equals(Decision.DENY)) { this.generateddenies++; } else if (decision.equals(Decision.NOTAPPLICABLE)) { this.generatednotapplicables++; } else if (decision.equals(Decision.INDETERMINATE)) { this.generatedindeterminates++; } continue; } if (decision.equals(Decision.PERMIT)) { this.permits++; if (!group.equals("Permit")) { succeeded = false; logger.error("Expected " + group + " got " + decision); } } else if (decision.equals(Decision.DENY)) { this.denies++; if (!group.equals("Deny")) { succeeded = false; logger.error("Expected " + group + " got " + decision); } } else if (decision.equals(Decision.NOTAPPLICABLE)) { this.notapplicables++; if (!group.equals("NA")) { succeeded = false; logger.error("Expected " + group + " got " + decision); } } else if (decision.equals(Decision.INDETERMINATE)) { this.indeterminates++; if (!group.equals("Indeterminate")) { succeeded = false; logger.error("Expected " + group + " got " + decision); } } } } if (succeeded) { logger.info("REQUEST SUCCEEDED"); } else { logger.info("REQUEST FAILED"); } }
From source file:org.apache.nifi.controller.repository.FileSystemRepository.java
private synchronized void initializeRepository() throws IOException { final Map<String, Path> realPathMap = new HashMap<>(); final ExecutorService executor = Executors.newFixedThreadPool(containers.size()); final List<Future<Long>> futures = new ArrayList<>(); // Run through each of the containers. For each container, create the sections if necessary. // Then, we need to scan through the archived data so that we can determine what the oldest // archived data is, so that we know when we have to start aging data off. for (final Map.Entry<String, Path> container : containers.entrySet()) { final String containerName = container.getKey(); final ContainerState containerState = containerStateMap.get(containerName); final Path containerPath = container.getValue(); final boolean pathExists = Files.exists(containerPath); final Path realPath; if (pathExists) { realPath = containerPath.toRealPath(); } else {//from w w w . j a va2 s . co m realPath = Files.createDirectories(containerPath).toRealPath(); } for (int i = 0; i < SECTIONS_PER_CONTAINER; i++) { Files.createDirectories(realPath.resolve(String.valueOf(i))); } realPathMap.put(containerName, realPath); // We need to scan the archive directories to find out the oldest timestamp so that know whether or not we // will have to delete archived data based on time threshold. Scanning all of the directories can be very // expensive because of all of the disk accesses. So we do this in multiple threads. Since containers are // often unique to a disk, we just map 1 thread to each container. final Callable<Long> scanContainer = new Callable<Long>() { @Override public Long call() throws IOException { final AtomicLong oldestDateHolder = new AtomicLong(0L); // the path already exists, so scan the path to find any files and update maxIndex to the max of // all filenames seen. Files.walkFileTree(realPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { LOG.warn("Content repository contains un-readable file or directory '" + file.getFileName() + "'. Skipping. ", exc); return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (attrs.isDirectory()) { return FileVisitResult.CONTINUE; } // Check if this is an 'archive' directory final Path relativePath = realPath.relativize(file); if (relativePath.getNameCount() > 3 && ARCHIVE_DIR_NAME.equals(relativePath.subpath(1, 2).toString())) { final long lastModifiedTime = getLastModTime(file); if (lastModifiedTime < oldestDateHolder.get()) { oldestDateHolder.set(lastModifiedTime); } containerState.incrementArchiveCount(); } return FileVisitResult.CONTINUE; } }); return oldestDateHolder.get(); } }; // If the path didn't exist to begin with, there's no archive directory, so don't bother scanning. if (pathExists) { futures.add(executor.submit(scanContainer)); } } executor.shutdown(); for (final Future<Long> future : futures) { try { final Long oldestDate = future.get(); if (oldestDate < oldestArchiveDate.get()) { oldestArchiveDate.set(oldestDate); } } catch (final ExecutionException | InterruptedException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new RuntimeException(e); } } } containers.clear(); containers.putAll(realPathMap); }
From source file:org.apache.nifi.controller.repository.FileSystemRepository.java
private void removeIncompleteContent(final String containerName, final Path containerPath, final Path fileToRemove) { if (Files.isDirectory(fileToRemove)) { final Path lastPathName = fileToRemove.subpath(1, fileToRemove.getNameCount()); final String fileName = lastPathName.toFile().getName(); if (fileName.equals(ARCHIVE_DIR_NAME)) { return; }//from w ww . j a va 2 s. c om final File[] children = fileToRemove.toFile().listFiles(); if (children != null) { for (final File child : children) { removeIncompleteContent(containerName, containerPath, child.toPath()); } } return; } final Path relativePath = containerPath.relativize(fileToRemove); final Path sectionPath = relativePath.subpath(0, 1); if (relativePath.getNameCount() < 2) { return; } final Path idPath = relativePath.subpath(1, relativePath.getNameCount()); final String id = idPath.toFile().getName(); final String sectionName = sectionPath.toFile().getName(); final ResourceClaim resourceClaim = resourceClaimManager.newResourceClaim(containerName, sectionName, id, false, false); if (resourceClaimManager.getClaimantCount(resourceClaim) == 0) { removeIncompleteContent(fileToRemove); } }