[jboss-svn-commits] JBL Code SVN: r34813 - in labs/jbossesb/branches/JBESB_4_9_CP/product: rosetta/tests/src/org/jboss/soa/esb/actions/routing/http and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Aug 20 09:40:55 EDT 2010


Author: kevin.conner at jboss.com
Date: 2010-08-20 09:40:55 -0400 (Fri, 20 Aug 2010)
New Revision: 34813

Added:
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java
Modified:
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java
   labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java
   labs/jbossesb/branches/JBESB_4_9_CP/product/services/soap/src/main/java/org/jboss/soa/esb/actions/soap/RemoteWsdlLoader.java
Log:
Add support for retry handler configuration: JBESB-3378

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java	2010-08-20 13:26:16 UTC (rev 34812)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java	2010-08-20 13:40:55 UTC (rev 34813)
@@ -27,8 +27,10 @@
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpMethodRetryHandler;
 import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.jboss.soa.esb.Configurable;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.util.ClassUtil;
 
 /**
  * AbstractHttpMethodFactory.
@@ -37,8 +39,12 @@
  */
 public abstract class AbstractHttpMethodFactory implements HttpMethodFactory
 {
+    public static final String RETRY_HANDLER = "org.jboss.soa.esb.actions.routing.http.retryHandler";
 	
 	private URL endpoint;
+	private Class<?> retryHandlerClass ;
+	private boolean isConfigurable ;
+	private ConfigTree config ;
 
     public void setEndpoint(URL endpoint)
     {
@@ -59,15 +65,68 @@
     	return sb.toString();
     }
     
-    public void setConfiguration(ConfigTree config) throws ConfigurationException {}
+    public void setConfiguration(ConfigTree config)
+        throws ConfigurationException
+    {
+        this.config = config ;
+        final String retryHandler = config.getAttribute(RETRY_HANDLER) ;
+        if (retryHandler != null)
+        {
+            final Class<?> retryHandlerClass ;
+            try
+            {
+                retryHandlerClass = ClassUtil.forName(retryHandler, getClass()) ;
+            }
+            catch (final ClassNotFoundException cnfe)
+            {
+                throw new ConfigurationException("Could not locate Retry Handler: " + retryHandler, cnfe) ;
+            }
+            if (!HttpMethodRetryHandler.class.isAssignableFrom(retryHandlerClass))
+            {
+                throw new ConfigurationException("Retry Handler does not implement HttpMethodRetryHandler: " + retryHandler) ;
+            }
+            try
+            {
+                retryHandlerClass.getConstructor() ;
+            }
+            catch (final NoSuchMethodException nsme)
+            {
+                throw new ConfigurationException("Retry Handler does not have a default constructor: " + retryHandler, nsme) ;
+            }
+            this.retryHandlerClass = retryHandlerClass ;
+            isConfigurable = Configurable.class.isAssignableFrom(retryHandlerClass) ;
+        }
+    }
     
-    protected void wrapRetryHandler(HttpMethod method) {
+    protected void initialiseRetryHandler(HttpMethod method) throws IOException {
     	HttpMethodParams params = method.getParams();
-    	HttpMethodRetryHandler other = (HttpMethodRetryHandler)params.getParameter(HttpMethodParams.RETRY_HANDLER);
-    	params.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandlerWrapper(other));
+    	if (retryHandlerClass != null) {
+    	    final Object handler ;
+    	    try {
+    	        handler = retryHandlerClass.newInstance() ;
+    	    } catch (final Throwable th) {
+    	        final IOException ioe = new IOException("Failed to instantiate Retry Handler") ;
+    	        ioe.initCause(th) ;
+    	        throw ioe ;
+    	    }
+    	    if (isConfigurable) {
+    	        final Configurable configurable = Configurable.class.cast(handler) ;
+    	        try {
+    	            configurable.setConfiguration(config) ;
+    	        } catch (final ConfigurationException ce) {
+    	            final IOException ioe = new IOException("Failed to configure Retry Handler") ;
+    	            ioe.initCause(ce) ;
+    	            throw ioe ;
+    	        }
+    	    }
+    	    params.setParameter(HttpMethodParams.RETRY_HANDLER, handler);
+    	} else {
+    	    HttpMethodRetryHandler other = (HttpMethodRetryHandler)params.getParameter(HttpMethodParams.RETRY_HANDLER);
+    	    params.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandlerWrapper(other));
+    	}
     }
     
