This is an automated email from the ASF dual-hosted git repository.
ffang pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
new 46871b29585 CXF-9285: Use precompiled namespace prefix mappers instead
of generat… (#3816)
46871b29585 is described below
commit 46871b2958511e8f31d69fca166d70cccbfe720e
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Thu Dec 3 11:38:08 2026 -0500
CXF-9285: Use precompiled namespace prefix mappers instead of generat…
(#3816)
* CXF-9285: Use precompiled namespace prefix mappers instead of generating
them
The ASM generated NamespaceMapper[RI|Internal] classes always extended
org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper, so they were
copies of the precompiled org.apache.cxf.common.jaxb.NamespaceMapper. The
EclipseLink MOXy mapper is now precompiled too, as
org.apache.cxf.common.jaxb.EclipseNamespaceMapper, against an optional
org.eclipse.persistence.core dependency (an optional OSGi import), so no
namespace mapper class is generated any more. Both implement the new
ContextualNamespaceMapper interface, which DataWriterImpl calls directly;
reflection is only kept for mappers from a custom ClassLoaderService.
This also fixes:
- GeneratedNamespaceClassLoader looked up
org.apache.cxf.jaxb.NamespaceMapper
for a GlassFish marshaller while NamespaceClassGenerator generated
NamespaceMapperRI, and found the MOXy mapper only if it had been captured
at build time, so the load only setup lost the namespace prefixes
- ClassLoaderProxyService logged an NPE at WARNING when no mapper class was
available
- the precompiled NamespaceMapper named its method setContextualNamespace
while DataWriterImpl calls setContextualNamespaceDecls; the old name is
kept as a deprecated alias
* CXF-9285: Address review comments
- Import the EclipseLink packages with version="[4,6)";
MapNamespacePrefixMapper is unchanged from
EclipseLink 4.0 to 5.0. Document why cxf-core has the optional
EclipseLink dependency.
- Only resolve EclipseNamespaceMapper for a MOXy marshaller, and test that
no mapper is returned
when EclipseLink is not visible to cxf-core.
- Assert the exact MOXy output in XMLStreamDataWriterTest: the mapped
prefix is used, while the
contextual namespace is still declared (EclipseLink bug 421463).
- Use the NamespaceClassGenerator logger, note that no namespace class is
captured any more, and add
the missing newline at the end of NamespaceMapper.java.
(cherry picked from commit 83913d8e3f061251d24068c944c6ab4f23c9288a)
---
core/pom.xml | 11 +
.../cxf/common/jaxb/ContextualNamespaceMapper.java | 35 ++
...paceMapper.java => EclipseNamespaceMapper.java} | 45 ++-
.../apache/cxf/common/jaxb/NamespaceMapper.java | 18 +-
.../cxf/common/spi/ClassLoaderProxyService.java | 4 +
.../spi/GeneratedClassClassLoaderCapture.java | 2 +
.../common/spi/GeneratedNamespaceClassLoader.java | 21 +-
.../cxf/common/spi/NamespaceClassGenerator.java | 430 +--------------------
.../cxf/common/spi/NamespaceClassCreatorTest.java | 151 ++++++++
parent/pom.xml | 8 +-
.../org/apache/cxf/jaxb/io/DataWriterImpl.java | 24 +-
.../cxf/jaxb/io/XMLStreamDataWriterTest.java | 29 +-
12 files changed, 317 insertions(+), 461 deletions(-)
diff --git a/core/pom.xml b/core/pom.xml
index 9a1342ecd6f..806715131af 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -46,6 +46,7 @@
com.sun.tools*;resolution:=optional,
com.sun.xml*;resolution:=optional,
com.sun.codemodel.writer;resolution:=optional,
+ org.eclipse.persistence*;resolution:=optional;version="[4,6)",
org.slf4j*;resolution:=optional;version="${cxf.osgi.slf4j.version}",
org.springframework.cglib*;resolution:=optional;version="${cxf.osgi.spring.version}",
org.springframework.aop*;resolution:=optional;version="${cxf.osgi.spring.version}",
@@ -107,6 +108,16 @@
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
+ <dependency>
+ <!-- only for org.apache.cxf.common.jaxb.EclipseNamespaceMapper,
the namespace prefix mapper of
+ EclipseLink MOXy, which has to extend the MOXy class
+
org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper
(unchanged from
+ EclipseLink 4.0 to 5.0). Without EclipseLink,
NamespaceClassGenerator returns no mapper for MOXy. -->
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>org.eclipse.persistence.core</artifactId>
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
diff --git
a/core/src/main/java/org/apache/cxf/common/jaxb/ContextualNamespaceMapper.java
b/core/src/main/java/org/apache/cxf/common/jaxb/ContextualNamespaceMapper.java
new file mode 100644
index 00000000000..a203e0f5a68
--- /dev/null
+++
b/core/src/main/java/org/apache/cxf/common/jaxb/ContextualNamespaceMapper.java
@@ -0,0 +1,35 @@
+/**
+ * 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.cxf.common.jaxb;
+
+/**
+ * A namespace prefix mapper that can leave out the namespace declarations
already made by the context
+ * the JAXB object is written into, e.g. the SOAP envelope.
+ */
+public interface ContextualNamespaceMapper {
+
+ /**
+ * @param contextualNamespaceDecls the prefixes and namespace URIs
declared by the context:
+ * prefix1, uri1, prefix2, uri2, ...
+ */
+ void setContextualNamespaceDecls(String[] contextualNamespaceDecls);
+
+ String[] getContextualNamespaceDecls();
+}
diff --git a/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
b/core/src/main/java/org/apache/cxf/common/jaxb/EclipseNamespaceMapper.java
similarity index 51%
copy from core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
copy to
core/src/main/java/org/apache/cxf/common/jaxb/EclipseNamespaceMapper.java
index 95d0bfae22b..888826ae106 100644
--- a/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
+++ b/core/src/main/java/org/apache/cxf/common/jaxb/EclipseNamespaceMapper.java
@@ -19,38 +19,43 @@
package org.apache.cxf.common.jaxb;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Map;
-import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper;
+import
org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper;
+/**
+ * The namespace prefix mapper for EclipseLink MOXy.
+ */
+public final class EclipseNamespaceMapper extends MapNamespacePrefixMapper
implements ContextualNamespaceMapper {
+ private String[] nsctxt;
-public final class NamespaceMapper extends NamespacePrefixMapper {
- private static final String[] EMPTY_STRING = new String[0];
-
- private final Map<String, String> nspref;
- private String[] nsctxt = EMPTY_STRING;
-
- public NamespaceMapper(Map<String, String> nspref) {
- this.nspref = nspref;
+ public EclipseNamespaceMapper(Map<String, String> nspref) {
+ super(nspref);
}
- public String getPreferredPrefix(String namespaceUri,
- String suggestion,
- boolean requirePrefix) {
- String prefix = nspref.get(namespaceUri);
- if (prefix != null) {
- return prefix;
+ @Override
+ public String[] getPreDeclaredNamespaceUris() {
+ String[] sup = super.getPreDeclaredNamespaceUris();
+ if (nsctxt == null) {
+ return sup;
}
- return suggestion;
+ List<String> s = new ArrayList<>(Arrays.asList(sup));
+ for (int x = 1; x < nsctxt.length; x = x + 2) {
+ s.remove(nsctxt[x]);
+ }
+ return s.toArray(new String[0]);
}
- public void setContextualNamespace(String[] contextualNamespaceDecls) {
+ @Override
+ public void setContextualNamespaceDecls(String[] contextualNamespaceDecls)
{
this.nsctxt = contextualNamespaceDecls;
}
+ @Override
public String[] getContextualNamespaceDecls() {
return nsctxt;
}
-
-
-}
\ No newline at end of file
+}
diff --git a/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
b/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
index 95d0bfae22b..9f4128a2f21 100644
--- a/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
+++ b/core/src/main/java/org/apache/cxf/common/jaxb/NamespaceMapper.java
@@ -24,7 +24,7 @@ import java.util.Map;
import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper;
-public final class NamespaceMapper extends NamespacePrefixMapper {
+public final class NamespaceMapper extends NamespacePrefixMapper implements
ContextualNamespaceMapper {
private static final String[] EMPTY_STRING = new String[0];
private final Map<String, String> nspref;
@@ -44,13 +44,21 @@ public final class NamespaceMapper extends
NamespacePrefixMapper {
return suggestion;
}
- public void setContextualNamespace(String[] contextualNamespaceDecls) {
+ @Override
+ public void setContextualNamespaceDecls(String[] contextualNamespaceDecls)
{
this.nsctxt = contextualNamespaceDecls;
}
+ /**
+ * @deprecated use {@link #setContextualNamespaceDecls(String[])}, which
is the method the JAXB data binding calls
+ */
+ @Deprecated
+ public void setContextualNamespace(String[] contextualNamespaceDecls) {
+ setContextualNamespaceDecls(contextualNamespaceDecls);
+ }
+
+ @Override
public String[] getContextualNamespaceDecls() {
return nsctxt;
}
-
-
-}
\ No newline at end of file
+}
diff --git
a/core/src/main/java/org/apache/cxf/common/spi/ClassLoaderProxyService.java
b/core/src/main/java/org/apache/cxf/common/spi/ClassLoaderProxyService.java
index aed992250bb..9d178cfa4a3 100644
--- a/core/src/main/java/org/apache/cxf/common/spi/ClassLoaderProxyService.java
+++ b/core/src/main/java/org/apache/cxf/common/spi/ClassLoaderProxyService.java
@@ -38,6 +38,10 @@ public class ClassLoaderProxyService implements
ClassLoaderService {
@Override
public Object createNamespaceWrapperInstance(Class<?> mcls, Map<String,
String> map) {
Class<?> cls = srv.createNamespaceWrapperClass(mcls, map);
+ if (cls == null) {
+ // no mapper for this JAXB runtime, the creator has already logged
why if it is unexpected
+ return null;
+ }
try {
return cls.getConstructor(Map.class).newInstance(map);
} catch (Throwable e) {
diff --git
a/core/src/main/java/org/apache/cxf/common/spi/GeneratedClassClassLoaderCapture.java
b/core/src/main/java/org/apache/cxf/common/spi/GeneratedClassClassLoaderCapture.java
index 00c6c05bbfe..c58573532d5 100644
---
a/core/src/main/java/org/apache/cxf/common/spi/GeneratedClassClassLoaderCapture.java
+++
b/core/src/main/java/org/apache/cxf/common/spi/GeneratedClassClassLoaderCapture.java
@@ -29,6 +29,8 @@ package org.apache.cxf.common.spi;
* bus.setExtension(new WrapperClassLoader(bus), WrapperClassCreator.class);
* bus.setExtension(new FactoryClassLoader(bus), FactoryClassCreator.class);
* bus.setExtension(new GeneratedNamespaceClassLoader(bus),
NamespaceClassCreator.class);
+ * The namespace prefix mappers are precompiled, so no namespace class is
captured; GeneratedNamespaceClassLoader
+ * returns the precompiled mapper.
* @author olivier dufour
*/
public interface GeneratedClassClassLoaderCapture {
diff --git
a/core/src/main/java/org/apache/cxf/common/spi/GeneratedNamespaceClassLoader.java
b/core/src/main/java/org/apache/cxf/common/spi/GeneratedNamespaceClassLoader.java
index affa7d5e0f7..f3e8dbf3f4a 100644
---
a/core/src/main/java/org/apache/cxf/common/spi/GeneratedNamespaceClassLoader.java
+++
b/core/src/main/java/org/apache/cxf/common/spi/GeneratedNamespaceClassLoader.java
@@ -23,9 +23,9 @@ import java.util.Map;
import org.apache.cxf.Bus;
-/** If class has been generated during build time
- * (use @see org.apache.cxf.common.spi.GeneratedClassClassLoaderCapture
capture to save bytes)
- * you can set class loader to avoid class generation during runtime:
+/**
+ * The namespace prefix mappers are precompiled now, so this returns the same
classes as
+ * {@link NamespaceClassGenerator}; it is kept for the setups that use it to
avoid class generation at runtime:
* bus.setExtension(new GeneratedNamespaceClassLoader(bus),
NamespaceClassCreator.class);
* @author olivier dufour
*/
@@ -35,18 +35,7 @@ public class GeneratedNamespaceClassLoader extends
GeneratedClassClassLoader imp
}
@Override
- public synchronized Class<?> createNamespaceWrapperClass(Class<?> mcls,
Map<String, String> map) {
- String postFix = "";
-
- if (mcls.getName().contains("eclipse")) {
- return findClass("org.apache.cxf.jaxb.EclipseNamespaceMapper",
- NamespaceClassCreator.class);
- } else if (mcls.getName().contains(".internal")) {
- postFix = "Internal";
- } else if (mcls.getName().contains("com.sun")) {
- postFix = "RI";
- }
-
- return findClass("org.apache.cxf.jaxb.NamespaceMapper" + postFix,
NamespaceClassCreator.class);
+ public Class<?> createNamespaceWrapperClass(Class<?> mcls, Map<String,
String> map) {
+ return NamespaceClassGenerator.getNamespaceMapper(mcls);
}
}
diff --git
a/core/src/main/java/org/apache/cxf/common/spi/NamespaceClassGenerator.java
b/core/src/main/java/org/apache/cxf/common/spi/NamespaceClassGenerator.java
index c1f5a8dbcc3..5829ef57f73 100644
--- a/core/src/main/java/org/apache/cxf/common/spi/NamespaceClassGenerator.java
+++ b/core/src/main/java/org/apache/cxf/common/spi/NamespaceClassGenerator.java
@@ -24,428 +24,40 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cxf.Bus;
-import org.apache.cxf.common.classloader.ClassLoaderUtils;
+import org.apache.cxf.common.jaxb.EclipseNamespaceMapper;
+import org.apache.cxf.common.jaxb.NamespaceMapper;
import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.ASMHelper;
-import org.apache.cxf.common.util.OpcodesProxy;
+/**
+ * Returns the namespace prefix mapper class for the JAXB runtime of a
marshaller. The mappers are precompiled
+ * ({@link NamespaceMapper} for the GlassFish JAXB runtime, {@link
EclipseNamespaceMapper} for EclipseLink MOXy),
+ * so no class is generated any more. The class keeps its name, as it is the
default {@link NamespaceClassCreator}.
+ */
public class NamespaceClassGenerator extends ClassGeneratorClassLoader
implements NamespaceClassCreator {
- private static final Logger LOG =
LogUtils.getL7dLogger(ClassGeneratorClassLoader.class);
- private final ASMHelper helper;
+ private static final Logger LOG =
LogUtils.getL7dLogger(NamespaceClassGenerator.class);
public NamespaceClassGenerator(Bus bus) {
super(bus);
- helper = bus.getExtension(ASMHelper.class);
}
@Override
- public synchronized Class<?> createNamespaceWrapperClass(Class<?> mcls,
Map<String, String> map) {
- String postFix = "";
-
- if (mcls.getName().contains("eclipse")) {
- return createEclipseNamespaceMapper();
- } else if (mcls.getName().contains(".internal")) {
- postFix = "Internal";
- } else if (mcls.getName().contains("org.glassfish")) {
- postFix = "RI";
- }
-
- String className = "org.apache.cxf.jaxb.NamespaceMapper";
- className += postFix;
- Class<?> cls = findClass(className, NamespaceClassCreator.class);
- Throwable t = null;
- if (cls == null) {
- try {
- byte[] bts = createNamespaceWrapperInternal(postFix);
- className = "org.apache.cxf.jaxb.NamespaceMapper" + postFix;
- return loadClass(className, NamespaceClassCreator.class, bts);
- } catch (RuntimeException ex) {
- // continue
- t = ex;
- }
- }
- if (cls == null
- && (!mcls.getName().contains(".internal.") &&
mcls.getName().contains("com.sun"))) {
- try {
- cls =
ClassLoaderUtils.loadClass("org.apache.cxf.common.jaxb.NamespaceMapper",
- NamespaceClassCreator.class);
- } catch (Throwable ex2) {
- // ignore
- t = ex2;
- }
- }
- if (cls == null) {
- LOG.log(Level.INFO, "Could not create a NamespaceMapper compatible
with Marshaller class "
- + mcls.getName(), t);
- }
- return cls;
- }
-
- private Class<?> createEclipseNamespaceMapper() {
- String className = "org.apache.cxf.jaxb.EclipseNamespaceMapper";
- Class<?> cls = findClass(className, NamespaceClassCreator.class);
- if (cls != null) {
- return cls;
- }
- byte[] bts = doCreateEclipseNamespaceMapper();
- //previous code use mcls instead of NamespaceClassGenerator.class
- return loadClass(className, NamespaceClassCreator.class, bts);
+ public Class<?> createNamespaceWrapperClass(Class<?> mcls, Map<String,
String> map) {
+ return getNamespaceMapper(mcls);
}
- /*
- // This is the "prototype" for the ASM generated class below
- public static class MapNamespacePrefixMapper2
- extends
org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper
{
-
- String[] nsctxt;
-
- public MapNamespacePrefixMapper2(Map<String, String> foo) {
- super(foo);
- }
- public String[] getPreDeclaredNamespaceUris() {
- String[] sup = super.getPreDeclaredNamespaceUris();
- if (nsctxt == null) {
- return sup;
- }
- List<String> s = new ArrayList<>(Arrays.asList(sup));
- for (int x = 1; x < nsctxt.length; x = x + 2) {
- s.remove(nsctxt[x]);
- }
- return s.toArray(new String[s.size()]);
- }
- public void setContextualNamespaceDecls(String[] f) {
- nsctxt = f;
- }
- public String[] getContextualNamespaceDecls() {
- return nsctxt;
- }
- }
- */
- //CHECKSTYLE:OFF
- //bunch of really long ASM based methods that cannot be shortened easily
- private byte[] doCreateEclipseNamespaceMapper() {
- OpcodesProxy Opcodes = helper.getOpCodes();
- String slashedName = "org/apache/cxf/jaxb/EclipseNamespaceMapper";
- ASMHelper.ClassWriter cw = helper.createClassWriter();
- if (cw == null) {
+ static Class<?> getNamespaceMapper(Class<?> mcls) {
+ if (!mcls.getName().contains("eclipse")) {
+ return NamespaceMapper.class;
+ }
+ try {
+ // EclipseNamespaceMapper extends a MOXy class and EclipseLink is
an optional dependency of cxf-core,
+ // so resolving it throws a NoClassDefFoundError when EclipseLink
is not visible to cxf-core
+ return EclipseNamespaceMapper.class;
+ } catch (LinkageError ex) {
+ LOG.log(Level.INFO, "Could not create a NamespaceMapper compatible
with Marshaller class "
+ + mcls.getName(), ex);
return null;
}
- String superName =
"org/eclipse/persistence/internal/oxm/record/namespaces/MapNamespacePrefixMapper";
- ASMHelper.FieldVisitor fv;
- ASMHelper.MethodVisitor mv;
- cw.visit(Opcodes.V1_6,
- Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
- slashedName, null,
- superName, null);
-
- cw.visitSource("EclipseNamespaceMapper.java", null);
-
- fv = cw.visitField(Opcodes.ACC_PRIVATE, "nsctxt",
"[Ljava/lang/String;", null, null);
- fv.visitEnd();
-
-
- mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V",
- "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V",
null);
- mv.visitCode();
- ASMHelper.Label l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitVarInsn(Opcodes.ALOAD, 1);
- mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
- superName, "<init>", "(Ljava/util/Map;)V", false);
- ASMHelper.Label l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitInsn(Opcodes.RETURN);
- ASMHelper.Label l2 = helper.createLabel();
- mv.visitLabel(l2);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
-
- mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setContextualNamespaceDecls",
"([Ljava/lang/String;)V",
- null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(47, l0);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitVarInsn(Opcodes.ALOAD, 1);
- mv.visitFieldInsn(Opcodes.PUTFIELD, slashedName, "nsctxt",
"[Ljava/lang/String;");
- l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitLineNumber(48, l1);
- mv.visitInsn(Opcodes.RETURN);
- l2 = helper.createLabel();
- mv.visitLabel(l2);
- mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l2,
0);
- mv.visitLocalVariable("contextualNamespaceDecls",
"[Ljava/lang/String;", null, l0, l2, 1);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getContextualNamespaceDecls",
"()[Ljava/lang/String;", null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(51, l0);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt",
"[Ljava/lang/String;");
- mv.visitInsn(Opcodes.ARETURN);
- l1 = helper.createLabel();
-
- mv.visitLabel(l1);
- mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l1,
0);
-
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
-
- mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPreDeclaredNamespaceUris",
"()[Ljava/lang/String;", null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(1036, l0);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
- superName,
- "getPreDeclaredNamespaceUris", "()[Ljava/lang/String;", false);
- mv.visitVarInsn(Opcodes.ASTORE, 1);
- l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitLineNumber(1037, l1);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt",
"[Ljava/lang/String;");
- l2 = helper.createLabel();
- mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
- ASMHelper.Label l3 = helper.createLabel();
- mv.visitLabel(l3);
- mv.visitLineNumber(1038, l3);
- mv.visitVarInsn(Opcodes.ALOAD, 1);
- mv.visitInsn(Opcodes.ARETURN);
- mv.visitLabel(l2);
- mv.visitLineNumber(1040, l2);
- mv.visitFrame(Opcodes.F_APPEND, 1, new Object[]
{"[Ljava/lang/String;"}, 0, null);
- mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
- mv.visitInsn(Opcodes.DUP);
- mv.visitVarInsn(Opcodes.ALOAD, 1);
- mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Arrays", "asList",
- "([Ljava/lang/Object;)Ljava/util/List;", false);
- mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList",
"<init>",
- "(Ljava/util/Collection;)V", false);
- mv.visitVarInsn(Opcodes.ASTORE, 2);
- ASMHelper.Label l4 = helper.createLabel();
- mv.visitLabel(l4);
- mv.visitLineNumber(1041, l4);
- mv.visitInsn(Opcodes.ICONST_1);
- mv.visitVarInsn(Opcodes.ISTORE, 3);
- ASMHelper.Label l5 = helper.createLabel();
- mv.visitLabel(l5);
- ASMHelper.Label l6 = helper.createLabel();
- mv.visitJumpInsn(Opcodes.GOTO, l6);
- ASMHelper.Label l7 = helper.createLabel();
- mv.visitLabel(l7);
- mv.visitLineNumber(1042, l7);
- mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] {"java/util/List",
Opcodes.INTEGER}, 0, null);
- mv.visitVarInsn(Opcodes.ALOAD, 2);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt",
"[Ljava/lang/String;");
- mv.visitVarInsn(Opcodes.ILOAD, 3);
- mv.visitInsn(Opcodes.AALOAD);
- mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List",
"remove", "(Ljava/lang/Object;)Z", true);
- mv.visitInsn(Opcodes.POP);
- ASMHelper.Label l8 = helper.createLabel();
- mv.visitLabel(l8);
- mv.visitLineNumber(1041, l8);
- mv.visitIincInsn(3, 2);
- mv.visitLabel(l6);
- mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
- mv.visitVarInsn(Opcodes.ILOAD, 3);
- mv.visitVarInsn(Opcodes.ALOAD, 0);
- mv.visitFieldInsn(Opcodes.GETFIELD,
- slashedName,
- "nsctxt", "[Ljava/lang/String;");
- mv.visitInsn(Opcodes.ARRAYLENGTH);
- mv.visitJumpInsn(Opcodes.IF_ICMPLT, l7);
- ASMHelper.Label l9 = helper.createLabel();
- mv.visitLabel(l9);
- mv.visitLineNumber(1044, l9);
- mv.visitVarInsn(Opcodes.ALOAD, 2);
- mv.visitVarInsn(Opcodes.ALOAD, 2);
- mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "size",
"()I", true);
- mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
- mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List",
- "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", true);
- mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/String;");
- mv.visitInsn(Opcodes.ARETURN);
- ASMHelper.Label l10 = helper.createLabel();
- mv.visitLabel(l10);
- mv.visitLocalVariable("this", "L" + slashedName + ";",
- null, l0, l10, 0);
- mv.visitLocalVariable("sup", "[Ljava/lang/String;", null, l1, l10, 1);
- mv.visitLocalVariable("s", "Ljava/util/List;",
"Ljava/util/List<Ljava/lang/String;>;", l4, l10, 2);
- mv.visitLocalVariable("x", "I", null, l5, l9, 3);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- cw.visitEnd();
-
- return cw.toByteArray();
- }
-
- private byte[] createNamespaceWrapperInternal(String postFix) {
-
- String superName =
"org/glassfish/jaxb/runtime/marshaller/NamespacePrefixMapper";
- String postFixedName = "org/apache/cxf/jaxb/NamespaceMapper" + postFix;
- ASMHelper.ClassWriter cw = helper.createClassWriter();
- if (cw == null) {
- return null;
- }
- ASMHelper.FieldVisitor fv;
- ASMHelper.MethodVisitor mv;
- OpcodesProxy opcodes= helper.getOpCodes();
- cw.visit(opcodes.V1_6,
- opcodes.ACC_PUBLIC + opcodes.ACC_FINAL + opcodes.ACC_SUPER,
- postFixedName, null,
- superName, null);
-
- cw.visitSource("NamespaceMapper.java", null);
-
- fv = cw.visitField(opcodes.ACC_PRIVATE + opcodes.ACC_FINAL,
- "nspref", "Ljava/util/Map;",
- "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
- fv.visitEnd();
-
- fv = cw.visitField(opcodes.ACC_PRIVATE, "nsctxt",
"[Ljava/lang/String;", null, null);
- fv.visitEnd();
-
- fv = cw.visitField(opcodes.ACC_PRIVATE + opcodes.ACC_FINAL +
opcodes.ACC_STATIC,
- "EMPTY_STRING", "[Ljava/lang/String;", null, null);
- fv.visitEnd();
-
- mv = cw.visitMethod(opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
- mv.visitCode();
- ASMHelper.Label l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(30, l0);
- mv.visitInsn(opcodes.ICONST_0);
- mv.visitTypeInsn(opcodes.ANEWARRAY, "java/lang/String");
- mv.visitFieldInsn(opcodes.PUTSTATIC, postFixedName, "EMPTY_STRING",
"[Ljava/lang/String;");
- mv.visitInsn(opcodes.RETURN);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- mv = cw.visitMethod(opcodes.ACC_PUBLIC, "<init>",
- "(Ljava/util/Map;)V",
- "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V",
null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(32, l0);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitMethodInsn(opcodes.INVOKESPECIAL, superName, "<init>", "()V",
false);
- ASMHelper.Label l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitLineNumber(29, l1);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitFieldInsn(opcodes.GETSTATIC, postFixedName, "EMPTY_STRING",
"[Ljava/lang/String;");
- mv.visitFieldInsn(opcodes.PUTFIELD, postFixedName, "nsctxt",
"[Ljava/lang/String;");
- ASMHelper.Label l2 = helper.createLabel();
- mv.visitLabel(l2);
- mv.visitLineNumber(33, l2);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitVarInsn(opcodes.ALOAD, 1);
- mv.visitFieldInsn(opcodes.PUTFIELD, postFixedName, "nspref",
"Ljava/util/Map;");
- ASMHelper.Label l3 = helper.createLabel();
- mv.visitLabel(l3);
- mv.visitLineNumber(34, l3);
- mv.visitInsn(opcodes.RETURN);
- ASMHelper.Label l4 = helper.createLabel();
- mv.visitLabel(l4);
- mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4,
0);
- mv.visitLocalVariable("nspref",
- "Ljava/util/Map;",
"Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;",
- l0, l4, 1);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- mv = cw.visitMethod(opcodes.ACC_PUBLIC, "getPreferredPrefix",
- "(Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;",
- null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(39, l0);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitFieldInsn(opcodes.GETFIELD, postFixedName, "nspref",
"Ljava/util/Map;");
- mv.visitVarInsn(opcodes.ALOAD, 1);
- mv.visitMethodInsn(opcodes.INVOKEINTERFACE, "java/util/Map",
- "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
- mv.visitTypeInsn(opcodes.CHECKCAST, "java/lang/String");
- mv.visitVarInsn(opcodes.ASTORE, 4);
- l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitLineNumber(40, l1);
- mv.visitVarInsn(opcodes.ALOAD, 4);
- l2 = helper.createLabel();
- mv.visitJumpInsn(opcodes.IFNULL, l2);
- l3 = helper.createLabel();
- mv.visitLabel(l3);
- mv.visitLineNumber(41, l3);
- mv.visitVarInsn(opcodes.ALOAD, 4);
- mv.visitInsn(opcodes.ARETURN);
- mv.visitLabel(l2);
- mv.visitLineNumber(43, l2);
- mv.visitFrame(opcodes.F_APPEND, 1, new Object[] {"java/lang/String"},
0, null);
- mv.visitVarInsn(opcodes.ALOAD, 2);
- mv.visitInsn(opcodes.ARETURN);
- l4 = helper.createLabel();
- mv.visitLabel(l4);
- mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4,
0);
- mv.visitLocalVariable("namespaceUri", "Ljava/lang/String;", null, l0,
l4, 1);
- mv.visitLocalVariable("suggestion", "Ljava/lang/String;", null, l0,
l4, 2);
- mv.visitLocalVariable("requirePrefix", "Z", null, l0, l4, 3);
- mv.visitLocalVariable("prefix", "Ljava/lang/String;", null, l1, l4, 4);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- mv = cw.visitMethod(opcodes.ACC_PUBLIC, "setContextualNamespaceDecls",
"([Ljava/lang/String;)V", null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(47, l0);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitVarInsn(opcodes.ALOAD, 1);
- mv.visitFieldInsn(opcodes.PUTFIELD, postFixedName, "nsctxt",
"[Ljava/lang/String;");
- l1 = helper.createLabel();
- mv.visitLabel(l1);
- mv.visitLineNumber(48, l1);
- mv.visitInsn(opcodes.RETURN);
- l2 = helper.createLabel();
- mv.visitLabel(l2);
- mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l2,
0);
- mv.visitLocalVariable("contextualNamespaceDecls",
"[Ljava/lang/String;", null, l0, l2, 1);
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- mv = cw.visitMethod(opcodes.ACC_PUBLIC, "getContextualNamespaceDecls",
"()[Ljava/lang/String;", null, null);
- mv.visitCode();
- l0 = helper.createLabel();
- mv.visitLabel(l0);
- mv.visitLineNumber(51, l0);
- mv.visitVarInsn(opcodes.ALOAD, 0);
- mv.visitFieldInsn(opcodes.GETFIELD, postFixedName, "nsctxt",
"[Ljava/lang/String;");
- mv.visitInsn(opcodes.ARETURN);
- l1 = helper.createLabel();
-
- mv.visitLabel(l1);
- mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l1,
0);
-
- mv.visitMaxs(0, 0);
- mv.visitEnd();
-
- cw.visitEnd();
-
- return cw.toByteArray();
}
- //CHECKSTYLE:ON
}
diff --git
a/core/src/test/java/org/apache/cxf/common/spi/NamespaceClassCreatorTest.java
b/core/src/test/java/org/apache/cxf/common/spi/NamespaceClassCreatorTest.java
new file mode 100644
index 00000000000..d74f50058ed
--- /dev/null
+++
b/core/src/test/java/org/apache/cxf/common/spi/NamespaceClassCreatorTest.java
@@ -0,0 +1,151 @@
+/**
+ * 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.cxf.common.spi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+import org.apache.cxf.common.jaxb.EclipseNamespaceMapper;
+import org.apache.cxf.common.jaxb.NamespaceMapper;
+import org.eclipse.persistence.oxm.XMLMarshaller;
+import org.glassfish.jaxb.runtime.v2.runtime.MarshallerImpl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+public class NamespaceClassCreatorTest {
+ private static final Map<String, String> PREFIXES = Map.of("urn:a", "a");
+
+ private Bus bus;
+ private final Map<String, byte[]> generated = new HashMap<>();
+
+ @Before
+ public void setUp() {
+ bus = new ExtensionManagerBus();
+ bus.setExtension((GeneratedClassClassLoaderCapture)generated::put,
GeneratedClassClassLoaderCapture.class);
+ }
+
+ @After
+ public void tearDown() {
+ bus.shutdown(true);
+ }
+
+ @Test
+ public void testGeneratorUsesPrecompiledMapperForGlassFish() {
+ Class<?> cls = new
NamespaceClassGenerator(bus).createNamespaceWrapperClass(MarshallerImpl.class,
PREFIXES);
+
+ assertSame(NamespaceMapper.class, cls);
+ assertTrue("No class should be generated: " + generated.keySet(),
generated.isEmpty());
+ }
+
+ @Test
+ public void testLoaderFindsMapperForGlassFish() {
+ Object mapper = new ClassLoaderProxyService(new
GeneratedNamespaceClassLoader(bus))
+ .createNamespaceWrapperInstance(MarshallerImpl.class, PREFIXES);
+
+ assertEquals(NamespaceMapper.class, mapper.getClass());
+ NamespaceMapper nsMapper = (NamespaceMapper)mapper;
+ assertEquals("a", nsMapper.getPreferredPrefix("urn:a", "ns1", false));
+ assertEquals("ns1", nsMapper.getPreferredPrefix("urn:b", "ns1",
false));
+ String[] decls = {"a", "urn:a"};
+ nsMapper.setContextualNamespaceDecls(decls);
+ assertArrayEquals(decls, nsMapper.getContextualNamespaceDecls());
+ }
+
+ @Test
+ public void testPrecompiledMapperForEclipseLinkMoxy() {
+ Class<?> cls = new
NamespaceClassGenerator(bus).createNamespaceWrapperClass(XMLMarshaller.class,
PREFIXES);
+ assertSame(EclipseNamespaceMapper.class, cls);
+ assertTrue("No class should be generated: " + generated.keySet(),
generated.isEmpty());
+
+ Object mapper = new ClassLoaderProxyService(new
GeneratedNamespaceClassLoader(bus))
+ .createNamespaceWrapperInstance(XMLMarshaller.class,
Map.of("urn:a", "a", "urn:b", "b"));
+ EclipseNamespaceMapper nsMapper = (EclipseNamespaceMapper)mapper;
+ assertEquals("a", nsMapper.getPreferredPrefix("urn:a", "ns1", false));
+ assertEquals(Set.of("urn:a", "urn:b"),
Set.of(nsMapper.getPreDeclaredNamespaceUris()));
+
+ // the namespaces declared by the context are not declared again
+ nsMapper.setContextualNamespaceDecls(new String[] {"a", "urn:a"});
+ assertEquals(Set.of("urn:b"),
Set.of(nsMapper.getPreDeclaredNamespaceUris()));
+ }
+
+ @Test
+ public void testNoMapperForEclipseLinkMoxyWithoutEclipseLink() throws
Exception {
+ ClassLoader loader = new
WithoutEclipseLinkClassLoader(NamespaceClassCreatorTest.class.getClassLoader());
+ Method getNamespaceMapper =
loader.loadClass(NamespaceClassGenerator.class.getName())
+ .getDeclaredMethod("getNamespaceMapper", Class.class);
+ getNamespaceMapper.setAccessible(true);
+
+ assertNull(getNamespaceMapper.invoke(null, XMLMarshaller.class));
+ assertSame(NamespaceMapper.class, getNamespaceMapper.invoke(null,
MarshallerImpl.class));
+ }
+
+ @Test
+ public void testNoMapperClass() {
+ assertNull(new ClassLoaderProxyService((mcls, map) -> null)
+ .createNamespaceWrapperInstance(MarshallerImpl.class, PREFIXES));
+ }
+
+ /**
+ * Defines NamespaceClassGenerator and EclipseNamespaceMapper itself and
hides EclipseLink, as when EclipseLink
+ * is not visible to cxf-core.
+ */
+ private static final class WithoutEclipseLinkClassLoader extends
ClassLoader {
+ WithoutEclipseLinkClassLoader(ClassLoader parent) {
+ super(parent);
+ }
+
+ @Override
+ protected Class<?> loadClass(String name, boolean resolve) throws
ClassNotFoundException {
+ synchronized (getClassLoadingLock(name)) {
+ if (name.startsWith("org.eclipse.persistence.")) {
+ throw new ClassNotFoundException(name);
+ }
+ if (!name.equals(NamespaceClassGenerator.class.getName())
+ && !name.equals(EclipseNamespaceMapper.class.getName())) {
+ return super.loadClass(name, resolve);
+ }
+ Class<?> cls = findLoadedClass(name);
+ if (cls == null) {
+ try (InputStream in =
getParent().getResourceAsStream(name.replace('.', '/') + ".class")) {
+ byte[] bytes = in.readAllBytes();
+ cls = defineClass(name, bytes, 0, bytes.length);
+ } catch (IOException ex) {
+ throw new ClassNotFoundException(name, ex);
+ }
+ }
+ return cls;
+ }
+ }
+ }
+}
diff --git a/parent/pom.xml b/parent/pom.xml
index ac4271f6183..1e9041249dc 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -120,6 +120,7 @@
<cxf.geronimo.openapi.version>1.0.15</cxf.geronimo.openapi.version>
<cxf.glassfish.el.version>4.0.2</cxf.glassfish.el.version>
<cxf.glassfish.json.version>2.0.1</cxf.glassfish.json.version>
+ <cxf.eclipselink.version>4.0.10</cxf.eclipselink.version>
<cxf.hamcrest.version>3.0</cxf.hamcrest.version>
<cxf.hazelcast.version>5.7.0</cxf.hazelcast.version>
<cxf.hibernate.em.version>6.6.49.Final</cxf.hibernate.em.version>
@@ -1648,6 +1649,11 @@
<artifactId>istack-commons-tools</artifactId>
<version>${cxf.istack.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>org.eclipse.persistence.core</artifactId>
+ <version>${cxf.eclipselink.version}</version>
+ </dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
@@ -2065,7 +2071,7 @@
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
- <version>4.0.10</version>
+ <version>${cxf.eclipselink.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
diff --git
a/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java
b/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java
index 01f502db37f..f4d7ced70f6 100644
---
a/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java
+++
b/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java
@@ -39,6 +39,7 @@ import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import jakarta.xml.bind.attachment.AttachmentMarshaller;
import org.apache.cxf.Bus;
import org.apache.cxf.common.i18n.Message;
+import org.apache.cxf.common.jaxb.ContextualNamespaceMapper;
import org.apache.cxf.common.jaxb.JAXBUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.ReflectionUtil;
@@ -197,23 +198,28 @@ public class DataWriterImpl<T> extends JAXBDataBase
implements DataWriter<T> {
return marshaller;
}
- //REVISIT should this go into JAXBUtils?
private static void setContextualNamespaceDecls(Object mapper, Map<String,
String> nsctxt) {
+ if (mapper == null) {
+ return;
+ }
+ String[] args = new String[nsctxt.size() * 2];
+ int ai = 0;
+ for (Entry<String, String> nsp : nsctxt.entrySet()) {
+ args[ai++] = nsp.getValue();
+ args[ai++] = nsp.getKey();
+ }
+ if (mapper instanceof ContextualNamespaceMapper) {
+
((ContextualNamespaceMapper)mapper).setContextualNamespaceDecls(args);
+ return;
+ }
+ // a mapper from a custom ClassLoaderService
try {
Method m = ReflectionUtil.getDeclaredMethod(mapper.getClass(),
"setContextualNamespaceDecls", new Class<?>[]{String[].class});
- String[] args = new String[nsctxt.size() * 2];
- int ai = 0;
- for (Entry<String, String> nsp : nsctxt.entrySet()) {
- args[ai++] = nsp.getValue();
- args[ai++] = nsp.getKey();
- }
m.invoke(mapper, new Object[]{args});
} catch (Exception e) {
- // ignore
LOG.log(Level.WARNING, "Failed to set the contextual namespace
map", e);
}
-
}
public void write(Object obj, MessagePartInfo part, T output) {
diff --git
a/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/io/XMLStreamDataWriterTest.java
b/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/io/XMLStreamDataWriterTest.java
index fc5c50a81e0..658f77583d1 100644
---
a/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/io/XMLStreamDataWriterTest.java
+++
b/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/io/XMLStreamDataWriterTest.java
@@ -33,6 +33,11 @@ import javax.xml.stream.XMLStreamWriter;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.ValidationEventHandler;
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+import org.apache.cxf.common.spi.ClassLoaderProxyService;
+import org.apache.cxf.common.spi.ClassLoaderService;
+import org.apache.cxf.common.spi.GeneratedNamespaceClassLoader;
import org.apache.cxf.databinding.DataWriter;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxb.JAXBDataBinding;
@@ -340,7 +345,25 @@ public class XMLStreamDataWriterTest {
@Test
public void testWriteWithContextualNamespaceDecls() throws Exception {
- JAXBDataBinding db = getTestWriterFactory(GreetMe.class);
+ writeWithContextualNamespaceDecls(getTestWriterFactory(GreetMe.class));
+ }
+
+ @Test
+ public void testWriteWithContextualNamespaceDeclsWithoutClassGeneration()
throws Exception {
+ // the load only setup recommended when the classes are generated at
build time, e.g. for GraalVM
+ Bus bus = new ExtensionManagerBus();
+ try {
+ bus.setExtension(new ClassLoaderProxyService(new
GeneratedNamespaceClassLoader(bus)),
+ ClassLoaderService.class);
+ JAXBDataBinding db = getTestWriterFactory(GreetMe.class);
+ db.setBus(bus);
+ writeWithContextualNamespaceDecls(db);
+ } finally {
+ bus.shutdown(true);
+ }
+ }
+
+ private void writeWithContextualNamespaceDecls(JAXBDataBinding db) throws
Exception {
Map<String, String> nspref = new HashMap<>();
nspref.put("http://apache.org/hello_world_soap_http/types", "x");
db.setNamespaceMap(nspref);
@@ -362,6 +385,10 @@ public class XMLStreamDataWriterTest {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=421463
assertEquals("<x:greetMe><x:requestType>Hello</x:requestType></x:greetMe>",
xstr);
+ } else {
+ // MOXy still declares the contextual namespace (see the bug
above), but uses the mapped prefix
+ assertEquals("<x:greetMe
xmlns:x=\"http://apache.org/hello_world_soap_http/types\">"
+ + "<x:requestType>Hello</x:requestType></x:greetMe>",
xstr);
}
}