socketrequestinfo.java

来自「一个java方面的消息订阅发送的源码」· Java 代码 · 共 269 行

JAVA
269
字号
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: SocketRequestInfo.java,v 1.6 2005/05/03 13:45:59 tanderson Exp $
 */
package org.exolab.jms.net.socket;

import org.exolab.jms.net.connector.URIRequestInfo;
import org.exolab.jms.net.connector.ResourceException;
import org.exolab.jms.net.uri.URI;
import org.exolab.jms.net.util.Properties;


/**
 * Implementation of the {@link org.exolab.jms.net.connector.ConnectionRequestInfo}
 * interface that enables socket based connectors to pass data across the
 * connection request flow.
 *
 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
 * @version $Revision: 1.6 $ $Date: 2005/05/03 13:45:59 $
 */
public class SocketRequestInfo extends URIRequestInfo {

    /**
     * The alternative URI for clients to connect to, if connections to {@link
     * #getURI} fail. May be <code>null</code>
     */
    private URI _alternativeURI = null;

    /**
     * The maximum queue size for incoming connection indications (a request to
     * connect). If a connection indication arrives when the queue is full, the
     * connection is refused.
     */
    private int _connectionRequestQueueSize = 50;

    /**
     * Determines if connections should be accepted on all addresses, on a
     * multi-homed host. If <code>true</code>, server sockets will accept
     * connections on all local addresses. If <code>false</code>, only
     * connections to a specified address will be accepted.
     */
    private boolean _bindAll = true;

    /**
     * The maximum no. of threads used to handle concurrent invocations.
     */
    private int _maxThreads = 10;

    /**
     * Connection property name to indicate the alternative URI to connect on,
     * if a connection cannot be established to the primary URI.
     */
    private static final String ALTERNATIVE_URI = "alternativeURI";

    /**
     * Connection property name to indicate if connections should be accepted
     * on all addresses, on a multi-homed host.
     */
    private static final String BIND_ALL = "bindAll";

    /**
     * Connection property name to indicate the maximum no. of threads to uset.
     */
    private static final String MAX_THREADS = "maxThreads";


    /**
     * Construct a new <code>SocketRequestInfo</code>.
     *
     * @param uri the URI
     */
    public SocketRequestInfo(URI uri) {
        super(uri);
    }

    /**
     * Construct a new <code>SocketRequestInfo</code>.
     *
     * @param uri the URI
     * @param properties the properties to populate this from
     * @throws ResourceException if any of the properties are invalid
     */
    public SocketRequestInfo(URI uri, Properties properties)
            throws ResourceException {
        super(uri);
        setAlternativeURI(properties.getURI(ALTERNATIVE_URI));
        setBindAll(properties.getBoolean(BIND_ALL, _bindAll));
        setMaxThreads(properties.getInt(MAX_THREADS, _maxThreads));
    }

    /**
     * Sets the alternative URI. This is used as an alternative address for
     * clients to connect to, if connections to {@link #getURI} fail.
     * <p/>
     * This can be useful if the server is behind a NAT firewall, and clients
     * need to connect from both outside and inside the firewall.
     *
     * @param uri the alternative URI
     */
    public void setAlternativeURI(URI uri) {
        _alternativeURI = uri;
    }

    /**
     * Returns the alternative URI.
     *
     * @return the alternative URI, or <code>null</code> if none has been set.
     */
    public URI getAlternativeURI() {
        return _alternativeURI;
    }

    /**
     * Sets the maximum queue size for incoming connection indications (a
     * request to connect). If a connection indication arrives when the queue is
     * full, the connection is refused.
     *
     * @param size the queue size
     */
    public void setConnectionRequestQueueSize(int size) {
        _connectionRequestQueueSize = size;
    }

    /**
     * Returns the maximum queue size for incoming connection indications.
     *
     * @return the maximum queue size for incoming connection indications.
     */
    public int getConnectionRequestQueueSize() {
        return _connectionRequestQueueSize;
    }

    /**
     * Sets how socket connections should be accepted, on a multi-homed host.
     *
     * @param bindAll if <code>true</code>, server sockets will accept
     *                connections on all local addresses. If <code>false</code>,
     *                only connections to a specified address will be accepted.
     */
    public void setBindAll(boolean bindAll) {
        _bindAll = bindAll;
    }

    /**
     * Determines if socket connections should be accepted on all addresses, on
     * a multi-homed host.
     *
     * @return <code>true</code> if server sockets should accept connections on
     *         all local addresses; otherwise <code>false</code>, indicating
     *         that only connections to a specified address will be accepted.
     */
    public boolean getBindAll() {
        return _bindAll;
    }

    /**
     * Set the maximum no. of threads that may be allocated to handle concurrent
     * invocations.
     *
     * @param count the maximum no. of threads to allocate
     */
    public void setMaxThreads(int count) {
        _maxThreads = count;
    }

    /**
     * Returns the maximum no. of threads that may be allocated to handle
     * concurrent invocations.
     *
     * @return the maximum no. of threads to allocate
     */
    public int getMaxThreads() {
        return _maxThreads;
    }

    /**
     * Helper to export this to a {@link Properties} instance.
     *
     * @param properties the properties to export to.
     */
    public void export(Properties properties) {
        super.export(properties);
        properties.setNonNull(ALTERNATIVE_URI, getAlternativeURI());
        properties.set(BIND_ALL, getBindAll());
        properties.set(MAX_THREADS, getMaxThreads());
    }

    /**
     * Checks whether this instance is equal to another.
     *
     * @param other the object to compare
     * @return <code>true</code> if the two instances are equal; otherwise
     *         <code>false</code>
     */
    public boolean equals(Object other) {
        boolean equal = false;
        if (other instanceof SocketRequestInfo && super.equals(other)) {
            SocketRequestInfo info = (SocketRequestInfo) other;
            if (equals(_alternativeURI, info._alternativeURI)
                    && _connectionRequestQueueSize
                    == info._connectionRequestQueueSize
                    && _bindAll == info._bindAll
                    && _maxThreads == info._maxThreads) {
                equal = true;
            }
        }
        return equal;
    }

    /**
     * Helper to compare two objects for equality.
     *
     * @param o1 the first object to compare
     * @param o2 the second object to compare
     * @return <code>true</code> if the objects are equal, otherwise
     *         <code>false</code>
     */
    protected boolean equals(Object o1, Object o2) {
        boolean equal = (o1 == null && o2 == null);
        if (!equal) {
            if (o1 != null && o1.equals(o2)) {
                equal = true;
            }
        }
        return equal;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?