.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory - ear

I am upgrading Weblogic server from 9 to 10.3.6. when I am trying to deploy my ear application and got below exception.
Caused By: java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:123)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:89)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353)
at org.springframework.context.access.ContextSingletonBeanFactoryLocator.initializeDefinition(ContextSingletonBeanFactoryLocator.java:141)
at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:384)
at org.springframework.web.context.ContextLoader.loadParentContext(ContextLoader.java:341)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:195)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:481)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:181)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1868)
I tried all sorts for things including adding a weblogic-application.xml but it still does not work.
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-application>
<xml>
<parser-factory>
<saxparser-factory>
org.apache.xerces.jaxp.SAXParserFactoryImpl
</saxparser-factory>
<document-builder-factory>
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
</document-builder-factory>
<transformer-factory>
org.apache.xalan.processor.TransformerFactoryImpl
</transformer-factory>
</parser-factory>
</xml>
<prefer-application-packages>
<package-name>org.apache.xerces.parsers.*</package-name>
</prefer-application-packages>
</weblogic-application>
my weblogic.xml has
<prefer-web-inf-classes>true</prefer-web-inf-classes>
This is part of my pom.xml:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
<scope>runtime</scope>
</dependency>
Please help. Thanks!

I answer my own question:
Below link inspired me of fixing this issue:
Dealing with "Xerces hell" in Java/Maven?
Basiclly I have removed all the dependencies of xml-api and xmlParserAPIs in pom.xml. The problem is fixed. The root cause is my classpath should not include any javax libraries which cause the library conflict with Weblogic app server. Hope it helps.

I had the similar problem. I was using an application installed on wildly server. The problem was that I have to place xerces jars in both framework's and third party lib folder of the application. After much research, I found the solution in the documentation of xerces.
Why do I get a ClassCastException when I use Xerces and WebSphere Application Server?
Xerces uses the ObjectFactory class to load some classes dynamically, e.g. the parser configuration. The ObjectFactory finds the specified implementation class by querying the system property, reading META-INF/services/factoryId file or using a fallback classname. After the implementation is found, the ObjectFactory tries to load the file using the context classloader and if it is null, the ObjectFactory uses the system classloader.
If you run Xerces in an environment, such as WebSphere® Application Server, that has multiple classloaders you may get ClassCastExceptions thrown from Xerces because different classloaders might get involved in loading Xerces classes. For example, ClassCastExceptions may occur when utility EAR classes that use Xerces load Xerces classes from WAR modules.
We suggest you read the "Avoiding ClassCastExceptions..." article which explains a workaround for this problem. Also you might want to read the "J2EE Class Loading Demystified" article that explains how multiple classloaders work in WebSphere Application Server.
"
Basically, use two util class to create domparser object. For the service call from your application, change the classloader and create the object. After doing the processing, revert back the classloader.
ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader currentClassLoader = this.getClass().getClassLoader() ;
Thread.currentThread().setContextClassLoader(currentClassLoader);
//do the processing, after that revert back
https://xerces.apache.org/xerces2-j/faq-general.html

Related

Optaplanner Quarkus reflection issues

I'm creating a solver factory based on an XML file using the built in
solverFactory = SolverFactory.createFromXmlResource("solverConfig.xml");
This works fine until compiling into a Quarkus native application, now I get reflection issues from Optaplanner enums:
ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): java.lang.NoSuchFieldError: org.optaplanner.core.api.domain.common.DomainAccessType: REFLECTION
at com.sun.xml.bind.v2.model.nav.ReflectionNavigator.getEnumConstants(ReflectionNavigator.java:25)
at com.sun.xml.bind.v2.model.nav.ReflectionNavigator.getEnumConstants(ReflectionNavigator.java:34)
at com.sun.xml.bind.v2.model.impl.EnumLeafInfoImpl.calcConstants(EnumLeafInfoImpl.java:117)
at com.sun.xml.bind.v2.model.impl.EnumLeafInfoImpl.getConstants(EnumLeafInfoImpl.java:181)
at com.sun.xml.bind.v2.model.impl.EnumLeafInfoImpl.link(EnumLeafInfoImpl.java:188)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:427)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:103)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:439)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:273)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at java.lang.reflect.Method.invoke(Method.java:566)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:275)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:264)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:403)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:89)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:81)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.<init>(SolverConfigIO.java:27)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:207)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:182)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:122)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:97)
at org.optaplanner.core.api.solver.SolverFactory.createFromXmlResource(SolverFactory.java:55)
I can work through these issues by adding #RegisterForReflection on my class calling SolverFactory.createFromXmlResource("solverConfig.xml"), but it feels like these reflection registrations should not have to be a manual step. Currently I'm at #RegisterForReflection(targets = {EnvironmentMode.class, RandomType.class, DomainAccessType.class}), but I'm guessing there will be a lot more enums to add to that list... Should it really be this complex? I'm using the Quarkus Optaplanner dependency
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-quarkus</artifactId>
</dependency>
By doing it another way, I was able to solve the issue.
In my class that previously called solverFactory = SolverFactory.createFromXmlResource("solverConfig.xml"), I now just inject the factory:
#Inject
SolverFactory<Schedule> solverFactory;
And moved the XML file name to application.properties instead:
quarkus.optaplanner.solver-config-xml=solverConfig.xml

