View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client;
28  
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  import org.apache.http.conn.routing.HttpRoute;
33  import org.apache.http.pool.ConnPoolControl;
34  import org.apache.http.pool.PoolStats;
35  
36  public final class MockConnPoolControl implements ConnPoolControl<HttpRoute> {
37  
38      private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
39  
40      private volatile int totalMax;
41      private volatile int defaultMax;
42  
43      public MockConnPoolControl() {
44          super();
45          this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
46          this.totalMax = 20;
47          this.defaultMax = 2;
48      }
49  
50      @Override
51      public void setMaxTotal(final int max) {
52          this.totalMax = max;
53      }
54  
55      @Override
56      public int getMaxTotal() {
57          return this.totalMax;
58      }
59  
60      @Override
61      public PoolStats getTotalStats() {
62          return new PoolStats(-1, -1, -1, this.totalMax);
63      }
64  
65      @Override
66      public PoolStats getStats(final HttpRoute route) {
67          return new PoolStats(-1, -1, -1, getMaxPerRoute(route));
68      }
69  
70      @Override
71      public int getDefaultMaxPerRoute() {
72          return this.defaultMax;
73      }
74  
75      @Override
76      public void setDefaultMaxPerRoute(final int max) {
77          this.defaultMax = max;
78      }
79  
80      @Override
81      public void setMaxPerRoute(final HttpRoute route, final int max) {
82          this.maxPerHostMap.put(route, Integer.valueOf(max));
83      }
84  
85      @Override
86      public int getMaxPerRoute(final HttpRoute route) {
87          final Integer max = this.maxPerHostMap.get(route);
88          return max != null ? max.intValue() : this.defaultMax;
89      }
90  
91      public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
92          if (map == null) {
93              return;
94          }
95          this.maxPerHostMap.clear();
96          this.maxPerHostMap.putAll(map);
97      }
98  
99      @Override
100     public String toString() {
101         return this.maxPerHostMap.toString();
102     }
103 
104 }