comparison src/com/go/trove/net/InetAddressResolver.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
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 import java.lang.ref.*;
58 import com.go.trove.util.SoftHashMap;
59
60 /******************************************************************************
61 * Allows network host names to resolve to inet addresses via event
62 * notification.
63 * <p>
64 * Note: This class makes use of the java.util.Timer class, available only in
65 * Java 2, version 1.3.
66 *
67 * @author Brian S O'Neill
68 * @version
69 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 01/01/23 <!-- $-->
70 */
71 public class InetAddressResolver {
72 // Wait for 10 minutes before trying to resolve again.
73 private static final long RESOLVE_PERIOD = 10 * 60 * 1000;
74 private static Timer cResolveTimer;
75
76 private static Map cResolvers = new SoftHashMap();
77
78 private static Timer getResolveTimer() {
79 Timer timer = cResolveTimer;
80 if (timer == null) {
81 timer = new Timer(true);
82 timer.schedule(new TimerTask() {
83 public void run() {
84 Thread.currentThread().setName("InetAddressResolver");
85 }
86 }, 0);
87 cResolveTimer = timer;
88 }
89 return timer;
90 }
91
92 /**
93 * Resolve the host name into InetAddresses and listen for changes.
94 * The caller must save a reference to the resolver to prevent it from
95 * being reclaimed by the garbage collector.
96 *
97 * @param host host to resolve into InetAddresses
98 * @param listener listens for InetAddresses
99 */
100 public static InetAddressResolver listenFor(String host,
101 InetAddressListener listener) {
102 synchronized (cResolvers) {
103 InetAddressResolver resolver =
104 (InetAddressResolver)cResolvers.get(host);
105 if (resolver == null) {
106 resolver = new InetAddressResolver(host);
107 cResolvers.put(host, resolver);
108 }
109 resolver.addListener(listener);
110 return resolver;
111 }
112 }
113
114 private final String mHost;
115
116 private List mListeners = new ArrayList();
117
118 // Is either an instance of UnknownHostException or InetAddress[].
119 private Object mResolutionResults;
120
121 private InetAddressResolver(String host) {
122 mHost = host;
123
124 // Initial delay is random so that requests against other hosts are
125 // staggered.
126 long delay = (long)(Math.random() * RESOLVE_PERIOD);
127 getResolveTimer().schedule(new Resolver(this), delay, RESOLVE_PERIOD);
128 }
129
130 private synchronized void addListener(InetAddressListener listener) {
131 mListeners.add(listener);
132 if (!resolveAddresses()) {
133 // Ensure that new listener is called the first time.
134 notifyListener(listener);
135 }
136 }
137
138 private synchronized void notifyListener(InetAddressListener listener) {
139 if (mResolutionResults instanceof UnknownHostException) {
140 listener.unknown((UnknownHostException)mResolutionResults);
141 }
142 else {
143 InetAddress[] addresses = (InetAddress[])mResolutionResults;
144 addresses = (InetAddress[])addresses.clone();
145 listener.resolved(addresses);
146 }
147 }
148
149 // Returns true if anything changed from the last time this was invoked.
150 private synchronized boolean resolveAddresses() {
151 boolean changed;
152 try {
153 InetAddress[] addresses = InetAddress.getAllByName(mHost);
154 if (mResolutionResults instanceof UnknownHostException) {
155 changed = true;
156 }
157 else {
158 // Results may be ordered differently, so keep them sorted.
159 Arrays.sort(addresses, new Comparator() {
160 public int compare(Object a, Object b) {
161 return ((InetAddress)a).getHostAddress()
162 .compareTo(((InetAddress)b).getHostAddress());
163 }
164 });
165
166 changed = !Arrays.equals
167 (addresses, (InetAddress[])mResolutionResults);
168 }
169 mResolutionResults = addresses;
170 }
171 catch (UnknownHostException e) {
172 changed = !(mResolutionResults instanceof UnknownHostException);
173 mResolutionResults = e;
174 }
175
176 if (changed) {
177 int size = mListeners.size();
178 for (int i=0; i<size; i++) {
179 notifyListener((InetAddressListener)mListeners.get(i));
180 }
181 }
182
183 return changed;
184 }
185
186 private class Resolver extends TimerTask {
187 // Weakly references owner so that the timer won't prevent it from
188 // being garbage collected.
189 private final Reference mOwner;
190
191 public Resolver(InetAddressResolver owner) {
192 mOwner = new WeakReference(owner);
193 }
194
195 public void run() {
196 InetAddressResolver owner = (InetAddressResolver)mOwner.get();
197 if (owner == null) {
198 cancel();
199 }
200 Thread t = Thread.currentThread();
201 String originalName = t.getName();
202 t.setName("InetAddressResolver:" + owner.mHost);
203 try {
204 owner.resolveAddresses();
205 }
206 finally {
207 t.setName(originalName);
208 }
209 }
210 }
211 }