-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MRESOLVER-219] Implement NamedLock with advisory file locking
This closes #131
- Loading branch information
Showing
14 changed files
with
885 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...l/src/main/java/org/eclipse/aether/internal/impl/synccontext/named/FileGAVNameMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.eclipse.aether.internal.impl.synccontext.named; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.eclipse.aether.RepositorySystemSession; | ||
import org.eclipse.aether.artifact.Artifact; | ||
import org.eclipse.aether.metadata.Metadata; | ||
import org.eclipse.aether.named.support.FileSystemFriendly; | ||
|
||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.TreeSet; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* A {@link NameMapper} that creates same name mapping as Takari Local Repository does, with | ||
* {@code baseDir} (local repo). Part of code blatantly copies parts of the Takari | ||
* {@code LockingSyncContext}. | ||
* | ||
* @see <a href="https://github.com/takari/takari-local-repository/blob/24133e50a0478dccb5620ac2f2255187608f165b/src/main/java/io/takari/aether/concurrency/LockingSyncContext.java">Takari | ||
* LockingSyncContext.java</a> | ||
*/ | ||
@Singleton | ||
@Named( FileGAVNameMapper.NAME ) | ||
public class FileGAVNameMapper | ||
implements NameMapper, FileSystemFriendly | ||
{ | ||
public static final String NAME = "file-gav"; | ||
|
||
private static final String LOCK_SUFFIX = ".resolverlock"; | ||
|
||
private static final char SEPARATOR = '~'; | ||
|
||
private final ConcurrentMap<String, Path> baseDirs; | ||
|
||
public FileGAVNameMapper() | ||
{ | ||
this.baseDirs = new ConcurrentHashMap<>(); | ||
} | ||
|
||
@Override | ||
public TreeSet<String> nameLocks( final RepositorySystemSession session, | ||
final Collection<? extends Artifact> artifacts, | ||
final Collection<? extends Metadata> metadatas ) | ||
{ | ||
File localRepositoryBasedir = session.getLocalRepository().getBasedir(); | ||
// here we abuse concurrent hash map to make sure costly getCanonicalFile is invoked only once | ||
Path baseDir = baseDirs.computeIfAbsent( | ||
localRepositoryBasedir.getPath(), k -> | ||
{ | ||
try | ||
{ | ||
return new File( localRepositoryBasedir, ".locks" ).getCanonicalFile().toPath(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new UncheckedIOException( e ); | ||
} | ||
} | ||
); | ||
|
||
TreeSet<String> paths = new TreeSet<>(); | ||
if ( artifacts != null ) | ||
{ | ||
for ( Artifact artifact : artifacts ) | ||
{ | ||
paths.add( getPath( baseDir, artifact ) + LOCK_SUFFIX ); | ||
} | ||
} | ||
if ( metadatas != null ) | ||
{ | ||
for ( Metadata metadata : metadatas ) | ||
{ | ||
paths.add( getPath( baseDir, metadata ) + LOCK_SUFFIX ); | ||
} | ||
} | ||
return paths; | ||
} | ||
|
||
private String getPath( final Path baseDir, final Artifact artifact ) | ||
{ | ||
// NOTE: Don't use LRM.getPath*() as those paths could be different across processes, e.g. due to staging LRMs. | ||
String path = artifact.getGroupId() | ||
+ SEPARATOR + artifact.getArtifactId() | ||
+ SEPARATOR + artifact.getBaseVersion(); | ||
return baseDir.resolve( path ).toAbsolutePath().toString(); | ||
} | ||
|
||
private String getPath( final Path baseDir, final Metadata metadata ) | ||
{ | ||
// NOTE: Don't use LRM.getPath*() as those paths could be different across processes, e.g. due to staging. | ||
String path = ""; | ||
if ( metadata.getGroupId().length() > 0 ) | ||
{ | ||
path += metadata.getGroupId(); | ||
if ( metadata.getArtifactId().length() > 0 ) | ||
{ | ||
path += SEPARATOR + metadata.getArtifactId(); | ||
if ( metadata.getVersion().length() > 0 ) | ||
{ | ||
path += SEPARATOR + metadata.getVersion(); | ||
} | ||
} | ||
} | ||
return baseDir.resolve( path ).toAbsolutePath().toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...-impl/src/test/java/org/eclipse/aether/internal/impl/synccontext/FileLockAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.eclipse.aether.internal.impl.synccontext; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper; | ||
import org.eclipse.aether.named.providers.FileLockNamedLockFactory; | ||
import org.junit.BeforeClass; | ||
|
||
public class FileLockAdapterTest | ||
extends NamedLockFactoryAdapterTestSupport | ||
{ | ||
@BeforeClass | ||
public static void createNamedLockFactory() | ||
{ | ||
nameMapper = new FileGAVNameMapper(); | ||
namedLockFactory = new FileLockNamedLockFactory(); | ||
createAdapter(); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...amed-locks/src/main/java/org/eclipse/aether/named/providers/FileLockNamedLockFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.eclipse.aether.named.providers; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import org.eclipse.aether.named.support.FileLockNamedLock; | ||
import org.eclipse.aether.named.support.FileSystemFriendly; | ||
import org.eclipse.aether.named.support.NamedLockFactorySupport; | ||
import org.eclipse.aether.named.support.NamedLockSupport; | ||
|
||
/** | ||
* Named locks factory of {@link FileLockNamedLock}s. This is a bit special implementation, as it | ||
* expects locks names to be fully qualified absolute file system paths. | ||
* | ||
* @since TBD | ||
*/ | ||
@Singleton | ||
@Named( FileLockNamedLockFactory.NAME ) | ||
public class FileLockNamedLockFactory | ||
extends NamedLockFactorySupport | ||
implements FileSystemFriendly | ||
{ | ||
public static final String NAME = "file-lock"; | ||
|
||
private final ConcurrentMap<String, FileChannel> fileChannels; | ||
|
||
public FileLockNamedLockFactory() | ||
{ | ||
this.fileChannels = new ConcurrentHashMap<>(); | ||
} | ||
|
||
@Override | ||
protected NamedLockSupport createLock( final String name ) | ||
{ | ||
Path path = Paths.get( name ); | ||
FileChannel fileChannel = fileChannels.computeIfAbsent( name, k -> | ||
{ | ||
try | ||
{ | ||
Files.createDirectories( path.getParent() ); | ||
return FileChannel.open( | ||
path, | ||
StandardOpenOption.READ, StandardOpenOption.WRITE, | ||
StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE | ||
); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new UncheckedIOException( "Failed to open file channel for '" | ||
+ name + "'", e ); | ||
} | ||
} ); | ||
return new FileLockNamedLock( name, fileChannel, this ); | ||
} | ||
|
||
@Override | ||
protected void destroyLock( final String name ) | ||
{ | ||
FileChannel fileChannel = fileChannels.remove( name ); | ||
if ( fileChannel == null ) | ||
{ | ||
throw new IllegalStateException( "File channel expected, but does not exist: " + name ); | ||
} | ||
|
||
try | ||
{ | ||
fileChannel.close(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new UncheckedIOException( "Failed to close file channel for '" | ||
+ name + "'", e ); | ||
} | ||
} | ||
} |
Oops, something went wrong.