You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ru...@apache.org on 2009/10/12 07:09:54 UTC

svn commit: r824227 - in /webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms: JMSOutTransportInfo.java JMSUtils.java

Author: ruwan
Date: Mon Oct 12 05:09:54 2009
New Revision: 824227

URL: http://svn.apache.org/viewvc?rev=824227&view=rev
Log:
Refactoring the getDestination of the JMS transport (half way through WSCOMMONS-468)

Modified:
    webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSOutTransportInfo.java
    webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java

Modified: webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSOutTransportInfo.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSOutTransportInfo.java?rev=824227&r1=824226&r2=824227&view=diff
==============================================================================
--- webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSOutTransportInfo.java (original)
+++ webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSOutTransportInfo.java Mon Oct 12 05:09:54 2009
@@ -20,23 +20,9 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.Destination;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.Queue;
-import javax.jms.QueueConnection;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.QueueSession;
-import javax.jms.Session;
-import javax.jms.Topic;
-import javax.jms.TopicConnection;
-import javax.jms.TopicConnectionFactory;
-import javax.jms.TopicSession;
+import javax.jms.*;
 import javax.naming.Context;
 import javax.naming.InitialContext;
-import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
 import java.util.Hashtable;
 
@@ -88,7 +74,7 @@
      *
      * @param jmsConnectionFactory the JMS connection factory
      * @param dest the destination
