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 */ 017package org.apache.camel.builder; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.model.DataFormatDefinition; 021import org.apache.camel.model.Model; 022import org.apache.camel.model.transformer.CustomTransformerDefinition; 023import org.apache.camel.model.transformer.DataFormatTransformerDefinition; 024import org.apache.camel.model.transformer.EndpointTransformerDefinition; 025import org.apache.camel.model.transformer.TransformerDefinition; 026import org.apache.camel.spi.DataType; 027import org.apache.camel.spi.Transformer; 028 029/** 030 * A <a href="http://camel.apache.org/dsl.html">Java DSL</a> which is used to 031 * build a {@link org.apache.camel.spi.Transformer} and register into 032 * {@link org.apache.camel.CamelContext}. It requires 'scheme' or a pair of 033 * 'from' and 'to' to be specified by scheme(), from() and to() method. And then 034 * you can choose a type of transformer by withUri(), withDataFormat(), 035 * withJava() or withBean() method. 036 */ 037public class TransformerBuilder { 038 039 private String scheme; 040 private String from; 041 private String to; 042 private String uri; 043 private DataFormatDefinition dataFormat; 044 private Class<? extends Transformer> clazz; 045 private String beanRef; 046 047 /** 048 * Set the scheme name supported by the transformer. If you specify 'csv', 049 * the transformer will be picked up for all of 'csv' from/to Java 050 * transformation. Note that the scheme matching is performed only when no 051 * exactly matched transformer exists. 052 * 053 * @param scheme scheme name 054 */ 055 public TransformerBuilder scheme(String scheme) { 056 this.scheme = scheme; 057 return this; 058 } 059 060 /** 061 * Set the 'from' data type name. If you specify 'xml:XYZ', the transformer 062 * will be picked up if source type is 'xml:XYZ'. If you specify just 'xml', 063 * the transformer matches with all of 'xml' source type like 'xml:ABC' or 064 * 'xml:DEF'. 065 * 066 * @param from 'from' data type name 067 */ 068 public TransformerBuilder fromType(String from) { 069 this.from = from; 070 return this; 071 } 072 073 /** 074 * Set the 'from' data type using Java class. 075 * 076 * @param from 'from' Java class 077 */ 078 public TransformerBuilder fromType(Class<?> from) { 079 this.from = new DataType(from).toString(); 080 return this; 081 } 082 083 /** 084 * Set the 'to' data type name. If you specify 'json:XYZ', the transformer 085 * will be picked up if destination type is 'json:XYZ'. If you specify just 086 * 'json', the transformer matches with all of 'json' destination type like 087 * 'json:ABC' or 'json:DEF'. 088 * 089 * @param to 'to' data type 090 */ 091 public TransformerBuilder toType(String to) { 092 this.to = to; 093 return this; 094 } 095 096 /** 097 * Set the 'to' data type using Java class. 098 * 099 * @param to 'to' Java class 100 */ 101 public TransformerBuilder toType(Class<?> to) { 102 this.to = new DataType(to).toString(); 103 return this; 104 } 105 106 /** 107 * Set the URI to be used for the endpoint {@code Transformer}. 108 * 109 * @param uri endpoint URI 110 */ 111 public TransformerBuilder withUri(String uri) { 112 resetType(); 113 this.uri = uri; 114 return this; 115 } 116 117 /** 118 * Set the {@code DataFormatDefinition} to be used for the 119 * {@code DataFormat} {@code Transformer}. 120 */ 121 public TransformerBuilder withDataFormat(DataFormatDefinition dataFormatDefinition) { 122 resetType(); 123 this.dataFormat = dataFormatDefinition; 124 return this; 125 } 126 127 /** 128 * Set the Java {@code Class} represents a custom {@code Transformer} 129 * implementation class. 130 */ 131 public TransformerBuilder withJava(Class<? extends Transformer> clazz) { 132 resetType(); 133 this.clazz = clazz; 134 return this; 135 } 136 137 /** 138 * Set the Java Bean name to be used for custom {@code Transformer}. 139 */ 140 public TransformerBuilder withBean(String ref) { 141 resetType(); 142 this.beanRef = ref; 143 return this; 144 } 145 146 private void resetType() { 147 this.uri = null; 148 this.dataFormat = null; 149 this.clazz = null; 150 this.beanRef = null; 151 } 152 153 /** 154 * Configure a Transformer according to the configurations built on this 155 * builder and register it into given {@code CamelContext}. 156 * 157 * @param camelContext {@code CamelContext} 158 */ 159 public void configure(CamelContext camelContext) { 160 TransformerDefinition transformer; 161 if (uri != null) { 162 EndpointTransformerDefinition etd = new EndpointTransformerDefinition(); 163 etd.setUri(uri); 164 transformer = etd; 165 } else if (dataFormat != null) { 166 DataFormatTransformerDefinition dtd = new DataFormatTransformerDefinition(); 167 dtd.setDataFormatType(dataFormat); 168 transformer = dtd; 169 } else if (clazz != null) { 170 CustomTransformerDefinition ctd = new CustomTransformerDefinition(); 171 ctd.setClassName(clazz.getName()); 172 transformer = ctd; 173 } else if (beanRef != null) { 174 CustomTransformerDefinition ctd = new CustomTransformerDefinition(); 175 ctd.setRef(beanRef); 176 transformer = ctd; 177 } else { 178 throw new IllegalArgumentException("No Transformer type was specified"); 179 } 180 181 if (scheme != null) { 182 transformer.setScheme(scheme); 183 } else { 184 transformer.setFromType(from); 185 transformer.setToType(to); 186 } 187 188 camelContext.getExtension(Model.class).getTransformers().add(transformer); 189 } 190}