Ok, so I found a way to get what I want here, but am suspicious. It seems too easy, and feels like I'm just pushing one problem off to be discovered later as a more insidious one.
I'm hoping someone who knows this stuff (spring oxm, spring dm and osgi itself) can validate or show me that this should not be the way to go.
What I did was to subclass the org.springframework.oxm.castor.CastorMarshaller class and override the createXMLContext() method:
Code:
@Override
protected XMLContext createXMLContext(Resource[] mappingLocations, Class[] targetClasses, String[] targetPackages)
throws MappingException, ResolverException, IOException {
XMLContext context = new XMLContext();
if (!ObjectUtils.isEmpty(mappingLocations)) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Mapping mapping = new Mapping(cl);
for (Resource mappingLocation : mappingLocations) {
mapping.loadMapping(SaxResourceUtils.createInputSource(mappingLocation));
}
context.addMapping(mapping);
}
if (!ObjectUtils.isEmpty(targetClasses)) {
context.addClasses(targetClasses);
}
if (!ObjectUtils.isEmpty(targetPackages)) {
context.addPackages(targetPackages);
}
return context;
}
The only change I made here was the creation of the Mapping class - I now pass it in the TCCL rather than not giving it anything (which defaults to this.getClass().getClassLoader()).
This happens to work for me because the spring context is being created from the same bundle that also imports the classes castor needs to create for me.
So, as long as this precondition is true - that my oxm CastorMarshaller bean is created in a context whose classloader has access to the class it needs to create, won't this just always work?
And if so, should this maybe be submitted back into spring oxm as a possible patch?