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