You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ge...@apache.org on 2009/02/10 22:40:37 UTC

svn commit: r743113 - in /camel/trunk/components/camel-scala/src: main/scala/org/apache/camel/scala/dsl/ main/scala/org/apache/camel/scala/dsl/builder/ test/scala/org/apache/camel/scala/dsl/

Author: gertv
Date: Tue Feb 10 21:40:37 2009
New Revision: 743113

URL: http://svn.apache.org/viewvc?rev=743113&view=rev
Log:
CAMEL-463: Adding support for setting the MEP from the DSL

Added:
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SetExchangePatternTest.scala
Modified:
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/RichTestUri.scala

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala?rev=743113&r1=743112&r2=743113&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala Tue Feb 10 21:40:37 2009
@@ -23,28 +23,30 @@
  */
 trait DSL {
   
+  def aggregate(expression: Exchange => Any) : SAggregatorType
+  def as[Target](toType: Class[Target]) : DSL
   def attempt : STryType
   def bean(bean: Any) : DSL
   def choice : SChoiceType
-  def -->(uris: String*) : DSL
-  def to(uris: String*) : DSL
-  def when(filter: Exchange => Boolean) : SChoiceType
-  def as[Target](toType: Class[Target]) : DSL
-  def recipients(expression: Exchange => Any) : DSL
-  def splitter(expression: Exchange => Any) : SSplitterType
-  def otherwise : DSL
+  def delay(delay: Period) : SDelayerType
+  def idempotentconsumer(expression: Exchange => Any): SIdempotentConsumerType
+  def inOnly(): SProcessorType
+  def inOut(): SProcessorType
+  def loadbalance : SLoadBalanceType
   def marshal(format : DataFormatType) : DSL
   def multicast : SMulticastType
+  def otherwise : DSL
   def process(function: Exchange => Unit) : DSL
-  def throttle(frequency: Frequency) : SThrottlerType
-  def loadbalance : SLoadBalanceType
-  def delay(delay: Period) : SDelayerType
+  def recipients(expression: Exchange => Any) : DSL
   def resequence(expression: Exchange => Any) : SResequencerType
   def setbody(expression: Exchange => Any) : DSL
   def setheader(header: String, expression: Exchange => Any) : DSL
+  def splitter(expression: Exchange => Any) : SSplitterType
   def thread(number: Int) : SThreadType
+  def throttle(frequency: Frequency) : SThrottlerType
+  def to(uris: String*) : DSL
   def unmarshal(format: DataFormatType) : DSL
-  def aggregate(expression: Exchange => Any) : SAggregatorType
-  def idempotentconsumer(expression: Exchange => Any): SIdempotentConsumerType
+  def when(filter: Exchange => Boolean) : SChoiceType
+  def -->(uris: String*) : DSL
 
 }

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala?rev=743113&r1=743112&r2=743113&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala Tue Feb 10 21:40:37 2009
@@ -27,6 +27,8 @@
 
 abstract class SAbstractType extends DSL {
   
+  type RawProcessorType = ProcessorType[P] forSome {type P}
+  
   val target : ProcessorType[T] forSome {type T}
   implicit val builder: RouteBuilder
   implicit def expressionBuilder(expression: Exchange => Any) = new ScalaExpression(expression)
@@ -48,7 +50,7 @@
     
   def as[Target](toType: Class[Target]) = {
     target.convertBodyTo(toType)
-    new SProcessorType(target.asInstanceOf[ProcessorType[P] forSome {type P}])
+    new SProcessorType(target.asInstanceOf[RawProcessorType])
   }
   
   def attempt : STryType = new STryType(target.tryBlock)
@@ -57,7 +59,7 @@
     new SSplitterType(target.split(expression))
     
   def recipients(expression: Exchange => Any) = 
-    new SProcessorType(target.recipientList(expression).asInstanceOf[ProcessorType[P] forSome {type P}])
+    new SProcessorType(target.recipientList(expression).asInstanceOf[RawProcessorType])
 
   def apply(block: => Unit) = {
     builder.build(this, block)
@@ -65,9 +67,9 @@
   }
 
   def bean(bean: Any) = bean match {
-    case cls: Class[_] => new SProcessorType(target.bean(cls).asInstanceOf[ProcessorType[P] forSome {type P}])
-    case ref: String => new SProcessorType(target.beanRef(ref).asInstanceOf[ProcessorType[P] forSome {type P}])
-    case obj: Any => new SProcessorType(target.bean(obj).asInstanceOf[ProcessorType[P] forSome {type P}])
+    case cls: Class[_] => new SProcessorType(target.bean(cls).asInstanceOf[RawProcessorType])
+    case ref: String => new SProcessorType(target.beanRef(ref).asInstanceOf[RawProcessorType])
+    case obj: Any => new SProcessorType(target.bean(obj).asInstanceOf[RawProcessorType])
   }
   
   def choice = new SChoiceType(target.choice)
@@ -76,6 +78,9 @@
     throw new Exception("otherwise is only supported in a choice block or after a when statement")
   
   def idempotentconsumer(expression: Exchange => Any) = new SIdempotentConsumerType(target.idempotentConsumer(expression, null))
+  
+  def inOnly = new SProcessorType(target.inOnly.asInstanceOf[RawProcessorType])
+  def inOut = new SProcessorType(target.inOut.asInstanceOf[RawProcessorType])
  
   def marshal(format: DataFormatType) = {
     target.marshal(format)
@@ -89,7 +94,6 @@
     this
   }
  
-  
   def throttle(frequency: Frequency) = new SThrottlerType(target.throttle(frequency.count).timePeriodMillis(frequency.period.milliseconds))
   
   def loadbalance = new SLoadBalanceType(target.loadBalance)

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala?rev=743113&r1=743112&r2=743113&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala Tue Feb 10 21:40:37 2009
@@ -55,7 +55,7 @@
   def handle[E](block: => Unit)(implicit manifest: Manifest[E]) = {
      val exception = new SExceptionType(builder.onException(manifest.erasure))(this)
      exception.apply(block)
-  }  
+  }
 
   def attempt = stack.top.attempt
   def bean(bean: Any) = stack.top.bean(bean)
