mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/params/ConnPerRouteBean.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/params/ConnPerRouteBean.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.conn.params;
    1.31 +
    1.32 +import java.util.Map;
    1.33 +import java.util.concurrent.ConcurrentHashMap;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
    1.38 +
    1.39 +/**
    1.40 + * This class maintains a map of HTTP routes to maximum number of connections allowed
    1.41 + * for those routes. This class can be used by pooling
    1.42 + * {@link ch.boye.httpclientandroidlib.conn.ClientConnectionManager connection managers} for
    1.43 + * a fine-grained control of connections on a per route basis.
    1.44 + *
    1.45 + * @since 4.0
    1.46 + */
    1.47 +@ThreadSafe
    1.48 +public final class ConnPerRouteBean implements ConnPerRoute {
    1.49 +
    1.50 +    /** The default maximum number of connections allowed per host */
    1.51 +    public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2;   // Per RFC 2616 sec 8.1.4
    1.52 +
    1.53 +    private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
    1.54 +
    1.55 +    private volatile int defaultMax;
    1.56 +
    1.57 +    public ConnPerRouteBean(int defaultMax) {
    1.58 +        super();
    1.59 +        this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
    1.60 +        setDefaultMaxPerRoute(defaultMax);
    1.61 +    }
    1.62 +
    1.63 +    public ConnPerRouteBean() {
    1.64 +        this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * @deprecated Use {@link #getDefaultMaxPerRoute()} instead
    1.69 +     */
    1.70 +    @Deprecated
    1.71 +    public int getDefaultMax() {
    1.72 +        return this.defaultMax;
    1.73 +    }
    1.74 +
    1.75 +    /**
    1.76 +     * @since 4.1
    1.77 +     */
    1.78 +    public int getDefaultMaxPerRoute() {
    1.79 +        return this.defaultMax;
    1.80 +    }
    1.81 +
    1.82 +    public void setDefaultMaxPerRoute(int max) {
    1.83 +        if (max < 1) {
    1.84 +            throw new IllegalArgumentException
    1.85 +                ("The maximum must be greater than 0.");
    1.86 +        }
    1.87 +        this.defaultMax = max;
    1.88 +    }
    1.89 +
    1.90 +    public void setMaxForRoute(final HttpRoute route, int max) {
    1.91 +        if (route == null) {
    1.92 +            throw new IllegalArgumentException
    1.93 +                ("HTTP route may not be null.");
    1.94 +        }
    1.95 +        if (max < 1) {
    1.96 +            throw new IllegalArgumentException
    1.97 +                ("The maximum must be greater than 0.");
    1.98 +        }
    1.99 +        this.maxPerHostMap.put(route, Integer.valueOf(max));
   1.100 +    }
   1.101 +
   1.102 +    public int getMaxForRoute(final HttpRoute route) {
   1.103 +        if (route == null) {
   1.104 +            throw new IllegalArgumentException
   1.105 +                ("HTTP route may not be null.");
   1.106 +        }
   1.107 +        Integer max = this.maxPerHostMap.get(route);
   1.108 +        if (max != null) {
   1.109 +            return max.intValue();
   1.110 +        } else {
   1.111 +            return this.defaultMax;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
   1.116 +        if (map == null) {
   1.117 +            return;
   1.118 +        }
   1.119 +        this.maxPerHostMap.clear();
   1.120 +        this.maxPerHostMap.putAll(map);
   1.121 +    }
   1.122 +
   1.123 +    @Override
   1.124 +    public String toString() {
   1.125 +        return this.maxPerHostMap.toString();
   1.126 +    }
   1.127 +
   1.128 +}

mercurial