Skip to content

Commit

Permalink
Merge pull request #3426 from jamezp/RESTEASY-3245
Browse files Browse the repository at this point in the history
[RESTEASY-3245] Migrate to SmallRye Jandex.
  • Loading branch information
jamezp committed Feb 16, 2023
2 parents a22d4ce + 70a9364 commit 54885c7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 39 deletions.
8 changes: 4 additions & 4 deletions docbook/reference/en/en-US/modules/Jakarta_Se_Bootstrap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
</programlisting>
</para>
<para>
It&apos;s also suggested that if the resources to be used in the application are not explicitly define,
then use the <code>org.jboss.jandex:jandex-maven-plugin</code> to create a Jandex Index. Without this
It&apos;s also suggested that if you do not explicitly define the resources to be used in your application
that you use the <code>io.smallrye:jandex-maven-plugin</code> to create a Jandex Index. Without this
the class path will be scanned for resources which could have significant performance impacts.
<programlisting>
&lt;plugin&gt;
&lt;groupId&gt;org.jboss.jandex&lt;/groupId&gt;
&lt;groupId&gt;io.smallrye&lt;/groupId&gt;
&lt;artifactId&gt;jandex-maven-plugin&lt;/artifactId&gt;
&lt;executions&gt;
&lt;execution&gt;
Expand Down Expand Up @@ -96,4 +96,4 @@ final SeBootstrap.Configuration configuration = SeBootstrap.Configuration.builde
</programlisting>
</para>
</section>
</chapter>
</chapter>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@
<exclude>javax.xml.soap:saaj-api</exclude>
<exclude>javax.xml.stream:stax-api</exclude>
<exclude>log4j:log4j</exclude>
<exlude>org.jboss:jandex</exlude>
</excludes>
</bannedDependencies>
</rules>
Expand Down
2 changes: 1 addition & 1 deletion resteasy-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</dependency>

<dependency>
<groupId>org.jboss</groupId>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -55,6 +51,8 @@
import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.jandex.JarIndexer;
import org.jboss.jandex.Result;

/**
* A scanner for locating resources.
Expand Down Expand Up @@ -123,10 +121,10 @@ public static ResourceScanner fromClassPath(final ClassLoader cl) throws IOExcep
* @throws IOException if there is an error reading the index
*/
public static ResourceScanner fromClassPath(final ClassLoader cl, final Predicate<Path> filter) throws IOException {
final Collection<IndexView> indexes = new ArrayList<>();
// Check for a jandex.idx
final Enumeration<URL> resources = cl.getResources("META-INF/jandex.idx");
if (resources.hasMoreElements()) {
final Collection<IndexView> indexes = new ArrayList<>();
while (resources.hasMoreElements()) {
try (InputStream in = resources.nextElement().openStream()) {
final IndexReader reader = new IndexReader(in);
Expand All @@ -136,6 +134,7 @@ public static ResourceScanner fromClassPath(final ClassLoader cl, final Predicat
return new ResourceScanner(CompositeIndex.create(indexes));
}
final Indexer indexer = new Indexer();

final String[] cpEntries = System.getProperty("java.class.path").split(File.pathSeparator);
for (String entry : cpEntries) {
final Path path = Paths.get(entry);
Expand All @@ -154,11 +153,12 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
} else if (path.getFileName().toString().endsWith(".class")) {
indexClass(indexer, path);
} else if (path.getFileName().toString().endsWith(".jar")) {
indexJar(indexer, path);
indexes.add(indexJar(indexer, path).getIndex());
}
}
}
return new ResourceScanner(indexer.complete());
indexes.add(indexer.complete());
return new ResourceScanner(CompositeIndex.create(indexes));
}

/**
Expand Down Expand Up @@ -239,19 +239,8 @@ private Collection<ClassInfo> resolveTypeFromAnnotation(final DotName annotation
return results;
}

private static void indexJar(final Indexer indexer, final Path jar) throws IOException {
try (FileSystem fs = getZipFs(jar)) {
Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
if (file.getFileName().toString().endsWith(".class")) {
indexClass(indexer, file);
}
return FileVisitResult.CONTINUE;
}
});
}
private static Result indexJar(final Indexer indexer, final Path jar) throws IOException {
return JarIndexer.createJarIndex(jar.toFile(), indexer, false, false, false);
}

private static void indexClass(final Indexer indexer, final Path file) throws IOException {
Expand All @@ -260,24 +249,15 @@ private static void indexClass(final Indexer indexer, final Path file) throws IO
}
}

private static FileSystem getZipFs(final Path path) throws IOException {
final URI uri = URI.create("jar:" + path.toUri());
try {
return FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException ignore) {
}
return FileSystems.newFileSystem(uri, Collections.emptyMap());
}

private static class PrioritySorter implements Comparator<ClassInfo> {
static final PrioritySorter INSTANCE = new PrioritySorter();

@Override
public int compare(final ClassInfo o1, final ClassInfo o2) {
int p1 = Integer.MAX_VALUE;
int p2 = Integer.MAX_VALUE;
final AnnotationInstance pa1 = o1.classAnnotation(PRIORITY);
final AnnotationInstance pa2 = o2.classAnnotation(PRIORITY);
final AnnotationInstance pa1 = o1.declaredAnnotation(PRIORITY);
final AnnotationInstance pa2 = o2.declaredAnnotation(PRIORITY);
if (pa1 != null) {
p1 = pa1.value().asInt();
}
Expand Down
6 changes: 3 additions & 3 deletions resteasy-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<version.com.github.java-json-tools.jackson-coreutils>2.0</version.com.github.java-json-tools.jackson-coreutils>
<version.com.github.java-json-tools.json-patch>1.13</version.com.github.java-json-tools.json-patch>
<version.com.github.java-json-tools.msg-simple>1.2</version.com.github.java-json-tools.msg-simple>
<version.org.jboss.jandex>2.4.3.Final</version.org.jboss.jandex>
<version.io.smallrye.jandex>3.0.5</version.io.smallrye.jandex>

<version.io.reactivex.rxjava2-rxjava>2.2.21</version.io.reactivex.rxjava2-rxjava>
<version.org.reactivestreams>1.0.4</version.org.reactivestreams>
Expand All @@ -119,9 +119,9 @@
</dependency>

<dependency>
<groupId>org.jboss</groupId>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<version>${version.org.jboss.jandex}</version>
<version>${version.io.smallrye.jandex}</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 54885c7

Please sign in to comment.