Skip to content

Commit

Permalink
Merge pull request #31 from FlintMC/fix-concurrent-modification-excep…
Browse files Browse the repository at this point in the history
…tion

fix concurrent modification exception
  • Loading branch information
R0bbyYT authored Jun 9, 2021
2 parents 682edc4 + ccd6e0b commit 40dab21
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ repositories {
mavenCentral()
}

version = System.getenv().getOrDefault("VERSION", "2.10.1")
version = System.getenv().getOrDefault("VERSION", "2.11.0")

dependencies {
implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.11.1")
Expand Down
98 changes: 55 additions & 43 deletions src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public MavenArtifactDownloader() {
* @param source The repository to add
*/
public void addSource(ReadableMavenRepository source) {
this.sources.add(source);
synchronized (this.sources) {
this.sources.add(source);
}
}

/**
Expand All @@ -70,7 +72,9 @@ public void addSource(ReadableMavenRepository source) {
* @return {@code true} if this downloader has the give repository as a source, {@code false} otherwise
*/
public boolean hasSource(ReadableMavenRepository source) {
return this.sources.contains(source);
synchronized (this.sources) {
return this.sources.contains(source);
}
}

/**
Expand All @@ -79,7 +83,9 @@ public boolean hasSource(ReadableMavenRepository source) {
* @param source The repository to remove
*/
public void removeSource(ReadableMavenRepository source) {
this.sources.remove(source);
synchronized (this.sources) {
this.sources.remove(source);
}
}

/**
Expand All @@ -97,29 +103,29 @@ public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boo
Path localPomPath = target.getPomPath(artifact);

MavenPom artifactPom;
if(artifact instanceof MavenPom) {
if (artifact instanceof MavenPom) {
// If the artifact is a POM already, don't try to read it from anywhere
artifactPom = (MavenPom) artifact;
} else {
// Query the local POM if it exists, else find the POM online
artifactPom = Files.exists(localPomPath) ? PomReader.read(localPomPath) : findPom(artifact);
}

if(artifactPom != null) {
if (artifactPom != null) {
// POM found, install it with all its dependencies
installAll(artifactPom, target);

if(installIfNotExists) {
if(!Files.exists(localPomPath)) {
if (installIfNotExists) {
if (!Files.exists(localPomPath)) {
// The POM file does not exist locally, write it down
PomWriter.write(artifactPom, localPomPath);
}
}
}

if(installIfNotExists) {
if (installIfNotExists) {
// If the artifact is not installed locally, try to install it
if(!target.isInstalled(artifact) && !installArtifact(artifact, target) && artifactPom == null) {
if (!target.isInstalled(artifact) && !installArtifact(artifact, target) && artifactPom == null) {
// The artifact failed to install and there was also no POM for it
throw new MavenResolveException("Could not resolve " + artifact);
}
Expand All @@ -141,19 +147,19 @@ private void installAll(MavenPom pom, SimpleMavenRepository target) throws IOExc
Set<MavenDependency> toProcess = new HashSet<>(pom.getDependencies());

// Process until the list to process is empty
while(!toProcess.isEmpty()) {
while (!toProcess.isEmpty()) {
// Copy over the dependencies to process into the current processing round
Set<MavenDependency> roundDependencies = new HashSet<>(toProcess);
toProcess.clear();

for(MavenDependency dependency : roundDependencies) {
for (MavenDependency dependency : roundDependencies) {
// Mark the dependency as found
dependencies.add(dependency);

// Sort out unwanted dependencies
if(shouldSkip(dependency)) {
if (shouldSkip(dependency)) {
continue;
} else if(dependency.isBroken()) {
} else if (dependency.isBroken()) {
continue;
}

Expand All @@ -162,30 +168,30 @@ private void installAll(MavenPom pom, SimpleMavenRepository target) throws IOExc

// Try to read the POM locally, if it does not exist fall back to reading it online
MavenPom dependencyPom = Files.exists(localPomPath) ? PomReader.read(localPomPath) : findPom(dependency);
if(dependencyPom != null) {
if (dependencyPom != null) {
// A POM file was found, iterate its dependencies
for(MavenDependency innerDependency : dependencyPom.getDependencies()) {
for (MavenDependency innerDependency : dependencyPom.getDependencies()) {
// Sort out unwanted dependencies
if(shouldSkip(innerDependency)) {
if (shouldSkip(innerDependency)) {
continue;
} else if(innerDependency.isBroken()) {
} else if (innerDependency.isBroken()) {
continue;
}

if(!dependencies.contains(innerDependency)) {
if (!dependencies.contains(innerDependency)) {
// The dependency has not been found already, process it
toProcess.add(innerDependency);
}
}

if(!Files.exists(localPomPath)) {
if (!Files.exists(localPomPath)) {
// If the POM does not exist locally, install it
PomWriter.write(dependencyPom, localPomPath);
}
}

// Try to install the dependency locally
if(!target.isInstalled(dependency) && !installArtifact(dependency, target) && dependencyPom == null) {
if (!target.isInstalled(dependency) && !installArtifact(dependency, target) && dependencyPom == null) {
// The dependency had no artifact and also no POM
throw new MavenResolveException("Could not resolve " + dependency);
}
Expand Down Expand Up @@ -213,16 +219,18 @@ private boolean shouldSkip(MavenDependency dependency) {
* @throws IOException If an I/O error occurs while opening the stream
*/
public InputStream findArtifactStream(MavenArtifact artifact) throws IOException {
for(ReadableMavenRepository source : sources) {
InputStream stream;
if((stream = source.getArtifactStream(artifact)) != null) {
// Found the requested artifact
return stream;
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
InputStream stream;
if ((stream = source.getArtifactStream(artifact)) != null) {
// Found the requested artifact
return stream;
}
}
}

// Artifact has not been found in any source
return null;
// Artifact has not been found in any source
return null;
}
}

/**
Expand All @@ -233,15 +241,17 @@ public InputStream findArtifactStream(MavenArtifact artifact) throws IOException
* @throws IOException If an I/O error occurs while checking for the artifact
*/
public Pair<ReadableMavenRepository, URI> findArtifactURI(MavenArtifact artifact) throws IOException {
for(ReadableMavenRepository source : sources) {
URI uri = source.getArtifactURI(artifact);
if(uri != null) {
return new Pair<>(source, uri);
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
URI uri = source.getArtifactURI(artifact);
if (uri != null) {
return new Pair<>(source, uri);
}
}
}

// Artifact has not been found in any source
return null;
// Artifact has not been found in any source
return null;
}
}

