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.spi;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.CamelContextAware;
021import org.apache.camel.Message;
022import org.apache.camel.model.InputTypeDefinition;
023import org.apache.camel.model.OutputTypeDefinition;
024import org.apache.camel.processor.ContractAdvice;
025import org.apache.camel.support.ServiceSupport;
026
027/**
028 * <a href="http://camel.apache.org/transformer.html">Transformer</a>
029 * performs message transformation according to the declared data type.
030 * {@link ContractAdvice} looks for a required Transformer and apply if
031 * input/output type declared on a route is different from current message type.
032 *  
033 * @see {@link ContractAdvice}
034 * {@link DataType} {@link InputTypeDefinition} {@link OutputTypeDefinition}
035 */
036public abstract class Transformer extends ServiceSupport implements CamelContextAware {
037
038    private CamelContext camelContext;
039    private String model;
040    private DataType from;
041    private DataType to;
042
043    /**
044     * Perform data transformation with specified from/to type.
045     *
046     * @param message message to apply transformation
047     * @param from 'from' data type
048     * @param to 'to' data type
049     */
050    public abstract void transform(Message message, DataType from, DataType to) throws Exception;
051
052    /**
053     * Get a data model which is supported by this transformer.
054     */
055    public String getModel() {
056        return model;
057    };
058
059    /**
060     * Get 'from' data type.
061     */
062    public DataType getFrom() {
063        return from;
064    };
065
066    /**
067     * Get 'to' data type.
068     */
069    public DataType getTo() {
070        return to;
071    };
072
073    /**
074     * Set data model.
075     *
076     * @param model data model
077     */
078    public Transformer setModel(String model) {
079        this.model = model;
080        return this;
081    }
082
083    /**
084     * Set 'from' data type.
085     *
086     * @param from 'from' data type
087     */
088    public Transformer setFrom(String from) {
089        this.from = new DataType(from);
090        return this;
091    }
092
093    /**
094     * Set 'to' data type.
095     *
096     * @param to 'to' data type
097     */
098    public Transformer setTo(String to) {
099        this.to = new DataType(to);
100        return this;
101    }
102
103    @Override
104    public CamelContext getCamelContext() {
105        return this.camelContext;
106    }
107
108    @Override
109    public void setCamelContext(CamelContext context) {
110        this.camelContext = context;
111    }
112
113    @Override
114    public String toString() {
115        return String.format("%s[scheme='%s', from='%s', to='%s']", this.getClass().getSimpleName(), model, from, to);
116    }
117
118    @Override
119    protected void doStart() throws Exception {
120        // no-op
121    }
122
123    @Override
124    protected void doStop() throws Exception {
125        // no-op
126    }
127}