Search in sources :

Example 46 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project archiva by apache.

the class DuplicateArtifactsConsumerTest method testConsumerArtifactFileNotExist.

@Test
public void testConsumerArtifactFileNotExist() throws Exception {
    consumer.beginScan(config, new Date());
    try {
        consumer.processFile("com/example/test/test-artifact/2.0/test-artifact-2.0.jar");
        fail("Should have failed to find file");
    } catch (ConsumerException e) {
        assertTrue(e.getCause() instanceof NoSuchFileException);
    } finally {
        consumer.completeScan();
    }
    verify(metadataRepository, never()).addMetadataFacet(eq(TEST_REPO), Matchers.<MetadataFacet>anyObject());
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) ConsumerException(org.apache.archiva.consumers.ConsumerException) Date(java.util.Date) Test(org.junit.Test)

Example 47 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project archiva by apache.

the class DefaultFileLockManager method writeFileLock.

@Override
public Lock writeFileLock(Path file) throws FileLockException, FileLockTimeoutException {
    if (skipLocking) {
        return new Lock(file);
    }
    try {
        mkdirs(file.getParent());
    } catch (IOException e) {
        throw new FileLockException("Could not create directory " + file.getParent(), e);
    }
    StopWatch stopWatch = new StopWatch();
    boolean acquired = false;
    Lock lock = null;
    stopWatch.start();
    while (!acquired) {
        // Make sure that not a bad lock is returned, if a exception was thrown.
        lock = null;
        if (timeout > 0) {
            long delta = stopWatch.getTime();
            log.debug("delta {}, timeout {}", delta, timeout);
            if (delta > timeout) {
                log.warn("Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file);
                // we could not get the lock within the timeout period, so throw FileLockTimeoutException
                throw new FileLockTimeoutException();
            }
        }
        Lock current = lockFiles.get(file);
        try {
            if (current != null) {
                log.trace("write lock file exist continue wait");
                continue;
            }
            lock = new Lock(file, true);
            createNewFileQuietly(file);
            lock.openLock(true, timeout > 0);
            // We are not returning an existing lock. If the lock is not
            // exclusive, another thread may release the lock and the client
            // knows nothing about it.
            // The only atomic operation is the putIfAbsent operation, so if
            // this returns null everything is OK, otherwise we should start at
            // the beginning.
            current = lockFiles.putIfAbsent(file, lock);
            if (current == null) {
                // Success
                acquired = true;
            } else {
                // We try again
                lock.close();
                lock = null;
            }
        } catch (FileNotFoundException | NoSuchFileException e) {
            log.debug("write Lock skip: {} try to create file", e.getMessage());
            createNewFileQuietly(file);
        } catch (IOException e) {
            throw new FileLockException(e.getMessage(), e);
        } catch (IllegalStateException e) {
            log.trace("openLock {}:{}", e.getClass(), e.getMessage());
        }
    }
    return lock;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 48 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project archiva by apache.

the class DefaultFileLockManager method readFileLock.

@Override
public Lock readFileLock(Path file) throws FileLockException, FileLockTimeoutException {
    if (skipLocking) {
        return new Lock(file);
    }
    StopWatch stopWatch = new StopWatch();
    boolean acquired = false;
    try {
        mkdirs(file.getParent());
    } catch (IOException e) {
        throw new FileLockException("Could not create directories " + file.getParent(), e);
    }
    Lock lock = null;
    stopWatch.start();
    while (!acquired) {
        // Make sure that not a bad lock is returned, if a exception was thrown.
        lock = null;
        if (timeout > 0) {
            long delta = stopWatch.getTime();
            log.debug("delta {}, timeout {}", delta, timeout);
            if (delta > timeout) {
                log.warn("Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file);
                // we could not get the lock within the timeout period, so  throw  FileLockTimeoutException
                throw new FileLockTimeoutException();
            }
        }
        Lock current = lockFiles.get(file);
        if (current != null) {
            log.trace("read lock file exist continue wait");
            continue;
        }
        try {
            lock = new Lock(file, false);
            createNewFileQuietly(file);
            lock.openLock(false, timeout > 0);
            // We are not returning an existing lock. If the lock is not
            // exclusive, another thread may release the lock and the client
            // knows nothing about it.
            // The only atomic operation is the putIfAbsent operation, so if
            // this returns null everything is OK, otherwise we should start at
            // the beginning.
            current = lockFiles.putIfAbsent(file, lock);
            if (current == null) {
                // Success
                acquired = true;
            } else {
                // We try again
                lock.close();
                lock = null;
            }
        } catch (FileNotFoundException | NoSuchFileException e) {
            log.debug("read Lock skip: {} try to create file", e.getMessage());
            createNewFileQuietly(file);
        } catch (IOException e) {
            throw new FileLockException(e.getMessage(), e);
        } catch (IllegalStateException e) {
            log.trace("openLock {}:{}", e.getClass(), e.getMessage());
        }
    }
    return lock;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 49 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project Bytecoder by mirkosertic.

the class ModulePath method scan.

/**
 * Scan the given module path entry. If the entry is a directory then it is
 * a directory of modules or an exploded module. If the entry is a regular
 * file then it is assumed to be a packaged module.
 *
 * @throws FindException if an error occurs scanning the entry
 */
private Map<String, ModuleReference> scan(Path entry) {
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(entry, BasicFileAttributes.class);
    } catch (NoSuchFileException e) {
        return Collections.emptyMap();
    } catch (IOException ioe) {
        throw new FindException(ioe);
    }
    try {
        if (attrs.isDirectory()) {
            Path mi = entry.resolve(MODULE_INFO);
            if (!Files.exists(mi)) {
                // assume a directory of modules
                return scanDirectory(entry);
            }
        }
        // packaged or exploded module
        ModuleReference mref = readModule(entry, attrs);
        if (mref != null) {
            String name = mref.descriptor().name();
            return Collections.singletonMap(name, mref);
        }
        // not recognized
        String msg;
        if (!isLinkPhase && entry.toString().endsWith(".jmod")) {
            msg = "JMOD format not supported at execution time";
        } else {
            msg = "Module format not recognized";
        }
        throw new FindException(msg + ": " + entry);
    } catch (IOException ioe) {
        throw new FindException(ioe);
    }
}
Also used : Path(java.nio.file.Path) FindException(java.lang.module.FindException) ModuleReference(java.lang.module.ModuleReference) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 50 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project Bytecoder by mirkosertic.

the class JrtFileSystem method checkNode.

Node checkNode(JrtPath path) throws IOException {
    ensureOpen();
    String p = path.getResolvedPath();
    Node node = lookup(p);
    if (node == null) {
        node = lookupSymbolic(p);
        if (node == null) {
            throw new NoSuchFileException(p);
        }
    }
    return node;
}
Also used : Node(jdk.internal.jimage.ImageReader.Node) NoSuchFileException(java.nio.file.NoSuchFileException)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11