camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r673462 - in /activemq/camel/trunk/components/camel-jms/src: main/java/org/apache/camel/component/jms/ test/java/org/apache/camel/component/jms/issues/
Date Wed, 02 Jul 2008 18:43:58 GMT
Author: davsclaus
Date: Wed Jul  2 11:43:58 2008
New Revision: 673462

URL: http://svn.apache.org/viewvc?rev=673462&view=rev
Log:
CAMEL-653: JMS Component now preserves java identifiers as header names to support etc. bean
method names and file component filename set in headers.

Added:
    activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
  (with props)
    activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java

Modified: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java?rev=673462&r1=673461&r2=673462&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
(original)
+++ activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
Wed Jul  2 11:43:58 2008
@@ -46,8 +46,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-
-
 /**
  * A Strategy used to convert between a Camel {@link JmsExchange} and {@link JmsMessage}
  * to and from a JMS {@link Message}
@@ -182,7 +180,9 @@
                 LOG.debug("Ignoring JMS header: " + headerName + " with value: " + headerValue);
             }
         } else if (shouldOutputHeader(in, headerName, headerValue)) {
-            jmsMessage.setObjectProperty(headerName, headerValue);
+            // must encode to safe JMS header name before setting property on jmsMessage
+            String key = encodeToSafeJmsHeaderName(headerName);
+            jmsMessage.setObjectProperty(key, headerValue);
         }
     }
 
@@ -264,11 +264,39 @@
 
     /**
      * Strategy to allow filtering of headers which are put on the JMS message
+     * <p/>
+     * <b>Note</b>: Currently only supports sending java identifiers as keys
      */
     protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName,
                                          Object headerValue) {
+        String key = encodeToSafeJmsHeaderName(headerName);
         return headerValue != null && !getIgnoreJmsHeaders().contains(headerName)
-               && ObjectHelper.isJavaIdentifier(headerName);
+               && ObjectHelper.isJavaIdentifier(key);
+    }
+
+    /**
+     * Encoder to encode JMS header keys that is that can be sent over the JMS transport.
+     * <p/>
+     * For example: Sending dots is the key is not allowed. Especially the Bean component
has
+     * this problem if you want to provide the method name to invoke on the bean.
+     * <p/>
+     * <b>Note</b>: Currently this encoder is simple as it only supports encoding
dots to underscores.
+     *
+     * @param headerName  the header name
+     * @return  the key to use instead for storing properties and to be for lookup of the
same property
+     */
+    public static String encodeToSafeJmsHeaderName(String headerName) {
+        return headerName.replace(".", "_");
+    }
+
+    /**
+     * Decode operation for the {@link #encodeToSafeJmsHeaderName(String)}.
+     *
+     * @param headerName  the header name
+     * @return  the original key
+     */
+    public static String decodeFromSafeJmsHeaderName(String headerName) {
+        return headerName.replace("_", ".");
     }
 
     /**
@@ -276,12 +304,12 @@
      * an input message onto an outgoing message
      */
     protected void populateIgnoreJmsHeaders(Set<String> set) {
-        // ignore provider specified JMS extension headers
-        // see page 39 of JMS 1.1 specification
-        //
+        // ignore provider specified JMS extension headers see page 39 of JMS 1.1 specification
+
         // added "JMSXRecvTimestamp" as a workaround for an Oracle bug/typo in AqjmsMessage
         String[] ignore = {"JMSXUserID", "JMSXAppID", "JMSXDeliveryCount", "JMSXProducerTXID",
                            "JMSXConsumerTXID", "JMSXRcvTimestamp", "JMSXRecvTimestamp", "JMSXState"};
         set.addAll(Arrays.asList(ignore));
     }
+
 }

Modified: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java?rev=673462&r1=673461&r2=673462&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
(original)
+++ activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
Wed Jul  2 11:43:58 2008
@@ -185,7 +185,11 @@
                 String name = names.nextElement().toString();
                 try {
                     Object value = jmsMessage.getObjectProperty(name);
-                    map.put(name, value);
+
+                    // must decode back from safe JMS header name to original header name
+                    // when storing on this Camel JmsMessage object.
+                    String key = JmsBinding.decodeFromSafeJmsHeaderName(name);
+                    map.put(key, value);
                 } catch (JMSException e) {
                     throw new MessagePropertyAccessException(name, e);
                 }
