servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r1149199 - in /servicemix/smx5/trunk/core/src: main/scala/org/apache/servicemix/core/ test/scala/org/apache/servicemix/core/
Date Thu, 21 Jul 2011 14:39:42 GMT
Author: gertv
Date: Thu Jul 21 14:39:39 2011
New Revision: 1149199

URL: http://svn.apache.org/viewvc?rev=1149199&view=rev
Log:
Allow InterceptStrategy instances to be added/removed at runtime

Modified:
    servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/BreadcrumbStrategy.scala
    servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/GlobalInterceptStrategy.scala
    servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala

Modified: servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/BreadcrumbStrategy.scala
URL: http://svn.apache.org/viewvc/servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/BreadcrumbStrategy.scala?rev=1149199&r1=1149198&r2=1149199&view=diff
==============================================================================
--- servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/BreadcrumbStrategy.scala
(original)
+++ servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/BreadcrumbStrategy.scala
Thu Jul 21 14:39:39 2011
@@ -28,19 +28,15 @@ class BreadcrumbStrategy extends Interce
 
   import BreadcrumbStrategy.{hasBreadCrumb, addBreadCrumb}
 
-  def wrapProcessorInInterceptors(context: CamelContext, definition: ProcessorDefinition[_],
target: Processor, nextTarget: Processor) : Processor = {
-    new ProcessorWrapper(target)
-  }
-
-  class ProcessorWrapper(target: Processor) extends Processor {
-    def process(exchange: Exchange) {
-      if (!hasBreadCrumb(exchange)) {
-        addBreadCrumb(exchange)
+  def wrapProcessorInInterceptors(context: CamelContext, definition: ProcessorDefinition[_],
target: Processor, nextTarget: Processor) : Processor =
+    new Processor() {
+      def process(exchange: Exchange) {
+        if (!hasBreadCrumb(exchange)) {
+          addBreadCrumb(exchange)
+        }
+        target.process(exchange)
       }
-      target.process(exchange)
     }
-  }
-
 }
 
 object BreadcrumbStrategy {

Modified: servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/GlobalInterceptStrategy.scala
URL: http://svn.apache.org/viewvc/servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/GlobalInterceptStrategy.scala?rev=1149199&r1=1149198&r2=1149199&view=diff
==============================================================================
--- servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/GlobalInterceptStrategy.scala
(original)
+++ servicemix/smx5/trunk/core/src/main/scala/org/apache/servicemix/core/GlobalInterceptStrategy.scala
Thu Jul 21 14:39:39 2011
@@ -16,28 +16,52 @@
  */
 package org.apache.servicemix.core
 
-import collection.mutable.MutableList
-import org.apache.camel.spi.InterceptStrategy
+import collection.mutable.ListBuffer
 import org.apache.camel.model.ProcessorDefinition
-import org.apache.camel.{Processor, CamelContext}
+import org.apache.camel.{Exchange, Processor, CamelContext}
+import org.apache.camel.spi.InterceptStrategy
 
 class GlobalInterceptStrategy extends InterceptStrategy {
 
-  var strategies = new MutableList[InterceptStrategy]
+  val strategies = new ListBuffer[InterceptStrategy]
+  var lastUpdate = System.nanoTime();
 
   def addStrategy(strategy : InterceptStrategy) {
     strategies += strategy
+    lastUpdate = System.nanoTime()
   }
 
   def removeStrategy(strategy : InterceptStrategy) {
-    strategies.filterNot( _ == strategy)
+    strategies -= strategy
+    lastUpdate = System.nanoTime()
   }
 
-  def wrapProcessorInInterceptors(context: CamelContext, definition: ProcessorDefinition[_],
target: Processor, nextTarget: Processor) : Processor = {
-    var t = target
-    for( strategy <- strategies ) {
-      t = strategy.wrapProcessorInInterceptors(context, definition, t, nextTarget)
+  def configure(context: CamelContext, definition: ProcessorDefinition[_], target: Processor,
nextTarget: Processor) =
+    strategies.foldLeft(target){ (processor: Processor, strategy: InterceptStrategy) =>
+      strategy.wrapProcessorInInterceptors(context, definition, processor, nextTarget)
+    }
+
+  def wrapProcessorInInterceptors(context: CamelContext, definition: ProcessorDefinition[_],
target: Processor, nextTarget: Processor) : Processor =
+    new GlobalInterceptProcessor(context, definition, target, nextTarget, this)
+
+
+  class GlobalInterceptProcessor(context:CamelContext, definition: ProcessorDefinition[_],
target: Processor,
+                                 nextTarget: Processor, globals: GlobalInterceptStrategy)
extends Processor {
+
+    var currentProcessor = globals.configure(context, definition, target, nextTarget)
+    var lastUpdate = System.nanoTime();
+
+    def processor = {
+      if (lastUpdate < globals.lastUpdate) {
+        // global interceptor list has been updated - recreating the processor first
+        currentProcessor = globals.configure(context, definition, target, nextTarget)
+      }
+      currentProcessor
     }
-    t
+
+    def process(exchange: Exchange) {
+      processor.process(exchange)
+    }
+
   }
 }
\ No newline at end of file

Modified: servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala
URL: http://svn.apache.org/viewvc/servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala?rev=1149199&r1=1149198&r2=1149199&view=diff
==============================================================================
--- servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala
(original)
+++ servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala
Thu Jul 21 14:39:39 2011
@@ -19,7 +19,6 @@ package org.apache.servicemix.core
 import _root_.scala.Predef._
 import org.junit.runner.RunWith
 import org.scalatest.junit.JUnitRunner
-import org.scalatest.{BeforeAndAfterAll, FunSuite}
 import collection.immutable.List
 import org.apache.camel.component.mock.MockEndpoint
 import org.apache.camel.scala.dsl.builder.{RouteBuilderSupport, RouteBuilder}
@@ -28,9 +27,11 @@ import scala.collection.JavaConversions.
 import org.apache.camel.impl.{DefaultCamelContext, DefaultProducerTemplate}
 
 import org.apache.servicemix.core.BreadcrumbStrategy.{hasBreadCrumb, getBreadCrumb}
+import org.scalatest.Assertions._
+import org.scalatest.{BeforeAndAfterEach, BeforeAndAfterAll, FunSuite}
 
 @RunWith(classOf[JUnitRunner])
-class BreadcrumbStrategyTest extends FunSuite with RouteBuilderSupport with BeforeAndAfterAll
{
+class BreadcrumbStrategyTest extends FunSuite with RouteBuilderSupport with BeforeAndAfterAll
with BeforeAndAfterEach {
 
   val messages = List("<gingerbread/>", "<cakes/>", "<sugar/>")
 
@@ -39,7 +40,6 @@ class BreadcrumbStrategyTest extends Fun
   lazy val context = {
     val result = new DefaultCamelContext()
     result.addInterceptStrategy(globals)
-    globals.addStrategy(new BreadcrumbStrategy)
     result.addRoutes(createRouteBuilder())
     result.start()
     result
@@ -57,6 +57,8 @@ class BreadcrumbStrategyTest extends Fun
   }
 
   test("add breadcrumbs to message headers") {
+    globals.addStrategy(new BreadcrumbStrategy)
+
     for (body <- messages) {
       template.sendBody("direct:test", body)
     }
@@ -76,6 +78,53 @@ class BreadcrumbStrategyTest extends Fun
     assert(hansels == gretels, "Gretel should be able to find all of Hansel's bread crumbs")
   }
 
+  test("bread crumb strategy can be disabled if necessary") {
+    val breadcrumbs = new BreadcrumbStrategy
+    globals.addStrategy(breadcrumbs)
+
+    for (body <- messages) {
+      template.sendBody("direct:test", body)
+    }
+
+    val hansel = getMockEndpoint("mock:hansel")
+    hansel.expectedMessageCount(messages.size)
+
+    val gretel = getMockEndpoint("mock:gretel")
+    gretel.expectedMessageCount(messages.size)
+
+    List(hansel, gretel).foreach(_.assertIsSatisfied())
+
+    val hansels = for (exchange <- hansel.getExchanges) yield getBreadCrumb(exchange)
+    assert(hansels.toSet.size == messages.size, "We should have distinct breadcrumbs per
message")
+
+    val gretels = for (exchange <- gretel.getExchanges) yield getBreadCrumb(exchange)
+    assert(hansels == gretels, "Gretel should be able to find all of Hansel's bread crumbs")
+
+    // let's now disable the bread crumbs and just continue with same context/processors/...
+    globals.removeStrategy(breadcrumbs)
+    MockEndpoint.resetMocks(context)
+
+    for (body <- messages) {
+      template.sendBody("direct:test", body)
+    }
+
+    hansel.expectedMessageCount(messages.size)
+
+    gretel.expectedMessageCount(messages.size)
+
+    List(hansel, gretel).foreach(_.assertIsSatisfied())
+
+    for (exchange <- hansel.getExchanges)
+      assert(!hasBreadCrumb(exchange), "There should be no more bread crumbs here")
+
+    for (exchange <- gretel.getExchanges)
+      assert(!hasBreadCrumb(exchange), "There should be no more bread crumbs here")
+  }
+
+  override protected def afterEach() = {
+    MockEndpoint.resetMocks(context)
+    globals.strategies.clear()
+  }
 
   def getMockEndpoint(name: String) = context.getEndpoint(name, classOf[MockEndpoint])
 



Mime
View raw message