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