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 2008/08/30 11:19:26 UTC

svn commit: r690484 - in /activemq/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: Sat Aug 30 02:19:25 2008
New Revision: 690484

URL: http://svn.apache.org/viewvc?rev=690484&view=rev
Log:
CAMEL-463: Adding STryType for try-catch-finally processing

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

Modified: activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala?rev=690484&r1=690483&r2=690484&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala (original)
+++ activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala Sat Aug 30 02:19:25 2008
@@ -30,6 +30,7 @@
   def recipients(expression: Exchange => Any) : DSL
   def splitter(expression: Exchange => Any) : SSplitterType
   def otherwise : DSL
+  def monitor : STryType
   def multicast : SMulticastType
   def process(function: Exchange => Unit) : DSL
   def throttle(frequency: Frequency) : SThrottlerType

Modified: activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala?rev=690484&r1=690483&r2=690484&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala (original)
+++ activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractType.scala Sat Aug 30 02:19:25 2008
@@ -70,6 +70,8 @@
   def otherwise : SChoiceType = 
     throw new Exception("otherwise is only supported in a choice block or after a when statement")
   
+  def monitor : STryType = new STryType(target.tryBlock)
+  
   def multicast = new SMulticastType(target.multicast)
   
   def process(function: Exchange => Unit) = {

Added: activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/STryType.scala
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/STryType.scala?rev=690484&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/STryType.scala (added)
+++ activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/STryType.scala Sat Aug 30 02:19:25 2008
@@ -0,0 +1,41 @@
+/**
+ * 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 org.apache.camel.model.TryType
+import org.apache.camel.scala.dsl.builder.RouteBuilder
+
+/**
+ * Scala enrichment for Camel's DelayerType
+ */
+class STryType(val target: TryType)(implicit val builder: RouteBuilder) extends SAbstractType with Wrapper[TryType] {
+  
+  val unwrap = target
+  
+  override def apply(block: => Unit) : STryType = super.apply(block).asInstanceOf[STryType]
+  
+  def handle[Target](exception: Class[Target]) = {
+    target.handle(exception)
+    this
+  }
+  
+  def ensure = {
+    target.finallyBlock
+    this
+  }
+ 
+}
\ No newline at end of file

Modified: activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala?rev=690484&r1=690483&r2=690484&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala (original)
+++ activemq/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala Sat Aug 30 02:19:25 2008
@@ -59,6 +59,7 @@
   def recipients(expression: Exchange => Any) = stack.top.recipients(expression)
   def splitter(expression: Exchange => Any) = stack.top.splitter(expression)
   def otherwise = stack.top.otherwise
+  def monitor = stack.top.monitor
   def multicast = stack.top.multicast
   def process(function: Exchange => Unit) = stack.top.process(function)
   def throttle(frequency: Frequency) = stack.top.throttle(frequency)

Added: activemq/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TryCatchFinallyTest.scala
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TryCatchFinallyTest.scala?rev=690484&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TryCatchFinallyTest.scala (added)
+++ activemq/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/TryCatchFinallyTest.scala Sat Aug 30 02:19:25 2008
@@ -0,0 +1,70 @@
+/**
+ * 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 org.w3c.dom.Document
+import scala.dsl.builder.RouteBuilder
+import junit.framework.Assert._
+
+/**
+ * Test case for try (monitor) - catch (handle) - finally (always)
+ */
+class TryCatchFinallyTest extends ScalaTestSupport {
+  
+  var handled = false;
+  
+  def testTryCatchFinally = {
+    "mock:a" expect { _.count = 1 }
+    "mock:b" expect { _.count = 1 }
+    "mock:c" expect { _.count = 2 }
+    test {
+       "direct:a" ! ("any given message", 256) 
+    }
+  }
+    
+  val builder =
+    new RouteBuilder {
+       def failingProcessor(exchange: Exchange) = {
+         exchange.in match {
+           case text: String => //graciously do nothing
+           case _ => throw new RuntimeException("Strings are good, the rest is bad")
+         }
+       }
+       
+       def catchProcessor(exchange: Exchange) = {
+          // we shouldn't get any Strings here
+          assertFalse(exchange.getIn().getBody().getClass().equals(classOf[String]))
+          // the exchange shouldn't have been marked failed
+          assertFalse(exchange.isFailed)
+       }
+              
+       //START SNIPPET: block}
+       "direct:a" ==> {
+         monitor {
+           process(failingProcessor)
+           to ("mock:a")
+         } handle(classOf[Exception]) apply {
+           process(catchProcessor)
+           to ("mock:b")
+         } ensure {
+           to ("mock:c");
+         }
+       }
+       //END SNIPPET: block
+    }
+
+}