KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > socket > SocketRequestInfo


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

45 package org.exolab.jms.net.socket;
46
47 import org.exolab.jms.net.connector.URIRequestInfo;
48 import org.exolab.jms.net.connector.ResourceException;
49 import org.exolab.jms.net.uri.URI;
50 import org.exolab.jms.net.util.Properties;
51
52
53 /**
54  * Implementation of the {@link org.exolab.jms.net.connector.ConnectionRequestInfo}
55  * interface that enables socket based connectors to pass data across the
56  * connection request flow.
57  *
58  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
59  * @version $Revision: 1.6 $ $Date: 2005/05/03 13:45:59 $
60  */

61 public class SocketRequestInfo extends URIRequestInfo {
62
63     /**
64      * The alternative URI for clients to connect to, if connections to {@link
65      * #getURI} fail. May be <code>null</code>
66      */

67     private URI _alternativeURI = null;
68
69     /**
70      * The maximum queue size for incoming connection indications (a request to
71      * connect). If a connection indication arrives when the queue is full, the
72      * connection is refused.
73      */

74     private int _connectionRequestQueueSize = 50;
75
76     /**
77      * Determines if connections should be accepted on all addresses, on a
78      * multi-homed host. If <code>true</code>, server sockets will accept
79      * connections on all local addresses. If <code>false</code>, only
80      * connections to a specified address will be accepted.
81      */

82     private boolean _bindAll = true;
83
84     /**
85      * The maximum no. of threads used to handle concurrent invocations.
86      */

87     private int _maxThreads = 10;
88
89     /**
90      * Connection property name to indicate the alternative URI to connect on,
91      * if a connection cannot be established to the primary URI.
92      */

93     private static final String JavaDoc ALTERNATIVE_URI = "alternativeURI";
94
95     /**
96      * Connection property name to indicate if connections should be accepted
97      * on all addresses, on a multi-homed host.
98      */

99     private static final String JavaDoc BIND_ALL = "bindAll";
100
101     /**
102      * Connection property name to indicate the maximum no. of threads to uset.
103      */

104     private static final String JavaDoc MAX_THREADS = "maxThreads";
105
106
107     /**
108      * Construct a new <code>SocketRequestInfo</code>.
109      *
110      * @param uri the URI
111      */

112     public SocketRequestInfo(URI uri) {
113         super(uri);
114     }
115
116     /**
117      * Construct a new <code>SocketRequestInfo</code>.
118      *
119      * @param uri the URI
120      * @param properties the properties to populate this from
121      * @throws ResourceException if any of the properties are invalid
122      */

123     public SocketRequestInfo(URI uri, Properties properties)
124             throws ResourceException {
125         super(uri);
126         setAlternativeURI(properties.getURI(ALTERNATIVE_URI));
127         setBindAll(properties.getBoolean(BIND_ALL, _bindAll));
128         setMaxThreads(properties.getInt(MAX_THREADS, _maxThreads));
129     }
130
131     /**
132      * Sets the alternative URI. This is used as an alternative address for
133      * clients to connect to, if connections to {@link #getURI} fail.
134      * <p/>
135      * This can be useful if the server is behind a NAT firewall, and clients
136      * need to connect from both outside and inside the firewall.
137      *
138      * @param uri the alternative URI
139      */

140     public void setAlternativeURI(URI uri) {
141         _alternativeURI = uri;
142     }
143
144     /**
145      * Returns the alternative URI.
146      *
147      * @return the alternative URI, or <code>null</code> if none has been set.
148      */

149     public URI getAlternativeURI() {
150         return _alternativeURI;
151     }
152
153     /**
154      * Sets the maximum queue size for incoming connection indications (a
155      * request to connect). If a connection indication arrives when the queue is
156      * full, the connection is refused.
157      *
158      * @param size the queue size
159      */

160     public void setConnectionRequestQueueSize(int size) {
161         _connectionRequestQueueSize = size;
162     }
163
164     /**
165      * Returns the maximum queue size for incoming connection indications.
166      *
167      * @return the maximum queue size for incoming connection indications.
168      */

169     public int getConnectionRequestQueueSize() {
170         return _connectionRequestQueueSize;
171     }
172
173     /**
174      * Sets how socket connections should be accepted, on a multi-homed host.
175      *
176      * @param bindAll if <code>true</code>, server sockets will accept
177      * connections on all local addresses. If <code>false</code>,
178      * only connections to a specified address will be accepted.
179      */

180     public void setBindAll(boolean bindAll) {
181         _bindAll = bindAll;
182     }
183
184     /**
185      * Determines if socket connections should be accepted on all addresses, on
186      * a multi-homed host.
187      *
188      * @return <code>true</code> if server sockets should accept connections on
189      * all local addresses; otherwise <code>false</code>, indicating
190      * that only connections to a specified address will be accepted.
191      */

192     public boolean getBindAll() {
193         return _bindAll;
194     }
195
196     /**
197      * Set the maximum no. of threads that may be allocated to handle concurrent
198      * invocations.
199      *
200      * @param count the maximum no. of threads to allocate
201      */

202     public void setMaxThreads(int count) {
203         _maxThreads = count;
204     }
205
206     /**
207      * Returns the maximum no. of threads that may be allocated to handle
208      * concurrent invocations.
209      *
210      * @return the maximum no. of threads to allocate
211      */

212     public int getMaxThreads() {
213         return _maxThreads;
214     }
215
216     /**
217      * Helper to export this to a {@link Properties} instance.
218      *
219      * @param properties the properties to export to.
220      */

221     public void export(Properties properties) {
222         super.export(properties);
223         properties.setNonNull(ALTERNATIVE_URI, getAlternativeURI());
224         properties.set(BIND_ALL, getBindAll());
225         properties.set(MAX_THREADS, getMaxThreads());
226     }
227
228     /**
229      * Checks whether this instance is equal to another.
230      *
231      * @param other the object to compare
232      * @return <code>true</code> if the two instances are equal; otherwise
233      * <code>false</code>
234      */

235     public boolean equals(Object JavaDoc other) {
236         boolean equal = false;
237         if (other instanceof SocketRequestInfo && super.equals(other)) {
238             SocketRequestInfo info = (SocketRequestInfo) other;
239             if (equals(_alternativeURI, info._alternativeURI)
240                     && _connectionRequestQueueSize
241                     == info._connectionRequestQueueSize
242                     && _bindAll == info._bindAll
243                     && _maxThreads == info._maxThreads) {
244                 equal = true;
245             }
246         }
247         return equal;
248     }
249
250     /**
251      * Helper to compare two objects for equality.
252      *
253      * @param o1 the first object to compare
254      * @param o2 the second object to compare
255      * @return <code>true</code> if the objects are equal, otherwise
256      * <code>false</code>
257      */

258     protected boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
259         boolean equal = (o1 == null && o2 == null);
260         if (!equal) {
261             if (o1 != null && o1.equals(o2)) {
262                 equal = true;
263             }
264         }
265         return equal;
266     }
267
268 }
269
Popular Tags