Author: gertv Date: Fri Mar 27 14:41:34 2009 New Revision: 759156 URL: http://svn.apache.org/viewvc?rev=759156&view=rev Log: CAMEL-463: Add support for intercept() to the Scala DSL
Added: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SInterceptDefinition.scala camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/InterceptorTest.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/SAbstractDefinition.scala camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.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=759156&r1=759155&r2=759156&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 Fri Mar 27 14:41:34 2009 @@ -32,6 +32,7 @@ def idempotentconsumer(expression: Exchange => Any): SIdempotentConsumerDefinition def inOnly(): SProcessorDefinition def inOut(): SProcessorDefinition + def intercept(expression: Exchange => Boolean) : SInterceptDefinition def loadbalance : SLoadBalanceDefinition def loop(expression: Exchange => Any) : SLoopDefinition def marshal(format : DataFormatDefinition) : DSL 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=759156&r1=759155&r2=759156&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 Fri Mar 27 14:41:34 2009 @@ -81,6 +81,12 @@ def inOnly = new SProcessorDefinition(target.inOnly.asInstanceOf[RawProcessorDefinition]) def inOut = new SProcessorDefinition(target.inOut.asInstanceOf[RawProcessorDefinition]) + + def intercept(expression: Exchange => Boolean) = { + val intercept = target.intercept + intercept.when(new ScalaPredicate(expression)) + new SInterceptDefinition(intercept) + } def loop(expression: Exchange => Any) = new SLoopDefinition(target.loop(expression)) Added: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SInterceptDefinition.scala URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SInterceptDefinition.scala?rev=759156&view=auto ============================================================================== --- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SInterceptDefinition.scala (added) +++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SInterceptDefinition.scala Fri Mar 27 14:41:34 2009 @@ -0,0 +1,37 @@ +/** + * 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.InterceptDefinition +import org.apache.camel.scala.dsl.builder.RouteBuilder + +/** + * Scala enrichment for Camel's IdempotentConsumerDefinition + */ +class SInterceptDefinition(val target: InterceptDefinition)(implicit val builder: RouteBuilder) extends SAbstractDefinition with Wrapper[InterceptDefinition] { + + val unwrap = target + + override def apply(block: => Unit) : SInterceptDefinition = { + builder.build(this, block) + this + } + + def stop = { + target.setStopIntercept(true) + } +} 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=759156&r1=759155&r2=759156&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 Fri Mar 27 14:41:34 2009 @@ -70,6 +70,11 @@ def idempotentconsumer(expression: Exchange => Any) = stack.top.idempotentconsumer(expression) def inOnly = stack.top.inOnly def inOut = stack.top.inOut + def intercept(expression: Exchange => Boolean) = { + val intercept = builder.intercept + intercept.when(new ScalaPredicate(expression)) + new SInterceptDefinition(intercept)(this) + } def loop(expression: Exchange => Any) = stack.top.loop(expression) def split(expression: Exchange => Any) = stack.top.split(expression) def otherwise = stack.top.otherwise Added: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/InterceptorTest.scala URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/InterceptorTest.scala?rev=759156&view=auto ============================================================================== --- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/InterceptorTest.scala (added) +++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/InterceptorTest.scala Fri Mar 27 14:41:34 2009 @@ -0,0 +1,44 @@ +/** + * 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 + +/** + * Test for an interceptor + */ +class InterceptorTest extends ScalaTestSupport { + + def testSimple() = { + "mock:a" expect { _.count = 1} + "mock:intercepted" expect { _.count = 1} + test { + "seda:a" ! ("NightHawk", "SongBird") + } + } + + val builder = new RouteBuilder { + //START SNIPPET: simple + intercept(_.in(classOf[String]) == "Nighthawk") { + to ("mock:intercepted") + } stop + + "seda:a" --> "mock:a" + //END SNIPPET: simple + } + +}