/*
 * Copyright 2017 Matt Sicker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.musigma.logging;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.FileLock;
import java.util.concurrent.Phaser;

/**
 *
 */
class FileTransferAction implements Runnable, CompletionHandler<Integer, Void> {
    private final Phaser phaser;
    private final ByteBuffer src;
    private final AsynchronousFileChannel dst;

    private long pos;
    private FileLock lock;

    FileTransferAction(Phaser phaser, ByteBuffer src, AsynchronousFileChannel dst, long pos) {
        this.phaser = phaser;
        this.src = src;
        this.dst = dst;
        this.pos = pos;
    }

    @Override
    public void run() {
        phaser.register();
        dst.lock(pos, src.remaining(), false, this, LockHandler.INSTANCE);
    }

    private FileTransferAction lock(FileLock lock) {
        this.lock = lock;
        return this;
    }

    private void transfer() {
        if (src.hasRemaining()) {
            dst.write(src, pos, null, this);
        } else {
            src.rewind();
            unlock();
        }
    }

    private void unlock() {
        try {
            if (lock != null && lock.isValid()) {
                lock.release();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            phaser.arriveAndDeregister();
        }
    }

    @Override
    public void completed(Integer result, Void attachment) {
        pos += result;
        transfer();
    }

    @Override
    public void failed(Throwable exc, Void attachment) {
        exc.printStackTrace();
        unlock();
    }

    private enum LockHandler implements CompletionHandler<FileLock, FileTransferAction> {
        INSTANCE;

        @Override
        public void completed(FileLock lock, FileTransferAction channel) {
            channel.lock(lock).transfer();
        }

        @Override
        public void failed(Throwable exc, FileTransferAction channel) {
            exc.printStackTrace();
            channel.unlock();
        }
    }
}

@@ -622,73 +622,8 @@
 g;%0A%0A
-import java.io.IOException;%0Aimport java.io.UncheckedIOException;%0A
 impo
@@ -744,43 +744,8 @@
 er;%0A
-import java.nio.channels.FileLock;%0A
 impo
@@ -1009,35 +1009,8 @@
 pos;
-%0A    private FileLock lock;
 %0A%0A  
@@ -1288,180 +1288,18 @@
     
-dst.lock(pos, src.remaining(), false, this, LockHandler.INSTANCE);%0A    %7D%0A%0A    private FileTransferAction lock(FileLock lock) %7B%0A        this.lock = lock;%0A        return this
+transfer()
 ;%0A  
@@ -1526,223 +1526,8 @@
 ) %7B%0A
-        try %7B%0A            if (lock != null && lock.isValid()) %7B%0A                lock.release();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new UncheckedIOException(e);%0A        %7D finally %7B%0A    
     
@@ -1556,34 +1556,24 @@
 register();%0A
-        %7D%0A
     %7D%0A%0A    @
@@ -1823,433 +1823,6 @@
  %7D%0A%0A
-    private enum LockHandler implements CompletionHandler%3CFileLock, FileTransferAction%3E %7B%0A        INSTANCE;%0A%0A        @Override%0A        public void completed(FileLock lock, FileTransferAction channel) %7B%0A            channel.lock(lock).transfer();%0A        %7D%0A%0A        @Override%0A        public void failed(Throwable exc, FileTransferAction channel) %7B%0A            exc.printStackTrace();%0A            channel.unlock();%0A        %7D%0A    %7D%0A
 %7D%0A

Remove unnecessary file locking in AsyncFileAppender