-    private static class HttpMethodRetryHandlerWrapper implements HttpMethodRetryHandler {
+    static class HttpMethodRetryHandlerWrapper implements HttpMethodRetryHandler {
     	
     	private HttpMethodRetryHandler other;
     	

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java	2010-08-20 13:26:16 UTC (rev 34812)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java	2010-08-20 13:40:55 UTC (rev 34813)
@@ -34,7 +34,7 @@
 
     public HttpMethodBase getInstance(Message message) throws IOException {
     	GetMethod method = new GetMethod( getEndpointPathAndQuery() );
-    	wrapRetryHandler(method);
+    	initialiseRetryHandler(method);
     	return method;
     }
 }
\ No newline at end of file

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java	2010-08-20 13:26:16 UTC (rev 34812)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java	2010-08-20 13:40:55 UTC (rev 34813)
@@ -52,6 +52,7 @@
 
     @Override
     public void setConfiguration(ConfigTree config) throws ConfigurationException {
+        super.setConfiguration(config) ;
         payloadProxy = new MessagePayloadProxy(config);
         
         _contentType = config.getAttribute(CONTENT_TYPE);
@@ -83,7 +84,7 @@
         }
 
         method.setRequestEntity(entity);
-        wrapRetryHandler(method);
+        initialiseRetryHandler(method);
         return method;
     }
 }
\ No newline at end of file

