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.nio.channels.SocketChannel;
31  
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.util.Args;
34  
35  /**
36   * {@link SocketChannel} entry maintained by the I/O reactor. If the channel
37   *  represents an outgoing client connection, this entry also contains the
38   *  original {@link SessionRequestImpl} used to request it.
39   *
40   * @since 4.0
41   */
42  @Immutable
43  public class ChannelEntry {
44  
45      private final SocketChannel channel;
46      private final SessionRequestImpl sessionRequest;
47  
48      /**
49       * Creates new ChannelEntry.
50       *
51       * @param channel the channel
52       * @param sessionRequest original session request. Can be {@code null}
53       *   if the channel represents an incoming server-side connection.
54       */
55      public ChannelEntry(final SocketChannel channel, final SessionRequestImpl sessionRequest) {
56          super();
57          Args.notNull(channel, "Socket channel");
58          this.channel = channel;
59          this.sessionRequest = sessionRequest;
60      }
61  
62      /**
63       * Creates new ChannelEntry.
64       *
65       * @param channel the channel.
66       */
67      public ChannelEntry(final SocketChannel channel) {
68          this(channel, null);
69      }
70  
71      /**
72       * Returns the original session request, if available. If the channel
73       * entry represents an incoming server-side connection, returns
74       * {@code null}.
75       *
76       * @return the original session request, if client-side channel,
77       *  {@code null} otherwise.
78       */
79      public SessionRequestImpl getSessionRequest() {
80          return this.sessionRequest;
81      }
82  
83      /**
84       * Returns the original session request attachment, if available.
85       *
86       * @return the original session request attachment, if available,
87       *  {@code null} otherwise.
88       */
89      public Object getAttachment() {
90          if (this.sessionRequest != null) {
91              return this.sessionRequest.getAttachment();
92          } else {
93              return null;
94          }
95      }
96  
97      /**
98       * Returns the channel.
99       *
100      * @return the channel.
101      */
102     public SocketChannel getChannel() {
103         return this.channel;
104     }
105 
106 }