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