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  
28  package org.apache.http.impl.nio.reactor;
29  
30  import java.io.IOException;
31  import java.net.SocketAddress;
32  import java.nio.channels.Channel;
33  import java.nio.channels.SelectionKey;
34  
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.nio.reactor.ListenerEndpoint;
38  import org.apache.http.util.Args;
39  
40  /**
41   * Default implementation of {@link ListenerEndpoint}.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
46  public class ListenerEndpointImpl implements ListenerEndpoint {
47  
48      private volatile boolean completed;
49      private volatile boolean closed;
50      private volatile SelectionKey key;
51      private volatile SocketAddress address;
52      private volatile IOException exception;
53  
54      private final ListenerEndpointClosedCallback callback;
55  
56      public ListenerEndpointImpl(
57              final SocketAddress address,
58              final ListenerEndpointClosedCallback callback) {
59          super();
60          Args.notNull(address, "Address");
61          this.address = address;
62          this.callback = callback;
63      }
64  
65      @Override
66      public SocketAddress getAddress() {
67          return this.address;
68      }
69  
70      public boolean isCompleted() {
71          return this.completed;
72      }
73  
74      @Override
75      public IOException getException() {
76          return this.exception;
77      }
78  
79      @Override
80      public void waitFor() throws InterruptedException {
81          if (this.completed) {
82              return;
83          }
84          synchronized (this) {
85              while (!this.completed) {
86                  wait();
87              }
88          }
89      }
90  
91      public void completed(final SocketAddress address) {
92          Args.notNull(address, "Address");
93          if (this.completed) {
94              return;
95          }
96          this.completed = true;
97          synchronized (this) {
98              this.address = address;
99              notifyAll();
100         }
101     }
102 
103     public void failed(final IOException exception) {
104         if (exception == null) {
105             return;
106         }
107         if (this.completed) {
108             return;
109         }
110         this.completed = true;
111         synchronized (this) {
112             this.exception = exception;
113             notifyAll();
114         }
115     }
116 
117     public void cancel() {
118         if (this.completed) {
119             return;
120         }
121         this.completed = true;
122         this.closed = true;
123         synchronized (this) {
124             notifyAll();
125         }
126     }
127 
128     protected void setKey(final SelectionKey key) {
129         this.key = key;
130     }
131 
132     @Override
133     public boolean isClosed() {
134         return this.closed || (this.key != null && !this.key.isValid());
135     }
136 
137     @Override
138     public void close() {
139         if (this.closed) {
140             return;
141         }
142         this.completed = true;
143         this.closed = true;
144         if (this.key != null) {
145             this.key.cancel();
146             final Channel channel = this.key.channel();
147             try {
148                 channel.close();
149             } catch (final IOException ignore) {}
150         }
151         if (this.callback != null) {
152             this.callback.endpointClosed(this);
153         }
154         synchronized (this) {
155             notifyAll();
156         }
157     }
158 
159     @Override
160     public String toString() {
161         return "[address=" + address + ", key=" + key + ", closed=" + closed + ", completed="
162                 + completed + ", exception=" + exception + ", callback=" + callback + "]";
163     }
164 
165 }