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;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlTransient;
022    import javax.xml.bind.annotation.XmlType;
023    
024    import org.apache.camel.spi.DataFormat;
025    import org.apache.camel.spi.RouteContext;
026    import org.apache.camel.util.IntrospectionSupport;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Represents the base XML type for DataFormat.
031     *
032     * @version $Revision: 782535 $
033     */
034    @XmlType(name = "dataFormat")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class DataFormatDefinition extends IdentifiedType {
037        @XmlTransient
038        private DataFormat dataFormat;
039        @XmlTransient
040        private String dataFormatName;
041    
042        public DataFormatDefinition() {
043        }
044    
045        public DataFormatDefinition(DataFormat dataFormat) {
046            this.dataFormat = dataFormat;
047        }
048    
049        protected DataFormatDefinition(String dataFormatName) {
050            this.dataFormatName = dataFormatName;
051        }
052    
053        /**
054         * Factory method to create the data format
055         * @param routeContext route context
056         * @param type the data format type
057         * @param ref  reference to lookup for a data format
058         * @return the data format or null if not possible to create
059         */
060        public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefinition type, String ref) {
061            if (type == null) {
062                ObjectHelper.notNull(ref, "ref or dataFormat");
063    
064                DataFormat dataFormat = lookup(routeContext, ref, DataFormat.class);
065                if (dataFormat == null) {
066                    // lookup type and create the data format from it
067                    type = lookup(routeContext, ref, DataFormatDefinition.class);
068                    if (type == null) {
069                        type = routeContext.getDataFormat(ref);
070                    }
071                    if (type != null) {
072                        dataFormat = type.getDataFormat(routeContext);
073                    }
074                }
075    
076                if (dataFormat == null) {
077                    throw new IllegalArgumentException("Cannot find data format in registry with ref: " + ref);
078                }
079    
080                return dataFormat;
081            } else {
082                return type.getDataFormat(routeContext);
083            }
084        }
085    
086        private static <T> T lookup(RouteContext routeContext, String ref, Class<T> type) {
087            try {
088                return routeContext.lookup(ref, type);
089            } catch (Exception e) {
090                // need to ignore not same type and return it as null
091                return null;
092            }
093        }
094    
095        public DataFormat getDataFormat(RouteContext routeContext) {
096            if (dataFormat == null) {
097                dataFormat = createDataFormat(routeContext);
098                ObjectHelper.notNull(dataFormat, "dataFormat");
099                configureDataFormat(dataFormat);
100            }
101            return dataFormat;
102        }
103    
104        /**
105         * Factory method to create the data format instance
106         */
107        @SuppressWarnings("unchecked")
108        protected DataFormat createDataFormat(RouteContext routeContext) {
109            if (dataFormatName != null) {
110                Class type = routeContext.getCamelContext().getClassResolver().resolveClass(dataFormatName);
111                if (type == null) {
112                    throw new IllegalArgumentException("The class " + dataFormatName + " is not on the classpath! Cannot use the dataFormat " + this);
113                }
114                return (DataFormat) ObjectHelper.newInstance(type);
115            }
116            return null;
117        }
118    
119        /**
120         * Allows derived classes to customize the data format
121         */
122        protected void configureDataFormat(DataFormat dataFormat) {
123        }
124    
125        /**
126         * Sets a named property on the data format instance using introspection
127         */
128        protected void setProperty(Object bean, String name, Object value) {
129            try {
130                IntrospectionSupport.setProperty(bean, name, value);
131            } catch (Exception e) {
132                throw new IllegalArgumentException("Failed to set property: " + name + " on: " + bean + ". Reason: " + e, e);
133            }
134        }
135    
136        public String getDataFormatName() {
137            return dataFormatName;
138        }
139    
140        public void setDataFormatName(String dataFormatName) {
141            this.dataFormatName = dataFormatName;
142        }
143    
144        public DataFormat getDataFormat() {
145            return dataFormat;
146        }
147    
148        public void setDataFormat(DataFormat dataFormat) {
149            this.dataFormat = dataFormat;
150        }
151    }
152