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.model.transformer; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlType; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.spi.Metadata; 026import org.apache.camel.spi.Transformer; 027 028/** 029 * Represents a CustomTransformer. One of the bean reference (ref) or fully qualified class name (type) 030 * of the custom {@link Transformer} needs to be specified. 031 * 032 * {@see TransformerDefinition} 033 * {@see Transformer} 034 */ 035@Metadata(label = "transformation") 036@XmlType(name = "customTransformer") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class CustomTransformerDefinition extends TransformerDefinition { 039 040 @XmlAttribute 041 private String ref; 042 @XmlAttribute 043 private String className; 044 045 @Override 046 protected Transformer doCreateTransformer(CamelContext context) throws Exception { 047 if (ref == null && className == null) { 048 throw new IllegalArgumentException("'ref' or 'className' must be specified for customTransformer"); 049 } 050 Transformer transformer; 051 if (ref != null) { 052 transformer = context.getRegistry().lookupByNameAndType(ref, Transformer.class); 053 if (transformer == null) { 054 throw new IllegalArgumentException("Cannot find transformer with ref:" + ref); 055 } 056 if (transformer.getModel() != null || transformer.getFrom() != null || transformer.getTo() != null) { 057 throw new IllegalArgumentException(String.format("Transformer '%s' is already in use. Please check if duplicate transformer exists.", ref)); 058 } 059 } else { 060 Class<Transformer> transformerClass = context.getClassResolver().resolveMandatoryClass(className, Transformer.class); 061 if (transformerClass == null) { 062 throw new IllegalArgumentException("Cannot find transformer class: " + className); 063 } 064 transformer = context.getInjector().newInstance(transformerClass); 065 066 } 067 transformer.setCamelContext(context); 068 return transformer.setModel(getScheme()) 069 .setFrom(getFromType()) 070 .setTo(getToType()); 071 } 072 073 public String getRef() { 074 return ref; 075 } 076 077 /** 078 * Set a bean reference of the {@link Transformer} 079 * 080 * @param ref the bean reference of the Transformer 081 */ 082 public void setRef(String ref) { 083 this.ref = ref; 084 } 085 086 public String getClassName() { 087 return className; 088 } 089 090 /** 091 * Set a class name of the {@link Transformer} 092 * 093 * @param className the class name of the Transformer 094 */ 095 public void setClassName(String className) { 096 this.className = className; 097 } 098 099} 100