qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject qpid-jms git commit: QPIDJMS-147 Construct a default remote address from system properties or using default values of 'localhost' and port 5672.
Date Fri, 22 Jan 2016 20:41:22 GMT
Repository: qpid-jms
Updated Branches:
  refs/heads/master 2f3c19886 -> 4f72dde55


QPIDJMS-147 Construct a default remote address from system properties or using
default values of 'localhost' and port 5672.


Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/4f72dde5
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/4f72dde5
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/4f72dde5

Branch: refs/heads/master
Commit: 4f72dde55d693f5d304723163bf08601ed90d94d
Parents: 2f3c198
Author: Timothy Bish <tabish121@gmail.com>
Authored: Fri Jan 22 15:39:45 2016 -0500
Committer: Timothy Bish <tabish121@gmail.com>
Committed: Fri Jan 22 15:40:29 2016 -0500

----------------------------------------------------------------------
 .../apache/qpid/jms/JmsConnectionFactory.java   | 80 +++++++++++++++++++-
 .../qpid/jms/jndi/JmsInitialContextFactory.java |  4 +-
 .../qpid/jms/JmsConnectionFactoryTest.java      | 13 +---
 .../jms/jndi/JmsInitialContextFactoryTest.java  |  9 ++-
 4 files changed, 84 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/4f72dde5/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
index 92614f0..91f1827 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/JmsConnectionFactory.java
@@ -18,12 +18,13 @@ package org.apache.qpid.jms;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Map;
 
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.ExceptionListener;
-import javax.jms.IllegalStateException;
 import javax.jms.JMSException;
 import javax.jms.QueueConnection;
 import javax.jms.QueueConnectionFactory;
@@ -49,10 +50,15 @@ import org.slf4j.LoggerFactory;
 public class JmsConnectionFactory extends JNDIStorable implements ConnectionFactory, QueueConnectionFactory,
