You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/06/07 10:43:15 UTC

svn commit: r545108 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/util/ components/camel-jms/src/main/java/org/apache/camel/component/jms/ components/camel-jms/src/test/java/org/apache/camel/component/jms/

Author: jstrachan
Date: Thu Jun  7 01:43:14 2007
New Revision: 545108

URL: http://svn.apache.org/viewvc?view=rev&rev=545108
Log:
added bug fix which limited URI query strings from only being recognized if the notation scheme://whatnot?foo was used as opposed to the scheme:whatnot?foo notation. Also improved the test case for configuring the JMS endpoint via its URI

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
    activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsEndpointConfigurationTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java?view=diff&rev=545108&r1=545107&r2=545108
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java Thu Jun  7 01:43:14 2007
@@ -32,7 +32,6 @@
  * @version $Revision$
  */
 public class URISupport {
-    
     public static class CompositeData {
         String scheme;
         String path;
@@ -40,54 +39,61 @@
         Map parameters;
         String fragment;
         public String host;
-        
+
         public URI[] getComponents() {
             return components;
         }
+
         public String getFragment() {
             return fragment;
         }
+
         public Map getParameters() {
             return parameters;
         }
+
         public String getScheme() {
             return scheme;
         }
+
         public String getPath() {
             return path;
         }
+
         public String getHost() {
             return host;
         }
-        
+
         public URI toURI() throws URISyntaxException {
             StringBuffer sb = new StringBuffer();
-            if( scheme!=null ) {
+            if (scheme != null) {
                 sb.append(scheme);
                 sb.append(':');
             }
-            
-            if( host!=null && host.length()!=0 ) {
+
+            if (host != null && host.length() != 0) {
                 sb.append(host);
-            } else {
+            }
+            else {
                 sb.append('(');
                 for (int i = 0; i < components.length; i++) {
-                    if( i!=0 )
+                    if (i != 0) {
                         sb.append(',');
+                    }
                     sb.append(components[i].toString());
                 }
                 sb.append(')');
             }
-            
-            if( path !=null ) {
+
+            if (path != null) {
                 sb.append('/');
                 sb.append(path);
             }
-            if(!parameters.isEmpty()) {
+            if (!parameters.isEmpty()) {
                 sb.append("?");
                 sb.append(createQueryString(parameters));
             }
-            if( fragment!=null ) {
+            if (fragment != null) {
                 sb.append("#");
                 sb.append(fragment);
             }
@@ -95,30 +101,46 @@
         }
     }
 
-    public static Map parseQuery(String uri) throws URISyntaxException{
-        try{
-            Map rc=new HashMap();
-            if(uri!=null){
-                String[] parameters=uri.split("&");
-                for(int i=0;i<parameters.length;i++){
-                    int p=parameters[i].indexOf("=");
-                    if(p>=0){
-                        String name=URLDecoder.decode(parameters[i].substring(0,p),"UTF-8");
-                        String value=URLDecoder.decode(parameters[i].substring(p+1),"UTF-8");
-                        rc.put(name,value);
-                    }else{
-                        rc.put(parameters[i],null);
+    public static Map parseQuery(String uri) throws URISyntaxException {
+        try {
+            Map rc = new HashMap();
+            if (uri != null) {
+                String[] parameters = uri.split("&");
+                for (int i = 0; i < parameters.length; i++) {
+                    int p = parameters[i].indexOf("=");
+                    if (p >= 0) {
+                        String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
+                        String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
+                        rc.put(name, value);
+                    }
+                    else {
+                        rc.put(parameters[i], null);
                     }
                 }
             }
             return rc;
-        }catch(UnsupportedEncodingException e){
-            throw (URISyntaxException) new URISyntaxException(e.toString(),"Invalid encoding").initCause(e);
+        }
+        catch (UnsupportedEncodingException e) {
+            throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
         }
     }
-    
+
     public static Map parseParamters(URI uri) throws URISyntaxException {
-        return uri.getQuery()==null ? Collections.EMPTY_MAP : parseQuery(stripPrefix(uri.getQuery(), "?"));
+        String query = uri.getQuery();
+        if (query == null) {
+            String schemeSpecificPart = uri.getSchemeSpecificPart();
+            int idx = schemeSpecificPart.lastIndexOf('?');
+            if (idx < 0) {
+                return Collections.EMPTY_MAP;
+            }
+            else {
+                query = schemeSpecificPart.substring(idx + 1);
+            }
+        }
+        else {
+            query = stripPrefix(query, "?");
+        }
+        return parseQuery(query);
     }
 
     /**
@@ -134,15 +156,15 @@
     public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException {
         return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), query, uri.getFragment());
     }
-    
+
     public static CompositeData parseComposite(URI uri) throws URISyntaxException {
-        
+
         CompositeData rc = new CompositeData();
         rc.scheme = uri.getScheme();
         String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim();
 
         parseComposite(uri, rc, ssp);
-        
+
         rc.fragment = uri.getFragment();
         return rc;
     }
@@ -156,44 +178,46 @@
     private static void parseComposite(URI uri, CompositeData rc, String ssp) throws URISyntaxException {
         String componentString;
         String params;
-        
-        if(!checkParenthesis(ssp)){
+
+        if (!checkParenthesis(ssp)) {
             throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis");
         }
-        
+
         int p;
         int intialParen = ssp.indexOf("(");
-        if( intialParen==0 ) {
+        if (intialParen == 0) {
             rc.host = ssp.substring(0, intialParen);
             p = rc.host.indexOf("/");
-            if( p >= 0 ) {
+            if (p >= 0) {
                 rc.path = rc.host.substring(p);
-                rc.host = rc.host.substring(0,p);
+                rc.host = rc.host.substring(0, p);
             }
             p = ssp.lastIndexOf(")");
-            componentString = ssp.substring(intialParen+1,p);
-            params = ssp.substring(p+1).trim();
-            
-        } else {
+            componentString = ssp.substring(intialParen + 1, p);
+            params = ssp.substring(p + 1).trim();
+        }
+        else {
             componentString = ssp;
-            params="";
+            params = "";
         }
 
         String components[] = splitComponents(componentString);
-        rc.components=new URI[components.length];
+        rc.components = new URI[components.length];
         for (int i = 0; i < components.length; i++) {
             rc.components[i] = new URI(components[i].trim());
         }
-        
+
         p = params.indexOf("?");
-        if( p >= 0 ) {
-            if( p > 0) {
+        if (p >= 0) {
+            if (p > 0) {
                 rc.path = stripPrefix(params.substring(0, p), "/");
             }
-            rc.parameters = parseQuery(params.substring(p+1));
-        } else {
-            if( params.length() > 0 )
+            rc.parameters = parseQuery(params.substring(p + 1));
+        }
+        else {
+            if (params.length() > 0) {
                 rc.path = stripPrefix(params, "/");
+            }
             rc.parameters = Collections.EMPTY_MAP;
         }
     }
@@ -204,100 +228,108 @@
      */
     private static String[] splitComponents(String str) {
         ArrayList l = new ArrayList();
-        
-        int last=0;
+
+        int last = 0;
         int depth = 0;
         char chars[] = str.toCharArray();
-        for( int i=0; i < chars.length; i ++ ) {
-            switch( chars[i] ) {
-            case '(':
-                depth++;
-                break;
-            case ')':
-                depth--;
-                break;
-            case ',':
-                if( depth == 0 ) {
-                    String s = str.substring(last, i);
-                    l.add(s);
-                    last=i+1;
-                }
+        for (int i = 0; i < chars.length; i++) {
+            switch (chars[i]) {
+                case '(':
+                    depth++;
+                    break;
+                case ')':
+                    depth--;
+                    break;
+                case ',':
+                    if (depth == 0) {
+                        String s = str.substring(last, i);
+                        l.add(s);
+                        last = i + 1;
+                    }
             }
         }
-        
+
         String s = str.substring(last);
-        if( s.length() !=0 )
-            l.add(s);        
-        
+        if (s.length() != 0) {
+            l.add(s);
+        }
+
         String rc[] = new String[l.size()];
         l.toArray(rc);
         return rc;
     }
-    
+
     public static String stripPrefix(String value, String prefix) {
-        if( value.startsWith(prefix) )
+        if (value.startsWith(prefix)) {
             return value.substring(prefix.length());
+        }
         return value;
     }
-    
+
     public static URI stripScheme(URI uri) throws URISyntaxException {
-        return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//")); 
+        return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//"));
     }
 
     public static String createQueryString(Map options) throws URISyntaxException {
         try {
-            if(options.size()>0) {
+            if (options.size() > 0) {
                 StringBuffer rc = new StringBuffer();
-                boolean first=true;
+                boolean first = true;
                 for (Iterator iter = options.keySet().iterator(); iter.hasNext();) {
-                    if( first )
-                        first=false;
-                    else
+                    if (first) {
+                        first = false;
+                    }
+                    else {
                         rc.append("&");
-                                    
+                    }
+
                     String key = (String) iter.next();
-                    String value = (String)options.get(key);
+                    String value = (String) options.get(key);
                     rc.append(URLEncoder.encode(key, "UTF-8"));
                     rc.append("=");
                     rc.append(URLEncoder.encode(value, "UTF-8"));
                 }
                 return rc.toString();
-            } else {
+            }
+            else {
                 return "";
             }
-        } catch (UnsupportedEncodingException e) {
-            throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
+        }
+        catch (UnsupportedEncodingException e) {
+            throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
         }
     }
 
     /**
      * Creates a URI from the original URI and the remaining paramaters
-     * @throws URISyntaxException 
+     *
+     * @throws URISyntaxException
      */
     public static URI createRemainingURI(URI originalURI, Map params) throws URISyntaxException {
         String s = createQueryString(params);
-        if( s.length()==0 )
+        if (s.length() == 0) {
             s = null;
+        }
         return createURIWithQuery(originalURI, s);
     }
 
     static public URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException {
         return new URI(scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr.getPath(), bindAddr.getQuery(), bindAddr.getFragment());
     }
-    
-    public static boolean checkParenthesis(String str){
-        boolean result=true;
-        if(str!=null){
-            int open=0;
-            int closed=0;
-            
-            int i=0;
-            while((i=str.indexOf('(',i)) >=0 ){
+
+    public static boolean checkParenthesis(String str) {
+        boolean result = true;
+        if (str != null) {
+            int open = 0;
+            int closed = 0;
+
+            int i = 0;
+            while ((i = str.indexOf('(', i)) >= 0) {
                 i++;
                 open++;
             }
-            i=0;
-            while((i=str.indexOf(')',i)) >=0 ){
+            i = 0;
+            while ((i = str.indexOf(')', i)) >= 0) {
                 i++;
                 closed++;
             }
@@ -305,11 +337,10 @@
         }
         return result;
     }
-    
-    public int indexOfParenthesisMatch(String str){
+
+    public int indexOfParenthesisMatch(String str) {
         int result = -1;
-        
+
         return result;
     }
-
 }

Modified: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java?view=diff&rev=545108&r1=545107&r2=545108
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java (original)
+++ activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsConfiguration.java Thu Jun  7 01:43:14 2007
@@ -154,9 +154,17 @@
         if (clientId != null) {
             container.setClientId(clientId);
         }
+        container.setSubscriptionDurable(subscriptionDurable);
         if (durableSubscriptionName != null) {
             container.setDurableSubscriptionName(durableSubscriptionName);
         }
+
+        // lets default to durable subscription if the subscriber name and client ID are specified (as there's
+        // no reason to specify them if not! :)
+        if (durableSubscriptionName != null && clientId != null) {
+            container.setSubscriptionDurable(true);
+        }
+
         if (exceptionListener != null) {
             container.setExceptionListener(exceptionListener);
         }
@@ -171,8 +179,6 @@
         else if (acknowledgementModeName != null) {
             container.setSessionAcknowledgeModeName(acknowledgementModeName);
         }
-
-        container.setSubscriptionDurable(subscriptionDurable);
 
         if (container instanceof DefaultMessageListenerContainer) {
             // this includes DefaultMessageListenerContainer102

Modified: activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsEndpointConfigurationTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsEndpointConfigurationTest.java?view=diff&rev=545108&r1=545107&r2=545108
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsEndpointConfigurationTest.java (original)
+++ activemq/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsEndpointConfigurationTest.java Thu Jun  7 01:43:14 2007
@@ -31,11 +31,21 @@
  */
 public class JmsEndpointConfigurationTest extends ContextTestSupport {
 
-    public void testDurableSubscriber() throws Exception {
+    public void testDurableSubscriberConfiguredWithDoubleSlash() throws Exception {
         JmsEndpoint endpoint = (JmsEndpoint) resolveMandatoryEndpoint("jms://topic:Foo.Bar?durableSubscriptionName=James&clientId=ABC");
+        assertDurableSubscriberEndpointIsValid(endpoint);
+    }
+
+    public void testDurableSubscriberConfiguredWithNoSlashes() throws Exception {
+        JmsEndpoint endpoint = (JmsEndpoint) resolveMandatoryEndpoint("jms:topic:Foo.Bar?durableSubscriptionName=James&clientId=ABC");
+        assertDurableSubscriberEndpointIsValid(endpoint);
+    }
+
+    protected void assertDurableSubscriberEndpointIsValid(JmsEndpoint endpoint) throws Exception {
         JmsConfiguration configuration = endpoint.getConfiguration();
         assertEquals("getDurableSubscriptionName()", "James", configuration.getDurableSubscriptionName());
         assertEquals("getClientId()", "ABC", configuration.getClientId());
+        assertEquals("isDeliveryPersistent()", true, configuration.isDeliveryPersistent());
 
         JmsConsumer consumer = endpoint.createConsumer(new Processor() {
             public void process(Exchange exchange) throws Exception {
@@ -45,6 +55,7 @@
         AbstractMessageListenerContainer listenerContainer = consumer.getListenerContainer();
         assertEquals("getDurableSubscriptionName()", "James", listenerContainer.getDurableSubscriptionName());
         assertEquals("getClientId()", "ABC", listenerContainer.getClientId());
+        assertEquals("isSubscriptionDurable()", true, listenerContainer.isSubscriptionDurable());
     }
 
     protected CamelContext createCamelContext() throws Exception {