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.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.ConcurrentMap;
35 * @author Olivier Lamy
38 @Service( "fileLockManager#default" )
39 public class DefaultFileLockManager
40 implements FileLockManager
42 // TODO currently we create lock for read and write!!
43 // the idea could be to store lock here with various clients read/write
44 // only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
45 private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
47 private boolean skipLocking = true;
49 private Logger log = LoggerFactory.getLogger( getClass() );
51 private int timeout = 0;
55 public Lock readFileLock( File file )
56 throws FileLockException, FileLockTimeoutException
60 return new Lock( file );
63 StopWatch stopWatch = new StopWatch();
64 boolean acquired = false;
65 mkdirs( file.getParentFile() );
76 long delta = stopWatch.getTime();
77 log.debug( "delta {}, timeout {}", delta, timeout );
78 if ( delta > timeout )
80 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
81 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
82 throw new FileLockTimeoutException();
86 Lock current = lockFiles.get( file );
88 if ( current != null )
90 log.debug( "read lock file exist continue wait" );
96 lock = new Lock( file, false );
97 createNewFileQuietly( file );
98 lock.openLock( false, timeout > 0 );
101 catch ( FileNotFoundException e )
103 // can happen if an other thread has deleted the file
104 // close RandomAccessFile!!!
107 closeQuietly( lock.getRandomAccessFile() );
109 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
110 createNewFileQuietly( file );
112 catch ( IOException e )
114 throw new FileLockException( e.getMessage(), e );
116 catch ( IllegalStateException e )
118 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
121 Lock current = lockFiles.putIfAbsent( file, lock );
122 if ( current != null )
132 public Lock writeFileLock( File file )
133 throws FileLockException, FileLockTimeoutException
137 return new Lock( file );
140 mkdirs( file.getParentFile() );
142 StopWatch stopWatch = new StopWatch();
143 boolean acquired = false;
154 long delta = stopWatch.getTime();
155 log.debug( "delta {}, timeout {}", delta, timeout );
156 if ( delta > timeout )
158 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
159 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
160 throw new FileLockTimeoutException();
164 Lock current = lockFiles.get( file );
169 if ( current != null )
171 log.debug( "write lock file exist continue wait" );
175 lock = new Lock( file, true );
176 createNewFileQuietly( file );
177 lock.openLock( true, timeout > 0 );
180 catch ( FileNotFoundException e )
182 // can happen if an other thread has deleted the file
183 // close RandomAccessFile!!!
186 closeQuietly( lock.getRandomAccessFile() );
188 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
189 createNewFileQuietly( file );
191 catch ( IOException e )
193 throw new FileLockException( e.getMessage(), e );
195 catch ( IllegalStateException e )
197 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
201 Lock current = lockFiles.putIfAbsent( file, lock );
202 if ( current != null )
212 private void closeQuietly( RandomAccessFile randomAccessFile )
214 if ( randomAccessFile == null )
221 randomAccessFile.close();
223 catch ( IOException e )
229 private void createNewFileQuietly( File file )
233 file.createNewFile();
235 catch ( IOException e )
242 public void release( Lock lock )
243 throws FileLockException
247 log.debug( "skip releasing null" );
256 lockFiles.remove( lock.getFile() );
259 catch ( IOException e )
261 throw new FileLockException( e.getMessage(), e );
265 public void clearLockFiles()
270 private boolean mkdirs( File directory )
272 if ( directory == null )
277 if ( directory.exists() )
281 if ( directory.mkdir() )
286 File canonDir = null;
289 canonDir = directory.getCanonicalFile();
291 catch ( IOException e )
296 File parentDir = canonDir.getParentFile();
297 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
300 public int getTimeout()
305 public void setTimeout( int timeout )
307 this.timeout = timeout;
310 public boolean isSkipLocking()
315 public void setSkipLocking( boolean skipLocking )
317 this.skipLocking = skipLocking;