I was browsing through the site and noticed the following link under Best Practices (http://www.castor.org/xml-best-practice.html).  It mentions that if you reuse a ClassDescriptorResolver, it'll help improve performance.  I followed the sample code and came up with the following method:
 
    private static ClassDescriptorResolver getClassDescriptorResolver(String mapFileName, Mapping mapping) throws MappingException
    {
        ClassDescriptorResolver classDescriptorResolver = (ClassDescriptorResolver)s_classDescriptorResolvers.get(mapFileName);
        if (classDescriptorResolver == null)
        {
            classDescriptorResolver = ClassDescriptorResolverFactory.createClassDescriptorResolver(BindingType.XML);
            MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller();
            XMLMappingLoader mappingLoader = (XMLMappingLoader)mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML);
            classDescriptorResolver.setMappingLoader(mappingLoader);
        }
        return classDescriptorResolver;
    }
Something to note first on the code.  This is minor.  I had to cast the instance returned from mappingUnmarshaller.getMappingLoader(..) because classDescriptorResolver.setMappingLoader(.) expects a XMLMappingLoader.   Wondering why the set method isn't using the MappingLoader interface.  Then I wouldn't have to cast after calling mappingUnmarshaller.getMappingLoader().
 
Now to the errors I encountered.  Under v1.0.1, I get the following compiler errors:
   cannot access class org.castor.mapping.BindingType, file ... not found
   cannot access class org.castor.mapping.MappingUnmarshaller, file ... not found
   cannot access class org.exolab.castor.xml.ClassDescriptorResolverFactory, file ... not found
 
When I attempted to upgrade to v1.0.4, I received the following compiler errors:
  (Source code):  Marshaller marshaller.setResolver(getClassDescriptorResolver(mapFileName, mapping));
  Error(70,28): method setResolver(org.exolab.castor.xml.ClassDescriptorResolver) not found in class org.exolab.castor.xml.Marshaller
 
  (Source code):  Unmarshaller unmarshaller.setResolver(getClassDescriptorResolver(mapFileName, mapping));
  Error(114,26): method setResolver(org.exolab.castor.xml.ClassDescriptorResolver) not found in class org.exolab.castor.xml.Unmarshaller
 
Would like to use this bit of code to improve performance.  Do you have any suggestions?
 
Also, if I didn't have a mapping file, would creating an empty mapping instance work?  Something like:
  XMLMappingLoader mappingLoader = (XMLMappingLoader)mappingUnmarshaller.getMappingLoader(new Mapping(), BindingType.XML);
Thanks,
Tom

Reply via email to