public List<Artifact> scan(Contribution contribution) throws ContributionReadException {
// Assume the URL references a JAR file
try {
URL url = new URL(contribution.getLocation());
JarInputStream jar = new JarInputStream(IOHelper.openStream(url));
try {
Set<String> names = new HashSet<String>();
while (true) {
JarEntry entry = jar.getNextJarEntry();
if (entry == null) {
// EOF
break;
}
String name = entry.getName();
if (name.length() != 0 && !name.startsWith(".")) {
// Trim trailing /
if (name.endsWith("/")) {
name = name.substring(0, name.length() - 1);
}
// Add the entry name
if (!names.contains(name)) {
names.add(name);
// Add parent folder names to the list too
for (;;) {
int s = name.lastIndexOf('/');
if (s == -1) {
name = "";
} else {
name = name.substring(0, s);
}
if (name.length() != 0 && !names.contains(name)) {
names.add(name);
} else {
break;
}
}
}
}
}
// Return list of artifacts
List<Artifact> artifacts = new ArrayList<Artifact>();
for(String uri : names) {
Artifact artifact = contributionFactory.createArtifact();
artifact.setURI(uri);
artifact.setLocation(getArtifactURL(contribution, uri).toString());
artifacts.add(artifact);
}
contribution.getTypes().add(getContributionType());
return artifacts;
} finally {
jar.close();
}
} catch (IOException e) {
throw new ContributionReadException(e);
}
}