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     */
017    package org.apache.camel.builder;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    
023    /**
024     * A builder of a number of different {@link Processor} implementations
025     *
026     * @version $Revision: 1.1 $
027     */
028    public class ProcessorBuilder {
029        
030        /**
031         * Utility classes should not have a public constructor.
032         */
033        private ProcessorBuilder() {        
034        }
035    
036        /**
037         * Creates a processor which sets the body of the IN message to the value of the expression
038         */
039        public static Processor setBody(final Expression expression) {
040            return new Processor() {
041                public void process(Exchange exchange) {
042                    Object newBody = expression.evaluate(exchange);
043                    exchange.getIn().setBody(newBody);
044                }
045    
046                @Override
047                public String toString() {
048                    return "setBody(" + expression + ")";
049                }
050            };
051        }
052    
053        /**
054         * Creates a processor which sets the body of the IN message to the value of the expression
055         */
056        public static Processor setOutBody(final Expression expression) {
057            return new Processor() {
058                public void process(Exchange exchange) {
059                    Object newBody = expression.evaluate(exchange);
060                    exchange.getOut().setBody(newBody);
061                }
062    
063                @Override
064                public String toString() {
065                    return "setOutBody(" + expression + ")";
066                }
067            };
068        }
069    
070        /**
071         * Sets the header on the IN message
072         */
073        public static Processor setHeader(final String name, final Expression expression) {
074            return new Processor() {
075                public void process(Exchange exchange) {
076                    Object value = expression.evaluate(exchange);
077                    exchange.getIn().setHeader(name, value);
078                }
079    
080                @Override
081                public String toString() {
082                    return "setHeader(" + name + ", " + expression + ")";
083                }
084            };
085        }
086    
087        /**
088         * Sets the header on the OUT message
089         */
090        public static Processor setOutHeader(final String name, final Expression expression) {
091            return new Processor() {
092                public void process(Exchange exchange) {
093                    Object value = expression.evaluate(exchange);
094                    exchange.getOut().setHeader(name, value);
095                }
096    
097                @Override
098                public String toString() {
099                    return "setOutHeader(" + name + ", " + expression + ")";
100                }
101            };
102        }
103    
104        /**
105         * Sets the property on the exchange
106         */
107        public static Processor setProperty(final String name, final Expression expression) {
108            return new Processor() {
109                public void process(Exchange exchange) {
110                    Object value = expression.evaluate(exchange);
111                    exchange.setProperty(name, value);
112                }
113    
114                @Override
115                public String toString() {
116                    return "setProperty(" + name + ", " + expression + ")";
117                }
118            };
119        }
120    }