diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-05 18:31:43 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-05 18:31:43 +0000 |
commit | 2d8745982d5a56d20b95213016689ee07882d03c (patch) | |
tree | 315a245d3f4abffef67d943100ee97e8dd0c82a7 /java/sca | |
parent | c5692e414b93c3ecc4745b8026ad48bcb0b0d8e7 (diff) |
Set the thread context classloader so that JAXBContext.newInstance can find the correct factory
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@801350 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca')
3 files changed, 37 insertions, 14 deletions
diff --git a/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF b/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF index 9c9095b4eb..274579ba03 100644 --- a/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF +++ b/java/sca/modules/databinding-jaxb/META-INF/MANIFEST.MF @@ -38,6 +38,7 @@ Import-Package: javax.activation, org.apache.tuscany.sca.databinding.jaxb;version="2.0.0",
org.apache.tuscany.sca.databinding.util;version="2.0.0",
org.apache.tuscany.sca.databinding.xml;version="2.0.0",
+ org.apache.tuscany.sca.extensibility;version="2.0.0",
org.apache.tuscany.sca.interfacedef;version="2.0.0",
org.apache.tuscany.sca.interfacedef.impl;version="2.0.0",
org.apache.tuscany.sca.interfacedef.util;version="2.0.0",
diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCache.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCache.java index 4eaaf3db7f..ba94239a31 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCache.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCache.java @@ -55,6 +55,8 @@ import javax.xml.bind.annotation.XmlType; import javax.xml.transform.Source; import org.apache.tuscany.sca.databinding.util.LRUCache; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; /** * @version $Rev$ $Date$ @@ -116,12 +118,34 @@ public class JAXBContextCache { upool = new Pool<JAXBContext, Unmarshaller>(); defaultContext = getDefaultJAXBContext(); } - + private static JAXBContext newJAXBContext(final Class<?>... classesToBeBound) throws JAXBException { try { return AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() { public JAXBContext run() throws JAXBException { - return JAXBContext.newInstance(classesToBeBound); + // Try to set up TCCL so that JAXBContext service discovery works in OSGi + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + ClassLoader newTccl = tccl; + try { + ServiceDeclaration sd = + ServiceDiscovery.getInstance().getServiceDeclaration(JAXBContext.class.getName()); + if (sd != null) { + newTccl = sd.loadClass().getClassLoader(); + } + } catch (Exception e) { + // Ignore + } + if (newTccl != tccl) { + Thread.currentThread().setContextClassLoader(newTccl); + } + try { + JAXBContext context = JAXBContext.newInstance(classesToBeBound); + return context; + } finally { + if (newTccl != tccl) { + Thread.currentThread().setContextClassLoader(newTccl); + } + } } }); } catch (PrivilegedActionException e) { @@ -129,18 +153,6 @@ public class JAXBContextCache { } } - private static JAXBContext newJAXBContext(final String contextPath, final ClassLoader classLoader) - throws JAXBException { - try { - return AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() { - public JAXBContext run() throws JAXBException { - return JAXBContext.newInstance(contextPath, classLoader); - } - }); - } catch (PrivilegedActionException e) { - throw (JAXBException)e.getException(); - } - } public static JAXBContext getDefaultJAXBContext() { try { diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java index 58c8bdc175..830e8c5e23 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java @@ -37,6 +37,7 @@ import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlEnum; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSchema; +import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.bind.annotation.XmlType; import javax.xml.namespace.QName; @@ -100,6 +101,15 @@ public final class JAXBContextHelper { return createJAXBContext(dataType); } + + private Class<?>[] getSeeAlso(Class<?> interfaze) { + XmlSeeAlso seeAlso = interfaze.getAnnotation(XmlSeeAlso.class); + if (seeAlso == null) { + return null; + } else { + return seeAlso.value(); + } + } public static JAXBContext createJAXBContext(DataType dataType) throws JAXBException { return createJAXBContext(findClasses(dataType)); |