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