Skip to content

Commit

Permalink
Fix apache#1017 Do not hardcode the TransformerFactory implementation…
Browse files Browse the repository at this point in the history
… irreversibly
  • Loading branch information
ppalaga committed Apr 2, 2020
1 parent bcb6a89 commit ac163da
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.quarkus.support.xalan.deployment;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.xalan", phase = ConfigPhase.BUILD_TIME)
public class CamelXalanBuildTimeConfig {

/**
* A fully qualified class name to set as the {@code javax.xml.transform.TransformerFactory} system property early
* at the application startup.
* <p>
* The system property effectively overrides any service providers defined in
* {@code META-INF/services/javax.xml.transform.TransformerFactory} files available in the class path. If the option
* is not present in your {@code application.properties}, the default value is used and the service providers are
* overridden anyway. To avoid overriding the service providers, set it to an empty value in
* {@code application.properties}:
*
* <pre>
* quarkus.camel.xalan.transformer-factory =
* </pre>
* <p>
* Note that any custom transformer factory you pass will only work in native mode if all necessary classes are
* registered for reflection and all necessary resources are included in the native image. This may already be the
* case for {@code com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl} if you depend on
* {@code io.quarkus:quarkus-jaxb} or {@code org.apache.xalan.xsltc.trax.TransformerFactoryImpl} if you depend on
* {@code org.apache.camel.quarkus:camel-quarkus-support-xalan}.
*/
@ConfigItem(defaultValue = "org.apache.camel.quarkus.support.xalan.XalanTransformerFactory")
public Optional<String> transformerFactory;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import java.util.Arrays;
import java.util.List;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.SystemPropertyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;

class XalanNativeImageProcessor {
@BuildStep
Expand Down Expand Up @@ -52,4 +55,29 @@ List<NativeImageResourceBuildItem> resources() {
return Arrays.asList(
new NativeImageResourceBuildItem("org/apache/xml/serializer/output_xml.properties"));
}

@BuildStep
void installTransformerFactory(
CamelXalanBuildTimeConfig config,
BuildProducer<SystemPropertyBuildItem> properties,
BuildProducer<ServiceProviderBuildItem> serviceProviders,
BuildProducer<NativeImageResourceBuildItem> nativeResources) {

config.transformerFactory
.ifPresent(val -> properties.produce(
/*
* If we do not do this, the service provider defined in xalan.jar's
* META-INF/services/javax.xml.transform.TransformerFactory
* wins over our factory on Java 11+ native
* I any case, the user has an option to pass his preferred factory instead of ours
*/
new SystemPropertyBuildItem("javax.xml.transform.TransformerFactory", val)));

serviceProviders.produce(
new ServiceProviderBuildItem("javax.xml.transform.TransformerFactory",
"org.apache.camel.quarkus.support.xalan.XalanTransformerFactory"));

nativeResources.produce(new NativeImageResourceBuildItem("META-INF/services/javax.xml.transform.TransformerFactory"));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.camel.quarkus.support.xalan.XalanTransformerFactory

0 comments on commit ac163da

Please sign in to comment.