users@jaxb.java.net

Re: JAXB, ClassLoaders and Web apps

From: Katz Guy <Guy_Katz_at_icomverse.com>
Date: Sun, 22 Jun 2003 13:08:24 +0300

Hi;
My suggestion is to always use
Thread.currentThread().getContextClassLoader()
This will make your application most portable across different vendor
implemenations.
It will also inforce you to package your applications better.

So my suggestion is: JAXBContext ctx =
JAXBContext.getInstance(MY_CONTEXT_PATH,Thread.currentThread().getContextCla
ssLoader());


-----Original Message-----
From: Robert Lowe [mailto:rmlowe_at_rmlowe.com]
Sent: Sunday, June 22, 2003 12:40 PM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: JAXB, ClassLoaders and Web apps


What is the "right" way to create a JAXBContext instance in a Web
application, assuming the JAXB classes are deployed as part of the Web app
(i.e. not in the system classpath).

Basically, I can use the thread's context loader as follows:

JAXBContext ctx = JAXBContext.getInstance(MY_CONTEXT_PATH);

or I can explicitly use the current class loader:

JAXBContext ctx = JAXBContext.getInstance(MY_CONTEXT_PATH,
ThisClass.class.getClassLoader());

Up until now, I have been doing the latter, on the (possibly flawed)
assumption that it was more likely to pick up the correct classes and
resources from the Web app.

However, when using JUnit 3.8.1 to run a test case that involves JAXB, I get
a java.lang.LinkageError (stack trace below). This error goes away if I
switch to the first method.

Is one method "better" in this situation?

java.lang.LinkageError: Class org/relaxng/datatype/Datatype violates loader
constraints

 at java.lang.ClassLoader.defineClass0(Native Method)

 at java.lang.ClassLoader.defineClass(ClassLoader.java:486)

 at java.lang.ClassLoader.defineClass(ClassLoader.java:426)

 at junit.runner.TestCaseClassLoader.loadClass(TestCaseClassLoader.java:104)

 at java.lang.ClassLoader.loadClass(ClassLoader.java:253)

 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)

 at
org.imsglobal.xsd.imsmd_rootv1p2p1.impl.CatalogentryTypeImpl$Unmarshaller.te
xt(CatalogentryTypeImpl.java:197)

 at
com.sun.xml.bind.unmarshaller.ContentHandlerEx.revertToParentFromText(Conten
tHandlerEx.java:308)

 at
org.imsglobal.xsd.imsmd_rootv1p2p1.impl.EntryImpl$Unmarshaller.text(EntryImp
l.java:149)

 at
com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.consumeText(SAXUnma
rshallerHandlerImpl.java:211)

 at
com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.processText(SAXUnma
rshallerHandlerImpl.java:215)

 at
com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.endElement(SAXUnmar
shallerHandlerImpl.java:125)

 at
org.apache.xerces.parsers.AbstractSAXParser.endElement(AbstractSAXParser.jav
a:552)

 at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(XMLNSDocument
ScannerImpl.java:544)

 at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc
her.dispatch(XMLDocumentFragmentScannerImpl.java:1533)

 at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocume
ntFragmentScannerImpl.java:346)

 at
org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:529)

 at
org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:585)

 at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)

 at
org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:114
2)

 at
com.sun.xml.bind.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.ja
va:130)

 at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshall
erImpl.java:139)

 at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshall
erImpl.java:186)

 at
com.netdimen.cpackage.MetaDataImporter.getSummary(MetaDataImporter.java:236)

 at
com.netdimen.cpackage.MetaDataImporter.getSummary(MetaDataImporter.java:264)

 at com.netdimen.scorm.packaging.Metadata.getSummary(Metadata.java:247)

 at
com.netdimen.scorm.packaging.Organization.getMetaDataSummary(Organization.ja
va:260)

 at com.netdimen.scorm.packaging.Organization.create(Organization.java:282)

 at com.netdimen.scorm.packaging.Manifest.create(Manifest.java:167)

 at
com.netdimen.scorm.packaging.JaxpContentPackageImporter.readManifest(JaxpCon
tentPackageImporter.java:48)

 at
com.netdimen.scorm.packaging.ContentPackageImporter.importContentPackage(Con
tentPackageImporter.java:79)

 at
com.netdimen.unittest.scorm.ImportPackageTestCase.testInputScormPackage(Impo
rtPackageTestCase.java:59)



Robert Lowe
http://RMLowe.com/

+852 9520 4722
rmlowe_at_rmlowe.com