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