This project has retired. For details please refer to its Attic page.
Lock xref
View Javadoc
1   package org.apache.archiva.common.filelock;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Olivier Lamy
36   * @since 2.0.0
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             // ignore
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 }