@@ -219,4 +223,5 @@
     private String getSanitizedString(Object value) {
         return value != null ? value.toString().replaceAll("[^a-zA-Z0-9\\.\\_\\-]", "_")
: "";
     }
+
 }

Added: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java?rev=673462&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
(added)
+++ activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
Wed Jul  2 11:43:58 2008
@@ -0,0 +1,134 @@
+/**
+ * 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.component.jms.issues;
+
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.Body;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.bean.BeanProcessor;
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentClientAcknowledge;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * Unit test for sending the bean method name as a key over the JMS wire, that we now support
this.
+ */
+public class JmsBeanMethodHeaderTest extends ContextTestSupport {
+
+    public void testPlainHeader() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("foo", "yes");
+
+        template.sendBodyAndHeader("direct:in", "Hello World", "foo", "yes");
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testUnderscoreHeader() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("foo_bar", "yes");
+
+        template.sendBodyAndHeader("direct:in", "Hello World", "foo_bar", "yes");
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testUsingBeanNoJMS() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:approve");
+        mock.expectedBodiesReceived("Yes");
+
+        template.sendBodyAndHeader("direct:approve", "James",
+            BeanProcessor.METHOD_NAME, "approveLoan");
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testUsingBeanAndJMS() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:approve");
+        mock.expectedBodiesReceived("Yes");
+
+        template.sendBodyAndHeader("activemq:approve", "James",
+            BeanProcessor.METHOD_NAME, "approveLoan");
+
+        mock.assertIsSatisfied();
+    }
+
+    public void testUsingJMStoJMStoBean() throws Exception {
+        // the big one from jms to jms to test that we do not lost the bean method name
+        MockEndpoint mock = getMockEndpoint("mock:approve");
+        mock.expectedBodiesReceived("No");
+
+        template.sendBodyAndHeader("activemq:queue", "James",
+            BeanProcessor.METHOD_NAME, "approveSuperLoan");
+
+        mock.assertIsSatisfied();
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+
+        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
+            "vm://localhost?broker.persistent=false");
+        camelContext.addComponent("activemq", jmsComponentClientAcknowledge(connectionFactory));
+
+        return camelContext;
+    }
+
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry reg = super.createRegistry();
+        reg.bind("approveService", new ApproveService());
+        return reg;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:in").to("activemq:test.a");
+                from("activemq:test.a").to("mock:result");
+
+                from("activemq:queue").to("activemq:approve");
+
+                from("activemq:approve").to("direct:approve");
+
+                from("direct:approve").to("bean:approveService").to("mock:approve");
+            }
+        };
+    }
+
+    public static class ApproveService {
+
+        public void doSomeStuff(String input) {
+            // just to confuse Camel with more public methods to choose among
+        }
+
+        public String approveLoan(@Body String body) {
+            return "Yes";
+        }
+
+        public String approveSuperLoan(@Body String body) {
+            return "No";
+        }
+
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsBeanMethodHeaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java?rev=673462&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
(added)
+++ activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
Wed Jul  2 11:43:58 2008
@@ -0,0 +1,71 @@
+/**
+ * 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.component.jms.issues;
+
+import java.io.File;
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentClientAcknowledge;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test for sending the filename for file producer over the JMS wire.
+ */
+public class JmsFilenameHeaderTest extends ContextTestSupport {
+
+    public void testFileNameOverJMS() throws Exception {
+        String filename = "jmsfilenameheadertest.txt";
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBodyAndHeader("direct:in", "Hello World",
+            FileComponent.HEADER_FILE_NAME, filename);
+
+        mock.assertIsSatisfied();
+
+        File file = new File("target/" + filename);
+        file = file.getAbsoluteFile();
+        assertTrue("The file should have been produced with filename: " + filename, file.exists());
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+
+        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
+            "vm://localhost?broker.persistent=false");
+        camelContext.addComponent("activemq", jmsComponentClientAcknowledge(connectionFactory));
+
+        return camelContext;
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:in").to("activemq:test.a");
+                from("activemq:test.a").to("direct:save");
+
+                from("direct:save").to("file://target?append=false", "mock:result");
+            }
+        };
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsFilenameHeaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message