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() );
74 // Make sure that not a bad lock is returned, if a exception was thrown.
79 long delta = stopWatch.getTime();
80 log.debug( "delta {}, timeout {}", delta, timeout );
81 if ( delta > timeout )
83 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
84 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
85 throw new FileLockTimeoutException();
89 Lock current = lockFiles.get( file );
91 if ( current != null )
93 log.trace( "read lock file exist continue wait" );
99 lock = new Lock( file, false );
100 createNewFileQuietly( file );
101 lock.openLock( false, timeout > 0 );
102 // We are not returning an existing lock. If the lock is not
103 // exclusive, another thread may release the lock and the client
104 // knows nothing about it.
105 // The only atomic operation is the putIfAbsent operation, so if
106 // this returns null everything is OK, otherwise we should start at
108 current = lockFiles.putIfAbsent( file, lock );
109 if ( current == null )
119 catch ( FileNotFoundException e )
121 // can happen if an other thread has deleted the file
122 // close RandomAccessFile!!!
125 closeQuietly( lock.getRandomAccessFile() );
127 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
128 createNewFileQuietly( file );
130 catch ( IOException e )
132 throw new FileLockException( e.getMessage(), e );
134 catch ( IllegalStateException e )
136 log.trace( "openLock {}:{}", e.getClass(), e.getMessage() );
146 public Lock writeFileLock( File file )
147 throws FileLockException, FileLockTimeoutException
151 return new Lock( file );
154 mkdirs( file.getParentFile() );
156 StopWatch stopWatch = new StopWatch();
157 boolean acquired = false;
165 // Make sure that not a bad lock is returned, if a exception was thrown.
169 long delta = stopWatch.getTime();
170 log.debug( "delta {}, timeout {}", delta, timeout );
171 if ( delta > timeout )
173 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
174 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
175 throw new FileLockTimeoutException();
179 Lock current = lockFiles.get( file );
184 if ( current != null )
186 log.trace( "write lock file exist continue wait" );
190 lock = new Lock( file, true );
191 createNewFileQuietly( file );
192 lock.openLock( true, timeout > 0 );
193 // We are not returning an existing lock. If the lock is not
194 // exclusive, another thread may release the lock and the client
195 // knows nothing about it.
196 // The only atomic operation is the putIfAbsent operation, so if
197 // this returns null everything is OK, otherwise we should start at
199 current = lockFiles.putIfAbsent( file, lock );
200 if ( current == null )
210 catch ( FileNotFoundException e )
212 // can happen if an other thread has deleted the file
213 // close RandomAccessFile!!!
216 closeQuietly( lock.getRandomAccessFile() );
218 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
219 createNewFileQuietly( file );
221 catch ( IOException e )
223 throw new FileLockException( e.getMessage(), e );
225 catch ( IllegalStateException e )
227 log.trace( "openLock {}:{}", e.getClass(), e.getMessage() );
236 private void closeQuietly( RandomAccessFile randomAccessFile )
238 if ( randomAccessFile == null )
245 randomAccessFile.close();
247 catch ( IOException e )
253 private void createNewFileQuietly( File file )
257 file.createNewFile();
259 catch ( IOException e )
266 public void release( Lock lock )
267 throws FileLockException
271 log.debug( "skip releasing null" );
280 lockFiles.remove( lock.getFile() );
283 catch ( ClosedChannelException e )
286 log.debug( "ignore ClosedChannelException: {}", e.getMessage() );
288 catch ( IOException e )
290 throw new FileLockException( e.getMessage(), e );
295 public void clearLockFiles()
300 private boolean mkdirs( File directory )
302 return directory.mkdirs();
306 public int getTimeout()
312 public void setTimeout( int timeout )
314 this.timeout = timeout;
318 public boolean isSkipLocking()
324 public void setSkipLocking( boolean skipLocking )
326 this.skipLocking = skipLocking;