01: /*
02: * <copyright>
03: *
04: * Copyright 2002-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.wp;
28:
29: import java.io.UnsupportedEncodingException;
30: import java.net.URLEncoder;
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.Collections;
34: import java.util.HashSet;
35: import java.util.Iterator;
36: import java.util.List;
37: import java.util.Set;
38: import org.cougaar.core.service.wp.WhitePagesService;
39:
40: /**
41: * A <i>non-scalable</i> utility class to recursively find all agents
42: * in the white pages.
43: * <p>
44: * Not scalable, so the methods of this class are deprecated.
45: */
46: public class ListAllAgents {
47:
48: /**
49: * Get a Set of all agent names.
50: * <p>
51: * @deprecated not scalable!
52: */
53: public static Set listAllAgents(WhitePagesService wps)
54: throws Exception {
55: Set toSet = new HashSet();
56: recurse(toSet, wps, ".", 0);
57: return toSet;
58: }
59:
60: /**
61: * URLEncode and sort a set of Strings.
62: * <p>
63: * @deprecated only for "listAllAgents" use
64: */
65: public static List encodeAndSort(Set s) {
66: // URLEncode the names and sort
67: ArrayList l = new ArrayList(s);
68: Collections.sort(l);
69: for (int i = 0, n = l.size(); i < n; i++) {
70: String tmp = (String) l.get(i);
71: try {
72: tmp = URLEncoder.encode(tmp, "UTF-8");
73: } catch (UnsupportedEncodingException uee) {
74: throw new RuntimeException("No UTF-8?", uee);
75: }
76: l.set(i, tmp);
77: }
78: return l;
79: }
80:
81: // recursive!
82: private static void recurse(Set toSet, WhitePagesService wps,
83: String suffix, long timeout) throws Exception {
84: Set names = wps.list(suffix, timeout);
85: for (Iterator iter = names.iterator(); iter.hasNext();) {
86: String s = (String) iter.next();
87: if (s == null) {
88: } else if (s.length() > 0 && s.charAt(0) == '.') {
89: recurse(toSet, wps, s, timeout);
90: } else {
91: toSet.add(s);
92: }
93: }
94: }
95: }
|