Search in sources :

Example 1 with GeneratedNativeImageClassBuildItem

use of io.quarkus.deployment.builditem.GeneratedNativeImageClassBuildItem in project camel-quarkus by apache.

the class XalanNativeImageProcessor method installTransformerFactory.

@BuildStep
void installTransformerFactory(BuildProducer<GeneratedNativeImageClassBuildItem> nativeImageClass, BuildProducer<GeneratedResourceBuildItem> generatedResources) {
    final String serviceProviderFileContent = XalanTransformerFactory.class.getName() + "\n";
    /* This is primarily for the JVM mode */
    generatedResources.produce(new GeneratedResourceBuildItem(TRANSFORMER_FACTORY_SERVICE_FILE_PATH, serviceProviderFileContent.getBytes(StandardCharsets.UTF_8)));
    /* A low level way to embed only our service file in the native image.
         * There are at least two META-INF/services/javax.xml.transform.TransformerFactory files
         * in the class path: ours and the one from xalan.jar. As of GraalVM 19.3.1-java8, 19.3.1-java11,
         * 20.0.0-java8 and 20.0.0-java11, there is no way to ensure that ServiceProviderBuildItem
         * or NativeImageResourceBuildItem will pick the service file preferred by us.
         * We are thus forced to use this low level mechanism to ensure that.
         */
    final ClassCreator file = new ClassCreator(new ClassOutput() {

        @Override
        public void write(String s, byte[] bytes) {
            nativeImageClass.produce(new GeneratedNativeImageClassBuildItem(s, bytes));
        }
    }, getClass().getName() + "AutoFeature", null, Object.class.getName(), Feature.class.getName());
    file.addAnnotation("com.oracle.svm.core.annotate.AutomaticFeature");
    final MethodCreator beforeAn = file.getMethodCreator("beforeAnalysis", "V", Feature.BeforeAnalysisAccess.class.getName());
    final TryBlock overallCatch = beforeAn.tryBlock();
    overallCatch.invokeStaticMethod(ofMethod(ResourceUtils.class, "registerResources", void.class, String.class, String.class), overallCatch.load(TRANSFORMER_FACTORY_SERVICE_FILE_PATH), overallCatch.load(serviceProviderFileContent));
    final CatchBlockCreator print = overallCatch.addCatch(Throwable.class);
    print.invokeVirtualMethod(ofMethod(Throwable.class, "printStackTrace", void.class), print.getCaughtException());
    beforeAn.returnValue(null);
    file.close();
}
Also used : GeneratedResourceBuildItem(io.quarkus.deployment.builditem.GeneratedResourceBuildItem) GeneratedNativeImageClassBuildItem(io.quarkus.deployment.builditem.GeneratedNativeImageClassBuildItem) TryBlock(io.quarkus.gizmo.TryBlock) ClassCreator(io.quarkus.gizmo.ClassCreator) Feature(org.graalvm.nativeimage.hosted.Feature) MethodCreator(io.quarkus.gizmo.MethodCreator) ResourceUtils(org.apache.camel.quarkus.core.graal.ResourceUtils) ClassOutput(io.quarkus.gizmo.ClassOutput) XalanTransformerFactory(org.apache.camel.quarkus.support.xalan.XalanTransformerFactory) CatchBlockCreator(io.quarkus.gizmo.CatchBlockCreator) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BuildStep (io.quarkus.deployment.annotations.BuildStep)1 GeneratedNativeImageClassBuildItem (io.quarkus.deployment.builditem.GeneratedNativeImageClassBuildItem)1 GeneratedResourceBuildItem (io.quarkus.deployment.builditem.GeneratedResourceBuildItem)1 CatchBlockCreator (io.quarkus.gizmo.CatchBlockCreator)1 ClassCreator (io.quarkus.gizmo.ClassCreator)1 ClassOutput (io.quarkus.gizmo.ClassOutput)1 MethodCreator (io.quarkus.gizmo.MethodCreator)1 TryBlock (io.quarkus.gizmo.TryBlock)1 ResourceUtils (org.apache.camel.quarkus.core.graal.ResourceUtils)1 XalanTransformerFactory (org.apache.camel.quarkus.support.xalan.XalanTransformerFactory)1 Feature (org.graalvm.nativeimage.hosted.Feature)1