summaryrefslogtreecommitdiffstats
path: root/archiva-modules/archiva-base/archiva-proxy/src
diff options
context:
space:
mode:
authorOlivier Lamy <olamy@apache.org>2013-12-13 04:46:36 +0000
committerOlivier Lamy <olamy@apache.org>2013-12-13 04:46:36 +0000
commit06cb9ea4f943146390c6942b813d2cdb11bfadfe (patch)
tree2f5ae62d15b3994f05101b66bd48250ca0011db2 /archiva-modules/archiva-base/archiva-proxy/src
parentc3ba717d4cd6aa3c3825fb845a4650ca0d67f37e (diff)
downloadarchiva-06cb9ea4f943146390c6942b813d2cdb11bfadfe.tar.gz
archiva-06cb9ea4f943146390c6942b813d2cdb11bfadfe.zip
[MRM-1702] use the fileLockLManager in the code with possible race condition
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1550636 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules/archiva-base/archiva-proxy/src')
-rw-r--r--archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java87
1 files changed, 54 insertions, 33 deletions
diff --git a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java
index 72eb22e8e..278c8f76f 100644
--- a/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java
+++ b/archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/archiva/proxy/DefaultRepositoryProxyConnectors.java
@@ -25,6 +25,10 @@ import org.apache.archiva.admin.model.beans.NetworkProxy;
import org.apache.archiva.admin.model.beans.ProxyConnectorRuleType;
import org.apache.archiva.admin.model.beans.RemoteRepository;
import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
+import org.apache.archiva.common.filelock.FileLockException;
+import org.apache.archiva.common.filelock.FileLockManager;
+import org.apache.archiva.common.filelock.FileLockTimeoutException;
+import org.apache.archiva.common.filelock.Lock;
import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.configuration.Configuration;
import org.apache.archiva.configuration.ConfigurationNames;
@@ -144,6 +148,10 @@ public class DefaultRepositoryProxyConnectors
@Inject
private NetworkProxyAdmin networkProxyAdmin;
+ @Inject
+ @Named( value = "fileLockManager#default" )
+ private FileLockManager fileLockManager;
+
@PostConstruct
public void initialize()
{
@@ -431,8 +439,8 @@ public class DefaultRepositoryProxyConnectors
catch ( RepositoryAdminException e )
{
log.debug( MarkerFactory.getDetachedMarker( "transfer.error" ),
- "Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}",targetRepository.getRepository().getId(), path,
- e.getMessage(), e );
+ "Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}",
+ targetRepository.getRepository().getId(), path, e.getMessage(), e );
log.debug( MarkerFactory.getDetachedMarker( "transfer.error" ), "Full stack trace", e );
}
}
@@ -490,14 +498,16 @@ public class DefaultRepositoryProxyConnectors
}
catch ( ProxyException e )
{
- log.warn( "Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}",
- targetRepository.getRepository().getId(), logicalPath, e.getMessage() );
+ log.warn(
+ "Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}",
+ targetRepository.getRepository().getId(), logicalPath, e.getMessage() );
log.debug( "Full stack trace", e );
}
catch ( RepositoryAdminException e )
{
- log.warn( "Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}",
- targetRepository.getRepository().getId(), logicalPath, e.getMessage() );
+ log.warn(
+ "Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}",
+ targetRepository.getRepository().getId(), logicalPath, e.getMessage() );
log.debug( "Full stack trace", e );
}
}
@@ -898,7 +908,7 @@ public class DefaultRepositoryProxyConnectors
catch ( ProxyException e )
{
urlFailureCache.cacheFailure( url );
- log.warn( "Transfer failed on checksum: {} : {}",url ,e.getMessage(), e );
+ log.warn( "Transfer failed on checksum: {} : {}", url, e.getMessage(), e );
// Critical issue, pass it on.
throw e;
}
@@ -1059,7 +1069,7 @@ public class DefaultRepositoryProxyConnectors
log.warn(
"Transfer error from repository {} for artifact {} , continuing to next repository. Error message: {}",
- content.getRepository().getId(), Keys.toKey( artifact), exception.getMessage() );
+ content.getRepository().getId(), Keys.toKey( artifact ), exception.getMessage() );
log.debug( "Full stack trace", exception );
}
@@ -1086,40 +1096,51 @@ public class DefaultRepositoryProxyConnectors
private void moveTempToTarget( File temp, File target )
throws ProxyException
{
- if ( target.exists() && !target.delete() )
- {
- throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
- }
- target.getParentFile().mkdirs();
// TODO file lock library
- RandomAccessFile raf;
-
- if ( !temp.renameTo( target ) )
+ Lock lock = null;
+ try
{
- log.warn( "Unable to rename tmp file to its final name... resorting to copy command." );
-
- try
+ lock = fileLockManager.writeFileLock( target );
+ if ( lock.getFile().exists() && !lock.getFile().delete() )
{
- FileUtils.copyFile( temp, target );
+ throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
}
- catch ( IOException e )
+
+ lock.getFile().getParentFile().mkdirs();
+
+ if ( !temp.renameTo( lock.getFile() ) )
{
- if ( target.exists() )
+ log.warn( "Unable to rename tmp file to its final name... resorting to copy command." );
+
+ try
{
- log.debug( "Tried to copy file {} to {} but file with this name already exists.", temp.getName(),
- target.getAbsolutePath() );
+ FileUtils.copyFile( temp, lock.getFile() );
}
- else
+ catch ( IOException e )
{
- throw new ProxyException(
- "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e );
+ if ( lock.getFile().exists() )
+ {
+ log.debug( "Tried to copy file {} to {} but file with this name already exists.",
+ temp.getName(), lock.getFile().getAbsolutePath() );
+ }
+ else
+ {
+ throw new ProxyException(
+ "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e );
+ }
+ }
+ finally
+ {
+ FileUtils.deleteQuietly( temp );
}
}
- finally
- {
- FileUtils.deleteQuietly( temp );
- }
+ } catch( FileLockException e)
+ {
+ throw new ProxyException( e.getMessage(), e );
+ } catch (FileLockTimeoutException e)
+ {
+ throw new ProxyException( e.getMessage(), e );
}
}
@@ -1187,12 +1208,12 @@ public class DefaultRepositoryProxyConnectors
}
catch ( ConnectionException e )
{
- log.warn( "Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage() );
+ log.warn( "Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage() );
connected = false;
}
catch ( AuthenticationException e )
{
- log.warn( "Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage() );
+ log.warn( "Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage() );
connected = false;
}