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
36 @Service( "fileLockManager#default" )
37 public class DefaultFileLockManager
38 implements FileLockManager
40 private static final ConcurrentMap<File, Lock> lockFiles = new ConcurrentHashMap<File, Lock>( 64 );
42 private boolean skipLocking = false;
44 private Logger log = LoggerFactory.getLogger( getClass() );
46 private int timeout = 0;
50 public Lock readFileLock( File file )
51 throws FileLockException, FileLockTimeoutException
55 return new Lock( file );
58 StopWatch stopWatch = new StopWatch();
59 boolean acquired = false;
60 mkdirs( file.getParentFile() );
63 Lock lock = new Lock( file, false );
71 long delta = stopWatch.getTime();
72 log.debug( "delta {}, timeout {}", delta, timeout );
73 if ( delta > timeout )
75 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
76 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
77 throw new FileLockTimeoutException();
82 lock.openLock( false, timeout > 0 );
85 catch ( IOException e )
87 throw new FileLockException( e.getMessage(), e );
89 catch ( IllegalStateException e )
91 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
96 catch ( FileNotFoundException e )
98 throw new FileLockException( e.getMessage(), e );
104 public Lock writeFileLock( File file )
105 throws FileLockException, FileLockTimeoutException
109 return new Lock( file );
112 mkdirs( file.getParentFile() );
114 StopWatch stopWatch = new StopWatch();
115 boolean acquired = false;
119 Lock lock = new Lock( file, true );
127 long delta = stopWatch.getTime();
128 log.debug( "delta {}, timeout {}", delta, timeout );
129 if ( delta > timeout )
131 log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file );
132 // we could not get the lock within the timeout period, so throw FileLockTimeoutException
133 throw new FileLockTimeoutException();
138 lock.openLock( true, timeout > 0 );
141 catch ( IOException e )
143 throw new FileLockException( e.getMessage(), e );
145 catch ( IllegalStateException e )
147 log.debug( "openLock {}:{}", e.getClass(), e.getMessage() );
152 catch ( FileNotFoundException e )
154 throw new FileLockException( e.getMessage(), e );
160 public void release( Lock lock )
161 throws FileLockException
165 log.debug( "skip releasing null" );
176 catch ( IOException e )
178 throw new FileLockException( e.getMessage(), e );
182 private boolean mkdirs( File directory )
184 if ( directory == null )
189 if ( directory.exists() )
193 if ( directory.mkdir() )
198 File canonDir = null;
201 canonDir = directory.getCanonicalFile();
203 catch ( IOException e )
208 File parentDir = canonDir.getParentFile();
209 return ( parentDir != null && ( mkdirs( parentDir ) || parentDir.exists() ) && canonDir.mkdir() );
212 public int getTimeout()
217 public void setTimeout( int timeout )
219 this.timeout = timeout;
222 public boolean isSkipLocking()
227 public void setSkipLocking( boolean skipLocking )
229 this.skipLocking = skipLocking;