Skip to content

Commit b68366a

Browse files
authored
Merge pull request #150 from scijava/scijava-meta/cache-poms
Add internal cache for caching POMs
2 parents 887e6b6 + 276a304 commit b68366a

File tree

1 file changed

+30
-6
lines changed
  • scijava-meta/src/main/java/org/scijava/meta

1 file changed

+30
-6
lines changed

scijava-meta/src/main/java/org/scijava/meta/POM.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
import java.io.IOException;
3434
import java.io.InputStream;
3535
import java.net.URL;
36-
import java.util.ArrayList;
37-
import java.util.Comparator;
38-
import java.util.Enumeration;
39-
import java.util.List;
36+
import java.util.*;
4037

4138
import javax.xml.parsers.ParserConfigurationException;
4239

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

228+
/**
229+
* internal cache used for calls to {@link #getPOM(Class, String, String)}
230+
*/
231+
private static final Map<URL, POM> POMS = new HashMap<>();
232+
231233
/**
232234
* Gets the Maven POM associated with the given class.
233235
*
@@ -239,17 +241,39 @@ public static POM getPOM(final Class<?> c) {
239241
*/
240242
public static POM getPOM(final Class<?> c, final String groupId,
241243
final String artifactId)
244+
{
245+
final URL location = Classes.location(c);
246+
return POMS.computeIfAbsent( //
247+
location, //
248+
l -> findPOM(l, groupId, artifactId) //
249+
);
250+
}
251+
252+
/**
253+
* Gets the Maven POM associated with the given class, always returning a new
254+
* {@link POM} object.
255+
*
256+
* @param location The {@link URL} to use as a base when searching for a
257+
* pom.xml.
258+
* @param groupId The Maven groupId of the desired POM.
259+
* @param artifactId The Maven artifactId of the desired POM.
260+
* @return {@link POM} object representing the discovered POM, or null if no
261+
* POM could be found.
262+
* @see #getPOM(Class, String, String), which does the same thing, but with an
263+
* internal cache that makes repeated calls trivial
264+
*/
265+
private static POM findPOM(final URL location, final String groupId,
266+
final String artifactId)
242267
{
243268
try {
244-
final URL location = Classes.location(c);
245269
if (!location.getProtocol().equals("file") || location.toString()
246270
.endsWith(".jar"))
247271
{
248272
// look for pom.xml in JAR's META-INF/maven subdirectory
249273
if (groupId == null || artifactId == null) {
250274
// groupId and/or artifactId is unknown; scan for the POM
251275
final URL pomBase = new URL("jar:" + //
252-
location.toString() + "!/META-INF/maven");
276+
location + "!/META-INF/maven");
253277
for (final URL url : URLs.listContents(pomBase, true, true)) {
254278
if (url.toExternalForm().endsWith("/pom.xml")) {
255279
return new POM(url);

0 commit comments

Comments
 (0)