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: 834603 $
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         *
056         * @param routeContext route context
057         * @param type         the data format type
058         * @param ref          reference to lookup for a data format
059         * @return the data format or null if not possible to create
060         */
061        public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefinition type, String ref) {
062            if (type == null) {
063                ObjectHelper.notNull(ref, "ref or type");
064    
065                // try to let resolver see if it can resolve it, its not always possible
066                type = routeContext.getCamelContext().resolveDataFormatDefinition(ref);
067    
068                if (type != null) {
069                    return type.getDataFormat(routeContext);
070                }
071    
072                DataFormat dataFormat = routeContext.getCamelContext().resolveDataFormat(ref);
073                if (dataFormat == null) {
074                    throw new IllegalArgumentException("Cannot find data format in registry with ref: " + ref);
075                }
076    
077                return dataFormat;
078            } else {
079                return type.getDataFormat(routeContext);
080            }
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        @SuppressWarnings("unchecked")
096        protected DataFormat createDataFormat(RouteContext routeContext) {
097            if (dataFormatName != null) {
098                return routeContext.getCamelContext().resolveDataFormat(dataFormatName);
099            }
100            return null;
101        }
102    
103        /**
104         * Allows derived classes to customize the data format
105         */
106        protected void configureDataFormat(DataFormat dataFormat) {
107        }
108    
109        /**
110         * Sets a named property on the data format instance using introspection
111         */
112        protected void setProperty(Object bean, String name, Object value) {
113            try {
114                IntrospectionSupport.setProperty(bean, name, value);
115            } catch (Exception e) {
116                throw new IllegalArgumentException("Failed to set property: " + name + " on: " + bean + ". Reason: " + e, e);
117            }
118        }
119    
120        public String getDataFormatName() {
121            return dataFormatName;
122        }
123    
124        public void setDataFormatName(String dataFormatName) {
125            this.dataFormatName = dataFormatName;
126        }
127    
128        public DataFormat getDataFormat() {
129            return dataFormat;
130        }
131    
132        public void setDataFormat(DataFormat dataFormat) {
133            this.dataFormat = dataFormat;
134        }
135    
136        public String getShortName() {
137            String name = getClass().getSimpleName();
138            if (name.endsWith("DataFormat")) {
139                name = name.substring(0, name.indexOf("DataFormat"));
140            }
141            return name;
142        }
143    
144    }
145