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.language.groovy; 018 019 import groovy.lang.Closure; 020 import org.apache.camel.Exchange; 021 import org.apache.camel.impl.ExpressionSupport; 022 import org.apache.camel.model.ChoiceType; 023 import org.apache.camel.model.FilterType; 024 import org.apache.camel.model.ProcessorType; 025 026 /** 027 * @version $Revision: 640731 $ 028 */ 029 public final class CamelGroovyMethods { 030 private CamelGroovyMethods() { 031 // Utility Class 032 } 033 034 public static FilterType filter(ProcessorType self, Closure filter) { 035 return self.filter(toExpression(filter)); 036 } 037 038 public static ChoiceType when(ChoiceType self, Closure filter) { 039 return self.when(toExpression(filter)); 040 } 041 042 public static ExpressionSupport toExpression(final Closure filter) { 043 return new ExpressionSupport<Exchange>() { 044 protected String assertionFailureMessage(Exchange exchange) { 045 return filter.toString(); 046 } 047 048 public Object evaluate(Exchange exchange) { 049 return filter.call(exchange); 050 } 051 052 @Override 053 public String toString() { 054 return "Groovy[" + filter + "]"; 055 } 056 }; 057 } 058 059 }