comparison src/org/dancres/jini/tools/DumpRegistry.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 package org.dancres.jini.tools;
2
3 import net.jini.core.discovery.LookupLocator;
4 import net.jini.core.lookup.ServiceRegistrar;
5 import net.jini.discovery.DiscoveryEvent;
6 import net.jini.discovery.DiscoveryListener;
7 import net.jini.discovery.LookupDiscovery;
8 import net.jini.discovery.LookupDiscoveryManager;
9
10 import org.dancres.jini.util.DiscoveryUtil;
11
12 import java.io.IOException;
13 import java.rmi.RMISecurityManager;
14 import java.rmi.RemoteException;
15 import java.util.ArrayList;
16
17 /**
18 This is an example of a simple lookup via multicast. It gets of rid of
19 the need to be aware of static lookup information because we can "discover"
20 lookup services dynamically at startup. This basic implementation has
21 one fault which is that it only scans each LookupService it finds once
22 for service matches (see the comments in DiscoveryListenerImpl). We could
23 go to the trouble of fixing this all ourselves but there's an easier way -
24 ServiceDiscoveryManager.
25 */
26 public class DumpRegistry {
27 public static void main(String args[]) throws Exception {
28 /*
29 Gotta do this to enable remote class downloading
30 */
31 System.setSecurityManager(new RMISecurityManager());
32
33 /*
34 We're going to look for all ServiceRegistrar instances in all
35 groups - everything we can find via a multicast
36 */
37 String[] myGroups = LookupDiscovery.ALL_GROUPS;
38 LookupLocator[] myLocators = new LookupLocator[0];
39
40 if (args.length != 0) {
41 ArrayList mySpecGroups = new ArrayList();
42 ArrayList mySpecLocs = new ArrayList();
43
44 for (int i = 0; i < args.length; i++) {
45 if (args[i].startsWith("jini:")) {
46 mySpecLocs.add(new LookupLocator(args[i]));
47 } else {
48 mySpecGroups.add(args[i]);
49 }
50 }
51
52 myGroups = new String[mySpecGroups.size()];
53 myGroups = (String[]) mySpecGroups.toArray(myGroups);
54
55 myLocators = new LookupLocator[mySpecLocs.size()];
56 myLocators = (LookupLocator[]) mySpecLocs.toArray(myLocators);
57 }
58
59 try {
60 LookupDiscoveryManager myDiscovery =
61 new LookupDiscoveryManager(myGroups, myLocators,
62 new DiscoveryListenerImpl());
63 } catch (IOException anIOE) {
64 System.err.println("Couldn't setup LookupDiscovery - exiting");
65 anIOE.printStackTrace(System.err);
66 System.exit(-1);
67 }
68
69 /*
70 The thing we need to remember about this kind of discovery is
71 that things take *time*. Locating a ServiceRegistrar doesn't happen
72 immediately and is actually being done *asynchronously* (on another
73 thread). So, we wait a while or, in this case, forever. Note
74 that, depending on implementation, this might not be necessary.
75 It may be enough to have an active LookupDiscovery instance if it
76 uses normal threads. However, we assume it uses all daemon threads.
77 */
78 try {
79 Object myLock = new Object();
80
81 synchronized(myLock) {
82 myLock.wait(0);
83 }
84 } catch (InterruptedException anIE) {
85 System.err.println("Whoops main thread interrupted");
86 }
87 }
88
89 private static class DiscoveryListenerImpl implements DiscoveryListener {
90 public void discarded(DiscoveryEvent anEvent) {
91 // We don't care about these
92 }
93
94 public void discovered(DiscoveryEvent anEvent) {
95 ServiceRegistrar[] myRegs = anEvent.getRegistrars();
96
97 for (int i = 0; i < myRegs.length; i++) {
98
99 try {
100 ServiceRegistrar myRegistrar = myRegs[i];
101
102 if (myRegistrar != null) {
103 System.out.println("Found registrar:");
104
105 DiscoveryUtil.dumpRegistrar(myRegistrar);
106
107 DiscoveryUtil.dumpContents(myRegistrar);
108 }
109
110 } catch (RemoteException aRE) {
111 System.err.println("Couldn't talk to ServiceRegistrar");
112 aRE.printStackTrace(System.err);
113 } catch (IOException anIOE) {
114 System.err.println("Whoops couldn't talk to ServiceRegistrar");
115 anIOE.printStackTrace(System.err);
116 }
117 }
118 }
119 }
120 }