This project has retired. For details please refer to its
Attic page.
Lock xref
1 package org.apache.archiva.common.filelock;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Closeable;
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.channels.FileChannel;
28 import java.nio.channels.FileLock;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import java.util.concurrent.atomic.AtomicInteger;
33
34
35
36
37
38 public class Lock
39 {
40 private File file;
41
42 private AtomicBoolean write;
43
44 private final Map<Thread, AtomicInteger> fileClients = new HashMap<>();
45
46 private FileLock fileLock;
47
48 private RandomAccessFile randomAccessFile;
49
50 private FileChannel fileChannel;
51
52 public Lock( File file )
53 {
54 this.file = file;
55 }
56
57 public Lock( File file, boolean write )
58 throws FileNotFoundException
59 {
60 this.file = file;
61 this.write = new AtomicBoolean( write );
62 randomAccessFile = new RandomAccessFile( file, write ? "rw" : "r" );
63 fileChannel = randomAccessFile.getChannel();
64 }
65
66 public File getFile()
67 {
68 return file;
69 }
70
71 public AtomicBoolean isWrite()
72 {
73 return write;
74 }
75
76 public void setFile( File file )
77 {
78 this.file = file;
79 }
80
81 public void setWrite( boolean write )
82 {
83 this.write.set( write );
84 }
85
86 public boolean isShared()
87 {
88 return this.fileLock.isValid() && this.fileLock.isShared();
89 }
90
91 public boolean isValid()
92 {
93 return this.fileLock!=null && this.fileLock.isValid();
94 }
95
96 public Map<Thread, AtomicInteger> getFileClients()
97 {
98 return fileClients;
99 }
100
101 public void addFileClient( Thread thread )
102 {
103 this.fileClients.put( thread, new AtomicInteger( 1 ) );
104 }
105
106 public boolean removeFileClient( Thread thread )
107 {
108 return this.fileClients.remove( thread ) != null;
109 }
110
111 protected void close()
112 throws IOException
113 {
114 IOException ioException = null;
115 try
116 {
117 this.fileLock.release();
118 }
119 catch ( IOException e )
120 {
121 ioException = e;
122 }
123
124 closeQuietly( fileChannel );
125 closeQuietly( randomAccessFile );
126
127 fileClients.remove( Thread.currentThread() );
128
129 if ( ioException != null )
130 {
131 throw ioException;
132 }
133
134 }
135
136 protected void openLock( boolean write, boolean timeout )
137 throws IOException
138 {
139 fileClients.put( Thread.currentThread(), new AtomicInteger( 1 ) );
140
141 this.fileLock = timeout
142 ? fileChannel.tryLock( 0L, Long.MAX_VALUE, write ? false : true )
143 : fileChannel.lock( 0L, Long.MAX_VALUE, write ? false : true );
144
145 }
146
147 protected RandomAccessFile getRandomAccessFile()
148 {
149 return randomAccessFile;
150 }
151
152 private void closeQuietly( Closeable closeable )
153 {
154 try
155 {
156 closeable.close();
157 }
158 catch ( IOException e )
159 {
160
161 }
162 }
163
164
165 @Override
166 public String toString()
167 {
168 final StringBuilder sb = new StringBuilder( "Lock{" );
169 sb.append( "file=" ).append( file );
170 sb.append( '}' );
171 return sb.toString();
172 }
173 }