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.pool;
28  
29  import java.util.concurrent.TimeUnit;
30  
31  import org.apache.http.annotation.ThreadingBehavior;
32  import org.apache.http.annotation.Contract;
33  import org.apache.http.util.Args;
34  
35  /**
36   * Pool entry containing a pool connection object along with its route.
37   * <p>
38   * The connection contained by the pool entry may have an expiration time which
39   * can be either set upon construction time or updated with
40   * the {@link #updateExpiry(long, TimeUnit)}.
41   * <p>
42   * Pool entry may also have an object associated with it that represents
43   * a connection state (usually a security principal or a unique token identifying
44   * the user whose credentials have been used while establishing the connection).
45   *
46   * @param <T> the route type that represents the opposite endpoint of a pooled
47   *   connection.
48   * @param <C> the connection type.
49   * @since 4.2
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       * Creates new {@code PoolEntry} instance.
68       *
69       * @param id unique identifier of the pool entry. May be {@code null}.
70       * @param route route to the opposite endpoint.
71       * @param conn the connection.
72       * @param timeToLive maximum time to live. May be zero if the connection
73       *   does not have an expiry deadline.
74       * @param tunit time unit.
75       */
76      public PoolEntry(final String id, final T route, final C conn,
77              final long timeToLive, final TimeUnit tunit) {
78          super();
79          Args.notNull(route, "Route");
80          Args.notNull(conn, "Connection");
81          Args.notNull(tunit, "Time unit");
82          this.id = id;
83          this.route = route;
84          this.conn = conn;
85          this.created = System.currentTimeMillis();
86          if (timeToLive > 0) {
87              this.validityDeadline = this.created + tunit.toMillis(timeToLive);
88          } else {
89              this.validityDeadline = Long.MAX_VALUE;
90          }
91          this.expiry = this.validityDeadline;
92      }
93  
94      /**
95       * Creates new {@code PoolEntry} instance without an expiry deadline.
96       *
97       * @param id unique identifier of the pool entry. May be {@code null}.
98       * @param route route to the opposite endpoint.
99       * @param conn the connection.
100      */
101     public PoolEntry(final String id, final T route, final C conn) {
102         this(id, route, conn, 0, TimeUnit.MILLISECONDS);
103     }
104 
105     public String getId() {
106         return this.id;
107     }
108 
109     public T getRoute() {
110         return this.route;
111     }
112 
113     public C getConnection() {
114         return this.conn;
115     }
116 
117     public long getCreated() {
118         return this.created;
119     }
120 
121     /**
122      * @since 4.4
123      */
124     public long getValidityDeadline() {
125         return this.validityDeadline;
126     }
127 
128     /**
129      * @deprecated use {@link #getValidityDeadline()}
130      */
131     @Deprecated
132     public long getValidUnit() {
133         return this.validityDeadline;
134     }
135 
136     public Object getState() {
137         return this.state;
138     }
139 
140     public void setState(final Object state) {
141         this.state = state;
142     }
143 
144     public synchronized long getUpdated() {
145         return this.updated;
146     }
147 
148     public synchronized long getExpiry() {
149         return this.expiry;
150     }
151 
152     public synchronized void updateExpiry(final long time, final TimeUnit tunit) {
153         Args.notNull(tunit, "Time unit");
154         this.updated = System.currentTimeMillis();
155         final long newExpiry;
156         if (time > 0) {
157             newExpiry = this.updated + tunit.toMillis(time);
158         } else {
159             newExpiry = Long.MAX_VALUE;
160         }
161         this.expiry = Math.min(newExpiry, this.validityDeadline);
162     }
163 
164     public synchronized boolean isExpired(final long now) {
165         return now >= this.expiry;
166     }
167 
168     /**
169      * Invalidates the pool entry and closes the pooled connection associated
170      * with it.
171      */
172     public abstract void close();
173 
174     /**
175      * Returns {@code true} if the pool entry has been invalidated.
176      */
177     public abstract boolean isClosed();
178 
179     @Override
180     public String toString() {
181         final StringBuilder buffer = new StringBuilder();
182         buffer.append("[id:");
183         buffer.append(this.id);
184         buffer.append("][route:");
185         buffer.append(this.route);
186         buffer.append("][state:");
187         buffer.append(this.state);
188         buffer.append("]");
189         return buffer.toString();
190     }
191 
192 }