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