@Test public void testSafeCreateDirectoryMultiThreaded() throws InterruptedException { final int threadCount = 16; final AtomicInteger errorCount = new AtomicInteger(); final Path directory = folder.getRoot().toPath().resolve("this/is/the/directory/for/all/of/my/downloads"); final CountDownLatch latchStart = new CountDownLatch(threadCount); final CountDownLatch latchStop = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { new Thread(() -> { latchStart.countDown(); try { latchStart.await(10, TimeUnit.SECONDS); Path result = SW360AttachmentUtils.safeCreateDirectory(directory, true); if(!result.equals(directory)) { errorCount.incrementAndGet(); } } catch (InterruptedException | IOException e) { errorCount.incrementAndGet(); } latchStop.countDown(); }).start(); } assertThat(latchStop.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(Files.isDirectory(directory)).isTrue(); assertThat(errorCount.get()).isEqualTo(0); }
static Path safeCreateDirectory(Path path, boolean withParents) throws IOException { while (!Files.exists(path)) { try { LOGGER.info("Creating attachment download path {}.", path); return withParents ? Files.createDirectories(path) : Files.createDirectory(path); } catch (FileAlreadyExistsException e) { LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path); } } if (!Files.isDirectory(path)) { throw new FileAlreadyExistsException(path.toString()); } return path; }
SW360AttachmentUtils { static Path safeCreateDirectory(Path path, boolean withParents) throws IOException { while (!Files.exists(path)) { try { LOGGER.info("Creating attachment download path {}.", path); return withParents ? Files.createDirectories(path) : Files.createDirectory(path); } catch (FileAlreadyExistsException e) { LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path); } } if (!Files.isDirectory(path)) { throw new FileAlreadyExistsException(path.toString()); } return path; } }
SW360AttachmentUtils { static Path safeCreateDirectory(Path path, boolean withParents) throws IOException { while (!Files.exists(path)) { try { LOGGER.info("Creating attachment download path {}.", path); return withParents ? Files.createDirectories(path) : Files.createDirectory(path); } catch (FileAlreadyExistsException e) { LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path); } } if (!Files.isDirectory(path)) { throw new FileAlreadyExistsException(path.toString()); } return path; } private  SW360AttachmentUtils(); }
SW360AttachmentUtils { static Path safeCreateDirectory(Path path, boolean withParents) throws IOException { while (!Files.exists(path)) { try { LOGGER.info("Creating attachment download path {}.", path); return withParents ? Files.createDirectories(path) : Files.createDirectory(path); } catch (FileAlreadyExistsException e) { LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path); } } if (!Files.isDirectory(path)) { throw new FileAlreadyExistsException(path.toString()); } return path; } private  SW360AttachmentUtils(); static CompletableFuture<AttachmentUploadResult<T>> uploadAttachments(SW360AttachmentAwareClient<T> client, AttachmentUploadRequest<T> uploadRequest,
                      Function<? super T, Set<SW360SparseAttachment>> getAttachmentsFunc); static CompletableFuture<Optional<Path>> downloadAttachment(SW360AttachmentAwareClient<? extends T> client, T entity, SW360SparseAttachment attachment,
                       Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(SW360SparseAttachment attachment,
                                                                                 Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(String fileName, Path downloadPath); static String calculateHash(Path file, MessageDigest digest); static String calculateSha1Hash(Path file); }
SW360AttachmentUtils { static Path safeCreateDirectory(Path path, boolean withParents) throws IOException { while (!Files.exists(path)) { try { LOGGER.info("Creating attachment download path {}.", path); return withParents ? Files.createDirectories(path) : Files.createDirectory(path); } catch (FileAlreadyExistsException e) { LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path); } } if (!Files.isDirectory(path)) { throw new FileAlreadyExistsException(path.toString()); } return path; } private  SW360AttachmentUtils(); static CompletableFuture<AttachmentUploadResult<T>> uploadAttachments(SW360AttachmentAwareClient<T> client, AttachmentUploadRequest<T> uploadRequest,
                      Function<? super T, Set<SW360SparseAttachment>> getAttachmentsFunc); static CompletableFuture<Optional<Path>> downloadAttachment(SW360AttachmentAwareClient<? extends T> client, T entity, SW360SparseAttachment attachment,
                       Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(SW360SparseAttachment attachment,
                                                                                 Path downloadPath); static AttachmentDownloadProcessor defaultAttachmentDownloadProcessor(String fileName, Path downloadPath); static String calculateHash(Path file, MessageDigest digest); static String calculateSha1Hash(Path file);  }