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     * @version $Revision: 834590 $
038     */
039    public final class JaxbConverter {
040        private XmlConverter xmlConverter = new XmlConverter();
041        private Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
042    
043        @Converter
044        public JAXBSource toSource(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException {
045            if (value == null) {
046                throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
047            }
048            JAXBContext context = getJaxbContext(value);
049            return new JAXBSource(context, value);
050        }
051    
052        @Converter
053        public Document toDocument(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException, ParserConfigurationException {
054            if (value == null) {
055                throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
056            }
057            JAXBContext context = getJaxbContext(value);
058            // must create a new instance of marshaller as its not thred safe
059            Marshaller marshaller = context.createMarshaller();
060    
061            Document doc = xmlConverter.createDocument();
062            marshaller.marshal(value, doc);
063            return doc;
064        }
065    
066        @Converter
067        public static MessageDefinition toMessageType(Exchange exchange) {
068            return toMessageType(exchange.getIn());
069        }
070    
071        @Converter
072        public static MessageDefinition toMessageType(Message in) {
073            MessageDefinition answer = new MessageDefinition();
074            answer.copyFrom(in);
075            return answer;
076        }
077    
078        private synchronized JAXBContext getJaxbContext(Object value) throws JAXBException {
079            Class<?> type = value.getClass();
080            JAXBContext context = contexts.get(type);
081            if (context == null) {
082                context = JAXBContext.newInstance(type);
083                contexts.put(type, context);
084            }
085            return context;
086        }
087    }