001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java,v 1.9 2004/09/13 16:25:20 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.params;
032:
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import org.apache.commons.httpclient.HostConfiguration;
037: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
038:
039: /**
040: * This class represents a collection of HTTP protocol parameters applicable to
041: * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}.
042: * Protocol parameters may be linked together to form a hierarchy. If a particular
043: * parameter value has not been explicitly defined in the collection itself, its
044: * value will be drawn from the parent collection of parameters.
045: *
046: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
047: * @author Michael Becke
048: *
049: * @version $Revision: 480424 $
050: *
051: * @since 3.0
052: */
053: public class HttpConnectionManagerParams extends HttpConnectionParams {
054:
055: /**
056: * Defines the maximum number of connections allowed per host configuration.
057: * These values only apply to the number of connections from a particular instance
058: * of HttpConnectionManager.
059: * <p>
060: * This parameter expects a value of type {@link java.util.Map}. The value
061: * should map instances of {@link org.apache.commons.httpclient.HostConfiguration}
062: * to {@link Integer integers}. The default value can be specified using
063: * {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
064: * </p>
065: */
066: public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
067:
068: /**
069: * Defines the maximum number of connections allowed overall. This value only applies
070: * to the number of connections from a particular instance of HttpConnectionManager.
071: * <p>
072: * This parameter expects a value of type {@link Integer}.
073: * </p>
074: */
075: public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
076:
077: /**
078: * Sets the default maximum number of connections allowed for a given
079: * host config.
080: *
081: * @param maxHostConnections The default maximum.
082: *
083: * @see #MAX_HOST_CONNECTIONS
084: */
085: public void setDefaultMaxConnectionsPerHost(int maxHostConnections) {
086: setMaxConnectionsPerHost(
087: HostConfiguration.ANY_HOST_CONFIGURATION,
088: maxHostConnections);
089: }
090:
091: /**
092: * Sets the maximum number of connections to be used for the given host config.
093: *
094: * @param hostConfiguration The host config to set the maximum for. Use
095: * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
096: * per host.
097: * @param maxHostConnections The maximum number of connections, <code>> 0</code>
098: *
099: * @see #MAX_HOST_CONNECTIONS
100: */
101: public void setMaxConnectionsPerHost(
102: HostConfiguration hostConfiguration, int maxHostConnections) {
103:
104: if (maxHostConnections <= 0) {
105: throw new IllegalArgumentException(
106: "maxHostConnections must be greater than 0");
107: }
108:
109: Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS);
110: // param values are meant to be immutable so we'll make a copy
111: // to modify
112: Map newValues = null;
113: if (currentValues == null) {
114: newValues = new HashMap();
115: } else {
116: newValues = new HashMap(currentValues);
117: }
118: newValues.put(hostConfiguration,
119: new Integer(maxHostConnections));
120: setParameter(MAX_HOST_CONNECTIONS, newValues);
121: }
122:
123: /**
124: * Gets the default maximum number of connections allowed for a given
125: * host config.
126: *
127: * @return The default maximum.
128: *
129: * @see #MAX_HOST_CONNECTIONS
130: */
131: public int getDefaultMaxConnectionsPerHost() {
132: return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
133: }
134:
135: /**
136: * Gets the maximum number of connections to be used for a particular host config. If
137: * the value has not been specified for the given host the default value will be
138: * returned.
139: *
140: * @param hostConfiguration The host config.
141: * @return The maximum number of connections to be used for the given host config.
142: *
143: * @see #MAX_HOST_CONNECTIONS
144: */
145: public int getMaxConnectionsPerHost(
146: HostConfiguration hostConfiguration) {
147:
148: Map m = (Map) getParameter(MAX_HOST_CONNECTIONS);
149: if (m == null) {
150: // MAX_HOST_CONNECTIONS have not been configured, using the default value
151: return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
152: } else {
153: Integer max = (Integer) m.get(hostConfiguration);
154: if (max == null
155: && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
156: // the value has not been configured specifically for this host config,
157: // use the default value
158: return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
159: } else {
160: return (max == null ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS
161: : max.intValue());
162: }
163: }
164: }
165:
166: /**
167: * Sets the maximum number of connections allowed.
168: *
169: * @param maxTotalConnections The maximum number of connections allowed.
170: *
171: * @see #MAX_TOTAL_CONNECTIONS
172: */
173: public void setMaxTotalConnections(int maxTotalConnections) {
174: setIntParameter(
175: HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
176: maxTotalConnections);
177: }
178:
179: /**
180: * Gets the maximum number of connections allowed.
181: *
182: * @return The maximum number of connections allowed.
183: *
184: * @see #MAX_TOTAL_CONNECTIONS
185: */
186: public int getMaxTotalConnections() {
187: return getIntParameter(
188: HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
189: MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
190: }
191:
192: }
|