-     * @param contentTypeProperty
+     * @param contentTypeProperty the content type
      */
     JMSOutTransportInfo(JMSConnectionFactory jmsConnectionFactory, Destination dest,
             String contentTypeProperty) {
@@ -176,20 +162,11 @@
      */
     private Destination getDestination(Context context, String url) {
         String destinationName = JMSUtils.getDestination(url);
-        try {
-            return JMSUtils.lookup(context, Destination.class, destinationName);
-        } catch (NameNotFoundException e) {
-            try {
-                return JMSUtils.lookup(context, Destination.class,
-                    (JMSConstants.DESTINATION_TYPE_TOPIC.equals(destinationType) ?
-                        "dynamicTopics/" : "dynamicQueues/") + destinationName);
-            } catch (NamingException x) {
-                handleException("Cannot locate destination : " + destinationName + " using " + url);
-            }
-        } catch (NamingException e) {
-            handleException("Cannot locate destination : " + destinationName + " using " + url, e);
+        if (log.isDebugEnabled()) {
+            log.debug("Lookup the JMS destination " + destinationName + " of type "
+                    + destinationType + " extracted from the URL " + url);
         }
-        return null;
+        return JMSUtils.lookupDestination(context, destinationName, destinationType);
     }
 
     /**
@@ -201,21 +178,11 @@
      */
     private Destination getReplyDestination(Context context, String url) {
         String replyDestinationName = properties.get(JMSConstants.PARAM_REPLY_DESTINATION);
-        if(replyDestinationName == null) {
-            return null;
-        }
-
-        try {
-            return JMSUtils.lookup(context, Destination.class, replyDestinationName);
-        } catch (NameNotFoundException e) {
-            if (log.isDebugEnabled()) {
-                log.debug("Cannot locate destination : " + replyDestinationName + " using " + url);
-            }
-        } catch (NamingException e) {
-            handleException("Cannot locate destination : " + replyDestinationName + " using " + url, e);
+        if (log.isDebugEnabled()) {
+            log.debug("Lookup the JMS destination " + replyDestinationName + " of type "
+                    + replyDestinationType + " extracted from the URL " + url);
         }
-
-        return null;
+        return JMSUtils.lookupDestination(context, replyDestinationName, replyDestinationType);
     }
 
     /**
@@ -224,17 +191,12 @@
      * @return Destination for the JNDI name passed
      */
     public Destination getReplyDestination(String replyDest) {
-        try {
-            return JMSUtils.lookup(jmsConnectionFactory.getContext(), Destination.class,
-                    replyDest);
-        } catch (NameNotFoundException e) {
-            if (log.isDebugEnabled()) {
-                log.debug("Cannot locate reply destination : " + replyDest, e);
-            }
-        } catch (NamingException e) {
-            handleException("Cannot locate reply destination : " + replyDest, e);
+        if (log.isDebugEnabled()) {
+            log.debug("Lookup the JMS destination " + replyDest + " of type "
+                    + replyDestinationType);
         }
-        return null;
+        return JMSUtils.lookupDestination(
+                jmsConnectionFactory.getContext(), replyDest, replyDestinationType);
     }
 
 
@@ -360,26 +322,36 @@
             }
         }
 
-        if (connection == null && jmsConnectionFactory != null) {
-            connection = jmsConnectionFactory.getConnection();
+        if (connection == null) {
+            connection = jmsConnectionFactory != null ? jmsConnectionFactory.getConnection() : null;
         }
 
         Session session = null;
         MessageProducer producer = null;
 
-        if (destType == JMSConstants.QUEUE) {
-            session = ((QueueConnection) connection).
-                createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-            producer = ((QueueSession) session).createSender((Queue) destination);
-        } else {
-            session = ((TopicConnection) connection).
-                createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
-            producer = ((TopicSession) session).createPublisher((Topic) destination);
+        if (connection != null) {
+            if (destType == JMSConstants.QUEUE) {
+                session = ((QueueConnection) connection).
+                        createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+                producer = ((QueueSession) session).createSender((Queue) destination);
+            } else {
+                session = ((TopicConnection) connection).
+                        createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
+                producer = ((TopicSession) session).createPublisher((Topic) destination);
+            }
         }
 
-        return new JMSMessageSender(connection, session, producer,
-            destination, (jmsConnectionFactory == null ?
-            JMSConstants.CACHE_NONE : jmsConnectionFactory.getCacheLevel()), false,
-            destType == -1 ? null : destType == JMSConstants.QUEUE ? Boolean.TRUE : Boolean.FALSE);
+        return new JMSMessageSender(
+                connection,
+                session,
+                producer,
+                destination,
+                jmsConnectionFactory == null ?
+                        JMSConstants.CACHE_NONE : jmsConnectionFactory.getCacheLevel(),
+                false,
+                destType == -1 ?
+                        null : destType == JMSConstants.QUEUE ? Boolean.TRUE : Boolean.FALSE
+        );
+
     }
 }

Modified: webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java?rev=824227&r1=824226&r2=824227&view=diff
==============================================================================
--- webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java (original)
+++ webservices/commons/trunk/modules/transport/modules/jms/src/main/java/org/apache/axis2/transport/jms/JMSUtils.java Mon Oct 12 05:09:54 2009
@@ -40,6 +40,7 @@
 import javax.naming.Context;
 import javax.naming.NamingException;
 import javax.naming.Reference;
+import javax.naming.NameNotFoundException;
 
 import java.lang.reflect.Method;
 import java.util.*;
@@ -687,4 +688,36 @@
             return "Generic";
         }
     }
+
+    /**
+     * Return the JMS destination with the given destination name looked up from the context
+     * 
+     * @param context the Context to lookup
+     * @param destinationName name of the destination to be looked up
+     * @param destinationType type of the destination to be looked up
+     * @return the JMS destination, or null if it does not exist
+     */
+    public static Destination lookupDestination(Context context, String destinationName,
+                                                String destinationType) {
+
+        if (destinationName == null) {
+            return null;
+        }
+
+        try {
+            return JMSUtils.lookup(context, Destination.class, destinationName);
+        } catch (NameNotFoundException e) {
+            try {
+                return JMSUtils.lookup(context, Destination.class,
+                    (JMSConstants.DESTINATION_TYPE_TOPIC.equals(destinationType) ?
+                        "dynamicTopics/" : "dynamicQueues/") + destinationName);
+            } catch (NamingException x) {
+                handleException("Cannot locate destination : " + destinationName);
+            }
+        } catch (NamingException e) {
+            handleException("Cannot locate destination : " + destinationName, e);
+        }
+
+        return null;
+    }
 }