View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 Internet2
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  /*
17   * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java,v 1.1 2008-11-30 10:57:20 mchyzer Exp $
18   * $Revision: 1.1 $
19   * $Date: 2008-11-30 10:57:20 $
20   *
21   * ====================================================================
22   *
23   *  Licensed to the Apache Software Foundation (ASF) under one or more
24   *  contributor license agreements.  See the NOTICE file distributed with
25   *  this work for additional information regarding copyright ownership.
26   *  The ASF licenses this file to You under the Apache License, Version 2.0
27   *  (the "License"); you may not use this file except in compliance with
28   *  the License.  You may obtain a copy of the License at
29   *
30   *      http://www.apache.org/licenses/LICENSE-2.0
31   *
32   *  Unless required by applicable law or agreed to in writing, software
33   *  distributed under the License is distributed on an "AS IS" BASIS,
34   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   *  See the License for the specific language governing permissions and
36   *  limitations under the License.
37   * ====================================================================
38   *
39   * This software consists of voluntary contributions made by many
40   * individuals on behalf of the Apache Software Foundation.  For more
41   * information on the Apache Software Foundation, please see
42   * <http://www.apache.org/>.
43   *
44   */
45  
46  package edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.params;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HostConfiguration;
52  import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
53  
54  /**
55   * This class represents a collection of HTTP protocol parameters applicable to 
56   * {@link edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}. 
57   * Protocol parameters may be linked together to form a hierarchy. If a particular 
58   * parameter value has not been explicitly defined in the collection itself, its 
59   * value will be drawn from the parent collection of parameters.
60   * 
61   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
62   * @author Michael Becke
63   * 
64   * @version $Revision: 1.1 $
65   * 
66   * @since 3.0
67   */
68  public class HttpConnectionManagerParams extends HttpConnectionParams {
69  
70      /** 
71       * Defines the maximum number of connections allowed per host configuration. 
72       * These values only apply to the number of connections from a particular instance 
73       * of HttpConnectionManager.
74       * <p>
75       * This parameter expects a value of type {@link java.util.Map}.  The value
76       * should map instances of {@link edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HostConfiguration}
77       * to {@link Integer integers}.  The default value can be specified using
78       * {@link edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
79       * </p>
80       */
81      public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
82  
83      /** 
84       * Defines the maximum number of connections allowed overall. This value only applies
85       * to the number of connections from a particular instance of HttpConnectionManager.
86       * <p>
87       * This parameter expects a value of type {@link Integer}.
88       * </p>
89       */
90      public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
91      
92      /**
93       * Sets the default maximum number of connections allowed for a given
94       * host config.
95       *
96       * @param maxHostConnections The default maximum.
97       * 
98       * @see #MAX_HOST_CONNECTIONS
99       */
100     public void setDefaultMaxConnectionsPerHost(int maxHostConnections) {
101         setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections);
102     }
103 
104     /**
105      * Sets the maximum number of connections to be used for the given host config.
106      * 
107      * @param hostConfiguration The host config to set the maximum for.  Use 
108      * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
109      * per host.
110      * @param maxHostConnections The maximum number of connections, <code>> 0</code>
111      * 
112      * @see #MAX_HOST_CONNECTIONS
113      */
114     public void setMaxConnectionsPerHost(
115         HostConfiguration hostConfiguration,
116         int maxHostConnections) {
117         
118         if (maxHostConnections <= 0) {
119             throw new IllegalArgumentException("maxHostConnections must be greater than 0");
120         }
121         
122         Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS);
123         // param values are meant to be immutable so we'll make a copy
124         // to modify
125         Map newValues = null;
126         if (currentValues == null) {
127             newValues = new HashMap();
128         } else {
129             newValues = new HashMap(currentValues);
130         }
131         newValues.put(hostConfiguration, new Integer(maxHostConnections));
132         setParameter(MAX_HOST_CONNECTIONS, newValues);
133     }
134     
135     /**
136      * Gets the default maximum number of connections allowed for a given
137      * host config.
138      *
139      * @return The default maximum.
140      * 
141      * @see #MAX_HOST_CONNECTIONS
142      */
143     public int getDefaultMaxConnectionsPerHost() {
144         return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
145     }
146 
147     /**
148      * Gets the maximum number of connections to be used for a particular host config.  If
149      * the value has not been specified for the given host the default value will be
150      * returned.
151      * 
152      * @param hostConfiguration The host config.
153      * @return The maximum number of connections to be used for the given host config.
154      * 
155      * @see #MAX_HOST_CONNECTIONS
156      */
157     public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) {
158         
159         Map m = (Map) getParameter(MAX_HOST_CONNECTIONS);
160         if (m == null) {
161             // MAX_HOST_CONNECTIONS have not been configured, using the default value
162             return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
163         } else {
164             Integer max = (Integer) m.get(hostConfiguration);
165             if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
166                 // the value has not been configured specifically for this host config,
167                 // use the default value
168                 return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
169             } else {
170                 return (
171                         max == null 
172                         ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 
173                         : max.intValue()
174                     );
175             }
176         }
177     }
178 
179     /**
180      * Sets the maximum number of connections allowed.
181      *
182      * @param maxTotalConnections The maximum number of connections allowed.
183      * 
184      * @see #MAX_TOTAL_CONNECTIONS
185      */
186     public void setMaxTotalConnections(int maxTotalConnections) {
187         setIntParameter(
188             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
189             maxTotalConnections);
190     }
191 
192     /**
193      * Gets the maximum number of connections allowed.
194      *
195      * @return The maximum number of connections allowed.
196      * 
197      * @see #MAX_TOTAL_CONNECTIONS
198      */
199     public int getMaxTotalConnections() {
200         return getIntParameter(
201             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
202             MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
203     }
204 
205 }