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!!!
105 closeQuietly( lock.getRandomAccessFile() );
106 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
107 createNewFileQuietly( file );
109 catch ( IOException e )
111 throw new FileLockException( e.getMessage(), e );
113 catch ( IllegalStateException e )
115 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
118 Lock current = lockFiles.putIfAbsent( file, lock );
119 if ( current != null )
129 public Lock writeFileLock( File file )
130 throws FileLockException, FileLockTimeoutException
134 return new Lock( file );
137 mkdirs( file.getParentFile() );
139 StopWatch stopWatch = new StopWatch();
140 boolean acquired = false;
151 long delta = stopWatch.getTime();
152 log.debug( "delta {}, timeout {}", delta, timeout );
153 if ( delta > timeout )
155 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
156 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
157 throw new FileLockTimeoutException();
161 Lock current = lockFiles.get( file );
166 if ( current != null )
168 log.debug( "write lock file exist continue wait" );
172 lock = new Lock( file, true );
173 createNewFileQuietly( file );
174 lock.openLock( true, timeout > 0 );
177 catch ( FileNotFoundException e )
179 // can happen if an other thread has deleted the file
180 // close RandomAccessFile!!!
181 closeQuietly( lock.getRandomAccessFile() );
183 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
184 createNewFileQuietly( file );
186 catch ( IOException e )
188 throw new FileLockException( e.getMessage(), e );
190 catch ( IllegalStateException e )
192 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
196 Lock current = lockFiles.putIfAbsent( file, lock );
197 if ( current != null )
207 private void closeQuietly( RandomAccessFile randomAccessFile )
209 if ( randomAccessFile == null )
216 randomAccessFile.close();
218 catch ( IOException e )
224 private void createNewFileQuietly( File file )
228 file.createNewFile();
230 catch ( IOException e )
237 public void release( Lock lock )
238 throws FileLockException
242 log.debug( "skip releasing null" );
251 lockFiles.remove( lock.getFile() );
254 catch ( IOException e )
256 throw new FileLockException( e.getMessage(), e );
260 public void clearLockFiles()
265 private boolean mkdirs( File directory )
267 if ( directory == null )
272 if ( directory.exists() )
276 if ( directory.mkdir() )
281 File canonDir = null;
284 canonDir = directory.getCanonicalFile();
286 catch ( IOException e )
291 File parentDir = canonDir.getParentFile();
292 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
295 public int getTimeout()
300 public void setTimeout( int timeout )
302 this.timeout = timeout;
305 public boolean isSkipLocking()
310 public void setSkipLocking( boolean skipLocking )
312 this.skipLocking = skipLocking;