Skip to content

Commit

Permalink
Merge pull request quarkusio#116 from stuartwdouglas/pre-computed-ind…
Browse files Browse the repository at this point in the history
…exes

Add support for pre-generated jandex indexes
  • Loading branch information
stuartwdouglas authored Nov 20, 2018
2 parents dad0773 + 25a946f commit d4c26d0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
Expand All @@ -20,7 +19,10 @@
import java.util.function.Consumer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.shamrock.deployment.ApplicationArchive;
Expand All @@ -35,6 +37,8 @@ public class ApplicationArchiveLoader {
private static final String INDEX_DEPENDENCIES = "index-dependencies";
private static final String INDEX_JAR = "index-jar";

private static final String JANDEX_INDEX = "META-INF/jandex.idx";

public static List<ApplicationArchive> scanForOtherIndexes(ClassLoader classLoader, BuildConfig config, Set<String> applicationArchiveFiles, Path appRoot, List<Path> additionalApplicationArchives) throws IOException {

Set<Path> dependenciesToIndex = new HashSet<>();
Expand All @@ -44,7 +48,9 @@ public static List<ApplicationArchive> scanForOtherIndexes(ClassLoader classLoad
//get paths that are included via index-dependencies
dependenciesToIndex.addAll(getIndexDependencyPaths(config, classLoader));
//get paths that are included via marker files
dependenciesToIndex.addAll(getMarkerFilePaths(classLoader, applicationArchiveFiles));
Set<String> markers = new HashSet<>(applicationArchiveFiles);
markers.add(JANDEX_INDEX);
dependenciesToIndex.addAll(getMarkerFilePaths(classLoader, markers));

//we don't index the application root, this is handled elsewhere
dependenciesToIndex.remove(appRoot);
Expand All @@ -59,19 +65,16 @@ private static List<ApplicationArchive> indexPaths(Set<Path> dependenciesToIndex

for (final Path dep : dependenciesToIndex) {
if (Files.isDirectory(dep)) {
Indexer indexer = new Indexer();
handleFilePath(dep, indexer);
IndexView indexView = indexer.complete();
IndexView indexView = handleFilePath(dep);
ret.add(new ApplicationArchiveImpl(indexView, dep, null));
} else {
Indexer indexer = new Indexer();
handleJarPath(dep, indexer);
IndexView index = indexer.complete();
IndexView index = handleJarPath(dep);
FileSystem fs = FileSystems.newFileSystem(dep, classLoader);
ret.add(new ApplicationArchiveImpl(index, fs.getRootDirectories().iterator().next(), fs));
}
}


return ret;
}

Expand Down Expand Up @@ -148,7 +151,16 @@ public static List<Path> getIndexDependencyPaths(BuildConfig config, ClassLoader
}
}

private static void handleFilePath(Path path, Indexer indexer) throws IOException {
private static Index handleFilePath(Path path) throws IOException {
Path existing = path.resolve(JANDEX_INDEX);
if (Files.exists(existing)) {
try (FileInputStream in = new FileInputStream(existing.toFile())) {
IndexReader r = new IndexReader(in);
return r.read();
}
}

Indexer indexer = new Indexer();
Files.walk(path).forEach(new Consumer<Path>() {
@Override
public void accept(Path path) {
Expand All @@ -161,10 +173,20 @@ public void accept(Path path) {
}
}
});
return indexer.complete();
}

private static void handleJarPath(Path path, Indexer indexer) throws IOException {
private static Index handleJarPath(Path path) throws IOException {
Indexer indexer = new Indexer();
try (JarFile file = new JarFile(path.toFile())) {
ZipEntry existing = file.getEntry(JANDEX_INDEX);
if (existing != null) {
try (InputStream in = file.getInputStream(existing)) {
IndexReader r = new IndexReader(in);
return r.read();
}
}

Enumeration<JarEntry> e = file.entries();
while (e.hasMoreElements()) {
JarEntry entry = e.nextElement();
Expand All @@ -175,5 +197,6 @@ private static void handleJarPath(Path path, Indexer indexer) throws IOException
}
}
}
return indexer.complete();
}
}
21 changes: 21 additions & 0 deletions examples/shared-library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,26 @@
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
<!-- phase is 'process-classes by default' -->
<configuration>
<!-- Nothing needed here for simple cases -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

This file was deleted.

0 comments on commit d4c26d0

Please sign in to comment.