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