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
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 = ((ModelCamelContext)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 if (dataFormat != null) {
087 configureDataFormat(dataFormat);
088 } else {
089 throw new IllegalArgumentException(
090 "Data format '" + (dataFormatName != null ? dataFormatName : "<null>") + "' could not be created. "
091 + "Ensure that the data format is valid and the associated Camel component is present on the classpath");
092 }
093 }
094 return dataFormat;
095 }
096
097 /**
098 * Factory method to create the data format instance
099 */
100 protected DataFormat createDataFormat(RouteContext routeContext) {
101 if (dataFormatName != null) {
102 return routeContext.getCamelContext().resolveDataFormat(dataFormatName);
103 }
104 return null;
105 }
106
107 /**
108 * Allows derived classes to customize the data format
109 */
110 protected void configureDataFormat(DataFormat dataFormat) {
111 }
112
113 /**
114 * Sets a named property on the data format instance using introspection
115 */
116 protected void setProperty(Object bean, String name, Object value) {
117 try {
118 IntrospectionSupport.setProperty(bean, name, value);
119 } catch (Exception e) {
120 throw new IllegalArgumentException("Failed to set property: " + name + " on: " + bean + ". Reason: " + e, e);
121 }
122 }
123
124 public String getDataFormatName() {
125 return dataFormatName;
126 }
127
128 public void setDataFormatName(String dataFormatName) {
129 this.dataFormatName = dataFormatName;
130 }
131
132 public DataFormat getDataFormat() {
133 return dataFormat;
134 }
135
136 public void setDataFormat(DataFormat dataFormat) {
137 this.dataFormat = dataFormat;
138 }
139
140 public String getShortName() {
141 String name = getClass().getSimpleName();
142 if (name.endsWith("DataFormat")) {
143 name = name.substring(0, name.indexOf("DataFormat"));
144 }
145 return name;
146 }
147
148 }
149