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.Endpoint;
025import org.apache.camel.ExchangePattern;
026import org.apache.camel.impl.transformer.ProcessorTransformer;
027import org.apache.camel.processor.SendProcessor;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.Transformer;
030
031/**
032 * Represents an endpoint {@link Transformer} which leverages camel {@link Endpoint} to
033 * perform transformation. A {@link ProcessorTransformer} will be created internally
034 * with a {@link SendProcessor} which forwards the message to the specified Endpoint.
035 * One of the Endpoint 'ref' or 'uri' needs to be specified.
036 * 
037 * {@see TransformerDefinition}
038 * {@see ProcessorTransformer}
039 */
040@Metadata(label = "transformation")
041@XmlType(name = "endpointTransformer")
042@XmlAccessorType(XmlAccessType.FIELD)
043public class EndpointTransformerDefinition extends TransformerDefinition {
044
045    @XmlAttribute
046    private String ref;
047    @XmlAttribute
048    private String uri;
049
050    @Override
051    protected Transformer doCreateTransformer(CamelContext context) throws Exception {
052        Endpoint endpoint = uri != null ? context.getEndpoint(uri)
053            : context.getRegistry().lookupByNameAndType(ref, Endpoint.class);
054        SendProcessor processor = new SendProcessor(endpoint, ExchangePattern.InOut);
055        return new ProcessorTransformer(context)
056            .setProcessor(processor)
057            .setModel(getScheme())
058            .setFrom(getFromType())
059            .setTo(getToType());
060    }
061
062    public String getRef() {
063        return ref;
064    }
065
066    /**
067     * Set the reference of the Endpoint.
068     *
069     * @param ref reference of the Endpoint
070     */
071    public void setRef(String ref) {
072        this.ref = ref;
073    }
074
075    public String getUri() {
076        return uri;
077    }
078
079    /**
080     * Set the URI of the Endpoint.
081     *
082     * @param uri URI of the Endpoint
083     */
084    public void setUri(String uri) {
085        this.uri = uri;
086    }
087
088}
089