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