001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.nio.reactor;
029
030import java.nio.channels.SocketChannel;
031
032import org.apache.http.util.Args;
033
034/**
035 * {@link SocketChannel} entry maintained by the I/O reactor. If the channel
036 *  represents an outgoing client connection, this entry also contains the
037 *  original {@link SessionRequestImpl} used to request it.
038 *
039 * @since 4.0
040 */
041public class ChannelEntry {
042
043    private final SocketChannel channel;
044    private final SessionRequestImpl sessionRequest;
045
046    /**
047     * Creates new ChannelEntry.
048     *
049     * @param channel the channel
050     * @param sessionRequest original session request. Can be {@code null}
051     *   if the channel represents an incoming server-side connection.
052     */
053    public ChannelEntry(final SocketChannel channel, final SessionRequestImpl sessionRequest) {
054        super();
055        Args.notNull(channel, "Socket channel");
056        this.channel = channel;
057        this.sessionRequest = sessionRequest;
058    }
059
060    /**
061     * Creates new ChannelEntry.
062     *
063     * @param channel the channel.
064     */
065    public ChannelEntry(final SocketChannel channel) {
066        this(channel, null);
067    }
068
069    /**
070     * Returns the original session request, if available. If the channel
071     * entry represents an incoming server-side connection, returns
072     * {@code null}.
073     *
074     * @return the original session request, if client-side channel,
075     *  {@code null} otherwise.
076     */
077    public SessionRequestImpl getSessionRequest() {
078        return this.sessionRequest;
079    }
080
081    /**
082     * Returns the original session request attachment, if available.
083     *
084     * @return the original session request attachment, if available,
085     *  {@code null} otherwise.
086     */
087    public Object getAttachment() {
088        if (this.sessionRequest != null) {
089            return this.sessionRequest.getAttachment();
090        } else {
091            return null;
092        }
093    }
094
095    /**
096     * Returns the channel.
097     *
098     * @return the channel.
099     */
100    public SocketChannel getChannel() {
101        return this.channel;
102    }
103
104}