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.nio.pool;
28  
29  import org.apache.http.annotation.Immutable;
30  import org.apache.http.concurrent.BasicFuture;
31  import org.apache.http.pool.PoolEntry;
32  
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  @Immutable
36  class LeaseRequest<T, C, E extends PoolEntry<T, C>> {
37  
38      private final T route;
39      private final Object state;
40      private final long connectTimeout;
41      private final long deadline;
42      private final BasicFuture<E> future;
43      private final AtomicBoolean completed;
44      private volatile E result;
45      private volatile Exception ex;
46  
47      /**
48       * Contructor
49       * @param route route
50       * @param state state
51       * @param connectTimeout http connection timeout
52       * @param leaseTimeout timeout to wait in a request queue until kicked off
53       * @param future future callback
54       */
55      public LeaseRequest(
56              final T route,
57              final Object state,
58              final long connectTimeout,
59              final long leaseTimeout,
60              final BasicFuture<E> future) {
61          super();
62          this.route = route;
63          this.state = state;
64          this.connectTimeout = connectTimeout;
65          this.deadline = leaseTimeout > 0 ? System.currentTimeMillis() + leaseTimeout :
66                  Long.MAX_VALUE;
67          this.future = future;
68          this.completed = new AtomicBoolean(false);
69      }
70  
71      public T getRoute() {
72          return this.route;
73      }
74  
75      public Object getState() {
76          return this.state;
77      }
78  
79      public long getConnectTimeout() {
80          return this.connectTimeout;
81      }
82  
83      public long getDeadline() {
84          return this.deadline;
85      }
86  
87      public boolean isDone() {
88          return this.completed.get();
89      }
90  
91      public void failed(final Exception ex) {
92          if (this.completed.compareAndSet(false, true)) {
93              this.ex = ex;
94          }
95      }
96  
97      public void completed(final E result) {
98          if (this.completed.compareAndSet(false, true)) {
99              this.result = result;
100         }
101     }
102 
103     public BasicFuture<E> getFuture() {
104         return this.future;
105     }
106 
107     public E getResult() {
108         return this.result;
109     }
110 
111     public Exception getException() {
112         return this.ex;
113     }
114 
115     @Override
116     public String toString() {
117         final StringBuilder buffer = new StringBuilder();
118         buffer.append("[");
119         buffer.append(this.route);
120         buffer.append("][");
121         buffer.append(this.state);
122         buffer.append("]");
123         return buffer.toString();
124     }
125 
126 }