[jboss-svn-commits] JBL Code SVN: r34668 - in labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta: tests/src/org/jboss/soa/esb/actions/routing/http and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Aug 12 08:24:14 EDT 2010
Author: kevin.conner at jboss.com
Date: 2010-08-12 08:24:13 -0400 (Thu, 12 Aug 2010)
New Revision: 34668
Added:
labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java
Modified:
labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java
labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java
labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java
Log:
Allow retry handler to be configured: JBESB-3456
Modified: labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java 2010-08-12 10:10:45 UTC (rev 34667)
+++ labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/AbstractHttpMethodFactory.java 2010-08-12 12:24:13 UTC (rev 34668)
@@ -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 ROUTING_HANDLER = "org.jboss.soa.esb.actions.routing.http.routingHandler";
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(ROUTING_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_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java 2010-08-12 10:10:45 UTC (rev 34667)
+++ labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/GETHttpMethodFactory.java 2010-08-12 12:24:13 UTC (rev 34668)
@@ -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_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java 2010-08-12 10:10:45 UTC (rev 34667)
+++ labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/POSTHttpMethodFactory.java 2010-08-12 12:24:13 UTC (rev 34668)
@@ -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
Added: 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_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java 2010-08-12 12:24:13 UTC (rev 34668)
@@ -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.ROUTING_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);
+ }
+}
Property changes on: labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpMethodFactoryTest.java
___________________________________________________________________
Name: svn:keywords
+ Rev Date
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list