spring-test, groovy library and qualifier tag incompatibility

Im trying to add some groovy scripts to existing project and I stuck with making integration tests work.
I have few beans marked with <qualifier /> tag, that are used for autowiring both in test and in production code.
Right after I add 'org.codehaus.groovy:groovy-all:2.4.0'
(tried other versions aswell) to dependencies, without even any groovy usage, my integration tests stop working, with exception:
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener#5e5f7983] to prepare test instance [com.dph.groovy.vs.springtest.IntegrationTest#299c9fe7]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at
......
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing
Offending resource: class path resource [spring/app-config.xml]
Bean 'service'; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Tag 'qualifier' must have a 'type' attribute
Offending resource: class path resource [spring/app-config.xml]
Bean 'service'
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:323)
Runnning project (with jetty 6 if this matters) doesnt cause any issues though, so I assume there's some trick with spring-test union with groovy.
I might just add 'type' to my qualifiers, however it doesnt solve problem, because I have external dependencies with same qualifier tags configuration, besides this attribute is optional as far as I know.
I'd love to find out at least what are roots of this problem.
I created example project to reproduce the described question and will appreciate any ideas:
https://github.com/ametiste/groovy-vs-spring-test
You have discovered a bug in Spring's testing support.
Fixed in Spring Framework 4.1.6 and 4.2 RC1
I have fixed this bug for Spring Framework 4.1.6 (scheduled for release at the end of March 2015) and 4.2 (scheduled for release in Q3 2015). For further details, please see JIRA issue SPR-12768.
If you wish to try out the fix before the aforementioned releases, consider building against one of the upcoming nightly snapshots.
Temporary Work-around
In the meantime, (for XML configuration files that you are permitted to edit) you can circumvent this bug by explicitly setting the type attribute in the <qualifier> tag to the intended default value which is "org.springframework.beans.factory.annotation.Qualifier". See the following XML config for an example.
<bean id="foo" class="java.lang.String" c:_="bar">
<qualifier value="foo" type="org.springframework.beans.factory.annotation.Qualifier" />
</bean>
Regards,
Sam
(author of the Spring TestContext Framework)

Linkage error in websphere 8.5 for SAXParser

I am facing an issue related to linkage when i deployed my application websphere 8.5
Error in the XML parsing of the included Input Stream: java.lang.LinkageError: loading constraint violation when resolving method "javax/xml/parsers/SAXParser.parse(Lorg/xml/sax/InputSource;Lorg/xml/sax/helpers/DefaultHandler;)V" : loader "com/ibm/ws/classloader/CompoundClassLoader#aa54261e" of class "com/XMLParser/CreateParser" and loader "com/ibm/oti/vm/BootstrapClassLoader#1c4565b7" of class "javax/xml/parsers/SAXParser" have different types for the method signature.
I have the following jars in my class path.
I have set the loader to PARENT_LAST.
I tried to debug the class alone using a main method and found that it is taking the impl of saxparser of jdk 1.5 rt.jar and it is working as expected. After deploying the ear it is throwing the exception while the code hits the SAXParser.parse(InputSoruce,DefaultHandler) method.
jaxb-impl 2.2.6,
jaxb-libs-1.0.5,
jaxb-xjc-2.0EA3,
dom4j-1.1,
sax 2.0.1
Does any one have any idea about this problem?
Your class loader has visibility to two copies of org.xml.sax. The first because you've included the SAX APIs in your PARENT_LAST class loader, and the second indirectly via javax.xml.parsers in the JRE. You either need to remove the SAX API JAR from your application or you need to add the javax.xml (and perhaps more) APIs + impl to your application.

