camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1164847 - in /camel/trunk/components/camel-scala/src: main/scala/org/apache/camel/scala/ main/scala/org/apache/camel/scala/dsl/ main/scala/org/apache/camel/scala/dsl/languages/ test/scala/org/apache/camel/scala/dsl/
Date Sat, 03 Sep 2011 11:37:31 GMT
Author: davsclaus
Date: Sat Sep  3 11:37:30 2011
New Revision: 1164847

URL: http://svn.apache.org/viewvc?rev=1164847&view=rev
Log:
CAMEL-4403: Language functions now support both expression and predicate being evaluate at
runtime depending on need.

Added:
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/LanguageFunction.scala
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/CamelCBRTest.scala
      - copied, changed from r1164804, camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelTestSupportTest.scala
Modified:
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaExpression.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaPredicate.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/Languages.scala

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaExpression.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaExpression.scala?rev=1164847&r1=1164846&r2=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaExpression.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaExpression.scala
Sat Sep  3 11:37:30 2011
@@ -17,15 +17,23 @@
 package org.apache.camel
 package scala
 
+import dsl.languages.LanguageFunction
+
 /**
  * Scala implementation for an Apache Camel Expression
  */
 class ScalaExpression(val expression: Exchange => Any) extends Expression {
   
-  def evaluate(exchange: Exchange) = expression(exchange).asInstanceOf[Object]
+  def evaluate(exchange: Exchange) = {
+    val value = expression(exchange)
+    value match {
+      case f : LanguageFunction => f.evaluate(exchange, classOf[Object])
+      case _ => value.asInstanceOf[Object]
+    }
+  }
 
   def evaluate[Target](exchange: Exchange, toType: Class[Target]) = {
-    val value = expression(exchange)
+    val value = evaluate(exchange)
     exchange.getContext().getTypeConverter().convertTo(toType, value)
   }
 

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaPredicate.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaPredicate.scala?rev=1164847&r1=1164846&r2=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaPredicate.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/ScalaPredicate.scala
Sat Sep  3 11:37:30 2011
@@ -17,13 +17,18 @@
 package org.apache.camel
 package scala
 
+import dsl.languages.LanguageFunction
 import org.apache.camel.Predicate
-import org.apache.camel.util.ObjectHelper.evaluateValuePredicate
+import org.apache.camel.util.ObjectHelper._
 
 class ScalaPredicate(function: Exchange => Any) extends Predicate {
 
   override def matches(exchange: Exchange) = {
-    evaluateValuePredicate(function(exchange))
+    val predicate = function(exchange)
+    predicate match {
+      case f : LanguageFunction => f.matches(exchange)
+      case _ => evaluateValuePredicate(predicate)
+    }
   }
 
 }

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala?rev=1164847&r1=1164846&r2=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
Sat Sep  3 11:37:30 2011
@@ -18,15 +18,16 @@ package org.apache.camel
 package scala
 package dsl
 
+import languages.LanguageFunction
 import org.apache.camel.Exchange
 import org.apache.camel.model._
-import org.apache.camel.spi.Policy
 import org.apache.camel.processor.aggregate.AggregationStrategy
 import org.apache.camel.scala.dsl.builder.RouteBuilder
 
 import reflect.Manifest
 import java.lang.String
 import java.util.Comparator
+import spi.{Language, Policy}
 
 abstract class SAbstractDefinition[P <: ProcessorDefinition[_]] extends DSL with Wrapper[P]
with Block {
 
@@ -34,10 +35,10 @@ abstract class SAbstractDefinition[P <: 
   val unwrap = target
   implicit val builder: RouteBuilder
 
-  implicit def expressionBuilder(expression: Exchange => Any) = new ScalaExpression(expression)
-
   implicit def predicateBuilder(predicate: Exchange => Any) = new ScalaPredicate(predicate)
 
+  implicit def expressionBuilder(expression: Exchange => Any) = new ScalaExpression(expression)
+
   def apply(block: => Unit) = {
     builder.build(this, block)
     this

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala?rev=1164847&r1=1164846&r2=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
Sat Sep  3 11:37:30 2011
@@ -17,7 +17,6 @@
 package org.apache.camel
 package scala.dsl
 
-import org.apache.camel.builder.PredicateBuilder
 import org.apache.camel.model.ChoiceDefinition
 import org.apache.camel.scala.dsl.builder.RouteBuilder
 
@@ -29,8 +28,8 @@ case class SChoiceDefinition(override va
   }
   
   override def when(filter: Exchange => Any) = {
-    // should be evaluated as a predicate
-    val predicate = PredicateBuilder.toPredicate(filter)
+    // uses implicit conversion
+    val predicate = filter
     target.when(predicate)
     this
   }

Added: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/LanguageFunction.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/LanguageFunction.scala?rev=1164847&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/LanguageFunction.scala
(added)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/LanguageFunction.scala
Sat Sep  3 11:37:30 2011
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.scala.dsl.languages
+
+import org.apache.camel.{Expression, Predicate, Exchange}
+import org.apache.camel.spi.Language
+
+/**
+ * A language which has a function that can be evaluated as either an {@link org.apache.camel.Expression}
+ * or {@link org.apache.camel.Predicate}.
+ */
+class LanguageFunction(language: Language, expression: String) extends Predicate with Expression
{
+
+  def matches(exchange: Exchange) = {
+    language.createPredicate(expression).matches(exchange)
+  }
+
+  def evaluate[T](exchange: Exchange, toType : Class[T]) = {
+    language.createExpression(expression).evaluate(exchange, toType)
+  }
+
+  override def toString : String = language + "(" + expression + ")"
+
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/Languages.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/Languages.scala?rev=1164847&r1=1164846&r2=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/Languages.scala
(original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/languages/Languages.scala
Sat Sep  3 11:37:30 2011
@@ -71,6 +71,8 @@ object Languages {
 
   def evaluate(expression: String)(exchange: Exchange)(lang: String) : Any = {
     val language = exchange.getContext().resolveLanguage(lang)
-    language.createExpression(expression).evaluate(exchange, classOf[Object])
+    // return a language function as the language should support being
+    // evaluated as a predicate or expression depending on its usage
+    new LanguageFunction(language, expression)
   }
 }

Copied: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/CamelCBRTest.scala
(from r1164804, camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelTestSupportTest.scala)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/CamelCBRTest.scala?p2=camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/CamelCBRTest.scala&p1=camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelTestSupportTest.scala&r1=1164804&r2=1164847&rev=1164847&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/CamelTestSupportTest.scala
(original)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/CamelCBRTest.scala
Sat Sep  3 11:37:30 2011
@@ -14,29 +14,43 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.scala
+package org.apache.camel.scala.dsl
 
 import org.apache.camel.test.junit4.CamelTestSupport
-import org.apache.camel.component.mock.MockEndpoint
-import org.apache.camel.scala.dsl.builder.{RouteBuilder,RouteBuilderSupport}
+import org.apache.camel.scala.dsl.builder.{RouteBuilder, RouteBuilderSupport}
 import org.junit.Test
 
-class CamelTestSupportTest extends CamelTestSupport with RouteBuilderSupport {
-  
+class CamelCBRTest extends CamelTestSupport with RouteBuilderSupport {
+
   override protected def createRouteBuilder = builder
-  
+
+  @Test
+  def testFoo {
+    getMockEndpoint("mock:foo").expectedMessageCount(1)
+    getMockEndpoint("mock:other").expectedMessageCount(0)
+
+    template.sendBody("direct:start", "foo")
+
+    assertMockEndpointsSatisfied
+  }
+
   @Test
-  def testValidRequest{
-    val mock = getMockEndpoint("mock:output")
+  def testOther {
+    getMockEndpoint("mock:foo").expectedMessageCount(0)
+    getMockEndpoint("mock:other").expectedMessageCount(1)
+
+    template.sendBody("direct:start", "bar")
 
-    val message = "HelloWorld"
-    mock.expectedBodiesReceived(message)
-    template.sendBody("direct:start", message)
     assertMockEndpointsSatisfied
   }
-  
+
   val builder = new RouteBuilder {
-     "direct:start" to "mock:output"
+    "direct:start" ==> {
+      choice {
+        when(simple("${body} == 'foo'")) to "mock:foo"
+        otherwise to "mock:other"
+      }
+    }
   }
 
 }
\ No newline at end of file



Mime
View raw message