package ru.jnano.swing;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import javax.swing.JOptionPane;
public class AppLock {
public static boolean singletonApp(String namefile) throws IOException{
final File f = new File(namefile);
if (f.exists()) f.delete();
final FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
final FileLock lock = channel.tryLock();
if(lock == null){
JOptionPane.showMessageDialog(null, "���������� ��� ��������");
channel.close();
return false;
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if(lock != null){
try {
lock.release();
channel.close();
f.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}));
return true;
}
}