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.jaxp.XmlConverter;
034
035 /**
036 * As we have the JAXB FallbackTypeConverter, so we don't need to register this converter
037 *
038 * @deprecated will be removed in the near future
039 */
040 @Deprecated
041 public final class JaxbConverter {
042 private XmlConverter xmlConverter = new XmlConverter();
043 private Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
044
045 //@Converter
046 public JAXBSource toSource(Object value) throws JAXBException {
047 if (value == null) {
048 throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
049 }
050 // just need to check if the Object class has the XmlRootElement
051 if (value.getClass().getAnnotation(XmlRootElement.class) != null) {
052 JAXBContext context = getJaxbContext(value);
053 return new JAXBSource(context, value);
054 } else {
055 return null;
056 }
057 }
058
059 //@Converter
060 public Document toDocument(Object value) throws JAXBException, ParserConfigurationException {
061 if (value == null) {
062 throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
063 }
064 if (value.getClass().getAnnotation(XmlRootElement.class) != null) {
065 JAXBContext context = getJaxbContext(value);
066 // must create a new instance of marshaller as its not thread safe
067 Marshaller marshaller = context.createMarshaller();
068
069 Document doc = xmlConverter.createDocument();
070 marshaller.marshal(value, doc);
071 return doc;
072 } else {
073 return null;
074 }
075 }
076
077 @Converter
078 public static MessageDefinition toMessageType(Exchange exchange) {
079 return toMessageType(exchange.getIn());
080 }
081
082 @Converter
083 public static MessageDefinition toMessageType(Message in) {
084 MessageDefinition answer = new MessageDefinition();
085 answer.copyFrom(in);
086 return answer;
087 }
088
089 private synchronized JAXBContext getJaxbContext(Object value) throws JAXBException {
090 Class<?> type = value.getClass();
091 JAXBContext context = contexts.get(type);
092 if (context == null) {
093 context = JAXBContext.newInstance(type);
094 contexts.put(type, context);
095 }
096 return context;
097 }
098 }