Title: [705] trunk/core/src/test/java/org/servicemix/components: SM-135: chaining router component
Revision
705
Author
gnt
Date
2005-10-28 22:50:10 -0400 (Fri, 28 Oct 2005)

Log Message

SM-135: chaining router component

Added Paths


Diff

Added: trunk/core/src/main/java/org/servicemix/components/util/ChainedComponent.java (704 => 705)

--- trunk/core/src/main/java/org/servicemix/components/util/ChainedComponent.java	2005-10-29 01:38:29 UTC (rev 704)
+++ trunk/core/src/main/java/org/servicemix/components/util/ChainedComponent.java	2005-10-29 02:50:10 UTC (rev 705)
@@ -0,0 +1,110 @@
+/**
+ *
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ *
+ * Licensed 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.servicemix.components.util;
+
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+
+import org.servicemix.components.util.TransformComponentSupport;
+
+/**
+ * This class allows a series of componeents to be chained together. It will
+ * invoke the first service, then take the output of that first service as the
+ * input to the next service, and return the overall results when finished.
+ * 
+ * All properties and attachments are maintained.
+ * 
+ * @author birchfieldj
+ * 
+ */
+public class ChainedComponent extends TransformComponentSupport {
+
+    private QName[] services = new QName[0];
+
+    protected boolean transform(MessageExchange exchange, 
+                                NormalizedMessage in,
+                                NormalizedMessage out) throws MessagingException {
+        NormalizedMessage curIn = in;
+        MessageExchange curExchange = exchange;
+        for (int i = 0; i < services.length; i++) {
+            MessageExchange mexchange = this.getDeliveryChannel()
+                    .createExchangeFactoryForService(services[i])
+                    .createInOutExchange();
+            copyProperties(curExchange, mexchange);
+            curIn = invokeService(mexchange, curIn, services[i]);
+            curExchange = mexchange;
+        }
+        getMessageTransformer().transform(exchange, curIn, out);
+        copyProperties(curExchange, exchange);
+        return true;
+    }
+
+    /**
+     * Invokes the service with the given message, and returns the output
+     * 
+     * @param exchange
+     * @param in
+     * @param service
+     * @return the out message of the invoked service
+     * @throws MessagingException
+     */
+    private NormalizedMessage invokeService(MessageExchange exchange,
+                                            NormalizedMessage in, 
+                                            QName service) throws MessagingException {
+        NormalizedMessage msg = exchange.createMessage();
+        getMessageTransformer().transform(exchange, in, msg);
+        exchange.setMessage(msg, "in");
+        boolean result = this.getDeliveryChannel().sendSync(exchange);
+        if (result) {
+            return exchange.getMessage("out");
+        }
+        throw new MessagingException("Could not invoke service: " + service);
+    }
+
+    /**
+     * 
+     * @param in
+     *            echange to copy from
+     * @param out
+     *            excahnge to copy to
+     */
+    private void copyProperties(MessageExchange in, MessageExchange out) {
+        Set propertyNames = in.getPropertyNames();
+        for (Iterator iter = propertyNames.iterator(); iter.hasNext();) {
+            String name = (String) iter.next();
+            out.setProperty(name, in.getProperty(name));
+        }
+    }
+
+    /**
+     * Allows the services to be set
+     * 
+     * @param services
+     *            a collection of QNAmes representing the services to be
+     *            invoked.
+     */
+    public void setServices(QName[] services) {
+        this.services = services;
+    }
+
+}

Added: trunk/core/src/test/java/org/servicemix/components/util/ChainedComponentTest.java (704 => 705)

