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.converter;
018
019 import org.apache.camel.Converter;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Expression;
022 import org.apache.camel.Message;
023 import org.apache.camel.Predicate;
024 import org.apache.camel.Processor;
025
026 /**
027 * Some useful converters for Camel APIs such as to convert a {@link Predicate} or {@link Expression}
028 * to a {@link Processor}
029 *
030 * @version
031 */
032 @Converter
033 public final class CamelConverter {
034
035 /**
036 * Utility classes should not have a public constructor.
037 */
038 private CamelConverter() {
039 }
040
041 @Converter
042 public static Processor toProcessor(final Predicate predicate) {
043 return new Processor() {
044 public void process(Exchange exchange) throws Exception {
045 boolean answer = predicate.matches(exchange);
046 Message out = exchange.getOut();
047 out.copyFrom(exchange.getIn());
048 out.setBody(answer);
049 }
050 };
051
052 }
053
054 @Converter
055 public static Processor toProcessor(final Expression expresion) {
056 return new Processor() {
057 public void process(Exchange exchange) throws Exception {
058 Object answer = expresion.evaluate(exchange, Object.class);
059 Message out = exchange.getOut();
060 out.copyFrom(exchange.getIn());
061 out.setBody(answer);
062 }
063 };
064 }
065
066 }