Skip to content

Commit

Permalink
Merge pull request #150 from scijava/scijava-meta/cache-poms
Browse files Browse the repository at this point in the history
Add internal cache for caching POMs
  • Loading branch information
gselzer authored Apr 18, 2024
2 parents 887e6b6 + 276a304 commit b68366a
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions scijava-meta/src/main/java/org/scijava/meta/POM.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.*;

import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -228,6 +225,11 @@ public static POM getPOM(final Class<?> c) {
return getPOM(c, null, null);
}

/**
* internal cache used for calls to {@link #getPOM(Class, String, String)}
*/
private static final Map<URL, POM> POMS = new HashMap<>();

/**
* Gets the Maven POM associated with the given class.
*
Expand All @@ -239,17 +241,39 @@ public static POM getPOM(final Class<?> c) {
*/
public static POM getPOM(final Class<?> c, final String groupId,
final String artifactId)
{
final URL location = Classes.location(c);
return POMS.computeIfAbsent( //
location, //
l -> findPOM(l, groupId, artifactId) //
);
}

/**
* Gets the Maven POM associated with the given class, always returning a new
* {@link POM} object.
*
* @param location The {@link URL} to use as a base when searching for a
* pom.xml.
* @param groupId The Maven groupId of the desired POM.
* @param artifactId The Maven artifactId of the desired POM.
* @return {@link POM} object representing the discovered POM, or null if no
* POM could be found.
* @see #getPOM(Class, String, String), which does the same thing, but with an
* internal cache that makes repeated calls trivial
*/
private static POM findPOM(final URL location, final String groupId,
final String artifactId)
{
try {
final URL location = Classes.location(c);
if (!location.getProtocol().equals("file") || location.toString()
.endsWith(".jar"))
{
// look for pom.xml in JAR's META-INF/maven subdirectory
if (groupId == null || artifactId == null) {
// groupId and/or artifactId is unknown; scan for the POM
final URL pomBase = new URL("jar:" + //
location.toString() + "!/META-INF/maven");
location + "!/META-INF/maven");
for (final URL url : URLs.listContents(pomBase, true, true)) {
if (url.toExternalForm().endsWith("/pom.xml")) {
return new POM(url);
Expand Down

0 comments on commit b68366a

Please sign in to comment.