comparison examples/statsclient/stats/Lookup.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 stats;
2
3 import java.io.IOException;
4
5 import java.rmi.RemoteException;
6
7 import net.jini.core.lookup.ServiceRegistrar;
8 import net.jini.core.lookup.ServiceTemplate;
9
10 import net.jini.discovery.LookupDiscovery;
11 import net.jini.discovery.DiscoveryListener;
12 import net.jini.discovery.DiscoveryEvent;
13
14 /**
15 A class which supports a simple JINI multicast lookup. It doesn't register
16 with any ServiceRegistrars it simply interrogates each one that's
17 discovered for a ServiceItem associated with the passed interface class.
18 i.e. The service needs to already have registered because we won't notice
19 new arrivals. [ServiceRegistrar is the interface implemented by JINI
20 lookup services].
21
22 @todo Be more dynamic in our lookups - see above
23
24 @author Dan Creswell (dan@dancres.org)
25 @version 1.00, 7/9/2003
26 */
27 class Lookup implements DiscoveryListener {
28 private ServiceTemplate theTemplate;
29 private LookupDiscovery theDiscoverer;
30
31 private Object theProxy;
32
33 /**
34 @param aServiceInterface the class of the type of service you are
35 looking for. Class is usually an interface class.
36 */
37 Lookup(Class aServiceInterface) {
38 Class[] myServiceTypes = new Class[] {aServiceInterface};
39 theTemplate = new ServiceTemplate(null, myServiceTypes, null);
40 }
41
42 /**
43 Having created a Lookup (which means it now knows what type of service
44 you require), invoke this method to attempt to locate a service
45 of that type. The result should be cast to the interface of the
46 service you originally specified to the constructor.
47
48 @return proxy for the service type you requested - could be an rmi
49 stub or an intelligent proxy.
50 */
51 Object getService() {
52 synchronized(this) {
53 if (theDiscoverer == null) {
54
55 try {
56 theDiscoverer =
57 new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
58 theDiscoverer.addDiscoveryListener(this);
59 } catch (IOException anIOE) {
60 System.err.println("Failed to init lookup");
61 anIOE.printStackTrace(System.err);
62 }
63 }
64 }
65
66 return waitForProxy();
67 }
68
69 /**
70 Location of a service causes the creation of some threads. Call this
71 method to shut those threads down either before exiting or after a
72 proxy has been returned from getService().
73 */
74 void terminate() {
75 synchronized(this) {
76 if (theDiscoverer != null)
77 theDiscoverer.terminate();
78 }
79 }
80
81 /**
82 Caller of getService ends up here, blocked until we find a proxy.
83
84 @return the newly downloaded proxy
85 */
86 private Object waitForProxy() {
87 synchronized(this) {
88 while (theProxy == null) {
89
90 try {
91 wait();
92 } catch (InterruptedException anIE) {
93 }
94 }
95
96 return theProxy;
97 }
98 }
99
100 /**
101 Invoked to inform a blocked client waiting in waitForProxy that
102 one is now available.
103
104 @param aProxy the newly downloaded proxy
105 */
106 private void signalGotProxy(Object aProxy) {
107 synchronized(this) {
108 if (theProxy == null) {
109 theProxy = aProxy;
110 notify();
111 }
112 }
113 }
114
115 /**
116 Everytime a new ServiceRegistrar is found, we will be called back on
117 this interface with a reference to it. We then ask it for a service
118 instance of the type specified in our constructor.
119 */
120 public void discovered(DiscoveryEvent anEvent) {
121 synchronized(this) {
122 if (theProxy != null)
123 return;
124 }
125
126 ServiceRegistrar[] myRegs = anEvent.getRegistrars();
127
128 for (int i = 0; i < myRegs.length; i++) {
129 ServiceRegistrar myReg = myRegs[i];
130
131 Object myProxy = null;
132
133 try {
134 myProxy = myReg.lookup(theTemplate);
135
136 if (myProxy != null) {
137 signalGotProxy(myProxy);
138 break;
139 }
140 } catch (RemoteException anRE) {
141 System.err.println("ServiceRegistrar barfed");
142 anRE.printStackTrace(System.err);
143 }
144 }
145 }
146
147 /**
148 When a ServiceRegistrar "disappears" due to network partition etc.
149 we will be advised via a call to this method - as we only care about
150 new ServiceRegistrars, we do nothing here.
151 */
152 public void discarded(DiscoveryEvent anEvent) {
153 }
154 }