[jboss-svn-commits] JBL Code SVN: r31946 - in labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta: tests/src/org/jboss/soa/esb/http and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Mar 4 16:38:31 EST 2010


Author: kevin.conner at jboss.com
Date: 2010-03-04 16:38:31 -0500 (Thu, 04 Mar 2010)
New Revision: 31946

Added:
   labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/http/HttpClientFactoryUnitTest.java
Modified:
   labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/src/org/jboss/soa/esb/http/configurators/Connection.java
Log:
Store connection parameters for connection manager: JBESB-3188

Modified: labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/src/org/jboss/soa/esb/http/configurators/Connection.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/src/org/jboss/soa/esb/http/configurators/Connection.java	2010-03-04 21:14:47 UTC (rev 31945)
+++ labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/src/org/jboss/soa/esb/http/configurators/Connection.java	2010-03-04 21:38:31 UTC (rev 31946)
@@ -19,34 +19,14 @@
  */
 package org.jboss.soa.esb.http.configurators;
 
+import org.apache.commons.httpclient.HostConfiguration;
 import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.ProxyHost;
 import org.apache.commons.httpclient.HttpConnectionManager;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
-import org.apache.commons.httpclient.contrib.ssl.StrictSSLProtocolSocketFactory;
-import org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory;
-import org.apache.commons.httpclient.protocol.Protocol;
-import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
-import org.apache.commons.ssl.KeyMaterial;
-import org.apache.commons.ssl.SSLClient;
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.http.Configurator;
-import org.jboss.soa.esb.http.protocol.ProtocolSocketFactoryBuilder;
-import org.jboss.soa.esb.util.ClassUtil;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.security.GeneralSecurityException;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.CertificateException;
 import java.util.Properties;
 
 /**
@@ -73,7 +53,6 @@
  */
 public class Connection extends Configurator {
 
-    private Logger logger = Logger.getLogger(Connection.class);
     /**
      * Max total connections.
      */
@@ -88,35 +67,93 @@
         final String maxTotalConnections = properties.getProperty(MAX_TOTAL_CONNECTIONS) ;
         final String maxConnectionsPerHost = properties.getProperty(MAX_CONNECTIONS_PER_HOST) ;
 
-        if (maxTotalConnections != null) {
-            final HttpConnectionManagerParams params = getConnectionManagerParams(connectionManager);
-            try {
-                params.setMaxTotalConnections(Integer.parseInt(maxTotalConnections.trim())) ;
-            } catch (final NumberFormatException nfe) {
-                throw new ConfigurationException("Invalid " + MAX_TOTAL_CONNECTIONS + " property: " + maxTotalConnections) ;
+        connectionManager.setParams(new ESBHttpConnectionManagerParams(maxTotalConnections, maxConnectionsPerHost)) ;
+        httpClient.setHttpConnectionManager(connectionManager);
+    }
+    
+    /**
+     * Per connection manager instance of params.
+     * @author kevin
+     */
+    private static class ESBHttpConnectionManagerParams extends HttpConnectionManagerParams
+    {
+        /**
+         * Serial version UID for this class.
+         */
+        private static final long serialVersionUID = 6586457118564628500L;
+        /**
+         * Maximum total connections.
+         */
+        private final Integer maxTotalConnections ;
+        /**
+         * Maximum connections per host.
+         */
+        private final Integer maxConnectionsPerHost ;
+        
+        /**
+         * Construct the connection manager parameters.
+         * @param totalConnections The maximum total connections or null if defaults.
+         * @param connectionsPerHost The maximum connections per host or null if defaults.
+         * @throws ConfigurationException For errors parsing parameters.
+         */
+        ESBHttpConnectionManagerParams(final String totalConnections, final String connectionsPerHost)
+            throws ConfigurationException
+        {
+            if (totalConnections != null) {
+                try {
+                    maxTotalConnections = Integer.parseInt(totalConnections.trim()) ;
+                } catch (final NumberFormatException nfe) {
+                    throw new ConfigurationException("Invalid " + MAX_TOTAL_CONNECTIONS + " property: " + totalConnections) ;
+                }
+            } else {
+                maxTotalConnections = null ;
             }
-        }
+            
 
-        if (maxConnectionsPerHost != null) {
-            final HttpConnectionManagerParams params = getConnectionManagerParams(connectionManager);
-            try {
-                params.setMaxConnectionsPerHost(httpClient.getHostConfiguration(), Integer.parseInt(maxConnectionsPerHost.trim()));
-            } catch (final NumberFormatException nfe) {
-                throw new ConfigurationException("Invalid " + MAX_CONNECTIONS_PER_HOST + " property: " + maxConnectionsPerHost) ;
+            if (connectionsPerHost != null) {
+                try {
+                    maxConnectionsPerHost = Integer.parseInt(connectionsPerHost.trim());
+                } catch (final NumberFormatException nfe) {
+                    throw new ConfigurationException("Invalid " + MAX_CONNECTIONS_PER_HOST + " property: " + connectionsPerHost) ;
+                }
+            } else {
+                maxConnectionsPerHost = null ;
             }
         }
-
-        httpClient.setHttpConnectionManager(connectionManager);
-    }
-
-    private HttpConnectionManagerParams getConnectionManagerParams(HttpConnectionManager connectionManager) {
-        HttpConnectionManagerParams params = connectionManager.getParams();
-
-        if(params == null) {
-            params = new HttpConnectionManagerParams();
-            connectionManager.setParams(params) ;
+        
+        @Override
+        /**
+         * Get the maximum connections per host.
+         * @param The current host configuration.
+         * @return the maximum connections per host.
+         */
+        public int getMaxConnectionsPerHost(final HostConfiguration hostConfiguration)
+        {
+            if (maxConnectionsPerHost != null)
+            {
+                return maxConnectionsPerHost.intValue() ;
+            }
+            else
+            {
+                return super.getMaxConnectionsPerHost(hostConfiguration) ;
+            }
         }
-
-        return params;
+        
+        @Override
+        /**
+         * Get the maximum total connections.
+         * @return the maximum connections per host.
+         */
+        public int getMaxTotalConnections()
+        {
+            if (maxTotalConnections != null)
+            {
+                return maxTotalConnections.intValue() ;
+            }
+            else
+            {
+                return super.getMaxTotalConnections() ;
+            }
+        }
     }
 }
\ No newline at end of file

Copied: labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/http/HttpClientFactoryUnitTest.java (from rev 31407, labs/jbossesb/branches/JBESB_4_7_CP/product/rosetta/tests/src/org/jboss/soa/esb/http/HttpClientFactoryUnitTest.java)
===================================================================
--- labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/http/HttpClientFactoryUnitTest.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_4_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/http/HttpClientFactoryUnitTest.java	2010-03-04 21:38:31 UTC (rev 31946)
@@ -0,0 +1,146 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY 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
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.http;
+
+import static org.junit.Assert.*;
+
+import java.util.Properties;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
+import org.jboss.soa.esb.http.configurators.Connection;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link HttpClientFactory}.
+ * <p/>
+ * 
+ * @author <a href="mailto:kevin.conner at jboss.com">Kevin Conner</a>
+ */
+public class HttpClientFactoryUnitTest
+{
+    @Test
+    public void testMaximumTotalConnections()
+        throws Exception
+    {
+        final int maxTotalConnections = 1234 ;
+        final Properties properties = getProperties(Integer.toString(maxTotalConnections), null) ;
+        final HttpClient httpClient = HttpClientFactory.createHttpClient(properties) ;
+        try
+        {
+            final HostConfiguration configuration = httpClient.getHostConfiguration() ;
+            final HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams() ;
+            assertEquals("MAX_TOTAL_CONNECTIONS not set", maxTotalConnections, params.getMaxTotalConnections()) ;
+            assertTrue("maxConnectionsPerHost identical", maxTotalConnections !=  params.getMaxConnectionsPerHost(configuration)) ;
+        }
+        finally
+        {
+            HttpClientFactory.shutdown(httpClient) ;
+        }
+    }
+    
+    @Test
+    public void testMaximumConnectionsPerHost()
+        throws Exception
+    {
+        final int maxConnectionsPerHost = 1234 ;
+        final Properties properties = getProperties(null, Integer.toString(maxConnectionsPerHost)) ;
+        
+        final HttpClient httpClient = HttpClientFactory.createHttpClient(properties) ;
+        try
+        {
+            final HostConfiguration configuration = httpClient.getHostConfiguration() ;
+            final HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams() ;
+            assertEquals("MAX_CONNECTIONS_PER_HOST not set", maxConnectionsPerHost, params.getMaxConnectionsPerHost(configuration)) ;
+            assertTrue("maxTotalConnections identical", maxConnectionsPerHost !=  params.getMaxTotalConnections()) ;
+        }
+        finally
+        {
+            HttpClientFactory.shutdown(httpClient) ;
+        }
+    }
+
+    
+    @Test
+    public void testMultipleConfigurations()
+        throws Exception
+    {
+        final int firstMaxTotalConnections = 1234 ;
+        final int firstMaxConnectionsPerHost = 2345 ;
+        final Properties firstProperties = getProperties(Integer.toString(firstMaxTotalConnections), Integer.toString(firstMaxConnectionsPerHost)) ;
+
+        final int secondMaxTotalConnections = 3456 ;
+        final int secondMaxConnectionsPerHost = 4567 ;
+        final Properties secondProperties = getProperties(Integer.toString(secondMaxTotalConnections), Integer.toString(secondMaxConnectionsPerHost)) ;
+        
+        final HttpClient firstHttpClient = HttpClientFactory.createHttpClient(firstProperties) ;
+        try
+        {
+            final HttpClient secondHttpClient = HttpClientFactory.createHttpClient(secondProperties) ;
+            try
+            {
+                final HostConfiguration firstConfiguration = firstHttpClient.getHostConfiguration() ;
+                final HttpConnectionManagerParams firstParams = firstHttpClient.getHttpConnectionManager().getParams() ;
+                
+                assertEquals("MAX_TOTAL_CONNECTIONS not set", firstMaxTotalConnections, firstParams.getMaxTotalConnections()) ;
+                assertEquals("MAX_CONNECTIONS_PER_HOST not set", firstMaxConnectionsPerHost, firstParams.getMaxConnectionsPerHost(firstConfiguration)) ;
+                
+                final HostConfiguration secondConfiguration = secondHttpClient.getHostConfiguration() ;
+                final HttpConnectionManagerParams secondParams = secondHttpClient.getHttpConnectionManager().getParams() ;
+                
+                assertEquals("MAX_TOTAL_CONNECTIONS not set", secondMaxTotalConnections, secondParams.getMaxTotalConnections()) ;
+                assertEquals("MAX_CONNECTIONS_PER_HOST not set", secondMaxConnectionsPerHost, secondParams.getMaxConnectionsPerHost(secondConfiguration)) ;
+            }
+            finally
+            {
+                HttpClientFactory.shutdown(secondHttpClient) ;
+            }
+        }
+        finally
+        {
+            HttpClientFactory.shutdown(firstHttpClient) ;
+        }
+    }
+
+    private Properties getProperties(final String maxTotalConnections, final String maxConnectionsPerHost)
+    {
+        final Properties properties = new Properties() ;
+        properties.setProperty(HttpClientFactory.TARGET_HOST_URL, "http://example.com/targetHostUrl") ;
+        
+        if (maxTotalConnections != null)
+        {
+            properties.setProperty(Connection.MAX_TOTAL_CONNECTIONS, maxTotalConnections) ;
+        }
+        if (maxConnectionsPerHost != null)
+        {
+            properties.setProperty(Connection.MAX_CONNECTIONS_PER_HOST, maxConnectionsPerHost) ;
+        }
+        return properties ;
+    }
+
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(HttpClientFactoryUnitTest.class);
+    }
+}



More information about the jboss-svn-commits mailing list