1 30 31 package org.apache.commons.httpclient.params; 32 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import org.apache.commons.httpclient.HostConfiguration; 37 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 38 39 53 public class HttpConnectionManagerParams extends HttpConnectionParams { 54 55 66 public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host"; 67 68 75 public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total"; 76 77 85 public void setDefaultMaxConnectionsPerHost(int maxHostConnections) { 86 setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections); 87 } 88 89 99 public void setMaxConnectionsPerHost( 100 HostConfiguration hostConfiguration, 101 int maxHostConnections) { 102 103 if (maxHostConnections <= 0) { 104 throw new IllegalArgumentException ("maxHostConnections must be greater than 0"); 105 } 106 107 Map currentValues = (Map ) getParameter(MAX_HOST_CONNECTIONS); 108 Map newValues = null; 111 if (currentValues == null) { 112 newValues = new HashMap (); 113 } else { 114 newValues = new HashMap (currentValues); 115 } 116 newValues.put(hostConfiguration, new Integer (maxHostConnections)); 117 setParameter(MAX_HOST_CONNECTIONS, newValues); 118 } 119 120 128 public int getDefaultMaxConnectionsPerHost() { 129 return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); 130 } 131 132 142 public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) { 143 144 Map m = (Map ) getParameter(MAX_HOST_CONNECTIONS); 145 if (m == null) { 146 return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS; 148 } else { 149 Integer max = (Integer ) m.get(hostConfiguration); 150 if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) { 151 return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); 154 } else { 155 return ( 156 max == null 157 ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 158 : max.intValue() 159 ); 160 } 161 } 162 } 163 164 171 public void setMaxTotalConnections(int maxTotalConnections) { 172 setIntParameter( 173 HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 174 maxTotalConnections); 175 } 176 177 184 public int getMaxTotalConnections() { 185 return getIntParameter( 186 HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 187 MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS); 188 } 189 190 } 191 | Popular Tags |