001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter.jaxb;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import javax.xml.bind.JAXBContext;
022    import javax.xml.bind.JAXBException;
023    import javax.xml.bind.Marshaller;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.util.JAXBSource;
026    import javax.xml.parsers.ParserConfigurationException;
027    
028    import org.w3c.dom.Document;
029    
030    import org.apache.camel.Converter;
031    import org.apache.camel.Exchange;
032    import org.apache.camel.Message;
033    import org.apache.camel.converter.HasAnnotation;
034    import org.apache.camel.converter.jaxp.XmlConverter;
035    
036    /**
037     * As we have the JAXB FallbackTypeConverter, so we don't need to register this converter
038     * @version $Revision: 895993 $
039     */
040    public final class JaxbConverter {
041        private XmlConverter xmlConverter = new XmlConverter();
042        private Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
043    
044        //@Converter
045        public JAXBSource toSource(Object value) throws JAXBException {
046            if (value == null) {
047                throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
048            }
049            // just need to check if the Object class has the XmlRootElement
050            if (value.getClass().getAnnotation(XmlRootElement.class) != null) {
051                JAXBContext context = getJaxbContext(value);
052                return new JAXBSource(context, value);
053            } else {
054                return null;
055            }
056        }
057    
058        //@Converter
059        public Document toDocument(Object value) throws JAXBException, ParserConfigurationException {
060            if (value == null) {
061                throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
062            }
063            if (value.getClass().getAnnotation(XmlRootElement.class) != null) {
064                JAXBContext context = getJaxbContext(value);
065                // must create a new instance of marshaller as its not thread safe
066                Marshaller marshaller = context.createMarshaller();
067    
068                Document doc = xmlConverter.createDocument();
069                marshaller.marshal(value, doc);
070                return doc;
071            } else {
072                return null;
073            }
074        }
075    
076        @Converter
077        public static MessageDefinition toMessageType(Exchange exchange) {
078            return toMessageType(exchange.getIn());
079        }
080    
081        @Converter
082        public static MessageDefinition toMessageType(Message in) {
083            MessageDefinition answer = new MessageDefinition();
084            answer.copyFrom(in);
085            return answer;
086        }
087    
088        private synchronized JAXBContext getJaxbContext(Object value) throws JAXBException {
089            Class<?> type = value.getClass();
090            JAXBContext context = contexts.get(type);
091            if (context == null) {
092                context = JAXBContext.newInstance(type);
093                contexts.put(type, context);
094            }
095            return context;
096        }
097    }