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() );
68 Lock lock = new Lock( file, false );
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 current = lockFiles.get( file );
89 if ( current != null )
91 log.debug( "read lock file exist continue wait" );
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 log.debug( "read Lock skip: {} try to create file", e.getMessage() );
105 createNewFileQuietly( file );
107 catch ( IOException e )
109 throw new FileLockException( e.getMessage(), e );
111 catch ( IllegalStateException e )
113 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
116 Lock current = lockFiles.putIfAbsent( file, lock );
117 if ( current != null )
123 catch ( IOException e )
125 throw new FileLockException( e.getMessage(), e );
131 public Lock writeFileLock( File file )
132 throws FileLockException, FileLockTimeoutException
136 return new Lock( file );
139 mkdirs( file.getParentFile() );
141 StopWatch stopWatch = new StopWatch();
142 boolean acquired = false;
146 Lock lock = new Lock( file, true );
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 );
166 if ( current != null )
168 log.debug( "write lock file exist continue wait" );
174 createNewFileQuietly( file );
175 lock.openLock( true, timeout > 0 );
178 catch ( FileNotFoundException e )
180 // can happen if an other thread has deleted the file
181 log.debug( "write Lock skip: {} try to create file", e.getMessage() );
182 createNewFileQuietly( file );
184 catch ( IOException e )
186 throw new FileLockException( e.getMessage(), e );
188 catch ( IllegalStateException e )
190 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
194 Lock current = lockFiles.putIfAbsent( file, lock );
195 if ( current != null )
205 FileNotFoundException e
210 throw new FileLockException( e.getMessage(), e );
215 private void createNewFileQuietly( File file )
219 file.createNewFile();
221 catch ( IOException e )
228 public void release( Lock lock )
229 throws FileLockException
233 log.debug( "skip releasing null" );
242 lockFiles.remove( lock.getFile() );
245 catch ( IOException e )
247 throw new FileLockException( e.getMessage(), e );
251 public void clearLockFiles()
256 private boolean mkdirs( File directory )
258 if ( directory == null )
263 if ( directory.exists() )
267 if ( directory.mkdir() )
272 File canonDir = null;
275 canonDir = directory.getCanonicalFile();
277 catch ( IOException e )
282 File parentDir = canonDir.getParentFile();
283 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
286 public int getTimeout()
291 public void setTimeout( int timeout )
293 this.timeout = timeout;
296 public boolean isSkipLocking()
301 public void setSkipLocking( boolean skipLocking )
303 this.skipLocking = skipLocking;