XSLT ClassCastException in WebSphere when Spring tries to create an AnnotationMethodHandlerAdapter

When starting WebSphere, I get this exception:
Could not instantiate bean class [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter]:
Constructor threw exception; nested exception is java.lang.ClassCastException:
com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl incompatible with
javax.xml.transform.TransformerFactory
Caused by: java.lang.ClassCastException: com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl
incompatible with javax.xml.transform.TransformerFactory
at javax.xml.transform.TransformerFactory.newInstance(Unknown Source)
at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.<init>(AbstractXmlHttpMessageConverter.java:47)
at org.springframework.http.converter.xml.SourceHttpMessageConverter.<init>(SourceHttpMessageConverter.java:45)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.<init>(AnnotationMethodHandlerAdapter.java:197)
This doesn't seem have any impact on any beans in my applicationContext.xml but it's still odd. For me, this looks as if IBM classes are leaking into my application.
How can I fix this? I already set the option "Access to internal server classes" to "Restrict".
It was indeed a class-loading issue, however this cannot be solved by changing class-loader settings.
The problem was that the xml-apis and javax.xml jars were being imported over some maven dependencies.
Since we already set the class loader policies for the application to PARENT_LAST, the javax.xml.transform.TransformerFactory was being loaded from the WebApp-Class loader from our jar files.
However its implementation 'com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl' was coming from the server class loader, this one was linked to the javax.xml.transform.TransformerFactory provided by the JDK/JRE.
Since the classes were loaded from different sources a ClassCastException was thrown.
Removing all dependencies to xml-apis / xerces / javax.xml jars solved the problem.
Since these APIs are now part of the JDK they no longer need to be imported.
... and if you wonder why I know so much about this issue: I work together with Aaron. ;)
I can't speak for Restrict as I have no personal experience with it,But I think the problem is more to do with IBM Class Loader. The class you are referring to is part of IBM Java implementation of TransformerFactory, I think you can try one of the following to solve this issue on hand
Either change the server class loader policy to PARENT_LAST (This way class loader will find the class from application's local class path, before going to up the chain all the way to java run time)
The other option would be look at the jaxp.properties file, I think it is located in (was_root\java\jre\lib), I only read about this option never actually used it
Why do you say IBM classes are leaking into your application?
The TransformerFactory is asked to create a newInstance. It follows a sequence of steps to determine which TransformerFactory to use. If none of the config is specified, it simply chooses to use the default factory.
Here is the javadoc for TransformerFactory:
http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()
What is the OS ? Is that AIX?
http://www.ibm.com/developerworks/java/jdk/aix/j664/sdkguide.aix64.html
Looking at this doc (link above) for AIX it tells me that this is the default Impl:
javax.xml.transform.TransformerFactory
Selects the XSLT processor. Possible values are:
com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl
Use the XL TXE-J compiler. This value is the default.
Post back additional information so that we can try and troubleshoot this.
HTH
Manglu

Unable to locate Spring NamespaceHandler for element 'flow'

I am developing a spring webflow (2.0.7) project using SpringSource Tool Suite. I am trying to setup a basic flow.
My someflow.xml looks like this:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<!- view-state declarations -->
</flow>
On STS tool(Spring IDE on eclipse), I see a warning message near the flow schemaLocation:
Unable to locate Spring NamespaceHandler for element 'flow' of schema namespace 'http://
www.springframework.org/schema/webflow'
Then when tomcat starts up, I get the error
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/webflow]
Offending resource: ServletContext resource [/WEB-INF/flows/someflow.xml]
I googled a while and some posts suggested that the problem is spring-webflow jars not being in class path. In my case, springsource tool created the template and all jars are in place. I manually checked them as well. So that can't be the issue
One of the suggestions in this post http://forum.springsource.org/archive/index.php/t-49098.html was to splice the jar! That can't be a solution, but I tried to see if it fixes it. But no.
Stuck now.. Did anyone else face this issue?
I've had similar issues before and it usually boiled down to the jar missing from the built war. Can you open up the war you are using and check that the webflow jar is in the /WEB-INF/lib directory?
If you are using Maven to do your builds, check your dependency settings for webflow as well.
if you are using eclipse,please provide the appresource path name,right click on your test case select Run as --> Run Configurations --> click on the Classpath tab and copy the below line and give the full path name of your property file location.
appResourcePath = ../../environment-dev.properties).

Resources