You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/03/29 18:55:47 UTC

svn commit: r523768 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ test/java/org/apache/camel/ test/resources/

Author: jstrachan
Date: Thu Mar 29 09:55:42 2007
New Revision: 523768

URL: http://svn.apache.org/viewvc?view=rev&rev=523768
Log:
added helper methods and test cases to show the conversion of properties, headers and message bodies

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
    activemq/camel/trunk/camel-core/src/test/resources/log4j.properties

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?view=diff&rev=523768&r1=523767&r2=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Thu Mar 29 09:55:42 2007
@@ -46,6 +46,17 @@
      */
     Object getProperty(String name);
 
+
+    /**
+     * Returns a property associated with this exchange by name and specifying the type required
+     *
+     * @param name the name of the property
+     * @param type the type of the property
+     * @return the value of the given header or null if there is no property for the given name or
+     * null if it cannot be converted to the given type
+     */
+    <T> T getProperty(String name, Class<T> type);
+
     /**
      * Sets a property on the exchange
      *

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java?view=diff&rev=523768&r1=523767&r2=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java Thu Mar 29 09:55:42 2007
@@ -36,6 +36,16 @@
     Object getHeader(String name);
 
     /**
+     * Returns a header associated with this message by name and specifying the type required
+     *
+     * @param name the name of the header
+     * @param type the type of the header
+     * @return the value of the given header or null if there is no property for the given name or it cannot be
+     * converted to the given type
+     */
+    <T> T getHeader(String name, Class<T> type);
+
+    /**
      * Sets a header on the message
      *
      * @param name  of the header

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?view=diff&rev=523768&r1=523767&r2=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Thu Mar 29 09:55:42 2007
@@ -75,6 +75,11 @@
         return null;
     }
 
+    public <T> T getProperty(String name, Class<T> type) {
+        Object value = getProperty(name);
+        return getContext().getTypeConverter().convertTo(type, value);
+    }
+
     public void setProperty(String name, Object value) {
         getProperties().put(name, value);
     }
@@ -93,6 +98,7 @@
     public Message getIn() {
         if (in == null) {
             in = createInMessage();
+            configureMessage(in);
         }
         return in;
     }
@@ -105,6 +111,7 @@
     public Message getOut() {
         if (out == null) {
             out = createOutMessage();
+            configureMessage(out);
         }
         return out;
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?view=diff&rev=523768&r1=523767&r2=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java Thu Mar 29 09:55:42 2007
@@ -30,6 +30,11 @@
 public class DefaultMessage extends MessageSupport {
     private Map<String, Object> headers;
 
+    @Override
+    public String toString() {
+        return "Message: " + getBody();
+    }
+
     public Object getHeader(String name) {
         if (headers != null) {
             return headers.get(name);
@@ -37,9 +42,9 @@
         return null;
     }
 
-    @Override
-    public String toString() {
-        return "Message: " + getBody();
+    public <T> T getHeader(String name, Class<T> type) {
+        Object value = getHeader(name);
+        return getExchange().getContext().getTypeConverter().convertTo(type, value);
     }
 
     public void setHeader(String name, Object value) {

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java?view=auto&rev=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java Thu Mar 29 09:55:42 2007
@@ -0,0 +1,62 @@
+/**
+ *
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * @version $Revision$
+ */
+public class BodyAndHeaderConvertTest extends TestCase {
+    protected Exchange exchange;
+
+    public void testConversionOfBody() throws Exception {
+        Document document = exchange.getIn().getBody(Document.class);
+        assertNotNull(document);
+        Element element = document.getDocumentElement();
+        assertEquals("Root element name", "hello", element.getLocalName());
+    }
+
+    public void testConversionOfExchangeProperties() throws Exception {
+        String text = exchange.getProperty("foo", String.class);
+        assertEquals("foo property", "1234", text);
+
+        // TODO better conversion example when the property editor support is added
+    }
+
+    public void testConversionOfMessageHeaders() throws Exception {
+        String text = exchange.getIn().getHeader("bar", String.class);
+        assertEquals("bar header", "567", text);
+
+        // TODO better conversion example when the property editor support is added
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        exchange = new DefaultExchange(new DefaultCamelContext());
+        exchange.setProperty("foo", 1234);
+        Message message = exchange.getIn();
+        message.setBody("<hello>world!</hello>");
+        message.setHeader("bar", 567);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: activemq/camel/trunk/camel-core/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/resources/log4j.properties?view=diff&rev=523768&r1=523767&r2=523768
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/resources/log4j.properties (original)
+++ activemq/camel/trunk/camel-core/src/test/resources/log4j.properties Thu Mar 29 09:55:42 2007
@@ -22,7 +22,7 @@
 
 #log4j.logger.org.apache.activemq=DEBUG
 
-log4j.logger.org.apache.camel=DEBUG
+#log4j.logger.org.apache.camel=DEBUG
 log4j.logger.org.apache.camel.impl.converter=INFO
 
 # CONSOLE appender not used by default