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() );
78 long delta = stopWatch.getTime();
79 log.debug( "delta {}, timeout {}", delta, timeout );
80 if ( delta > timeout )
82 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
83 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
84 throw new FileLockTimeoutException();
88 lock = new Lock( file, false );
90 Lock current = lockFiles.get( file );
92 if ( current != null )
94 log.debug( "read lock file exist continue wait" );
95 // close RandomAccessFile!!!
96 RandomAccessFile raf = lock.getRandomAccessFile();
106 createNewFileQuietly( file );
107 lock.openLock( false, timeout > 0 );
110 catch ( FileNotFoundException e )
112 // can happen if an other thread has deleted the file
113 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
114 createNewFileQuietly( file );
116 catch ( IOException e )
118 throw new FileLockException( e.getMessage(), e );
120 catch ( IllegalStateException e )
122 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
125 Lock current = lockFiles.putIfAbsent( file, lock );
126 if ( current != null )
132 catch ( IOException e )
134 throw new FileLockException( e.getMessage(), e );
140 public Lock writeFileLock( File file )
141 throws FileLockException, FileLockTimeoutException
145 return new Lock( file );
148 mkdirs( file.getParentFile() );
150 StopWatch stopWatch = new StopWatch();
151 boolean acquired = false;
164 long delta = stopWatch.getTime();
165 log.debug( "delta {}, timeout {}", delta, timeout );
166 if ( delta > timeout )
168 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
169 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
170 throw new FileLockTimeoutException();
174 lock = new Lock( file, true );
176 Lock current = lockFiles.get( file );
181 if ( current != null )
183 log.debug( "write lock file exist continue wait" );
184 // close RandomAccessFile!!!
185 RandomAccessFile raf = lock.getRandomAccessFile();
193 createNewFileQuietly( file );
194 lock.openLock( true, timeout > 0 );
197 catch ( FileNotFoundException e )
199 // can happen if an other thread has deleted the file
200 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
201 createNewFileQuietly( file );
203 catch ( IOException e )
205 throw new FileLockException( e.getMessage(), e );
207 catch ( IllegalStateException e )
209 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
213 Lock current = lockFiles.putIfAbsent( file, lock );
214 if ( current != null )
224 FileNotFoundException e
229 throw new FileLockException( e.getMessage(), e );
234 private void createNewFileQuietly( File file )
238 file.createNewFile();
240 catch ( IOException e )
247 public void release( Lock lock )
248 throws FileLockException
252 log.debug( "skip releasing null" );
261 lockFiles.remove( lock.getFile() );
264 catch ( IOException e )
266 throw new FileLockException( e.getMessage(), e );
270 public void clearLockFiles()
275 private boolean mkdirs( File directory )
277 if ( directory == null )
282 if ( directory.exists() )
286 if ( directory.mkdir() )
291 File canonDir = null;
294 canonDir = directory.getCanonicalFile();
296 catch ( IOException e )
301 File parentDir = canonDir.getParentFile();
302 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
305 public int getTimeout()
310 public void setTimeout( int timeout )
312 this.timeout = timeout;
315 public boolean isSkipLocking()
320 public void setSkipLocking( boolean skipLocking )
322 this.skipLocking = skipLocking;