/*
 * Copyright 2008-2017 by Emeric Vernat
 *
 *     This file is part of Java Melody.
 *
 * 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 net.bull.javamelody.internal.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

import net.bull.javamelody.internal.common.Parameters;

/**
 * Lock sur le répertoire de stockage pour détecter si des instances distinctes écrivent dans le même répertoire.
 * @author Emeric Vernat
 */
class StorageLock {
	private static final String LOCK_FILENAME = "javamelody.lock";
	private final RandomAccessFile input;
	private final FileChannel fileChannel;
	private FileLock fileLock;

	StorageLock(String application) {
		super();
		final File storageDir = Parameters.getStorageDirectory(application);
		if (!storageDir.mkdirs() && !storageDir.exists()) {
			throw new IllegalStateException(
					"JavaMelody directory can't be created: " + storageDir.getPath());
		}
		final File lockFile = new File(storageDir, LOCK_FILENAME);
		try {
			this.input = new RandomAccessFile(lockFile, "rw");
			this.fileChannel = input.getChannel();
		} catch (final FileNotFoundException e) {
			throw new IllegalArgumentException(e);
		}
		// initialize the lock
		getFileLock();
	}

	void release() throws IOException {
		try {
			if (fileLock != null) {
				fileLock.release();
			}
		} finally {
			try {
				fileChannel.close();
			} finally {
				input.close();
			}
		}
	}

	private FileLock getFileLock() {
		if (fileLock == null) {
			try {
				fileLock = fileChannel.tryLock();
			} catch (final IOException e) {
				return null;
			} catch (final OverlappingFileLockException e) {
				return null;
			}
		}
		return fileLock;
	}

	boolean isAcquired() {
		return getFileLock() != null;
	}
}

@@ -712,46 +712,8 @@
 le;%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -1200,16 +1200,40 @@
 e final 
+File lockFile;%0A%09private 
 RandomAc
@@ -1253,30 +1253,24 @@
 t;%0A%09private 
-final 
 FileChannel 
@@ -1434,426 +1434,60 @@
 ;%0A%09%09
-if (!storageDir.mkdirs() && !storageDir.exists()) %7B%0A%09%09%09throw new IllegalStateException(%0A%09%09%09%09%09%22JavaMelody directory can't be created: %22 + storageDir.getPath());%0A%09%09%7D%0A%09%09final File lockFile = new File(storageDir, LOCK_FILENAME);%0A%09%09try %7B%0A%09%09%09this.input = new RandomAccessFile(lockFile, %22rw%22);%0A%09%09%09this.fileChannel = input.getChannel();%0A%09%09%7D catch (final FileNotFoundException e) %7B%0A%09%09%09throw new IllegalArgumentException(e);%0A%09%09%7D
+this.lockFile = new File(storageDir, LOCK_FILENAME);
 %0A%09%09/
@@ -1645,32 +1645,64 @@
 ally %7B%0A%09%09%09try %7B%0A
+%09%09%09%09if (fileChannel != null) %7B%0A%09
 %09%09%09%09fileChannel.
@@ -1706,24 +1706,30 @@
 el.close();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D finally
@@ -1727,24 +1727,50 @@
 %7D finally %7B%0A
+%09%09%09%09if (input != null) %7B%0A%09
 %09%09%09%09input.cl
@@ -1772,24 +1772,30 @@
 ut.close();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%7D%0A
@@ -1860,24 +1860,208 @@
  %7B%0A%09%09%09try %7B%0A
+%09%09%09%09if (input == null %7C%7C fileChannel == null) %7B%0A%09%09%09%09%09lockFile.getParentFile().mkdirs();%0A%09%09%09%09%09input = new RandomAccessFile(lockFile, %22rw%22);%0A%09%09%09%09%09fileChannel = input.getChannel();%0A%09%09%09%09%7D%0A
 %09%09%09%09fileLock

fix exception in tests