Skip to content

Commit

Permalink
Merge pull request jboss#1 from jimma/CXF-8371
Browse files Browse the repository at this point in the history
Set compiler target to 11; Fix several issues in JAXBUtils
  • Loading branch information
reta authored Feb 17, 2022
2 parents c81da77 + 9cfd9f9 commit f511b3a
Show file tree
Hide file tree
Showing 42 changed files with 92 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ && checkObjectFactoryNamespaces(clz)) {
if (HAS_MOXY) {
map.put("eclipselink.default-target-namespace", defaultNs);
}
map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs);
map.put("org.glassfish.jaxb.defaultNamespaceRemap", defaultNs);
}
if (props != null) {
map.putAll(props);
Expand Down Expand Up @@ -298,18 +298,12 @@ private static JAXBContext createContext(final Set<Class<?>> classes,
JAXBContext ctx;
if (typeRefs != null && !typeRefs.isEmpty()) {
Class<?> fact = null;
String pfx = "com.sun.xml.bind.";
String pfx = "org.glassfish.jaxb.";
try {
fact = ClassLoaderUtils.loadClass("com.sun.xml.bind.v2.ContextFactory",
fact = ClassLoaderUtils.loadClass("org.glassfish.jaxb.runtime.v2.ContextFactory",
JAXBContextCache.class);
} catch (Throwable t) {
try {
fact = ClassLoaderUtils.loadClass("com.sun.xml.internal.bind.v2.ContextFactory",
JAXBContextCache.class);
pfx = "com.sun.xml.internal.bind.";
} catch (Throwable t2) {
//ignore
}
//ignore
}
if (fact != null) {
for (Method m : fact.getMethods()) {
Expand Down Expand Up @@ -348,22 +342,14 @@ public JAXBContext run() throws Exception {
} catch (PrivilegedActionException e2) {
if (e2.getException() instanceof JAXBException) {
JAXBException ex = (JAXBException)e2.getException();
if (map.containsKey("com.sun.xml.bind.defaultNamespaceRemap")
&& ex.getMessage() != null
&& ex.getMessage().contains("com.sun.xml.bind.defaultNamespaceRemap")) {
map.put("com.sun.xml.internal.bind.defaultNamespaceRemap",
map.remove("com.sun.xml.bind.defaultNamespaceRemap"));
ctx = JAXBContext.newInstance(classes.toArray(new Class<?>[0]), map);
} else {
throw ex;
}
throw ex;
} else {
throw new RuntimeException(e2.getException());
}
}
return ctx;
}
// Now we can not add all the classes that Jaxb needed into JaxbContext,
// Now we can not add all the classes that Jaxb needed into JaxbContext
// especially when
// an ObjectFactory is pointed to by an jaxb @XmlElementDecl annotation
// added this workaround method to load the jaxb needed ObjectFactory class
Expand Down
45 changes: 22 additions & 23 deletions core/src/main/java/org/apache/cxf/common/jaxb/JAXBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
import org.apache.cxf.helpers.JavaUtils;

public final class JAXBUtils {
public static final String JAXB_URI = "http://java.sun.com/xml/ns/jaxb";
public static final String JAXB_URI = "https://jakarta.ee/xml/ns/jaxb";

private static final Logger LOG = LogUtils.getL7dLogger(JAXBUtils.class);

Expand Down Expand Up @@ -587,6 +587,7 @@ public static Class<?> getValidClass(Class<?> cls) {
}

private static synchronized ClassLoader getXJCClassLoader() {
//TODO: review and remove this after JDK11
if (jaxbXjcLoader == null) {
try {
Class.forName("com.sun.tools.internal.xjc.api.XJC");
Expand Down Expand Up @@ -620,11 +621,8 @@ public static Object setNamespaceMapper(Bus bus, final Map<String, String> nspre
ClassLoaderService classLoaderService = bus.getExtension(ClassLoaderService.class);
Object mapper = classLoaderService.createNamespaceWrapperInstance(marshaller.getClass(), nspref);
if (mapper != null) {
if (marshaller.getClass().getName().contains(".internal.")) {
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
mapper);
} else if (marshaller.getClass().getName().contains("com.sun")) {
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
if (marshaller.getClass().getName().contains("org.glassfish")) {
marshaller.setProperty("org.glassfish.jaxb.namespacePrefixMapper",
mapper);
} else if (marshaller.getClass().getName().contains("eclipse")) {
marshaller.setProperty("eclipselink.namespace-prefix-mapper",
Expand All @@ -640,13 +638,12 @@ public static BridgeWrapper createBridge(Set<Class<?>> ctxClasses,
try {
Class<?> cls;
Class<?> refClass;
String pkg = "com.sun.xml.bind.";
String pkg = "org.glassfish.jaxb.runtime.api.";
try {
cls = Class.forName("com.sun.xml.bind.api.JAXBRIContext");
cls = Class.forName("org.glassfish.jaxb.runtime.api.JAXBRIContext");
refClass = Class.forName(pkg + "api.TypeReference");
} catch (ClassNotFoundException e) {
cls = Class.forName("com.sun.xml.internal.bind.api.JAXBRIContext", true, getXJCClassLoader());
pkg = "com.sun.xml.internal.bind.";
cls = Class.forName("org.glassfish.jaxb.runtime.api.JAXBRIContext", true, getXJCClassLoader());
refClass = Class.forName(pkg + "api.TypeReference", true, getXJCClassLoader());
}
Object ref = refClass.getConstructor(QName.class,
Expand Down Expand Up @@ -705,7 +702,7 @@ public static SchemaCompiler createSchemaCompiler() throws JAXBException {
cls = Class.forName("com.sun.tools.xjc.api.XJC");
sc = cls.getMethod("createSchemaCompiler").invoke(null);
} catch (Throwable e) {
cls = Class.forName("com.sun.tools.internal.xjc.api.XJC", true, getXJCClassLoader());
cls = Class.forName("com.sun.tools.xjc.api.XJC", true, getXJCClassLoader());
sc = cls.getMethod("createSchemaCompiler").invoke(null);
}

Expand Down Expand Up @@ -775,6 +772,7 @@ public static Object createFileCodeWriter(File f, String encoding) throws JAXBEx
try {
cls = Class.forName("com.sun.codemodel.writer.FileCodeWriter");
} catch (ClassNotFoundException e) {
//TODO: review and remove this check
cls = Class.forName("com.sun.codemodel.internal.writer.FileCodeWriter",
true, getXJCClassLoader());
}
Expand Down Expand Up @@ -1084,7 +1082,8 @@ public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx) {
public static JAXBContextProxy createJAXBContextProxy(final JAXBContext ctx,
final SchemaCollection collection,
final String defaultNs) {
if (ctx.getClass().getName().contains("com.sun.")
if (ctx.getClass().getName().contains("org.glassfish.")
|| ctx.getClass().getName().contains("com.sun.")
|| collection == null) {
return ReflectionInvokationHandler.createProxyWrapper(ctx, JAXBContextProxy.class);
}
Expand All @@ -1103,13 +1102,14 @@ public static JAXBBeanInfo getBeanInfo(JAXBContextProxy context, Class<?> cls) {

private static String getPostfix(Class<?> cls) {
String className = cls.getName();
if (className.contains("com.sun.xml.internal")
|| className.contains("eclipse")) {
//eclipse moxy accepts sun package CharacterEscapeHandler
return ".internal";
} else if (className.contains("com.sun.xml.bind")
|| className.startsWith("com.ibm.xml")) {
//TODO: review and remove "com.sun" package name check
if (className.contains("org.glassfish.jaxb") || className.contains("com.sun.xml.bind")
|| className.startsWith("com.ibm.xml")) {
return "";
} else if (className.contains("com.sun.xml.internal")
|| className.contains("eclipse")) {
//eclipse moxy accepts sun package CharacterEscapeHandler
return ".internal";
}
return null;
}
Expand All @@ -1132,7 +1132,7 @@ public static void setEscapeHandler(Marshaller marshaller, Object escapeHandler)
try {
String postFix = getPostfix(marshaller.getClass());
if (postFix != null && escapeHandler != null) {
marshaller.setProperty("com.sun.xml" + postFix + ".bind.characterEscapeHandler", escapeHandler);
marshaller.setProperty("org.glassfish.jaxb" + postFix + ".characterEscapeHandler", escapeHandler);
}
} catch (PropertyException e) {
LOG.log(Level.INFO, "Failed to set MinumEscapeHandler to jaxb marshaller", e);
Expand All @@ -1155,11 +1155,10 @@ private static Object createEscapeHandler(Class<?> cls, String simpleClassName)
+ cls);
return null;
}
Class<?> handlerClass = ClassLoaderUtils.loadClass("com.sun.xml" + postFix
+ ".bind.marshaller." + simpleClassName,
cls);
Class<?> handlerClass = ClassLoaderUtils.loadClass("org.glassfish.jaxb.core.marshaller."
+ simpleClassName, cls);
Class<?> handlerInterface = ClassLoaderUtils
.loadClass("com.sun.xml" + postFix + ".bind.marshaller.CharacterEscapeHandler",
.loadClass("org.glassfish.jaxb.core.marshaller.CharacterEscapeHandler",
cls);
Object targetHandler = ReflectionUtil.getDeclaredField(handlerClass, "theInstance").get(null);
return ProxyHelper.getProxy(cls.getClassLoader(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public synchronized Class<?> createNamespaceWrapperClass(Class<?> mcls, Map<Stri
return createEclipseNamespaceMapper();
} else if (mcls.getName().contains(".internal")) {
postFix = "Internal";
} else if (mcls.getName().contains("com.sun")) {
} else if (mcls.getName().contains("org.glassfish")) {
postFix = "RI";
}

Expand Down Expand Up @@ -294,9 +294,7 @@ private byte[] doCreateEclipseNamespaceMapper() {

private byte[] createNamespaceWrapperInternal(String postFix) {

String superName = "com/sun/xml/"
+ ("RI".equals(postFix) ? "" : "internal/")
+ "bind/marshaller/NamespacePrefixMapper";
String superName = "org/glassfish/jaxb/runtime/marshaller/NamespacePrefixMapper";
String postFixedName = "org/apache/cxf/jaxb/NamespaceMapper" + postFix;
ASMHelper.ClassWriter cw = helper.createClassWriter();
if (cw == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public void testCanSetEscapeHandlerWithStandardJaxbImpl() throws Exception {
Marshaller m = new MarshallerImpl((JAXBContextImpl) JAXBContext.newInstance(this.getClass()), null);
final Object mockHandler = MinimumEscapeHandler.theInstance;
JAXBUtils.setEscapeHandler(m, mockHandler);
assertEquals(mockHandler, m.getProperty("com.sun.xml.bind.characterEscapeHandler"));
assertEquals(mockHandler, m.getProperty("org.glassfish.jaxb.characterEscapeHandler"));
}

@Test
public void testCanSetEscapeHandlerWithIbmJaxbImpl() throws Exception {
Marshaller m = new MockMarshaller();
final Object mockHandler = new Object();
JAXBUtils.setEscapeHandler(m, mockHandler);
assertEquals(mockHandler, m.getProperty("com.sun.xml.bind.characterEscapeHandler"));
assertEquals(mockHandler, m.getProperty("org.glassfish.jaxb.characterEscapeHandler"));
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<cxf.compiler.fork>false</cxf.compiler.fork>
<cxf.build-utils.version>3.4.5-SNAPSHOT</cxf.build-utils.version>
<cxf.xjc-utils.version>4.0.0-SNAPSHOT</cxf.xjc-utils.version>
<cxf.jdk.version>17</cxf.jdk.version>
<cxf.jdk.version>11</cxf.jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<site.deploy.url>scp://people.apache.org/www/cxf.apache.org/maven-site</site.deploy.url>
<maven-owasp-plugin-version>6.1.6</maven-owasp-plugin-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
See http://weblogs.java.net/blog/kohsuke/archive/2006/03/simple_and_bett.html
-->
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" jaxb:version="3.0"
xmlns:xjc="https://jakarta.ee/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">

<jaxb:globalBindings generateElementProperty="false">
<xjc:simple />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public void testInitializeUnmarshallerProperties() throws Exception {
Map<String, Object> unmarshallerProperties = new HashMap<>();
unmarshallerProperties.put("someproperty", "somevalue");
db.setUnmarshallerProperties(unmarshallerProperties);

db.initialize(service);

assertEquals("somevalue", db.getUnmarshallerProperties().get("someproperty"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

import org.w3c.dom.Node;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
Expand Down Expand Up @@ -198,7 +197,7 @@ public void testContextProperties() throws Exception {
nsMap.put("uri:ultima:thule", "");
db.setNamespaceMap(nsMap);
Map<String, Object> contextProperties = new HashMap<>();
contextProperties.put("com.sun.xml.bind.defaultNamespaceRemap", "uri:ultima:thule");
contextProperties.put("org.glassfish.jaxb.defaultNamespaceRemap", "uri:ultima:thule");
db.setContextProperties(contextProperties);
Set<Class<?>> classes = new HashSet<>();
classes.add(UnqualifiedBean.class);
Expand Down Expand Up @@ -226,29 +225,22 @@ JAXBDataBinding createJaxbContext(boolean internal) throws Exception {
classes.add(QualifiedBean.class);

//have to fastboot to avoid conflicts of generated accessors
System.setProperty("com.sun.xml.bind.v2.runtime.JAXBContextImpl.fastBoot", "true");
System.setProperty("com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.fastBoot", "true");
if (internal) {
System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY, "com.sun.xml.internal.bind.v2.ContextFactory");
db.setContext(db.createJAXBContext(classes));
System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
} else {
db.setContext(db.createJAXBContext(classes));
}
System.clearProperty("com.sun.xml.bind.v2.runtime.JAXBContextImpl.fastBoot");
System.clearProperty("com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.fastBoot");
System.setProperty("org.glassfish.jaxb.v2.runtime.JAXBContextImpl.fastBoot", "true");
db.setContext(db.createJAXBContext(classes));
System.clearProperty("org.glassfish.jaxb.v2.runtime.JAXBContextImpl.fastBoot");
return db;
}

void doNamespaceMappingTest(boolean internal, boolean asm) throws Exception {
if (internal) {
//TODO: review this to see if we can remove the below check
/*if (internal) {
try {
Class.forName("com.sun.xml.internal.bind.v2.ContextFactory");
} catch (Throwable t) {
//on a JVM (likely IBM's) that doesn't rename the ContextFactory package to include "internal"
return;
}
}
}*/
Bus b = new ExtensionManagerBus();
ASMHelper helper = new ASMHelperImpl();
b.setExtension(helper, ASMHelper.class);
Expand Down
2 changes: 1 addition & 1 deletion rt/databinding/jaxb/src/test/resources/schema/jms.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
specific language governing permissions and limitations
under the License.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jms="http://cxf.apache.org/transports/jms" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://cxf.apache.org/transports/jms" elementFormDefault="qualified" jaxb:version="2.0">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jms="http://cxf.apache.org/transports/jms" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://cxf.apache.org/transports/jms" elementFormDefault="qualified" jaxb:version="3.0">
<xs:import namespace="http://schemas.xmlsoap.org/wsdl/" schemaLocation="../schemas/wsdl/wsdl.xsd"/>
<xs:simpleType name="DestinationStyleType">
<xs:restriction base="xs:string">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public final class ToolConstants {

// Binding namespace
public static final String NS_JAXWS_BINDINGS = "http://java.sun.com/xml/ns/jaxws";
public static final String NS_JAXB_BINDINGS = "http://java.sun.com/xml/ns/jaxb";
public static final String NS_JAXB_BINDINGS = "https://jakarta.ee/xml/ns/jaxb";
public static final QName JAXWS_BINDINGS = new QName(NS_JAXWS_BINDINGS, "bindings");
public static final QName JAXB_BINDINGS = new QName(NS_JAXB_BINDINGS, "bindings");
public static final QName SCHEMA = new QName(SCHEMA_URI, "schema");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class JAXBUtils {

private static final String SCHEMA =
"<schema xmlns=\"http://www.w3.org/2001/XMLSchema\""
+ " xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\" jaxb:version=\"2.0\""
+ " xmlns:jaxb=\"http://jakarta.ee/xml/ns/jaxb\" jaxb:version=\"3.0\""
+ " targetNamespace=\"${targetNamespace}\">"
+ " <annotation>"
+ " <appinfo>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
under the License.
-->
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="hello_schema1.xsd" node="/xs:schema">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
</jxb:globalBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="hello_schema2.xsd" node="/xs:schema">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
</jxb:globalBindings>
</jxb:bindings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<jaxws:bindings wsdlLocation="calendar_embed.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://date.fortest.tools.cxf.apache.org/']">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
Expand Down
Loading

0 comments on commit f511b3a

Please sign in to comment.