|
1 /* |
|
2 * ==================================================================== |
|
3 * |
|
4 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
5 * contributor license agreements. See the NOTICE file distributed with |
|
6 * this work for additional information regarding copyright ownership. |
|
7 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
8 * (the "License"); you may not use this file except in compliance with |
|
9 * 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, software |
|
14 * distributed under the License is distributed on an "AS IS" BASIS, |
|
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
16 * See the License for the specific language governing permissions and |
|
17 * limitations under the License. |
|
18 * ==================================================================== |
|
19 * |
|
20 * This software consists of voluntary contributions made by many |
|
21 * individuals on behalf of the Apache Software Foundation. For more |
|
22 * information on the Apache Software Foundation, please see |
|
23 * <http://www.apache.org/>. |
|
24 * |
|
25 */ |
|
26 |
|
27 package ch.boye.httpclientandroidlib.impl.conn.tsccm; |
|
28 |
|
29 import java.lang.ref.ReferenceQueue; |
|
30 import java.util.concurrent.TimeUnit; |
|
31 |
|
32 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
|
33 import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; |
|
34 import ch.boye.httpclientandroidlib.conn.ClientConnectionOperator; |
|
35 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
|
36 import ch.boye.httpclientandroidlib.impl.conn.AbstractPoolEntry; |
|
37 |
|
38 /** |
|
39 * Basic implementation of a connection pool entry. |
|
40 * |
|
41 * @since 4.0 |
|
42 */ |
|
43 @NotThreadSafe |
|
44 public class BasicPoolEntry extends AbstractPoolEntry { |
|
45 |
|
46 private final long created; |
|
47 |
|
48 private long updated; |
|
49 private long validUntil; |
|
50 private long expiry; |
|
51 |
|
52 /** |
|
53 * @deprecated do not use |
|
54 */ |
|
55 @Deprecated |
|
56 public BasicPoolEntry(ClientConnectionOperator op, |
|
57 HttpRoute route, |
|
58 ReferenceQueue<Object> queue) { |
|
59 super(op, route); |
|
60 if (route == null) { |
|
61 throw new IllegalArgumentException("HTTP route may not be null"); |
|
62 } |
|
63 this.created = System.currentTimeMillis(); |
|
64 this.validUntil = Long.MAX_VALUE; |
|
65 this.expiry = this.validUntil; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Creates a new pool entry. |
|
70 * |
|
71 * @param op the connection operator |
|
72 * @param route the planned route for the connection |
|
73 */ |
|
74 public BasicPoolEntry(ClientConnectionOperator op, |
|
75 HttpRoute route) { |
|
76 this(op, route, -1, TimeUnit.MILLISECONDS); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Creates a new pool entry with a specified maximum lifetime. |
|
81 * |
|
82 * @param op the connection operator |
|
83 * @param route the planned route for the connection |
|
84 * @param connTTL maximum lifetime of this entry, <=0 implies "infinity" |
|
85 * @param timeunit TimeUnit of connTTL |
|
86 * |
|
87 * @since 4.1 |
|
88 */ |
|
89 public BasicPoolEntry(ClientConnectionOperator op, |
|
90 HttpRoute route, long connTTL, TimeUnit timeunit) { |
|
91 super(op, route); |
|
92 if (route == null) { |
|
93 throw new IllegalArgumentException("HTTP route may not be null"); |
|
94 } |
|
95 this.created = System.currentTimeMillis(); |
|
96 if (connTTL > 0) { |
|
97 this.validUntil = this.created + timeunit.toMillis(connTTL); |
|
98 } else { |
|
99 this.validUntil = Long.MAX_VALUE; |
|
100 } |
|
101 this.expiry = this.validUntil; |
|
102 } |
|
103 |
|
104 protected final OperatedClientConnection getConnection() { |
|
105 return super.connection; |
|
106 } |
|
107 |
|
108 protected final HttpRoute getPlannedRoute() { |
|
109 return super.route; |
|
110 } |
|
111 |
|
112 @Deprecated |
|
113 protected final BasicPoolEntryRef getWeakRef() { |
|
114 return null; |
|
115 } |
|
116 |
|
117 @Override |
|
118 protected void shutdownEntry() { |
|
119 super.shutdownEntry(); |
|
120 } |
|
121 |
|
122 /** |
|
123 * @since 4.1 |
|
124 */ |
|
125 public long getCreated() { |
|
126 return this.created; |
|
127 } |
|
128 |
|
129 /** |
|
130 * @since 4.1 |
|
131 */ |
|
132 public long getUpdated() { |
|
133 return this.updated; |
|
134 } |
|
135 |
|
136 /** |
|
137 * @since 4.1 |
|
138 */ |
|
139 public long getExpiry() { |
|
140 return this.expiry; |
|
141 } |
|
142 |
|
143 public long getValidUntil() { |
|
144 return this.validUntil; |
|
145 } |
|
146 |
|
147 /** |
|
148 * @since 4.1 |
|
149 */ |
|
150 public void updateExpiry(long time, TimeUnit timeunit) { |
|
151 this.updated = System.currentTimeMillis(); |
|
152 long newExpiry; |
|
153 if (time > 0) { |
|
154 newExpiry = this.updated + timeunit.toMillis(time); |
|
155 } else { |
|
156 newExpiry = Long.MAX_VALUE; |
|
157 } |
|
158 this.expiry = Math.min(validUntil, newExpiry); |
|
159 } |
|
160 |
|
161 /** |
|
162 * @since 4.1 |
|
163 */ |
|
164 public boolean isExpired(long now) { |
|
165 return now >= this.expiry; |
|
166 } |
|
167 |
|
168 } |
|
169 |
|
170 |