servicemix-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1149198&view=rev
Log:
Add bread crumb strategy implementation

Added:
    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
    servicemix/smx5/trunk/pom.xml

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=1149198&r1=1149197&r2=1149198&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:19 2011
@@ -20,18 +20,50 @@ import org.apache.camel.spi.InterceptStr
 import org.apache.camel.model.ProcessorDefinition
 import org.apache.camel.{Exchange, Processor, CamelContext}
 
-
+/**
+ * The ServiceMix bread crumb strategy adds a header to the message to ensure we can follow
the message throughout
+ * different routes and processors.
+ */
 class BreadcrumbStrategy extends InterceptStrategy {
 
+  import BreadcrumbStrategy.{hasBreadCrumb, addBreadCrumb}
+
   def wrapProcessorInInterceptors(context: CamelContext, definition: ProcessorDefinition[_],
target: Processor, nextTarget: Processor) : Processor = {
-    System.out.println("Wrapping processor: " + target)
     new ProcessorWrapper(target)
   }
 
   class ProcessorWrapper(target: Processor) extends Processor {
     def process(exchange: Exchange) {
+      if (!hasBreadCrumb(exchange)) {
+        addBreadCrumb(exchange)
+      }
       target.process(exchange)
     }
   }
 
 }
+
+object BreadcrumbStrategy {
+
+  /**
+   * ServiceMix bread crumb header name
+   */
+  val SERVICEMIX_BREAD_CRUMB = "ServiceMixBreadCrumb"
+
+  /**
+   * Does the exchange have a ServiceMix bread crumb set?
+   */
+  def hasBreadCrumb(exchange: Exchange) : Boolean = getBreadCrumb(exchange) != null
+
+  /**
+   * Get the ServiceMix bread crumb value for an Exchange
+   */
+  def getBreadCrumb(exchange: Exchange) : String = exchange.getIn.getHeader(SERVICEMIX_BREAD_CRUMB,
classOf[String])
+
+  /**
+   * Add a ServiceMix bread crumb to an Exchange
+   */
+  def addBreadCrumb(exchange: Exchange) : Unit = exchange.getIn.setHeader(SERVICEMIX_BREAD_CRUMB,
+                                                                          exchange.getContext.getUuidGenerator.generateUuid())
+
+}

Added: 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=1149198&view=auto
==============================================================================
--- servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala
(added)
+++ servicemix/smx5/trunk/core/src/test/scala/org/apache/servicemix/core/BreadcrumbStrategyTest.scala
Thu Jul 21 14:39:19 2011
@@ -0,0 +1,90 @@
+package org.apache.servicemix.core
+
+/**
+ * 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.
+ */
+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}
+
+import scala.collection.JavaConversions.asScalaBuffer
+import org.apache.camel.impl.{DefaultCamelContext, DefaultProducerTemplate}
+
+import org.apache.servicemix.core.BreadcrumbStrategy.{hasBreadCrumb, getBreadCrumb}
+
+@RunWith(classOf[JUnitRunner])
+class BreadcrumbStrategyTest extends FunSuite with RouteBuilderSupport with BeforeAndAfterAll
{
+
+  val messages = List("<gingerbread/>", "<cakes/>", "<sugar/>")
+
+  val globals = new GlobalInterceptStrategy
+
+  lazy val context = {
+    val result = new DefaultCamelContext()
+    result.addInterceptStrategy(globals)
+    globals.addStrategy(new BreadcrumbStrategy)
+    result.addRoutes(createRouteBuilder())
+    result.start()
+    result
+  }
+
+  lazy val template = {
+    val result = new DefaultProducerTemplate(context);
+    result.start()
+    result
+  }
+
+  override protected def afterAll() = {
+    template.stop()
+    context.stop()
+  }
+
+  test("add breadcrumbs to message headers") {
+    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 == 3, "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")
+  }
+
+
+  def getMockEndpoint(name: String) = context.getEndpoint(name, classOf[MockEndpoint])
+
+  def createRouteBuilder() = new RouteBuilder {
+      "direct:test" ==> {
+        to("mock:hansel")
+        to("seda:forest")
+      }
+
+      "seda:forest" to "mock:gretel"
+    }
+}
\ No newline at end of file

Modified: servicemix/smx5/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx5/trunk/pom.xml?rev=1149198&r1=1149197&r2=1149198&view=diff
==============================================================================
--- servicemix/smx5/trunk/pom.xml (original)
+++ servicemix/smx5/trunk/pom.xml Thu Jul 21 14:39:19 2011
@@ -36,7 +36,7 @@
     <inceptionYear>2005</inceptionYear>
 
     <properties>
-        <camel-version>2.7.2</camel-version>
+        <camel-version>2.8-SNAPSHOT</camel-version>
         <karaf-version>2.2.1</karaf-version>
         <scala-version>2.9.0-1</scala-version>
         <scala-plugin-version>2.15.0</scala-plugin-version>



Mime
View raw message