TopicConnectionFactory {
 
     private static final Logger LOG = LoggerFactory.getLogger(JmsConnectionFactory.class);
+
     private static final String CLIENT_ID_PROP = "clientID";
+    private static final String DEFAULT_REMOTE_HOST = "localhost";
+    private static final String DEFAULT_REMOTE_PORT = "5672";
 
     public static final String REMOTE_URI_PROP = "remoteURI";
 
+    private static String DEFAULT_REMOTE_URI;
+
     private URI remoteURI;
     private String username;
     private String password;
@@ -211,8 +217,8 @@ public class JmsConnectionFactory extends JNDIStorable implements ConnectionFact
     }
 
     protected Provider createProvider(URI remoteURI) throws Exception {
-        if(remoteURI == null) {
-            throw new IllegalStateException("No remoteURI has been provided");
+        if (remoteURI == null) {
+            remoteURI = new URI(getDefaultRemoteAddress());
         }
 
         Provider result = null;
@@ -263,7 +269,7 @@ public class JmsConnectionFactory extends JNDIStorable implements ConnectionFact
      * @return the remoteURI
      */
     public String getRemoteURI() {
-        return this.remoteURI != null ? this.remoteURI.toString() : "";
+        return this.remoteURI != null ? this.remoteURI.toString() : getDefaultRemoteAddress();
     }
 
     /**
@@ -763,4 +769,70 @@ public class JmsConnectionFactory extends JNDIStorable implements ConnectionFact
     public void setReceiveNoWaitLocalOnly(boolean receiveNoWaitLocalOnly) {
         this.receiveNoWaitLocalOnly = receiveNoWaitLocalOnly;
     }
+
+    //----- Static Methods ---------------------------------------------------//
+
+    /**
+     * @return the default remote address to connect to in the event that none was set.
+     */
+    public static String getDefaultRemoteAddress() {
+
+        if (DEFAULT_REMOTE_URI != null) {
+            return DEFAULT_REMOTE_URI;
+        }
+
+        synchronized (JmsConnectionFactory.class) {
+
+            if (DEFAULT_REMOTE_URI != null) {
+                return DEFAULT_REMOTE_URI;
+            }
+
+            String host = null;
+            String port = null;
+            String remoteAddress = null;
+
+            try {
+                host = AccessController.doPrivileged(new PrivilegedAction<String>()
{
+                    @Override
+                    public String run() {
+                        String result = System.getProperty("org.apache.qpid.jms.REMOTE_HOST",
DEFAULT_REMOTE_HOST);
+                        result = (result == null || result.isEmpty()) ? DEFAULT_REMOTE_HOST
: result;
+                        return result;
+                    }
+                });
+                port = AccessController.doPrivileged(new PrivilegedAction<String>()
{
+                    @Override
+                    public String run() {
+                        String result = System.getProperty("org.apache.qpid.jms.REMOTE_PORT",
DEFAULT_REMOTE_PORT);
+                        result = (result == null || result.isEmpty()) ? DEFAULT_REMOTE_PORT
: result;
+                        return result;
+                    }
+                });
+            } catch (Throwable e) {
+                LOG.debug("Failed to look up System properties for remote host and port",
e);
+            }
+
+            host = (host == null || host.isEmpty()) ? DEFAULT_REMOTE_HOST : host;
+            port = (port == null || port.isEmpty()) ? DEFAULT_REMOTE_PORT : port;
+
+            final String defaultURL = "amqp://" + DEFAULT_REMOTE_HOST + ":" + DEFAULT_REMOTE_PORT;
+
+            try {
+                remoteAddress = AccessController.doPrivileged(new PrivilegedAction<String>()
{
+                    @Override
+                    public String run() {
+                        String result = System.getProperty("org.apache.qpid.jms.REMOTE_URI",
defaultURL);
+                        result = (result == null || result.isEmpty()) ? defaultURL : result;
+                        return result;
+                    }
+                });
+            } catch (Throwable e) {
+                LOG.debug("Failed to look up System property for remote URI", e);
+            }
+
+            DEFAULT_REMOTE_URI = (remoteAddress == null || remoteAddress.isEmpty()) ? defaultURL
: remoteAddress;
+        }
+
+        return DEFAULT_REMOTE_URI;
+    }
 }

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/4f72dde5/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java
b/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java
index 64972f7..f6eba43 100644
--- a/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java
+++ b/qpid-jms-client/src/main/java/org/apache/qpid/jms/jndi/JmsInitialContextFactory.java
@@ -58,8 +58,6 @@ public class JmsInitialContextFactory implements InitialContextFactory {
     static final String[] DEFAULT_CONNECTION_FACTORY_NAMES = {
         "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory" };
 
-    static final String DEFAULT_REMOTE_URI = "amqp://localhost:5672";
-
     static final String CONNECTION_FACTORY_KEY_PREFIX = "connectionfactory.";
     static final String QUEUE_KEY_PREFIX = "queue.";
     static final String TOPIC_KEY_PREFIX = "topic.";
@@ -209,7 +207,7 @@ public class JmsInitialContextFactory implements InitialContextFactory
{
 
     protected Map<String, String> getConnectionFactoryDefaults(Map<Object, Object>
environment) {
         Map<String, String> map = new LinkedHashMap<String, String>();
-        map.put(JmsConnectionFactory.REMOTE_URI_PROP, DEFAULT_REMOTE_URI);
+        map.put(JmsConnectionFactory.REMOTE_URI_PROP, JmsConnectionFactory.getDefaultRemoteAddress());
 
         for (Iterator<Entry<Object, Object>> iter = environment.entrySet().iterator();
iter.hasNext();) {
             Map.Entry<Object, Object> entry = iter.next();

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/4f72dde5/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionFactoryTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionFactoryTest.java
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionFactoryTest.java
index d9ae1c7..679fff4 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionFactoryTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionFactoryTest.java
@@ -35,7 +35,6 @@ import java.util.concurrent.TimeUnit;
 
 import javax.jms.Connection;
 import javax.jms.ExceptionListener;
-import javax.jms.IllegalStateException;
 import javax.jms.JMSException;
 
 import org.apache.qpid.jms.test.QpidJmsTestCase;
@@ -58,6 +57,7 @@ public class JmsConnectionFactoryTest extends QpidJmsTestCase {
         JmsConnectionFactory factory = new JmsConnectionFactory();
         assertNull(factory.getUsername());
         assertNull(factory.getPassword());
+        assertNotNull(factory.getRemoteURI());
     }
 
     @Test
@@ -169,17 +169,6 @@ public class JmsConnectionFactoryTest extends QpidJmsTestCase {
     }
 
     @Test
-    public void testCreateConnectionWithoutUriThrowsJMSISE() throws Exception {
-        JmsConnectionFactory cf = new JmsConnectionFactory();
-        try {
-            cf.createConnection();
-            fail("Should have thrown exception");
-        } catch (IllegalStateException jmsise){
-            // expected
-        }
-    }
-
-    @Test
     public void testSetProperties() throws Exception {
         String clientID = getTestName();
         String queuePrefix = "q:";

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/4f72dde5/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java
index 188c6ca..c1a0652 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/jndi/JmsInitialContextFactoryTest.java
@@ -16,7 +16,10 @@
  */
 package org.apache.qpid.jms.jndi;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -61,7 +64,7 @@ public class JmsInitialContextFactoryTest extends QpidJmsTestCase {
 
             assertNotNull("No object returned", o);
             assertEquals("Unexpected class type for returned object", JmsConnectionFactory.class,
o.getClass());
-            assertEquals("Unexpected URI for returned factory", JmsInitialContextFactory.DEFAULT_REMOTE_URI,
((JmsConnectionFactory) o).getRemoteURI());
+            assertEquals("Unexpected URI for returned factory", JmsConnectionFactory.getDefaultRemoteAddress(),
((JmsConnectionFactory) o).getRemoteURI());
         }
     }
 
@@ -193,7 +196,7 @@ public class JmsInitialContextFactoryTest extends QpidJmsTestCase {
 
         assertNotNull("No object returned", o);
         assertEquals("Unexpected class type for returned object", JmsConnectionFactory.class,
o.getClass());
-        assertEquals("Unexpected URI for returned factory", JmsInitialContextFactory.DEFAULT_REMOTE_URI,
((JmsConnectionFactory) o).getRemoteURI());
+        assertEquals("Unexpected URI for returned factory", JmsConnectionFactory.getDefaultRemoteAddress(),
((JmsConnectionFactory) o).getRemoteURI());
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message