/**
Expand All @@ -252,11 +262,13 @@ public Pair<ReadableMavenRepository, URI> findArtifactURI(MavenArtifact artifact
* @throws IOException If an I/O error occurs while trying to read a POM
*/
private MavenPom findPom(MavenArtifact artifact) throws IOException {
for(ReadableMavenRepository source : sources) {
MavenPom pom;
if((pom = source.getArtifactPom(artifact)) != null) {
// Found the requested POM
return pom;
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
MavenPom pom;
if ((pom = source.getArtifactPom(artifact)) != null) {
// Found the requested POM
return pom;
}
}
}

Expand All @@ -274,12 +286,12 @@ private MavenPom findPom(MavenArtifact artifact) throws IOException {
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException {
try(InputStream stream = findArtifactStream(artifact)) {
try (InputStream stream = findArtifactStream(artifact)) {
// Try to find the given artifact
if(stream != null) {
if (stream != null) {
// The artifact has been found, install it
Path targetPath = target.getArtifactPath(artifact);
if(!Files.isDirectory(targetPath.getParent())) {
if (!Files.isDirectory(targetPath.getParent())) {
// Make sure the parent directories exist
Files.createDirectories(targetPath.getParent());
}
Expand Down

0 comments on commit 40dab21

Please sign in to comment.