Skip to content

Commit

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

fix build errors
  • Loading branch information
zortax authored Jun 9, 2021
2 parents 40dab21 + e6fd978 commit 4027f58
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,30 +432,32 @@ private Set<InstallInstructionModel> buildMavenInstallInstructions(
remoteMavenRepository = new RemoteMavenRepository(flintGradlePlugin.getHttpClient(), entry.getValue());
}

if (!internalRepository.isInstalled(artifact)) {
// The artifact is not installed already, install it
if (remoteMavenRepository == null) {
// Can't download anything in offline mode
throw new RuntimeException("Missing artifact " + artifact + " in local repository, " +
"but working in offline mode");
}

boolean setupSource = !downloader.hasSource(remoteMavenRepository);
if (setupSource) {
// The download has the source not set already, add it now
downloader.addSource(remoteMavenRepository);
}

try {
// Install the artifact including dependencies
downloader.installArtifact(artifact, internalRepository);
} catch (IOException e) {
throw new FlintGradleException("Failed to install maven artifact", e);
}

if (setupSource) {
// We added the source, clean up afterwards
downloader.removeSource(remoteMavenRepository);
synchronized (internalRepository) {
if (!internalRepository.isInstalled(artifact)) {
// The artifact is not installed already, install it
if (remoteMavenRepository == null) {
// Can't download anything in offline mode
throw new RuntimeException("Missing artifact " + artifact + " in local repository, " +
"but working in offline mode");
}

boolean setupSource = !downloader.hasSource(remoteMavenRepository);
if (setupSource) {
// The download has the source not set already, add it now
downloader.addSource(remoteMavenRepository);
}

try {
// Install the artifact including dependencies
downloader.installArtifact(artifact, internalRepository);
} catch (IOException e) {
throw new FlintGradleException("Failed to install maven artifact", e);
}

if (setupSource) {
// We added the source, clean up afterwards
downloader.removeSource(remoteMavenRepository);
}
}
}

Expand Down
95 changes: 44 additions & 51 deletions src/main/java/net/flintmc/gradle/maven/MavenArtifactDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -59,10 +60,8 @@ public MavenArtifactDownloader() {
*
* @param source The repository to add
*/
public void addSource(ReadableMavenRepository source) {
synchronized (this.sources) {
this.sources.add(source);
}
public synchronized void addSource(ReadableMavenRepository source) {
this.sources.add(source);
}

/**
Expand All @@ -71,21 +70,17 @@ public void addSource(ReadableMavenRepository source) {
* @param source The repository to check if it exists as a source
* @return {@code true} if this downloader has the give repository as a source, {@code false} otherwise
*/
public boolean hasSource(ReadableMavenRepository source) {
synchronized (this.sources) {
return this.sources.contains(source);
}
public synchronized boolean hasSource(ReadableMavenRepository source) {
return this.sources.contains(source);
}

/**
* Removes the given repository as a source.
*
* @param source The repository to remove
*/
public void removeSource(ReadableMavenRepository source) {
synchronized (this.sources) {
this.sources.remove(source);
}
public synchronized void removeSource(ReadableMavenRepository source) {
this.sources.remove(source);
}

/**
Expand All @@ -97,7 +92,7 @@ public void removeSource(ReadableMavenRepository source) {
* @throws IOException If an I/O error occurs while installing the artifact or one if its dependencies
* @throws MavenResolveException If the artifact or one of its dependencies can't be resolved
*/
public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boolean installIfNotExists)
public synchronized void installAll(MavenArtifact artifact, SimpleMavenRepository target, boolean installIfNotExists)
throws IOException, MavenResolveException {
// Get the local POM path
Path localPomPath = target.getPomPath(artifact);
Expand All @@ -124,10 +119,12 @@ public void installAll(MavenArtifact artifact, SimpleMavenRepository target, boo
}

if (installIfNotExists) {
// If the artifact is not installed locally, try to install it
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);
synchronized (target) {
// If the artifact is not installed locally, try to install it
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 Down Expand Up @@ -190,10 +187,12 @@ private void installAll(MavenPom pom, SimpleMavenRepository target) throws IOExc
}
}

// Try to install the dependency locally
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);
synchronized (target) {
// Try to install the dependency locally
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 All @@ -218,19 +217,17 @@ private boolean shouldSkip(MavenDependency dependency) {
* @return An input stream from which the artifact can be read, or {@code null} if not found in the sources
* @throws IOException If an I/O error occurs while opening the stream
*/
public InputStream findArtifactStream(MavenArtifact artifact) throws IOException {
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
InputStream stream;
if ((stream = source.getArtifactStream(artifact)) != null) {
// Found the requested artifact
return stream;
}
public synchronized InputStream findArtifactStream(MavenArtifact artifact) throws IOException {
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 @@ -240,18 +237,16 @@ public InputStream findArtifactStream(MavenArtifact artifact) throws IOException
* @return The found URI, or {@code null}, if not found in the sources
* @throws IOException If an I/O error occurs while checking for the artifact
*/
public Pair<ReadableMavenRepository, URI> findArtifactURI(MavenArtifact artifact) throws IOException {
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
URI uri = source.getArtifactURI(artifact);
if (uri != null) {
return new Pair<>(source, uri);
}
public synchronized 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);
}

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

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

/**
Expand All @@ -262,13 +257,11 @@ 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 {
synchronized (this.sources) {
for (ReadableMavenRepository source : sources) {
MavenPom pom;
if ((pom = source.getArtifactPom(artifact)) != null) {
// Found the requested POM
return pom;
}
for (ReadableMavenRepository source : sources) {
MavenPom pom;
if ((pom = source.getArtifactPom(artifact)) != null) {
// Found the requested POM
return pom;
}
}

Expand All @@ -285,7 +278,7 @@ private MavenPom findPom(MavenArtifact artifact) throws IOException {
* @throws IOException If an I/O error occurs while installing the artifact
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException {
public synchronized boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository target) throws IOException {
try (InputStream stream = findArtifactStream(artifact)) {
// Try to find the given artifact
if (stream != null) {
Expand All @@ -299,7 +292,7 @@ public boolean installArtifact(MavenArtifact artifact, SimpleMavenRepository tar
LOGGER.lifecycle("Installing artifact {}", formatArtifact(artifact));

// Copy the artifact to the local path
Files.copy(stream, targetPath);
Files.copy(stream, targetPath, StandardCopyOption.REPLACE_EXISTING);
return true;
} else {
return false;
Expand Down

0 comments on commit 4027f58

Please sign in to comment.