Skip to content

Commit 9f5a73e

Browse files
committed
Added repository fallback and resolution mode
1 parent 872ba2e commit 9f5a73e

File tree

3 files changed

+114
-15
lines changed

3 files changed

+114
-15
lines changed

core/src/main/java/com/alessiodp/libby/Library.java

+38-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public class Library {
3434
*/
3535
@NotNull
3636
private final Collection<String> repositories;
37+
38+
/**
39+
* Fallback repository URLs for this library
40+
*/
41+
@NotNull
42+
private final Collection<String> fallbackRepositories;
3743

3844
/**
3945
* Maven group ID
@@ -128,6 +134,7 @@ public class Library {
128134
*/
129135
private Library(@Nullable Collection<String> urls,
130136
@Nullable Collection<String> repositories,
137+
@Nullable Collection<String> fallbackRepositories,
131138
@NotNull String groupId,
132139
@NotNull String artifactId,
133140
@NotNull String version,
@@ -151,6 +158,7 @@ private Library(@Nullable Collection<String> urls,
151158
this.path = craftPath(this.partialPath, this.artifactId, this.version, this.classifier);
152159

153160
this.repositories = repositories != null ? Collections.unmodifiableList(new LinkedList<>(repositories)) : Collections.emptyList();
161+
this.fallbackRepositories = fallbackRepositories != null ? Collections.unmodifiableList(new LinkedList<>(fallbackRepositories)) : Collections.emptyList();
154162
relocatedPath = hasRelocations() ? path + "-relocated-" + Math.abs(this.relocations.hashCode()) + ".jar" : null;
155163
this.isolatedLoad = isolatedLoad;
156164
this.loaderId = loaderId;
@@ -169,14 +177,24 @@ public Collection<String> getUrls() {
169177
}
170178

171179
/**
172-
* Gets the repositories URLs for this library.
180+
* Gets the repository URLs for this library.
173181
*
174-
* @return repositories URLs
182+
* @return repository URLs
175183
*/
176184
@NotNull
177185
public Collection<String> getRepositories() {
178186
return repositories;
179187
}
188+
189+
/**
190+
* Gets the fallback repository URLs for this library.
191+
*
192+
* @return fallback repository URLs
193+
*/
194+
@NotNull
195+
public Collection<String> getFallbackRepositories() {
196+
return fallbackRepositories;
197+
}
180198

181199
/**
182200
* Gets the Maven group ID for this library.
@@ -380,6 +398,11 @@ public static class Builder {
380398
* Repository URLs for this library
381399
*/
382400
private final Collection<String> repositories = new LinkedList<>();
401+
402+
/**
403+
* Fallback repository URLs for this library
404+
*/
405+
private final Collection<String> fallbackRepositories = new LinkedList<>();
383406

384407
/**
385408
* Maven group ID
@@ -456,6 +479,18 @@ public Builder repository(@NotNull String url) {
456479
repositories.add(requireNonNull(url, "repository").endsWith("/") ? url : url + '/');
457480
return this;
458481
}
482+
483+
/**
484+
* Adds a fallback repository URL for this library. See {@link #repository(String)}.
485+
*
486+
* @param url fallback repository URL
487+
* @return this builder
488+
*/
489+
@NotNull
490+
public Builder fallbackRepository(@NotNull String url) {
491+
fallbackRepositories.add(requireNonNull(url, "fallbackRepository").endsWith("/") ? url : url + '/');
492+
return this;
493+
}
459494

460495
/**
461496
* Sets the Maven group ID for this library.
@@ -642,7 +677,7 @@ public Builder excludeTransitiveDependency(@NotNull String groupId, @NotNull Str
642677
*/
643678
@NotNull
644679
public Library build() {
645-
return new Library(urls, repositories, groupId, artifactId, version, classifier, checksum, relocations, isolatedLoad, loaderId, resolveTransitiveDependencies, excludedTransitiveDependencies);
680+
return new Library(urls, repositories, fallbackRepositories, groupId, artifactId, version, classifier, checksum, relocations, isolatedLoad, loaderId, resolveTransitiveDependencies, excludedTransitiveDependencies);
646681
}
647682
}
648683
}

core/src/main/java/com/alessiodp/libby/LibraryManager.java

+57-12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import java.util.List;
4848
import java.util.Map;
4949
import java.util.Set;
50+
import java.util.stream.Collectors;
51+
import java.util.stream.Stream;
5052

5153
import static java.util.Objects.requireNonNull;
5254

@@ -107,6 +109,11 @@ public abstract class LibraryManager {
107109
* Map of isolated class loaders and theirs id
108110
*/
109111
protected final Map<String, IsolatedClassLoader> isolatedLibraries = new HashMap<>();
112+
113+
/**
114+
* Repository resolution mode for libraries
115+
*/
116+
protected RepositoryResolutionMode repositoryResolutionMode = RepositoryResolutionMode.DEFAULT;
110117

111118
/**
112119
* Creates a new library manager.
@@ -282,6 +289,24 @@ public void addJitPack() {
282289
addRepository(Repositories.JITPACK);
283290
}
284291

292+
/**
293+
* Get the repository resolution mode.
294+
*
295+
* @return the repository resolution mode
296+
*/
297+
public RepositoryResolutionMode getRepositoryResolutionMode() {
298+
return repositoryResolutionMode;
299+
}
300+
301+
/**
302+
* Set the repository resolution mode.
303+
*
304+
* @param repositoryResolutionMode the repository resolution mode
305+
*/
306+
public void setRepositoryResolutionMode(RepositoryResolutionMode repositoryResolutionMode) {
307+
this.repositoryResolutionMode = repositoryResolutionMode;
308+
}
309+
285310
/**
286311
* Gets all the possible download URLs for this library. Entries are
287312
* ordered by direct download URLs first and then repository download URLs.
@@ -294,19 +319,9 @@ public void addJitPack() {
294319
public Collection<String> resolveLibrary(@NotNull Library library) {
295320
Set<String> urls = new LinkedHashSet<>(requireNonNull(library, "library").getUrls());
296321
boolean snapshot = library.isSnapshot();
322+
Collection<String> repos = resolveRepositories(library);
297323

298-
// Try from library-declared repos first
299-
for (String repository : library.getRepositories()) {
300-
if (snapshot) {
301-
String url = resolveSnapshot(repository, library);
302-
if (url != null)
303-
urls.add(repository + url);
304-
} else {
305-
urls.add(repository + library.getPath());
306-
}
307-
}
308-
309-
for (String repository : getRepositories()) {
324+
for (String repository : repos) {
310325
if (snapshot) {
311326
String url = resolveSnapshot(repository, library);
312327
if (url != null)
@@ -318,6 +333,36 @@ public Collection<String> resolveLibrary(@NotNull Library library) {
318333

319334
return Collections.unmodifiableSet(urls);
320335
}
336+
337+
/**
338+
* Resolves the repository URLs for this library.
339+
*
340+
* @param library the library to resolve repositories for
341+
* @return the resolved repositories
342+
*/
343+
protected Collection<String> resolveRepositories(@NotNull Library library) {
344+
switch (getRepositoryResolutionMode()) {
345+
case GLOBAL_FIRST:
346+
return Stream.of(
347+
getRepositories(),
348+
library.getRepositories(),
349+
library.getFallbackRepositories()
350+
).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
351+
case LIBRARY_FIRST:
352+
return Stream.of(
353+
library.getRepositories(),
354+
library.getFallbackRepositories(),
355+
getRepositories()
356+
).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
357+
case DEFAULT:
358+
default:
359+
return Stream.of(
360+
library.getRepositories(),
361+
getRepositories(),
362+
library.getFallbackRepositories()
363+
).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
364+
}
365+
}
321366

322367
/**
323368
* Resolves the URL of the artifact of a snapshot library.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.alessiodp.libby;
2+
3+
/**
4+
* Enum representing the resolution mode of repositories, determining the order in which repositories are searched when resolving dependencies.
5+
*/
6+
public enum RepositoryResolutionMode {
7+
/**
8+
* The default resolution mode searches for library repositories first, followed by global repositories, and then library fallback repositories if necessary.
9+
*/
10+
DEFAULT,
11+
/**
12+
* With GLOBAL_FIRST, global repositories are searched first, followed by library repositories, and then library fallback repositories if necessary.
13+
*/
14+
GLOBAL_FIRST,
15+
/**
16+
* With LIBRARY_FIRST, library repositories are searched first, followed by library fallback repositories, and then global repositories if necessary.
17+
*/
18+
LIBRARY_FIRST
19+
}

0 commit comments

Comments
 (0)