Search in sources :

Example 76 with FileLock

use of java.nio.channels.FileLock in project nifi-registry by apache.

the class FileUtils method copyFile.

/**
 * Copies the given source file to the given destination file. The given destination will be overwritten if it already exists.
 *
 * @param source the file to copy
 * @param destination the file to copy to
 * @param lockInputFile if true will lock input file during copy; if false will not
 * @param lockOutputFile if true will lock output file during copy; if false will not
 * @param move if true will perform what is effectively a move operation rather than a pure copy. This allows for potentially highly efficient movement of the file but if not possible this will
 * revert to a copy then delete behavior. If false, then the file is copied and the source file is retained. If a true rename/move occurs then no lock is held during that time.
 * @param logger if failures occur, they will be logged to this logger if possible. If this logger is null, an IOException will instead be thrown, indicating the problem.
 * @return long number of bytes copied
 * @throws FileNotFoundException if the source file could not be found
 * @throws IOException if unable to read or write the underlying streams
 * @throws SecurityException if a security manager denies the needed file operations
 */
public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final boolean move, final Logger logger) throws FileNotFoundException, IOException {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileLock inLock = null;
    FileLock outLock = null;
    long fileSize = 0L;
    if (!source.canRead()) {
        throw new IOException("Must at least have read permission");
    }
    if (move && source.renameTo(destination)) {
        fileSize = destination.length();
    } else {
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(destination);
            final FileChannel in = fis.getChannel();
            final FileChannel out = fos.getChannel();
            if (lockInputFile) {
                inLock = in.tryLock(0, Long.MAX_VALUE, true);
                if (null == inLock) {
                    throw new IOException("Unable to obtain shared file lock for: " + source.getAbsolutePath());
                }
            }
            if (lockOutputFile) {
                outLock = out.tryLock(0, Long.MAX_VALUE, false);
                if (null == outLock) {
                    throw new IOException("Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
                }
            }
            long bytesWritten = 0;
            do {
                bytesWritten += out.transferFrom(in, bytesWritten, TRANSFER_CHUNK_SIZE_BYTES);
                fileSize = in.size();
            } while (bytesWritten < fileSize);
            out.force(false);
            FileUtils.closeQuietly(fos);
            FileUtils.closeQuietly(fis);
            fos = null;
            fis = null;
            if (move && !FileUtils.deleteFile(source, null, 5)) {
                if (logger == null) {
                    FileUtils.deleteFile(destination, null, 5);
                    throw new IOException("Could not remove file " + source.getAbsolutePath());
                } else {
                    logger.warn("Configured to delete source file when renaming/move not successful.  However, unable to delete file at: " + source.getAbsolutePath());
                }
            }
        } finally {
            FileUtils.releaseQuietly(inLock);
            FileUtils.releaseQuietly(outLock);
            FileUtils.closeQuietly(fos);
            FileUtils.closeQuietly(fis);
        }
    }
    return fileSize;
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 77 with FileLock

use of java.nio.channels.FileLock in project Chronicle-Bytes by OpenHFT.

the class MappedFile method acquireByteStore.

@NotNull
public <T extends MappedBytesStore> T acquireByteStore(long position, @NotNull MappedBytesStoreFactory<T> mappedBytesStoreFactory) throws IOException, IllegalArgumentException, IllegalStateException {
    if (closed.get())
        throw new IOException("Closed");
    if (position < 0)
        throw new IOException("Attempt to access a negative position: " + position);
    int chunk = (int) (position / chunkSize);
    synchronized (stores) {
        while (stores.size() <= chunk) {
            stores.add(null);
        }
        WeakReference<MappedBytesStore> mbsRef = stores.get(chunk);
        if (mbsRef != null) {
            @NotNull T mbs = (T) mbsRef.get();
            if (mbs != null && mbs.tryReserve()) {
                return mbs;
            }
        }
        long start = System.nanoTime();
        long minSize = (chunk + 1L) * chunkSize + overlapSize;
        long size = fileChannel.size();
        if (size < minSize && !readOnly) {
            // handle a possible race condition between processes.
            try {
                synchronized (GLOBAL_FILE_LOCK) {
                    size = fileChannel.size();
                    if (size < minSize) {
                        try (FileLock ignore = fileChannel.lock()) {
                            size = fileChannel.size();
                            if (size < minSize) {
                                raf.setLength(minSize);
                            }
                        }
                    }
                }
            } catch (IOException ioe) {
                throw new IOException("Failed to resize to " + minSize, ioe);
            }
        }
        long mappedSize = chunkSize + overlapSize;
        FileChannel.MapMode mode = readOnly ? FileChannel.MapMode.READ_ONLY : FileChannel.MapMode.READ_WRITE;
        long address;
        try {
            address = OS.map(fileChannel, mode, chunk * chunkSize, mappedSize);
        } catch (IOException e) {
            // sometimes on Windows it doesn't like read only, but not always or on all systems.
            if (readOnly && e.getMessage().equals("Not enough storage is available to process this command")) {
                Jvm.warn().on(getClass(), "Mapping " + file + " as READ_ONLY failed, switching to READ_WRITE");
                address = OS.map(fileChannel, FileChannel.MapMode.READ_WRITE, chunk * chunkSize, mappedSize);
                readOnly = false;
            } else {
                throw e;
            }
        }
        final long safeCapacity = this.chunkSize + overlapSize / 2;
        T mbs2 = mappedBytesStoreFactory.create(this, chunk * this.chunkSize, address, mappedSize, safeCapacity);
        stores.set(chunk, new WeakReference<>(mbs2));
        if (newChunkListener != null)
            newChunkListener.onNewChunk(file.getPath(), chunk, (System.nanoTime() - start) / 1000);
        // new Throwable("chunk "+chunk).printStackTrace();
        return mbs2;
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with FileLock

use of java.nio.channels.FileLock in project tycho by eclipse.

the class LockProcess method main.

public static void main(String[] args) throws Exception {
    File file = new File(args[0]);
    long wait = Long.valueOf(args[1]);
    RandomAccessFile raFile = new RandomAccessFile(file, "rw");
    FileLock lock = raFile.getChannel().lock(0, 1, false);
    System.out.println(LOCK_ACQUIRED_MSG);
    Thread.sleep(wait);
    lock.release();
    raFile.close();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 79 with FileLock

use of java.nio.channels.FileLock in project mule by mulesoft.

the class FreePortFinder method releasePort.

/**
 * Indicates that the port is free from the point of view of the caller.
 * <p/>
 * Checks that the port was released, if it was not, then it would be marked as in use, so no other client receives the same
 * port again.
 *
 * @param port the port number to release.
 */
public synchronized void releasePort(int port) {
    if (isPortFree(port) && locks.containsKey(port) && files.containsKey(port)) {
        FileLock lock = locks.remove(port);
        FileChannel file = files.remove(port);
        try {
            lock.release();
            file.close();
        } catch (IOException e) {
        // Ignore
        }
    } else {
        if (logger.isInfoEnabled()) {
            logger.info(format("Port %d was not correctly released", port));
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 80 with FileLock

use of java.nio.channels.FileLock in project bgpcep by opendaylight.

the class ConfigLoaderImpl method parseDefaultConfigFile.

private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename) throws IOException, XMLStreamException {
    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
    final File newFile = new File(this.path, filename);
    FileChannel channel = new RandomAccessFile(newFile, READ).getChannel();
    FileLock lock = null;
    final Stopwatch stopwatch = Stopwatch.createStarted();
    while (lock == null || stopwatch.elapsed(TimeUnit.SECONDS) > TIMEOUT_SECONDS) {
        try {
            lock = channel.tryLock();
        } catch (final IllegalStateException e) {
        // Ignore
        }
        if (lock == null) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                LOG.warn("Failed to lock xml", e);
            }
        }
    }
    try (InputStream resourceAsStream = new FileInputStream(newFile)) {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
        final SchemaNode schemaNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, config.getSchemaPath());
        try (XmlParserStream xmlParser = XmlParserStream.create(streamWriter, this.schemaContext, schemaNode)) {
            xmlParser.parse(reader);
        } catch (final URISyntaxException | XMLStreamException | IOException | ParserConfigurationException | SAXException e) {
            LOG.warn("Failed to parse xml", e);
        } finally {
            reader.close();
            channel.close();
        }
    }
    return result.getResult();
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) FileChannel(java.nio.channels.FileChannel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Stopwatch(com.google.common.base.Stopwatch) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) RandomAccessFile(java.io.RandomAccessFile) XMLStreamException(javax.xml.stream.XMLStreamException) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) FileLock(java.nio.channels.FileLock) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

FileLock (java.nio.channels.FileLock)246 IOException (java.io.IOException)127 RandomAccessFile (java.io.RandomAccessFile)99 FileChannel (java.nio.channels.FileChannel)83 File (java.io.File)77 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)37 FileOutputStream (java.io.FileOutputStream)29 Test (org.junit.Test)19 Path (java.nio.file.Path)16 FileInputStream (java.io.FileInputStream)13 FileNotFoundException (java.io.FileNotFoundException)12 ByteBuffer (java.nio.ByteBuffer)10 InputStream (java.io.InputStream)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NonWritableChannelException (java.nio.channels.NonWritableChannelException)4 NoSuchFileException (java.nio.file.NoSuchFileException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3