Copied: labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java (from rev 34668, labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java	2010-08-20 13:40:55 UTC (rev 34813)
@@ -0,0 +1,164 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+
+package org.jboss.soa.esb.actions.routing.http;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.net.URL;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpMethodRetryHandler;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.jboss.soa.esb.Configurable;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Test;
+
+public class HttpMethodFactoryTest
+{
+    @Test
+    public void testDefaultRetryHandlerPOST()
+        throws Exception
+    {
+        testDefaultRetryHandler("POST") ;
+    }
+
+    @Test
+    public void testDefaultRetryHandlerGET()
+        throws Exception
+    {
+        testDefaultRetryHandler("GET") ;
+    }
+
+    private void testDefaultRetryHandler(final String method)
+        throws Exception
+    {
+        final HttpMethodBase httpMethodBase = executeTest(method, null) ;
+        final HttpMethodParams params = httpMethodBase.getParams() ;
+        final Object handler = params.getParameter(HttpMethodParams.RETRY_HANDLER) ;
+        assertNotNull("retry handler", handler) ;
+        assertEquals("handler class", AbstractHttpMethodFactory.HttpMethodRetryHandlerWrapper.class, handler.getClass()) ;
+    }
+
+    @Test
+    public void testRetryHandlerPOST()
+        throws Exception
+    {
+        testRetryHandler("POST") ;
+    }
+
+    @Test
+    public void testRetryHandlerGET()
+        throws Exception
+    {
+        testRetryHandler("GET") ;
+    }
+
+    private void testRetryHandler(final String method)
+        throws Exception
+    {
+        final Class<?> handlerClass = RetryHandler.class ;
+        final HttpMethodBase httpMethodBase = executeTest(method, handlerClass.getName()) ;
+        final HttpMethodParams params = httpMethodBase.getParams() ;
+        final Object handler = params.getParameter(HttpMethodParams.RETRY_HANDLER) ;
+        assertNotNull("retry handler", handler) ;
+        assertEquals("handler class", handlerClass, handler.getClass()) ;
+    }
+
+    @Test
+    public void testConfigurableRetryHandlerPOST()
+        throws Exception
+    {
+        testConfigurableRetryHandler("POST") ;
+    }
+
+    @Test
+    public void testConfigurableRetryHandlerGET()
+        throws Exception
+    {
+        testConfigurableRetryHandler("GET") ;
+    }
+
+    private void testConfigurableRetryHandler(final String method)
+        throws Exception
+    {
+        final Class<ConfigurableRetryHandler> handlerClass = ConfigurableRetryHandler.class ;
+        final HttpMethodBase httpMethodBase = executeTest(method, handlerClass.getName()) ;
+        final HttpMethodParams params = httpMethodBase.getParams() ;
+        final Object handler = params.getParameter(HttpMethodParams.RETRY_HANDLER) ;
+        assertNotNull("retry handler", handler) ;
+        assertEquals("handler class", handlerClass, handler.getClass()) ;
+        final ConfigurableRetryHandler configurableRetryHandler = handlerClass.cast(handler) ;
+        assertNotNull("config", configurableRetryHandler.getConfig()) ;
+    }
+
+    private HttpMethodBase executeTest(final String method, final String handlerName)
+        throws Exception
+    {
+        final Message message = MessageFactory.getInstance().getMessage() ;
+        message.getBody().add("test") ;
+        final ConfigTree config = new ConfigTree("test") ;
+        if (handlerName != null)
+        {
+            config.setAttribute(AbstractHttpMethodFactory.RETRY_HANDLER, handlerName) ;
+        }
+        final HttpMethodFactory factory = HttpMethodFactory.Factory.getInstance(method, config, new URL("http://dummy")) ;
+        return factory.getInstance(message) ;
+    }
+
+    public static final class RetryHandler implements HttpMethodRetryHandler
+    {
+        public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount)
+        {
+            return false;
+        }
+    }
+
+    public static final class ConfigurableRetryHandler implements HttpMethodRetryHandler, Configurable
+    {
+        private ConfigTree config ;
+
+        public boolean retryMethod(final HttpMethod method, final IOException exception, final int executionCount)
+        {
+            return false;
+        }
+
+        public void setConfiguration(final ConfigTree config)
+            throws ConfigurationException
+        {
+            this.config = config ;
+        }
+
+        public ConfigTree getConfig()
+        {
+            return config ;
+        }
+    }
+
+    public static junit.framework.Test suite() {
+        return new JUnit4TestAdapter(HttpMethodFactoryTest.class);
+    }
+}

Modified: labs/jbossesb/branches/JBESB_4_9_CP/product/services/soap/src/main/java/org/jboss/soa/esb/actions/soap/RemoteWsdlLoader.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_9_CP/product/services/soap/src/main/java/org/jboss/soa/esb/actions/soap/RemoteWsdlLoader.java	2010-08-20 13:26:16 UTC (rev 34812)
+++ labs/jbossesb/branches/JBESB_4_9_CP/product/services/soap/src/main/java/org/jboss/soa/esb/actions/soap/RemoteWsdlLoader.java	2010-08-20 13:40:55 UTC (rev 34813)
@@ -34,6 +34,7 @@
 import org.jboss.internal.soa.esb.util.StreamUtils;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.actions.routing.http.HttpMethodFactory;
+import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.http.HttpClientFactory;
 import org.jboss.soa.esb.util.ClassUtil;
 
@@ -74,10 +75,9 @@
             HttpMethodBase httpMethod;
             
             try {
-    			HttpMethodFactory methodFactory = HttpMethodFactory.Factory.getInstance("GET", null, new URL(url));
+    			HttpMethodFactory methodFactory = HttpMethodFactory.Factory.getInstance("GET", new ConfigTree("RemoteWsdlLoader"), new URL(url));
     			httpMethod = methodFactory.getInstance(null);
     		} catch (ConfigurationException ce) {
-    			// should never happen with "GET" method since ConfigTree isn't used with GET - only POST
     			throw (IOException)(new IOException(ce.getMessage()).initCause(ce));
     		}
 



More information about the jboss-svn-commits mailing list