Skip to content
Open

GURK #1679

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,25 @@
* the repository.
*/
public final class LocalRepository implements ArtifactRepository {
public static final String ID = "local";

private final Path basePath;

private final String type;

private final int hashCode;

/**
* Creates a new local repository with the specified base directory and unknown type.
*
* @param basedir The base directory of the repository, may be {@code null}.
* @deprecated Use {@link LocalRepository(Path)} instead.
*/
@Deprecated
public LocalRepository(String basedir) {
this.basePath = Paths.get(RepositoryUriUtils.toUri(basedir)).toAbsolutePath();
this.type = "";
this.hashCode = Objects.hash(this.basePath, this.type);
}

/**
Expand Down Expand Up @@ -99,6 +105,7 @@ public LocalRepository(File basedir, String type) {
public LocalRepository(Path basePath, String type) {
this.basePath = basePath;
this.type = (type != null) ? type : "";
this.hashCode = Objects.hash(this.basePath, this.type);
}

@Override
Expand All @@ -108,7 +115,7 @@ public String getContentType() {

@Override
public String getId() {
return "local";
return ID;
}

/**
Expand Down Expand Up @@ -153,13 +160,6 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + hash(basePath);
hash = hash * 31 + hash(type);
return hash;
}

private static int hash(Object obj) {
return obj != null ? obj.hashCode() : 0;
return hashCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.eclipse.aether.repository;

import java.util.Objects;
import java.util.UUID;

/**
Expand All @@ -27,11 +28,14 @@
* the contained artifacts is handled by a {@link WorkspaceReader}.
*/
public final class WorkspaceRepository implements ArtifactRepository {
public static final String ID = "workspace";

private final String type;

private final Object key;

private final int hashCode;

/**
* Creates a new workspace repository of type {@code "workspace"} and a random key.
*/
Expand All @@ -58,14 +62,15 @@ public WorkspaceRepository(String type) {
public WorkspaceRepository(String type, Object key) {
this.type = (type != null) ? type : "";
this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
this.hashCode = Objects.hash(type, key);
}

public String getContentType() {
return type;
}

public String getId() {
return "workspace";
return ID;
}

/**
Expand Down Expand Up @@ -99,9 +104,6 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + getKey().hashCode();
hash = hash * 31 + getContentType().hashCode();
return hash;
return hashCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.RepositorySystem;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class Resolver {

private final LocalRepository localRepository;

public Resolver(String[] args, String remoteRepository, String localRepository) {
public Resolver(String[] args, String remoteRepository, Path localRepository) {
this.args = args;
this.remoteRepository = remoteRepository;
this.repositorySystem = Booter.newRepositorySystem(Booter.selectFactory(args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.maven.resolver.examples.resolver;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

import org.eclipse.aether.artifact.Artifact;
Expand All @@ -37,7 +38,8 @@ public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(ResolverDemo.class.getSimpleName());

Resolver resolver = new Resolver(args, "https://repo.maven.apache.org/maven2/", "target/resolver-demo-repo");
Resolver resolver =
new Resolver(args, "https://repo.maven.apache.org/maven2/", Paths.get("target/resolver-demo-repo"));
ResolverResult result = resolver.resolve("junit", "junit", "4.13.2");

System.out.println("Result:");
Expand All @@ -47,8 +49,8 @@ public static void main(String[] args) throws Exception {
}

public void resolve(String[] args) throws DependencyResolutionException {
Resolver resolver =
new Resolver(args, "http://localhost:8081/nexus/content/groups/public", "target/aether-repo");
Resolver resolver = new Resolver(
args, "http://localhost:8081/nexus/content/groups/public", Paths.get("target/aether-repo"));

ResolverResult result = resolver.resolve("com.mycompany.app", "super-app", "1.0");

Expand All @@ -66,8 +68,8 @@ public void resolve(String[] args) throws DependencyResolutionException {
}

public void installAndDeploy(String[] args) throws InstallationException, DeploymentException {
Resolver resolver =
new Resolver(args, "http://localhost:8081/nexus/content/groups/public", "target/aether-repo");
Resolver resolver = new Resolver(
args, "http://localhost:8081/nexus/content/groups/public", Paths.get("target/aether-repo"));

Artifact artifact = new DefaultArtifact("com.mycompany.super", "super-core", "jar", "0.1-SNAPSHOT");
artifact = artifact.setFile(new File("jar-from-whatever-process.jar"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.function.Function;
import java.util.function.BiFunction;

import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.ArtifactRepository;
import org.eclipse.aether.util.repository.RepositoryIdHelper;
import org.eclipse.aether.repository.RemoteRepository;

import static org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory.repositoryKeyFunction;

/**
* Default local path prefix composer factory: it fully reuses {@link LocalPathPrefixComposerFactorySupport} class
Expand All @@ -48,7 +49,7 @@ public LocalPathPrefixComposer createComposer(RepositorySystemSession session) {
isSplitRemoteRepositoryLast(session),
getReleasesPrefix(session),
getSnapshotsPrefix(session),
RepositoryIdHelper.cachedIdToPathSegment(session));
repositoryKeyFunction(session));
}

/**
Expand All @@ -66,7 +67,7 @@ private DefaultLocalPathPrefixComposer(
boolean splitRemoteRepositoryLast,
String releasesPrefix,
String snapshotsPrefix,
Function<ArtifactRepository, String> idToPathSegmentFunction) {
BiFunction<RemoteRepository, String, String> repositoryKeyFunction) {
super(
split,
localPrefix,
Expand All @@ -77,7 +78,7 @@ private DefaultLocalPathPrefixComposer(
splitRemoteRepositoryLast,
releasesPrefix,
snapshotsPrefix,
idToPathSegmentFunction);
repositoryKeyFunction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import org.eclipse.aether.ConfigurationProperties;
import org.eclipse.aether.RepositoryCache;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.impl.RemoteRepositoryManager;
Expand All @@ -40,6 +44,8 @@
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
import org.eclipse.aether.util.ConfigUtils;
import org.eclipse.aether.util.repository.RepositoryIdHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,6 +56,51 @@
@Singleton
@Named
public class DefaultRemoteRepositoryManager implements RemoteRepositoryManager {
private static final String CONFIG_PROPS_PREFIX =
ConfigurationProperties.PREFIX_AETHER + "remoteRepositoryManager.";

/**
* <b>Experimental:</b> Configuration for "repository key" function.
* Note: repository key functions other than "nid" produce repository keys will be <em>way different
* that those produced with previous versions or without this option enabled</em>. Manager uses this key to
* detect "same" remote repositories, and in case of mirrors, to merge them.
*
* @since 2.0.14
* @configurationSource {@link RepositorySystemSession#getConfigProperties()}
* @configurationType {@link java.lang.String}
* @configurationDefaultValue {@link #DEFAULT_REPOSITORY_KEY_FUNCTION}
*/
public static final String CONFIG_PROP_REPOSITORY_KEY_FUNCTION = CONFIG_PROPS_PREFIX + "repositoryKeyFunction";

public static final String DEFAULT_REPOSITORY_KEY_FUNCTION = "nid";

/**
* Method that based on configuration returns the "repository key function". Used by {@link EnhancedLocalRepositoryManagerFactory}
* and {@link LocalPathPrefixComposerFactory}.
*
* @since 2.0.14
*/
@SuppressWarnings("unchecked")
private static BiFunction<RemoteRepository, String, String> repositoryKeyFunction(RepositorySystemSession session) {
final RepositoryIdHelper.RepositoryKeyFunction repositoryKeyFunction =
RepositoryIdHelper.getRepositoryKeyFunction(ConfigUtils.getString(
session, DEFAULT_REPOSITORY_KEY_FUNCTION, CONFIG_PROP_REPOSITORY_KEY_FUNCTION));
if (session.getCache() != null) {
// both are expensive methods; cache it in session (repo -> context -> ID)
return (repository, context) -> ((ConcurrentMap<RemoteRepository, ConcurrentMap<String, String>>)
session.getCache()
.computeIfAbsent(
session,
EnhancedLocalRepositoryManagerFactory.class.getName()
+ ".repositoryKeyFunction",
ConcurrentHashMap::new))
.computeIfAbsent(repository, k1 -> new ConcurrentHashMap<>())
.computeIfAbsent(
context == null ? "" : context, k2 -> repositoryKeyFunction.apply(repository, context));
} else {
return repositoryKeyFunction;
}
}

private static final class LoggedMirror {

Expand Down Expand Up @@ -102,6 +153,7 @@ public List<RemoteRepository> aggregateRepositories(
return dominantRepositories;
}

BiFunction<RemoteRepository, String, String> repositoryKeyFunction = repositoryKeyFunction(session);
MirrorSelector mirrorSelector = session.getMirrorSelector();
AuthenticationSelector authSelector = session.getAuthenticationSelector();
ProxySelector proxySelector = session.getProxySelector();
Expand All @@ -121,15 +173,16 @@ public List<RemoteRepository> aggregateRepositories(
}
}

String key = getKey(repository);
String key = repositoryKeyFunction.apply(repository, null);

for (ListIterator<RemoteRepository> it = result.listIterator(); it.hasNext(); ) {
RemoteRepository dominantRepository = it.next();

if (key.equals(getKey(dominantRepository))) {
if (key.equals(repositoryKeyFunction.apply(dominantRepository, null))) {
if (!dominantRepository.getMirroredRepositories().isEmpty()
&& !repository.getMirroredRepositories().isEmpty()) {
RemoteRepository mergedRepository = mergeMirrors(session, dominantRepository, repository);
RemoteRepository mergedRepository =
mergeMirrors(session, repositoryKeyFunction, dominantRepository, repository);
if (mergedRepository != dominantRepository) {
it.set(mergedRepository);
}
Expand Down Expand Up @@ -188,21 +241,20 @@ private void logMirror(RepositorySystemSession session, RemoteRepository origina
original.getUrl());
}

private String getKey(RemoteRepository repository) {
return repository.getId();
}

private RemoteRepository mergeMirrors(
RepositorySystemSession session, RemoteRepository dominant, RemoteRepository recessive) {
RepositorySystemSession session,
BiFunction<RemoteRepository, String, String> repositoryKeyFunction,
RemoteRepository dominant,
RemoteRepository recessive) {
RemoteRepository.Builder merged = null;
RepositoryPolicy releases = null, snapshots = null;

next:
for (RemoteRepository rec : recessive.getMirroredRepositories()) {
String recKey = getKey(rec);
String recKey = repositoryKeyFunction.apply(rec, null);

for (RemoteRepository dom : dominant.getMirroredRepositories()) {
if (recKey.equals(getKey(dom))) {
if (recKey.equals(repositoryKeyFunction.apply(dom, null))) {
continue next;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Function;
import java.util.function.BiFunction;

import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.ArtifactRepository;
import org.eclipse.aether.repository.LocalArtifactRegistration;
import org.eclipse.aether.repository.LocalArtifactRequest;
import org.eclipse.aether.repository.LocalArtifactResult;
Expand Down Expand Up @@ -72,11 +71,11 @@ class EnhancedLocalRepositoryManager extends SimpleLocalRepositoryManager {
EnhancedLocalRepositoryManager(
Path basedir,
LocalPathComposer localPathComposer,
Function<ArtifactRepository, String> idToPathSegmentFunction,
BiFunction<RemoteRepository, String, String> repositoryKeyFunction,
String trackingFilename,
TrackingFileManager trackingFileManager,
LocalPathPrefixComposer localPathPrefixComposer) {
super(basedir, "enhanced", localPathComposer, idToPathSegmentFunction);
super(basedir, "enhanced", localPathComposer, repositoryKeyFunction);
this.trackingFilename = requireNonNull(trackingFilename);
this.trackingFileManager = requireNonNull(trackingFileManager);
this.localPathPrefixComposer = requireNonNull(localPathPrefixComposer);
Expand Down
Loading