--- trunk/core/src/test/java/org/servicemix/components/util/ChainedComponentTest.java	2005-10-29 01:38:29 UTC (rev 704)
+++ trunk/core/src/test/java/org/servicemix/components/util/ChainedComponentTest.java	2005-10-29 02:50:10 UTC (rev 705)
@@ -0,0 +1,46 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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.servicemix.components.util;
+
+import org.servicemix.examples.MessageList;
+import org.servicemix.examples.SpringTestSupport;
+import org.servicemix.jbi.config.DebugClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+
+import javax.jbi.messaging.NormalizedMessage;
+
+import java.util.Iterator;
+
+
+public class ChainedComponentTest extends SpringTestSupport {
+
+    public void testSendingAndReceivingMessagesUsingSpring() throws Exception {
+        super.testSendingAndReceivingMessagesUsingSpring();
+        MessageList messageList = getReceivedMessageList();
+        for (Iterator iter = messageList.getMessages().iterator(); iter.hasNext();) {
+            NormalizedMessage msg = (NormalizedMessage) iter.next();
+            assertNotNull(msg.getProperty("prop1"));
+            assertNotNull(msg.getProperty("prop2"));
+        } 
+    }
+    
+    protected AbstractXmlApplicationContext createBeanFactory() {
+        return new DebugClassPathXmlApplicationContext("org/servicemix/components/util/chained-router.xml");
+    }
+
+}

Added: trunk/core/src/test/java/org/servicemix/components/util/PropertyAddTransformer.java (704 => 705)

--- trunk/core/src/test/java/org/servicemix/components/util/PropertyAddTransformer.java	2005-10-29 01:38:29 UTC (rev 704)
+++ trunk/core/src/test/java/org/servicemix/components/util/PropertyAddTransformer.java	2005-10-29 02:50:10 UTC (rev 705)
@@ -0,0 +1,50 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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.servicemix.components.util;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+public class PropertyAddTransformer extends CopyTransformer {
+
+    String name;
+    String value;
+    
+    public boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+        super.transform(exchange, in, out);
+        out.setProperty(name, value);
+        return true;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}
\ No newline at end of file

Added: trunk/core/src/test/resources/org/servicemix/components/util/chained-router.xml (704 => 705)

--- trunk/core/src/test/resources/org/servicemix/components/util/chained-router.xml	2005-10-29 01:38:29 UTC (rev 704)
+++ trunk/core/src/test/resources/org/servicemix/components/util/chained-router.xml	2005-10-29 02:50:10 UTC (rev 705)
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:sm="http://servicemix.org/config/1.0"
+	xmlns:foo="http://servicemix.org/demo/">
+
+	<!-- the JBI container -->
+	<sm:container id="jbi" flowName="st">
+		<sm:activationSpecs>
+
+			<sm:activationSpec componentName="sender"
+				service="foo:sender" endpoint="sender"
+				destinationService="foo:chain">
+				<sm:component>
+					<bean
+						class="org.servicemix.examples.SenderComponent" />
+				</sm:component>
+			</sm:activationSpec>
+
+			<sm:activationSpec componentName="chain" service="foo:chain"
+				destinationService="foo:receiver">
+				<sm:component>
+					<bean xmlns=""
+						class="org.servicemix.components.util.ChainedComponent">
+						<property name="services">
+							<list>
+								<bean
+									class="javax.xml.namespace.QName">
+									<constructor-arg
+										value="http://servicemix.org/demo/" />
+									<constructor-arg
+										value="transformer1" />
+								</bean>
+								<bean
+									class="javax.xml.namespace.QName">
+									<constructor-arg
+										value="http://servicemix.org/demo/" />
+									<constructor-arg
+										value="transformer2" />
+								</bean>
+							</list>
+						</property>
+					</bean>
+				</sm:component>
+			</sm:activationSpec>
+
+			<sm:activationSpec componentName="transformer1"
+				service="foo:transformer1">
+				<sm:component>
+					<TransformComponent
+						xmlns="java://org.servicemix.components.util">
+						<transformer>
+							<PropertyAddTransformer name="prop1"
+								value="value1" />
+						</transformer>
+					</TransformComponent>
+				</sm:component>
+			</sm:activationSpec>
+
+			<sm:activationSpec componentName="transformer2"
+				service="foo:transformer2">
+				<sm:component>
+					<TransformComponent
+						xmlns="java://org.servicemix.components.util">
+						<transformer>
+							<PropertyAddTransformer name="prop2"
+								value="value2" />
+						</transformer>
+					</TransformComponent>
+				</sm:component>
+			</sm:activationSpec>
+
+			<sm:activationSpec componentName="receiver"
+				service="foo:receiver" endpoint="receiver">
+				<sm:component>
+					<bean
+						class="org.servicemix.examples.ReceiverComponent" />
+				</sm:component>
+			</sm:activationSpec>
+		</sm:activationSpecs>
+  </sm:container>
+
+</beans>

Reply via email to