1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.pool;
28
29 import org.apache.http.annotation.Contract;
30 import org.apache.http.annotation.ThreadingBehavior;
31 import org.apache.http.util.Args;
32
33 import java.util.concurrent.TimeUnit;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
52 public abstract class PoolEntry<T, C> {
53
54 private final String id;
55 private final T route;
56 private final C conn;
57 private final long created;
58 private final long validityDeadline;
59
60 private long updated;
61
62 private long expiry;
63
64 private volatile Object state;
65
66
67
68
69
70
71
72
73
74
75
76 public PoolEntry(final String id, final T route, final C conn,
77 final long timeToLive, final TimeUnit timeUnit) {
78 super();
79 Args.notNull(route, "Route");
80 Args.notNull(conn, "Connection");
81 Args.notNull(timeUnit, "Time unit");
82 this.id = id;
83 this.route = route;
84 this.conn = conn;
85 this.created = System.currentTimeMillis();
86 this.updated = this.created;
87 if (timeToLive > 0) {
88 final long deadline = this.created + timeUnit.toMillis(timeToLive);
89
90 this.validityDeadline = deadline > 0 ? deadline : Long.MAX_VALUE;
91 } else {
92 this.validityDeadline = Long.MAX_VALUE;
93 }
94 this.expiry = this.validityDeadline;
95 }
96
97
98
99
100
101
102
103
104 public PoolEntry(final String id, final T route, final C conn) {
105 this(id, route, conn, 0, TimeUnit.MILLISECONDS);
106 }
107
108 public String getId() {
109 return this.id;
110 }
111
112 public T getRoute() {
113 return this.route;
114 }
115
116 public C getConnection() {
117 return this.conn;
118 }
119
120 public long getCreated() {
121 return this.created;
122 }
123
124
125
126
127 public long getValidityDeadline() {
128 return this.validityDeadline;
129 }
130
131
132
133
134 @Deprecated
135 public long getValidUnit() {
136 return this.validityDeadline;
137 }
138
139 public Object getState() {
140 return this.state;
141 }
142
143 public void setState(final Object state) {
144 this.state = state;
145 }
146
147 public synchronized long getUpdated() {
148 return this.updated;
149 }
150
151 public synchronized long getExpiry() {
152 return this.expiry;
153 }
154
155 public synchronized void updateExpiry(final long time, final TimeUnit timeUnit) {
156 Args.notNull(timeUnit, "Time unit");
157 this.updated = System.currentTimeMillis();
158 final long newExpiry;
159 if (time > 0) {
160 newExpiry = this.updated + timeUnit.toMillis(time);
161 } else {
162 newExpiry = Long.MAX_VALUE;
163 }
164 this.expiry = Math.min(newExpiry, this.validityDeadline);
165 }
166
167 public synchronized boolean isExpired(final long now) {
168 return now >= this.expiry;
169 }
170
171
172
173
174
175 public abstract void close();
176
177
178
179
180 public abstract boolean isClosed();
181
182 @Override
183 public String toString() {
184 final StringBuilder buffer = new StringBuilder();
185 buffer.append("[id:");
186 buffer.append(this.id);
187 buffer.append("][route:");
188 buffer.append(this.route);
189 buffer.append("][state:");
190 buffer.append(this.state);
191 buffer.append("]");
192 return buffer.toString();
193 }
194
195 }