activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r521256 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
Date Thu, 22 Mar 2007 13:27:07 GMT
Author: jstrachan
Date: Thu Mar 22 06:27:06 2007
New Revision: 521256

URL: http://svn.apache.org/viewvc?view=rev&rev=521256
Log:
minor refactor to add similar lazy-create callback for creating headers on inbound messages;
so its nice and easy to write efficient Message implementations

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
    activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java

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=521256&r1=521255&r2=521256
==============================================================================
--- 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 22 06:27:06 2007
@@ -38,12 +38,15 @@
     }
 
     public void setHeader(String name, Object value) {
-        getHeaders().put(name, value);
+        if (headers == null) {
+            headers = createHeaders();
+        }
+        headers.put(name, value);
     }
 
     public Map<String, Object> getHeaders() {
         if (headers == null) {
-            headers = new HashMap<String, Object>();
+            headers = createHeaders();
         }
         return headers;
     }
@@ -55,4 +58,15 @@
     public DefaultMessage newInstance() {
         return new DefaultMessage();
     }
+
+    /**
+     * A factory method to lazily create the headers to make it easy to create efficient
Message implementations
+     * which only construct and populate the Map on demand
+     *
+     * @return return a newly constructed Map possibly containing headers from the underlying
inbound transport
+     */
+    protected Map<String, Object> createHeaders() {
+        return new HashMap<String, Object>();
+    }
+
 }

Modified: activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java?view=diff&rev=521256&r1=521255&r2=521256
==============================================================================
--- activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
(original)
+++ activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsMessage.java
Thu Mar 22 06:27:06 2007
@@ -17,8 +17,7 @@
  */
 package org.apache.camel.component.jms;
 
-import org.apache.camel.InvalidHeaderTypeException;
-import org.apache.camel.impl.MessageSupport;
+import org.apache.camel.impl.DefaultMessage;
 
 import javax.jms.JMSException;
 import javax.jms.Message;
@@ -31,9 +30,8 @@
  *
  * @version $Revision:520964 $
  */
-public class JmsMessage extends MessageSupport {
+public class JmsMessage extends DefaultMessage {
     private Message jmsMessage;
-    private Map<String, Object> lazyHeaders;
 
     public JmsMessage() {
     }
@@ -56,43 +54,38 @@
     }
 
     public Object getHeader(String name) {
+        Object answer = null;
         if (jmsMessage != null) {
             try {
-                Object value = jmsMessage.getObjectProperty(name);
-                try {
-                    return value;
-                }
-                catch (ClassCastException e) {
-                    throw new InvalidHeaderTypeException(e.getMessage(), value);
-                }
+                answer = jmsMessage.getObjectProperty(name);
             }
             catch (JMSException e) {
                 throw new MessagePropertyAcessException(name, e);
             }
         }
-        return null;
+        if (answer == null) {
+            answer = super.getHeader(name);
+        }
+        return answer;
     }
 
-    public void setHeader(String name, Object value) {
+    @Override
+    public JmsMessage newInstance() {
+        return new JmsMessage();
+    }
+
+    @Override
+    protected Object createBody() {
         if (jmsMessage != null) {
-            try {
-                jmsMessage.setObjectProperty(name, value);
-            }
-            catch (JMSException e) {
-                throw new MessagePropertyAcessException(name, e);
-            }
-        }
-        else {
-            if (lazyHeaders == null) {
-                lazyHeaders = new HashMap<String, Object>();
-            }
-            lazyHeaders.put(name, value);
+            return getExchange().getBinding().extractBodyFromJms(getExchange(), jmsMessage);
         }
+        return null;
     }
 
-    public Map<String, Object> getHeaders() {
+    @Override
+    protected Map<String, Object> createHeaders() {
+        HashMap<String, Object> answer = new HashMap<String, Object>();
         if (jmsMessage != null) {
-            Map<String, Object> answer = new HashMap<String, Object>();
             Enumeration names;
             try {
                 names = jmsMessage.getPropertyNames();
@@ -110,24 +103,8 @@
                     throw new MessagePropertyAcessException(name, e);
                 }
             }
-            return answer;
-        }
-        else {
-            return lazyHeaders;
         }
-    }
-
-    @Override
-    public JmsMessage newInstance() {
-        return new JmsMessage();
-    }
-
-    @Override
-    protected Object createBody() {
-        if (jmsMessage != null) {
-            return  getExchange().getBinding().extractBodyFromJms(getExchange(), jmsMessage);
-        }
-        return null;
+        return answer;
     }
 }
 



Mime
View raw message