Hi All,

Below is the solution that I used to integrate JAXB with apache camel and to
resolve class-loading problem in OSGI:


1. I have written separate bean class to resolve Jaxb-Context something
like:

public class JAXBContextResolver {
    private static Logger log = Logger.getLogger(JAXBContextResolver.class);
      // annotatedPackages is the package , where I have put the generated
jaxb's class
    private final static String annotatedPackages =
"com.blogspot.techiesweek.jaxb.generated";
    private final static JAXBContext context = initContext();

    @SuppressWarnings({"ThrowInsideCatchBlockWhichIgnoresCaughtException"})
    private static JAXBContext initContext() {
        JAXBContext context;
        try {
            ClassLoader cl = JAXBContextResolver.class.getClassLoader();
            context = JAXBContext.newInstance(annotatedPackages, cl);
            log.info("jaxbContext created successfully!");
        }
        catch (PropertyException pEx) {
            log.error("unable to set jaxb property: ", pEx);
            throw new RuntimeException("unable to set jaxb property:", pEx);
        }
        catch (JAXBException jaxbEx) {
            log.error("jaxb error while converting object to xml: ",
jaxbEx);
            throw new RuntimeException("jaxb error while converting object
to xml: ", jaxbEx);
        }
        return context;
    }

    @SuppressWarnings({"MethodMayBeStatic"})
    public JAXBContext getContext() {
        log.debug("### inside JAXBContextResolver->getContext...");
        return context;
    }
}


2. I have declare this JAXBContextResolver bean class in spring application
context config file as:
<bean id="jaxbContextResolver"
class="com.blogspot.techiesweek.util.JAXBContextResolver"/>

and processor class bean, which make use of jaxb context is declare as:
    <bean id="dataExtractorBuilder"
class="com.blogspot.techiesweek.processor.DataExtractorBuilder">
        <property name="dataExtractorService" ref="dataExtractorService"/>
        <property name="tempReportFilePath" value="${tempReportFilePath}"/>
        <property name="validationSchemaUri"
value="${validationSchemaUri}"/>
        <property name="reportFilenamePrefix"
value="${reportFilenamePrefix}"/>
        <property name="jaxbContextResolver" ref="jaxbContextResolver"/>
    </bean>

3. Now in my camel processor class, I have used jaxb context to marshel jaxb
classes to XML file as :
   
public void process(Exchange exchange) throws ProcessingException {
        OutputStream os = null;
        String generatedFileName = getGeneratedFileName();        
        try {
            log.debug("loading jaxb context...");
            JAXBContext jaxbContext = jaxbContextResolver.getContext();
            Marshaller marshaller = jaxbContext.createMarshaller();

            RootElementData rootElementData =
myDataExtractorService.getRootElementData();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
           
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
validationSchemaUri);
            File xmlDocumentFile = new File(tempReportFilePath,
generatedFileName);

            os = new FileOutputStream(xmlDocumentFile);
            marshaller.marshal(rootElementData , os);

            message.setHeader("CamelFileName", generatedFileName );
            message.setBody(xmlDocumentFile);

that's it.

If anybody face any problem, do let me know. I will be more than happy to
resolve your queries.


Thanks,

Arun Kumar (Sharma_arun_se)

-----
Regards,

Arun Kumar (sharma_arun_se)

Expert SOA (Fuse ESB, Camel, ActiveMQ, OSGi) and RESTful Solution Architect,
Open Source Contributor.
linkedin: http://in.linkedin.com/in/aronkumar
twitter: https://twitter.com/#!/SharmaArunKumar
blog: http://techiesweek.blogspot.com/
Gtalk: arun.kaun...@gmail.com

-- 
View this message in context: 
http://camel.465427.n5.nabble.com/Help-with-Camel-JAXB-SMX4-Camel-2-1-0-tp470566p3308986.html
Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.

Reply via email to