44import org .slf4j .Logger ;
55import org .slf4j .LoggerFactory ;
66
7- import java .io .*;
7+ import java .io .File ;
8+ import java .io .IOException ;
89import java .nio .ByteBuffer ;
910import java .nio .channels .FileChannel ;
1011import java .nio .channels .FileLock ;
1112import java .nio .channels .OverlappingFileLockException ;
1213import java .nio .file .Files ;
1314import java .nio .file .StandardOpenOption ;
15+ import java .nio .file .attribute .FileAttribute ;
1416import java .nio .file .attribute .PosixFilePermission ;
1517import java .nio .file .attribute .PosixFilePermissions ;
1618import java .util .Arrays ;
1719import java .util .Date ;
1820import java .util .HashSet ;
21+ import java .util .Set ;
1922
2023import static java .nio .file .StandardOpenOption .*;
2124import static net .openhft .affinity .impl .VanillaCpuLayout .MAX_CPUS_SUPPORTED ;
2225
23- /**
24- * @author Tom Shercliff
25- */
2626public class FileLockBasedLockChecker extends FileBasedLockChecker {
2727
2828 private static final Logger LOGGER = LoggerFactory .getLogger (FileLockBasedLockChecker .class );
2929 private static final String OS = System .getProperty ("os.name" ).toLowerCase ();
3030
3131 private static final LockChecker instance = new FileLockBasedLockChecker ();
32+ private static final HashSet <StandardOpenOption > openOptions = new HashSet <>(Arrays .asList (CREATE_NEW , WRITE , READ , SYNC ));
33+ private static final FileAttribute <Set <PosixFilePermission >> fileAttr = PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rw-rw-rw-" ));
34+
3235 public static LockChecker getInstance () {
3336 return instance ;
3437 }
@@ -46,13 +49,13 @@ public boolean isLockFree(int id) {
4649
4750 private boolean isLockFree (File file , int id ) {
4851 //if no file exists - nobody has the lock for sure
49- if (!file .exists ()) {
52+ if (!file .exists ()) {
5053 return true ;
5154 }
5255
5356 //do we have the lock already?
5457 LockReference existingLock = locks [id ];
55- if (existingLock != null ) {
58+ if (existingLock != null ) {
5659 return false ;
5760 }
5861
@@ -77,16 +80,14 @@ private boolean isLockFree(File file, int id) {
7780 @ Override
7881 public boolean obtainLock (int id , String metaInfo ) throws IOException {
7982 final File file = toFile (id );
80- if (!isLockFree (file , id )) {
83+ if (!isLockFree (file , id )) {
8184 return false ;
8285 }
8386
84- FileChannel fc = FileChannel .open (file .toPath (),
85- new HashSet <>(Arrays .asList (CREATE_NEW , WRITE , READ , SYNC )),
86- PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rw-rw-rw-" )));
87+ FileChannel fc = FileChannel .open (file .toPath (), openOptions , fileAttr );
8788 FileLock fl = fc .tryLock ();
8889
89- if (fl == null ) {
90+ if (fl == null ) {
9091 LOGGER .error (String .format ("Could not obtain lock on file %s%n" , file .getAbsolutePath ()));
9192 return false ;
9293 } else {
@@ -95,7 +96,7 @@ public boolean obtainLock(int id, String metaInfo) throws IOException {
9596
9697 byte [] content = String .format ("%s%n%s" , metaInfo , df .format (new Date ())).getBytes ();
9798 ByteBuffer buffer = ByteBuffer .wrap (content );
98- while (buffer .hasRemaining ()) {
99+ while (buffer .hasRemaining ()) {
99100 fc .write (buffer );
100101 }
101102 return true ;
@@ -105,7 +106,7 @@ public boolean obtainLock(int id, String metaInfo) throws IOException {
105106 @ Override
106107 public boolean releaseLock (int id ) {
107108 LockReference lock = locks [id ];
108- if (lock == null ) {
109+ if (lock == null ) {
109110 LOGGER .error (String .format ("Cannot release lock for id %d as don't have it!" , id ));
110111 return false ;
111112 }
@@ -125,20 +126,20 @@ public boolean releaseLock(int id) {
125126 @ Override
126127 public String getMetaInfo (int id ) throws IOException {
127128 final File file = toFile (id );
128- if (isLockFree (file , id )) {
129+ if (isLockFree (file , id )) {
129130 LOGGER .warn ("Cannot obtain lock on lock file {}" , file .getAbsolutePath ());
130131 return null ;
131132 }
132133
133134 LockReference lr = locks [id ];
134- if (lr == null ) {
135+ if (lr == null ) {
135136 return null ;
136137 }
137138 FileChannel fc = lr .channel ;
138139 ByteBuffer buffer = ByteBuffer .allocate (64 );
139140 int len = fc .read (buffer , 0 );
140141 String content = new String (buffer .array (), 0 , len );
141- if (content == null || content .isEmpty ()) {
142+ if (content .isEmpty ()) {
142143 LOGGER .warn ("Empty lock file {}" , file .getAbsolutePath ());
143144 return null ;
144145 }
@@ -150,7 +151,7 @@ public String getMetaInfo(int id) throws IOException {
150151 protected File toFile (int id ) {
151152 File file = super .toFile (id );
152153 try {
153- if (file .exists () && OS .startsWith ("linux" )) {
154+ if (file .exists () && OS .startsWith ("linux" )) {
154155 Files .setPosixFilePermissions (file .toPath (), PosixFilePermissions .fromString ("rwxrwxrwx" ));
155156 }
156157 } catch (IOException e ) {
0 commit comments