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      @Override
65      public SocketAddress getAddress() {
66          return this.address;
67      }
68  
69      public boolean isCompleted() {
70          return this.completed;
71      }
72  
73      @Override
74      public IOException getException() {
75          return this.exception;
76      }
77  
78      @Override
79      public void waitFor() throws InterruptedException {
80          if (this.completed) {
81              return;
82          }
83          synchronized (this) {
84              while (!this.completed) {
85                  wait();
86              }
87          }
88      }
89  
90      public void completed(final SocketAddress address) {
91          Args.notNull(address, "Address");
92          if (this.completed) {
93              return;
94          }
95          this.completed = true;
96          synchronized (this) {
97              this.address = address;
98              notifyAll();
99          }
100     }
101 
102     public void failed(final IOException exception) {
103         if (exception == null) {
104             return;
105         }
106         if (this.completed) {
107             return;
108         }
109         this.completed = true;
110         synchronized (this) {
111             this.exception = exception;
112             notifyAll();
113         }
114     }
115 
116     public void cancel() {
117         if (this.completed) {
118             return;
119         }
120         this.completed = true;
121         this.closed = true;
122         synchronized (this) {
123             notifyAll();
124         }
125     }
126 
127     protected void setKey(final SelectionKey key) {
128         this.key = key;
129     }
130 
131     @Override
132     public boolean isClosed() {
133         return this.closed || (this.key != null && !this.key.isValid());
134     }
135 
136     @Override
137     public void close() {
138         if (this.closed) {
139             return;
140         }
141         this.completed = true;
142         this.closed = true;
143         if (this.key != null) {
144             this.key.cancel();
145             final Channel channel = this.key.channel();
146             try {
147                 channel.close();
148             } catch (final IOException ignore) {}
149         }
150         if (this.callback != null) {
151             this.callback.endpointClosed(this);
152         }
153         synchronized (this) {
154             notifyAll();
155         }
156     }
157 
158 }