@@ -66,6 +66,8 @@
   def as[Target](toType: Class[Target]) = stack.top.as(toType)
   def recipients(expression: Exchange => Any) = stack.top.recipients(expression)
   def idempotentconsumer(expression: Exchange => Any) = stack.top.idempotentconsumer(expression)
+  def inOnly = stack.top.inOnly
+  def inOut = stack.top.inOut
   def splitter(expression: Exchange => Any) = stack.top.splitter(expression)
   def otherwise = stack.top.otherwise
   def marshal(format: DataFormatType) = stack.top.marshal(format)

Modified: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/RichTestUri.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/RichTestUri.scala?rev=743113&r1=743112&r2=743113&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/RichTestUri.scala (original)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/RichTestUri.scala Tue Feb 10 21:40:37 2009
@@ -21,7 +21,12 @@
 class RichTestUri(uri: String, support: ScalaTestSupport) {
 
   def !(messages: Any*) = {
-    messages.foreach(support.getTemplate().sendBody(uri, _))
+    messages.foreach { 
+      _ match {
+        case exchange: Exchange => support.getTemplate().send(uri, exchange)
+        case anything: Any => support.getTemplate().sendBody(uri, anything)
+      }
+    }
   }
 
   def expect(block: MockEndpoint => Unit) = {

Added: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SetExchangePatternTest.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SetExchangePatternTest.scala?rev=743113&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SetExchangePatternTest.scala (added)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SetExchangePatternTest.scala Tue Feb 10 21:40:37 2009
@@ -0,0 +1,67 @@
+/**
+ * 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;
+ 
+import scala.dsl.builder.RouteBuilder
+import junit.framework.Assert.assertEquals
+import org.apache.camel.ExchangePattern._
+
+/**
+ * Test for setting the ExchangePattern 
+ */
+class SetExchangePatternTest extends ScalaTestSupport {
+
+  def testSimpleInOnly() = doTest("direct:a", "mock:a", InOnly)
+  def testBlockInOnly() = doTest("direct:b", "mock:b", InOnly)
+  def testSimpleInOut() = doTest("direct:c", "mock:c", InOut)
+  def testBlockInOut() = doTest("direct:d", "mock:d", InOut)
+  
+  
+  def doTest(from: String, to: String, expected: ExchangePattern) = {
+    to expect { _.count = 1}
+    val exchange = in("MyTestBody")
+    expected match {
+      case InOut => exchange.setPattern(InOnly)
+      case InOnly => exchange.setPattern(InOut)
+    }
+    test {
+      from ! exchange
+    }
+    assertEquals(expected, getMockEndpoint(to).getReceivedExchanges().get(0).getPattern())
+  }
+
+  val builder = new RouteBuilder {
+     //START SNIPPET: simple
+     "direct:a" inOnly() to ("mock:a")
+     "direct:c" inOut() to ("mock:c")
+     //END SNIPPET: simple
+     
+     //START SNIPPET: block
+     "direct:b" ==> {
+       inOnly() {
+         to ("mock:b")
+       }
+     }
+     "direct:d" ==> {
+       inOut() {
+         to ("mock:d")
+       }
+     }
+     //END SNIPPET: block
+   }
+
+}