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.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
34 * @author Olivier Lamy
37 @Service( "fileLockManager#default" )
38 public class DefaultFileLockManager
39 implements FileLockManager
41 // TODO currently we create lock for read and write!!
42 // the idea could be to store lock here with various clients read/write
43 // only read could be a more simple lock and acquire a write lock means waiting the end of all reading threads
44 private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
46 private boolean skipLocking = true;
48 private Logger log = LoggerFactory.getLogger( getClass() );
50 private int timeout = 0;
54 public Lock readFileLock( File file )
55 throws FileLockException, FileLockTimeoutException
59 return new Lock( file );
62 StopWatch stopWatch = new StopWatch();
63 boolean acquired = false;
64 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 = new Lock( file, false );
89 Lock current = lockFiles.get( file );
91 if ( current != null )
93 log.debug( "read lock file exist continue wait" );
99 createNewFileQuietly( file );
100 lock.openLock( false, timeout > 0 );
103 catch ( FileNotFoundException e )
105 // can happen if an other thread has deleted the file
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 )
125 catch ( IOException e )
127 throw new FileLockException( e.getMessage(), e );
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;
157 long delta = stopWatch.getTime();
158 log.debug( "delta {}, timeout {}", delta, timeout );
159 if ( delta > timeout )
161 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
162 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
163 throw new FileLockTimeoutException();
167 lock = new Lock( file, true );
169 Lock current = lockFiles.get( file );
171 if ( current != null )
173 log.debug( "write lock file exist continue wait" );
179 createNewFileQuietly( file );
180 lock.openLock( true, timeout > 0 );
183 catch ( FileNotFoundException e )
185 // can happen if an other thread has deleted the file
186 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
187 createNewFileQuietly( file );
189 catch ( IOException e )
191 throw new FileLockException( e.getMessage(), e );
193 catch ( IllegalStateException e )
195 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
199 Lock current = lockFiles.putIfAbsent( file, lock );
200 if ( current != null )
210 FileNotFoundException e
215 throw new FileLockException( e.getMessage(), e );
220 private void createNewFileQuietly( File file )
224 file.createNewFile();
226 catch ( IOException e )
233 public void release( Lock lock )
234 throws FileLockException
238 log.debug( "skip releasing null" );
247 lockFiles.remove( lock.getFile() );
250 catch ( IOException e )
252 throw new FileLockException( e.getMessage(), e );
256 public void clearLockFiles()
261 private boolean mkdirs( File directory )
263 if ( directory == null )
268 if ( directory.exists() )
272 if ( directory.mkdir() )
277 File canonDir = null;
280 canonDir = directory.getCanonicalFile();
282 catch ( IOException e )
287 File parentDir = canonDir.getParentFile();
288 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
291 public int getTimeout()
296 public void setTimeout( int timeout )
298 this.timeout = timeout;
301 public boolean isSkipLocking()
306 public void setSkipLocking( boolean skipLocking )
308 this.skipLocking = skipLocking;