1 package org.apache.archiva.common.filelock;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.time.StopWatch;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.stereotype.Service;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.RandomAccessFile;
31 import java.nio.channels.ClosedChannelException;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.concurrent.ConcurrentMap;
34 import java.util.concurrent.locks.ReentrantReadWriteLock;
37 * @author Olivier Lamy
40 @Service("fileLockManager#default")
41 public class DefaultFileLockManager
42 implements FileLockManager
44 // TODO currently we create lock for read and write!!
45 // the idea could be to store lock here with various clients read/write
46 // only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
47 private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
49 private boolean skipLocking = true;
51 private Logger log = LoggerFactory.getLogger( getClass() );
53 private int timeout = 0;
56 public Lock readFileLock( File file )
57 throws FileLockException, FileLockTimeoutException
61 return new Lock( file );
64 StopWatch stopWatch = new StopWatch();
65 boolean acquired = false;
66 mkdirs( file.getParentFile() );
77 long delta = stopWatch.getTime();
78 log.debug( "delta {}, timeout {}", delta, timeout );
79 if ( delta > timeout )
81 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
82 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
83 throw new FileLockTimeoutException();
87 Lock current = lockFiles.get( file );
89 if ( current != null )
91 log.debug( "read lock file exist continue wait" );
97 lock = new Lock( file, false );
98 createNewFileQuietly( file );
99 lock.openLock( false, timeout > 0 );
102 catch ( FileNotFoundException e )
104 // can happen if an other thread has deleted the file
105 // close RandomAccessFile!!!
108 closeQuietly( lock.getRandomAccessFile() );
110 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
111 createNewFileQuietly( file );
113 catch ( IOException e )
115 throw new FileLockException( e.getMessage(), e );
117 catch ( IllegalStateException e )
119 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
122 Lock current = lockFiles.putIfAbsent( file, lock );
123 if ( current != null )
133 public Lock writeFileLock( File file )
134 throws FileLockException, FileLockTimeoutException
138 return new Lock( file );
141 mkdirs( file.getParentFile() );
143 StopWatch stopWatch = new StopWatch();
144 boolean acquired = false;
155 long delta = stopWatch.getTime();
156 log.debug( "delta {}, timeout {}", delta, timeout );
157 if ( delta > timeout )
159 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
160 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
161 throw new FileLockTimeoutException();
167 Lock current = lockFiles.get( file );
168 if ( current != null )
170 log.debug( "write lock file exist continue wait" );
174 lock = new Lock(file, true);
175 createNewFileQuietly(file);
176 lock.openLock(true, timeout > 0);
179 catch ( FileNotFoundException e )
181 // can happen if an other thread has deleted the file
182 // close RandomAccessFile!!!
185 closeQuietly( lock.getRandomAccessFile() );
187 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
188 createNewFileQuietly( file );
190 catch ( IOException e )
192 if (lock!=null && lock.isValid()) {
195 } catch (IOException ex) {
199 throw new FileLockException( e.getMessage(), e );
201 catch ( Throwable e )
203 if (lock!=null && lock.isValid()) {
206 } catch (IOException ex) {
212 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
217 Lock current = lockFiles.putIfAbsent( file, lock );
218 if ( current != null )
228 private void closeQuietly( RandomAccessFile randomAccessFile )
230 if ( randomAccessFile == null )
237 randomAccessFile.close();
239 catch ( IOException e )
245 private void createNewFileQuietly( File file )
249 file.createNewFile();
251 catch ( IOException e )
258 public void release( Lock lock )
259 throws FileLockException
263 log.debug( "skip releasing null" );
272 lockFiles.remove( lock.getFile() );
275 catch ( ClosedChannelException e )
278 log.debug( "ignore ClosedChannelException: {}", e.getMessage() );
280 catch ( IOException e )
282 throw new FileLockException( e.getMessage(), e );
287 public void clearLockFiles()
292 private boolean mkdirs( File directory )
294 return directory.mkdirs();
298 public int getTimeout()
304 public void setTimeout( int timeout )
306 this.timeout = timeout;
310 public boolean isSkipLocking()
316 public void setSkipLocking( boolean skipLocking )
318 this.skipLocking = skipLocking;