You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/04/20 11:23:34 UTC
[camel] 01/03: CAMEL-17894: cache reading the jandex file whenever possible
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit f982a2c9d97170374d239e6afbb02e73dbbd0f73
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Apr 20 10:37:31 2022 +0200
CAMEL-17894: cache reading the jandex file whenever possible
---
.../packaging/AbstractGenerateConfigurerMojo.java | 18 +----
.../packaging/EndpointSchemaGeneratorMojo.java | 10 +--
.../packaging/GenerateInvokeOnHeaderMojo.java | 17 +---
.../packaging/ModelXmlParserGeneratorMojo.java | 3 +-
.../camel/maven/packaging/PackageJaxbMojo.java | 1 +
.../camel/maven/packaging/SchemaGeneratorMojo.java | 12 +--
.../camel/maven/packaging/SpiGeneratorMojo.java | 7 +-
.../packaging/UpdateEagerClassloadedHelper.java | 15 ++--
.../maven/packaging/generics/JandexStore.java | 94 ++++++++++++++++++++++
.../packaging/generics/PackagePluginUtils.java | 42 +++++++---
10 files changed, 149 insertions(+), 70 deletions(-)
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java
index 1b7a334e0ab..2cc465c118a 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java
@@ -18,7 +18,6 @@ package org.apache.camel.maven.packaging;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
@@ -26,10 +25,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.NoSuchFileException;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -38,6 +33,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
+import org.apache.camel.maven.packaging.generics.PackagePluginUtils;
import org.apache.camel.spi.Metadata;
import org.apache.camel.tooling.model.BaseOptionModel;
import org.apache.camel.tooling.util.ReflectionHelper;
@@ -59,7 +55,6 @@ import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
-import org.jboss.jandex.IndexReader;
import static org.apache.camel.maven.packaging.generics.PackagePluginUtils.readJandexIndex;
import static org.apache.camel.tooling.util.ReflectionHelper.doWithMethods;
@@ -134,16 +129,7 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo
Set<String> bootstrapAndExtendedSet = new LinkedHashSet<>();
if (discoverClasses) {
- Path output = Paths.get(project.getBuild().getOutputDirectory());
- Index index;
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- index = new IndexReader(is).read();
- } catch (NoSuchFileException e) {
- // ignore if no jandex index
- index = null;
- } catch (IOException e) {
- throw new MojoExecutionException("IOException: " + e.getMessage(), e);
- }
+ Index index = PackagePluginUtils.readJandexIndexIgnoreMissing(project, getLog());
if (index != null) {
// discover all classes annotated with @Configurer
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointSchemaGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointSchemaGeneratorMojo.java
index dfcbbc88466..0c5cbd86340 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointSchemaGeneratorMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointSchemaGeneratorMojo.java
@@ -50,6 +50,7 @@ import java.util.stream.Stream;
import org.apache.camel.Category;
import org.apache.camel.maven.packaging.generics.ClassUtil;
import org.apache.camel.maven.packaging.generics.GenericsUtil;
+import org.apache.camel.maven.packaging.generics.PackagePluginUtils;
import org.apache.camel.spi.ApiMethod;
import org.apache.camel.spi.ApiParam;
import org.apache.camel.spi.ApiParams;
@@ -93,7 +94,6 @@ import org.jboss.forge.roaster.model.source.MethodSource;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
-import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexView;
import static java.lang.reflect.Modifier.isStatic;
@@ -1646,13 +1646,9 @@ public class EndpointSchemaGeneratorMojo extends AbstractGeneratorMojo {
private IndexView getIndex() {
if (indexView == null) {
- Path output = Paths.get(project.getBuild().getOutputDirectory());
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- indexView = new IndexReader(is).read();
- } catch (IOException e) {
- throw new RuntimeException("IOException: " + e.getMessage(), e);
- }
+ indexView = PackagePluginUtils.readJandexIndexQuietly(project);
}
+
return indexView;
}
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateInvokeOnHeaderMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateInvokeOnHeaderMojo.java
index 561c939f16f..2e87b04c612 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateInvokeOnHeaderMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/GenerateInvokeOnHeaderMojo.java
@@ -18,12 +18,7 @@ package org.apache.camel.maven.packaging;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.io.StringWriter;
-import java.nio.file.Files;
-import java.nio.file.NoSuchFileException;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
@@ -33,6 +28,7 @@ import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeSet;
+import org.apache.camel.maven.packaging.generics.PackagePluginUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -42,7 +38,6 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
-import org.jboss.jandex.IndexReader;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
@@ -124,15 +119,9 @@ public class GenerateInvokeOnHeaderMojo extends AbstractGeneratorMojo {
resourcesOutputDir = new File(project.getBasedir(), "src/generated/resources");
}
- Path output = Paths.get(project.getBuild().getOutputDirectory());
- Index index;
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- index = new IndexReader(is).read();
- } catch (NoSuchFileException e) {
- // ignore
+ Index index = PackagePluginUtils.readJandexIndexIgnoreMissing(project, getLog());
+ if (index == null) {
return;
- } catch (IOException e) {
- throw new MojoExecutionException("IOException: " + e.getMessage(), e);
}
Map<String, Set<InvokeOnHeaderModel>> classes = new HashMap<>();
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ModelXmlParserGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ModelXmlParserGeneratorMojo.java
index 3b8a562f42d..655f6b90c27 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ModelXmlParserGeneratorMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/ModelXmlParserGeneratorMojo.java
@@ -60,6 +60,7 @@ import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import org.apache.camel.maven.packaging.generics.JandexStore;
import org.apache.camel.tooling.util.srcgen.GenericType;
import org.apache.camel.tooling.util.srcgen.JavaClass;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
@@ -144,7 +145,7 @@ public class ModelXmlParserGeneratorMojo extends AbstractGeneratorMojo {
expressionDefinitionClass = loadClass(classLoader, MODEL_PACKAGE + ".language.ExpressionDefinition");
String resName = routesDefinitionClass.getName().replace('.', '/') + ".class";
- String url = classLoader.getResource(resName).toExternalForm().replace(resName, "META-INF/jandex.idx");
+ String url = classLoader.getResource(resName).toExternalForm().replace(resName, JandexStore.DEFAULT_NAME);
Index index;
try (InputStream is = new URL(url).openStream()) {
index = new IndexReader(is).read();
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java
index 8e10145b2f0..207990e521e 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJaxbMojo.java
@@ -139,6 +139,7 @@ public class PackageJaxbMojo extends AbstractGeneratorMojo {
}
private IndexView createIndex(List<String> locations) throws MojoExecutionException {
+
if (index.exists()) {
try (InputStream is = new FileInputStream(index)) {
IndexReader r = new IndexReader(is);
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SchemaGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SchemaGeneratorMojo.java
index 5c4d9084ccd..26842e9dd2c 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SchemaGeneratorMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SchemaGeneratorMojo.java
@@ -18,15 +18,12 @@ package org.apache.camel.maven.packaging;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
-import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
@@ -47,6 +44,7 @@ import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import org.apache.camel.maven.packaging.generics.GenericsUtil;
+import org.apache.camel.maven.packaging.generics.PackagePluginUtils;
import org.apache.camel.spi.AsPredicate;
import org.apache.camel.spi.Metadata;
import org.apache.camel.tooling.model.EipModel;
@@ -71,7 +69,6 @@ import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassInfo.NestingType;
import org.jboss.jandex.DotName;
-import org.jboss.jandex.IndexReader;
import org.jboss.jandex.IndexView;
@Mojo(name = "generate-schema", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
@@ -243,12 +240,7 @@ public class SchemaGeneratorMojo extends AbstractGeneratorMojo {
private IndexView getIndex() {
if (indexView == null) {
- Path output = Paths.get(project.getBuild().getOutputDirectory());
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- indexView = new IndexReader(is).read();
- } catch (IOException e) {
- throw new RuntimeException("IOException: " + e.getMessage(), e);
- }
+ indexView = PackagePluginUtils.readJandexIndexQuietly(project);
}
return indexView;
}
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
index 80f7f19b435..b5256d51517 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpiGeneratorMojo.java
@@ -31,6 +31,7 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
+import org.apache.camel.maven.packaging.generics.PackagePluginUtils;
import org.apache.camel.spi.annotations.ConstantProvider;
import org.apache.camel.spi.annotations.ServiceFactory;
import org.apache.maven.plugin.MojoExecutionException;
@@ -158,10 +159,8 @@ public class SpiGeneratorMojo extends AbstractGeneratorMojo {
private IndexView getIndex() throws MojoExecutionException {
try {
List<IndexView> indices = new ArrayList<>();
- Path output = Paths.get(project.getBuild().getOutputDirectory());
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- indices.add(new IndexReader(is).read());
- }
+ indices.add(PackagePluginUtils.readJandexIndex(project));
+
for (String cpe : project.getCompileClasspathElements()) {
if (cpe.matches(".*/camel-[^/]+.jar")) {
try (JarFile jf = new JarFile(cpe)) {
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateEagerClassloadedHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateEagerClassloadedHelper.java
index 24bbc5ee2a4..3d5ec2d6c82 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateEagerClassloadedHelper.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateEagerClassloadedHelper.java
@@ -17,9 +17,6 @@
package org.apache.camel.maven.packaging;
import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@@ -27,6 +24,7 @@ import java.util.Set;
import java.util.StringJoiner;
import java.util.TreeSet;
+import org.apache.camel.maven.packaging.generics.JandexStore;
import org.apache.camel.tooling.util.PackageHelper;
import org.apache.camel.tooling.util.Strings;
import org.apache.maven.plugin.MojoExecutionException;
@@ -38,7 +36,6 @@ import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
-import org.jboss.jandex.IndexReader;
import static org.apache.camel.tooling.util.PackageHelper.findCamelDirectory;
@@ -97,13 +94,15 @@ public class UpdateEagerClassloadedHelper extends AbstractGeneratorMojo {
private void discoverClasses(Path output, Set<String> fqns) {
Index index;
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- index = new IndexReader(is).read();
- } catch (IOException e) {
- // ignore
+
+ final JandexStore.Jandex jandex = JandexStore.read(output);
+ if (jandex.getException() != null) {
+ getLog().warn("Jandex reading path failed" + jandex.getException().getMessage(), jandex.getException());
return;
}
+ index = jandex.getIndex();
+
// discover all classes annotated with @EagerClassloaded
List<AnnotationInstance> annotations = index.getAnnotations(EAGER_CLASSLOADED);
annotations.stream()
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/JandexStore.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/JandexStore.java
new file mode 100644
index 00000000000..a1f381627fd
--- /dev/null
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/JandexStore.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.maven.packaging.generics;
+
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.Path;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.jboss.jandex.Index;
+import org.jboss.jandex.IndexReader;
+
+public final class JandexStore {
+ public static class Jandex {
+ private final Index index;
+ private final Exception exception;
+
+ public Jandex(Index index) {
+ this(index, null);
+ }
+
+ public Jandex(Exception exception) {
+ this(null, exception);
+ }
+
+ private Jandex(Index index, Exception exception) {
+ this.index = index;
+ this.exception = exception;
+ }
+
+ public Index getIndex() {
+ return index;
+ }
+
+ public Exception getException() {
+ return exception;
+ }
+
+ public boolean doesNotExist() {
+ if (exception instanceof NoSuchFileException) {
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ public static final String DEFAULT_NAME = "META-INF/jandex.idx";
+ private static final Map<Path, Jandex> JANDEX_CACHE = new ConcurrentHashMap<>();
+
+ private JandexStore() {
+ }
+
+ public static Jandex nonCachedRead(Path path) {
+ try (InputStream is = Files.newInputStream(path)) {
+ IndexReader reader = new IndexReader(is);
+ Index index = reader.read();
+
+ return new Jandex(index);
+ } catch (Exception e) {
+ return new Jandex(e);
+ }
+ }
+
+ public static Jandex read(Path outputDir, String filename) {
+ Path path = outputDir.resolve(filename);
+
+ return JANDEX_CACHE.computeIfAbsent(path, k -> nonCachedRead(path));
+ }
+
+ public static Jandex read(Path outputDir) {
+ Path path = outputDir.resolve(DEFAULT_NAME);
+
+ return JANDEX_CACHE.computeIfAbsent(path, k -> nonCachedRead(path));
+ }
+
+}
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/PackagePluginUtils.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/PackagePluginUtils.java
index e4612094fcc..271b2bcddc3 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/PackagePluginUtils.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/PackagePluginUtils.java
@@ -16,16 +16,13 @@
*/
package org.apache.camel.maven.packaging.generics;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.jboss.jandex.Index;
-import org.jboss.jandex.IndexReader;
public final class PackagePluginUtils {
@@ -34,13 +31,38 @@ public final class PackagePluginUtils {
public static Index readJandexIndex(MavenProject project) throws MojoExecutionException {
Path output = Paths.get(project.getBuild().getOutputDirectory());
- Index index;
- try (InputStream is = Files.newInputStream(output.resolve("META-INF/jandex.idx"))) {
- index = new IndexReader(is).read();
- } catch (IOException e) {
- throw new MojoExecutionException("IOException: " + e.getMessage(), e);
+ final JandexStore.Jandex jandex = JandexStore.read(output);
+ if (jandex.getException() != null) {
+ throw new MojoExecutionException("IOException: " + jandex.getException(), jandex.getException());
}
- return index;
+
+ return jandex.getIndex();
+ }
+
+ public static Index readJandexIndexQuietly(MavenProject project) {
+ Path output = Paths.get(project.getBuild().getOutputDirectory());
+ final JandexStore.Jandex jandex = JandexStore.read(output);
+ if (jandex.getException() != null) {
+ throw new RuntimeException("IOException: " + jandex.getException(), jandex.getException());
+ }
+
+ return jandex.getIndex();
+ }
+
+ public static Index readJandexIndexIgnoreMissing(MavenProject project, Log log) throws MojoExecutionException {
+ Path output = Paths.get(project.getBuild().getOutputDirectory());
+ final JandexStore.Jandex jandex = JandexStore.read(output);
+
+ if (jandex.getException() != null) {
+ if (!jandex.doesNotExist()) {
+ throw new MojoExecutionException(
+ "IOException: " + jandex.getException().getMessage(), jandex.getException());
+ }
+
+ log.warn("Jandex reading failed: " + jandex.getException().getMessage());
+ }
+
+ return jandex.getIndex();
}
public static String joinHeaderAndSource(String licenseHeader, String source) {