View Javadoc

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 org.apache.http.impl.conn.tsccm;
28  
29  import java.lang.ref.ReferenceQueue;
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.http.conn.OperatedClientConnection;
33  import org.apache.http.conn.ClientConnectionOperator;
34  import org.apache.http.conn.routing.HttpRoute;
35  import org.apache.http.impl.conn.AbstractPoolEntry;
36  
37  /**
38   * Basic implementation of a connection pool entry.
39   *
40   * @since 4.0
41   *
42   * @deprecated (4.2)  use {@link org.apache.http.pool.PoolEntry}
43   */
44  @Deprecated
45  public class BasicPoolEntry extends AbstractPoolEntry {
46  
47      private final long created;
48  
49      private long updated;
50      private long validUntil;
51      private long expiry;
52  
53      public BasicPoolEntry(ClientConnectionOperator op,
54                            HttpRoute route,
55                            ReferenceQueue<Object> queue) {
56          super(op, route);
57          if (route == null) {
58              throw new IllegalArgumentException("HTTP route may not be null");
59          }
60          this.created = System.currentTimeMillis();
61          this.validUntil = Long.MAX_VALUE;
62          this.expiry = this.validUntil;
63      }
64  
65      /**
66       * Creates a new pool entry.
67       *
68       * @param op      the connection operator
69       * @param route   the planned route for the connection
70       */
71      public BasicPoolEntry(ClientConnectionOperator op,
72                            HttpRoute route) {
73          this(op, route, -1, TimeUnit.MILLISECONDS);
74      }
75  
76      /**
77       * Creates a new pool entry with a specified maximum lifetime.
78       *
79       * @param op        the connection operator
80       * @param route     the planned route for the connection
81       * @param connTTL   maximum lifetime of this entry, <=0 implies "infinity"
82       * @param timeunit  TimeUnit of connTTL
83       *
84       * @since 4.1
85       */
86      public BasicPoolEntry(ClientConnectionOperator op,
87                            HttpRoute route, long connTTL, TimeUnit timeunit) {
88          super(op, route);
89          if (route == null) {
90              throw new IllegalArgumentException("HTTP route may not be null");
91          }
92          this.created = System.currentTimeMillis();
93          if (connTTL > 0) {
94              this.validUntil = this.created + timeunit.toMillis(connTTL);
95          } else {
96              this.validUntil = Long.MAX_VALUE;
97          }
98          this.expiry = this.validUntil;
99      }
100 
101     protected final OperatedClientConnection getConnection() {
102         return super.connection;
103     }
104 
105     protected final HttpRoute getPlannedRoute() {
106         return super.route;
107     }
108 
109     protected final BasicPoolEntryRef getWeakRef() {
110         return null;
111     }
112 
113     @Override
114     protected void shutdownEntry() {
115         super.shutdownEntry();
116     }
117 
118     /**
119      * @since 4.1
120      */
121     public long getCreated() {
122         return this.created;
123     }
124 
125     /**
126      * @since 4.1
127      */
128     public long getUpdated() {
129         return this.updated;
130     }
131 
132     /**
133      * @since 4.1
134      */
135     public long getExpiry() {
136         return this.expiry;
137     }
138 
139     public long getValidUntil() {
140         return this.validUntil;
141     }
142 
143     /**
144      * @since 4.1
145      */
146     public void updateExpiry(long time, TimeUnit timeunit) {
147         this.updated = System.currentTimeMillis();
148         long newExpiry;
149         if (time > 0) {
150             newExpiry = this.updated + timeunit.toMillis(time);
151         } else {
152             newExpiry = Long.MAX_VALUE;
153         }
154         this.expiry = Math.min(validUntil, newExpiry);
155     }
156 
157     /**
158      * @since 4.1
159      */
160     public boolean isExpired(long now) {
161         return now >= this.expiry;
162     }
163 
164 }
165 
166