open-liberty: CharacterEscapeHandler is not working on liberty

Describe the bug
When using the jaxb-2.2 feature of liberty it’s not possible to add a CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler) to the marshaller. Sample Code

        JAXBContext jaxbContext 	= JAXBContext.newInstance( My.class );
        Marshaller jaxbMarshaller 	= jaxbContext.createMarshaller(); 
        jaxbMarshaller.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
                new MyCharacterEscapeHandler());

MarshallerImpl.setProperty

 public void setProperty(String name, Object value) throws PropertyException {
        if( INDENT_STRING.equals(name) ) {
            checkString(name, value);
            indent = (String)value;
            return;
        }
        if( ENCODING_HANDLER.equals(name) || ENCODING_HANDLER2.equals(name)) {
            if(!(value instanceof CharacterEscapeHandler)) 
                throw new PropertyException(
                    Messages.MUST_BE_X.format(
                            name,
                            CharacterEscapeHandler.class.getName(),
                            value.getClass().getName() ) );
            escapeHandler = (CharacterEscapeHandler)value;
            return;
        }

if(!(value instanceof CharacterEscapeHandler)) This will always fail on liberty with activated jaxb-2.2 feature because the Marshaller(Impl) is loaded with the equinox classloader of openliberty and MyCharacterEscapeHandler is loaded with the application class loader.

It leads to: javax.xml.bind.PropertyException:property "com.sun.xml.bind.marshaller.CharacterEscapeHandler" must be an instance of type com.sun.xml.bind.marshaller.CharacterEscapeHandler, not ...MyCharacterEscapeHandler

Diagnostic information:

  • OpenLiberty Version: [24.0.0.1+]
  • Affected feature(s) [jaxb-2.2]
  • Java Version: [several openjdk 11 distributions]

Additional context
Workaround was to don’t use the jaxb-2.2 feature of liberty. We added the relevant jaxb jars to our webapplication. This was working fine until we have upgraded liberty to 24.0.0.2. After the upgrade it was not possible to start the webapplication because no JaxbContextFactory could be found.

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBContextFactory cannot be found by com.ibm.ws.jaxb.tools_1.0.86.cl240220240212-1928

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 35 (19 by maintainers)

Most upvoted comments

Quick update, I was able to recreate. Now to find the problem…

I really appreciate you sending in what you’ve found on your own, I was playing with this as well over the last couple of days. I wasn’t seeing the java.lang.ClassNotFoundException when the JDK property was set, which is good in a way because it at least shows that the JAXB algorithm for Discovery of JAXB implementation is respected. I don’t think it should be required in this case, and marshaling shouldn’t load any of our internals.

This bundle com.ibm.websphere.javaee.jaxb.2.2_1.0.86.jar is part of our jaxb-2.2 feature, but to fully explain what’s happening here: most of the components that make up our jaxb-2.2 feature are being enabled by different features. The cdi-2.0 feature will pull in com.ibm.websphere.javaee.jaxb.2.2 as a dependency, but JAXB RI is also activated, com.ibm.ws.jaxb.tools_1.0.86.jar, which is being enabled by a long feature dependency chain that eventually leads to jms-2.0. What does this all mean? The features you need, depend on our internal JAXB feature, but it was written in such a way, that the RI should be shielded from your application classpath. That’s clearly broken from 24.0.0.1 and on.

What I suspect happened with the JAXB update is that the mechanism for looking up the Context impl has changed in such a way, that a JAXBContext on the application classpath can identify our “internal” ContextImpl as the impl to use, but since its on a different classpath it can’t find the spec JAXBContext class to load. This is just speculation from my debugging. I’m not 100% sure what the fix will look like, but I am getting closer. I’ll keep you posted as I know more.

Yep @krismarc, that would be the one! Good idea, just in case its not related, and the issue is still occurring after you run the nightly build.

As a further comment, the reason I addressed the xmlBinding-4.0 feature is we stopped exporting our internal specification apis needed by other features to prevent such conflict. That isn’t the case for our older features, so parentLast is the recommend way around this. However, the changes to JAXB in its implementation lookup that went into 24.0.0.2 are the same that xmlBinding-4.0 use, and so if the fix works for that, it should work here as well if we are lucky.

Hey @soilworker01, sorry for delay getting back to you, as you’ve seen I’ve made a very similar fix for xmlBinding-4.0 and I am in the process of exploring a similar fix for our jaxb-2.2 implementation. I want to avoid introducing any further regressions, but testing the changes locally have so far been promising, and I have pushed those changes to #28642 and ran a wider sweep of tests against a remote build. That also looks promising, but I want to do more testing of our features that depend on jaxb-2.2 to make sure before committing to it.

@soilworker01 one other thing you could try, if you really do not want to use parentLast, is removing the delegation property, and add the JAXB specific libraries to a new common library location and update the commonLibraryRef like so commonLibraryRef="OracleLib,ibmMq, jaxb23". The common libraries have no class loader delegation, so that might help in this situation. If that doesn’t work, and you are still having trouble with JSF, I think @volosied suggestion is a good one.

Perfect! Thank you, I’ll give it a go and see what I can find out. I’ll get back to you to shortly.