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;
36 * @author Olivier Lamy
39 @Service("fileLockManager#default")
40 public class DefaultFileLockManager
41 implements FileLockManager
43 // TODO currently we create lock for read and write!!
44 // the idea could be to store lock here with various clients read/write
45 // only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
46 private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
48 private boolean skipLocking = true;
50 private Logger log = LoggerFactory.getLogger( getClass() );
52 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();
165 Lock current = lockFiles.get( file );
170 if ( current != null )
172 log.debug( "write lock file exist continue wait" );
176 lock = new Lock( file, true );
177 createNewFileQuietly( file );
178 lock.openLock( true, timeout > 0 );
181 catch ( FileNotFoundException e )
183 // can happen if an other thread has deleted the file
184 // close RandomAccessFile!!!
187 closeQuietly( lock.getRandomAccessFile() );
189 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
190 createNewFileQuietly( file );
192 catch ( IOException e )
194 throw new FileLockException( e.getMessage(), e );
196 catch ( IllegalStateException e )
198 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
202 Lock current = lockFiles.putIfAbsent( file, lock );
203 if ( current != null )
213 private void closeQuietly( RandomAccessFile randomAccessFile )
215 if ( randomAccessFile == null )
222 randomAccessFile.close();
224 catch ( IOException e )
230 private void createNewFileQuietly( File file )
234 file.createNewFile();
236 catch ( IOException e )
243 public void release( Lock lock )
244 throws FileLockException
248 log.debug( "skip releasing null" );
257 lockFiles.remove( lock.getFile() );
260 catch ( ClosedChannelException e )
263 log.debug( "ignore ClosedChannelException: {}", e.getMessage() );
265 catch ( IOException e )
267 throw new FileLockException( e.getMessage(), e );
272 public void clearLockFiles()
277 private boolean mkdirs( File directory )
279 return directory.mkdirs();
283 public int getTimeout()
289 public void setTimeout( int timeout )
291 this.timeout = timeout;
295 public boolean isSkipLocking()
301 public void setSkipLocking( boolean skipLocking )
303 this.skipLocking = skipLocking;