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.model.dataformat;
018    
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlTransient;
025    import javax.xml.bind.annotation.XmlType;
026    
027    import org.apache.camel.Exchange;
028    import org.apache.camel.model.IdentifiedType;
029    import org.apache.camel.spi.DataFormat;
030    import org.apache.camel.spi.RouteContext;
031    import org.apache.camel.util.IntrospectionSupport;
032    import org.apache.camel.util.ObjectHelper;
033    import static org.apache.camel.util.ObjectHelper.notNull;
034    
035    /**
036     * Represents the base XML type for DataFormat.
037     *
038     * @version $Revision: 41908 $
039     */
040    @XmlType(name = "dataFormatType")
041    @XmlAccessorType(XmlAccessType.FIELD)
042    public class DataFormatType extends IdentifiedType implements DataFormat {
043        @XmlTransient
044        private DataFormat dataFormat;
045        @XmlTransient
046        private String dataFormatTypeName;
047    
048        public DataFormatType() {
049        }
050    
051        public DataFormatType(DataFormat dataFormat) {
052            this.dataFormat = dataFormat;
053        }
054    
055        protected DataFormatType(String dataFormatTypeName) {
056            this.dataFormatTypeName = dataFormatTypeName;
057        }
058    
059        public static DataFormat getDataFormat(RouteContext routeContext, DataFormatType type, String ref) {
060            if (type == null) {
061                notNull(ref, "ref or dataFormatType");
062                DataFormat dataFormat = routeContext.lookup(ref, DataFormat.class);
063                if (dataFormat instanceof DataFormatType) {
064                    type = (DataFormatType)dataFormat;
065                } else {
066                    return dataFormat;
067                }
068            }
069            return type.getDataFormat(routeContext);
070        }
071    
072    
073        public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
074            ObjectHelper.notNull(dataFormat, "dataFormat");
075            dataFormat.marshal(exchange, graph, stream);
076        }
077    
078        public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
079            ObjectHelper.notNull(dataFormat, "dataFormat");
080            return dataFormat.unmarshal(exchange, stream);
081        }
082    
083        public DataFormat getDataFormat(RouteContext routeContext) {
084            if (dataFormat == null) {
085                dataFormat = createDataFormat(routeContext);
086                ObjectHelper.notNull(dataFormat, "dataFormat");
087                configureDataFormat(dataFormat);
088            }
089            return dataFormat;
090        }
091    
092        /**
093         * Factory method to create the data format instance
094         */
095        protected DataFormat createDataFormat(RouteContext routeContext) {
096            if (dataFormatTypeName != null) {
097                Class type = ObjectHelper.loadClass(dataFormatTypeName, getClass().getClassLoader());
098                if (type == null) {
099                    throw new IllegalArgumentException("The class " + dataFormatTypeName + " is not on the classpath! Cannot use the dataFormat " + this);
100                }
101                return (DataFormat) ObjectHelper.newInstance(type);
102            }
103            return null;
104        }
105    
106        /**
107         * Allows derived classes to customize the data format
108         */
109        protected void configureDataFormat(DataFormat dataFormat) {
110        }
111    
112        /**
113         * Sets a named property on the data format instance using introspection
114         */
115        protected void setProperty(Object bean, String name, Object value) {
116            try {
117                IntrospectionSupport.setProperty(bean, name, value);
118            } catch (Exception e) {
119                throw new IllegalArgumentException("Failed to set property " + name + " on " + bean
120                                                   + ". Reason: " + e, e);
121            }
122        }
123    
124    
125    }