KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > net > MultiPooledSocketFactory


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.net;
54
55 import java.net.*;
56 import java.util.*;
57
58 /******************************************************************************
59  * Pooled SocketFactory implementation that connects to multiple hosts that may
60  * resolve to multiple InetAddresses. If running under Java 2, version 1.3,
61  * changes in the address resolution are automatically detected using
62  * {@link InetAddressResolver}.
63  * <p>
64  * Consider wrapping with a {@link LazySocketFactory} for automatic checking
65  * against socket factories that may be dead.
66  *
67  * @author Brian S O'Neill
68  * @version
69  * <!--$$Revision:--> 7 <!-- $-->, <!--$$JustDate:--> 00/12/12 <!-- $-->
70  */

71 public class MultiPooledSocketFactory extends DistributedSocketFactory {
72     // Just references InetAddressResolvers to keep them from going away.
73
private Object JavaDoc[] mResolvers;
74
75     /**
76      * @param hosts hosts to connect to; length matches ports
77      * @param ports ports to connect to; length matches hosts
78      * @param timeout Maximum time to wait (in milliseconds) for new
79      * connections to be established before throwing an exception
80      */

81     public MultiPooledSocketFactory(String JavaDoc[] hosts,
82                                     int[] ports,
83                                     long timeout) {
84         super(timeout);
85
86         try {
87             mResolvers = new InetAddressResolver[hosts.length];
88             for (int i=0; i<hosts.length; i++) {
89                 Listener listener = new Listener(ports[i], timeout);
90                 mResolvers[i] =
91                     InetAddressResolver.listenFor(hosts[i], listener);
92             }
93         }
94         catch (NoClassDefFoundError JavaDoc e) {
95             // Timer class probably wasn't found, so InetAddressResolver
96
// cannot be used.
97
mResolvers = null;
98             for (int i=0; i<hosts.length; i++) {
99                 Listener listener = new Listener(ports[i], timeout);
100                 try {
101                     listener.resolved(InetAddress.getAllByName(hosts[i]));
102                 }
103                 catch (UnknownHostException e2) {
104                     listener.unknown(e2);
105                 }
106             }
107         }
108     }
109
110     /**
111      * Create socket factories for newly resolved addresses. Default
112      * implementation returns a LazySocketFactory wrapping a
113      * PooledSocketFactory wrapping a PlainSocketFactory.
114      */

115     protected SocketFactory createSocketFactory(InetAddress address,
116                                                 int port, long timeout)
117     {
118         SocketFactory factory;
119         factory = new PlainSocketFactory(address, port, timeout);
120         factory = new PooledSocketFactory(factory);
121         factory = new LazySocketFactory(factory);
122         return factory;
123     }
124
125     private class Listener implements InetAddressListener {
126         private final int mPort;
127         private final long mTimeout;
128
129         // Maps InetAddress objects to SocketFactories.
130
private Map mAddressedFactories;
131     
132         public Listener(int port, long timeout) {
133             mPort = port;
134             mTimeout = timeout;
135             mAddressedFactories = new HashMap();
136         }
137
138         public void unknown(UnknownHostException e) {
139             Thread JavaDoc t = Thread.currentThread();
140             t.getThreadGroup().uncaughtException(t, e);
141
142             // Remove all the addressed factories.
143
Iterator it = mAddressedFactories.keySet().iterator();
144             while (it.hasNext()) {
145                 SocketFactory factory =
146                     (SocketFactory)mAddressedFactories.get(it.next());
147                 removeSocketFactory(factory);
148             }
149         }
150         
151         public void resolved(InetAddress[] addresses) {
152             // Add newly discovered addresses.
153
for (int i=0; i<addresses.length; i++) {
154                 InetAddress address = addresses[i];
155                 if (!mAddressedFactories.containsKey(address)) {
156                     SocketFactory factory =
157                         createSocketFactory(address, mPort, mTimeout);
158                     mAddressedFactories.put(address, factory);
159                     addSocketFactory(factory);
160                 }
161             }
162             
163             // Remove addresses no longer being routed to.
164
Iterator it = mAddressedFactories.keySet().iterator();
165         mainLoop:
166             while (it.hasNext()) {
167                 InetAddress address = (InetAddress)it.next();
168                 for (int i=0; i<addresses.length; i++) {
169                     if (addresses[i].equals(address)) {
170                         continue mainLoop;
171                     }
172                 }
173                 SocketFactory factory =
174                     (SocketFactory)mAddressedFactories.get(address);
175                 removeSocketFactory(factory);
176             }
177         }
178     }
179 }
180
Popular Tags