Mercurial > hg > blitz_condensed
comparison src/org/dancres/jini/util/ServiceLocator.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.util; | |
2 | |
3 import net.jini.lookup.entry.Name; | |
4 import net.jini.core.entry.Entry; | |
5 import net.jini.discovery.DiscoveryListener; | |
6 import net.jini.discovery.DiscoveryEvent; | |
7 import net.jini.discovery.LookupDiscovery; | |
8 import net.jini.core.discovery.LookupLocator; | |
9 import net.jini.core.lookup.ServiceRegistrar; | |
10 import net.jini.core.lookup.ServiceTemplate; | |
11 import java.rmi.RMISecurityManager; | |
12 import java.rmi.RemoteException; | |
13 | |
14 /** | |
15 * ServiceLoactor is a simple wrapper class over Jini's LookupDiscover. | |
16 * It which returns the first matching instance of a service either via | |
17 * unicast or multicast discovery | |
18 */ | |
19 | |
20 public class ServiceLocator{ | |
21 | |
22 static { | |
23 if (System.getSecurityManager() == null) { | |
24 System.setSecurityManager(new RMISecurityManager()); | |
25 } | |
26 } | |
27 | |
28 private Object _proxy; | |
29 private Object _lock=new Object(); | |
30 private ServiceTemplate _template; | |
31 | |
32 /** | |
33 * Locates a service via Unicast discovery | |
34 * @param lusHost The name of the host where a Jini lookup service is running | |
35 * @param serviceClass The class object representing the interface of the service | |
36 * @throws MalformedURLException | |
37 * @throws IOException | |
38 * @throws ClassNotFoundException | |
39 * @return The proxy to the discovered service | |
40 */ | |
41 public static Object getService(String lusHost,Class serviceClass) | |
42 throws java.net.MalformedURLException,java.io.IOException,ClassNotFoundException{ | |
43 | |
44 LookupLocator loc=new LookupLocator("jini://"+lusHost); | |
45 ServiceRegistrar reggie=loc.getRegistrar(); | |
46 ServiceTemplate tmpl=new ServiceTemplate(null, new Class[]{serviceClass},null); | |
47 return reggie.lookup(tmpl); | |
48 | |
49 } | |
50 /** | |
51 * | |
52 * @param lusHost | |
53 * @param serviceClass | |
54 * @param serviceName | |
55 * @return proxy or <code>null</code> | |
56 * @throws java.net.MalformedURLException | |
57 * @throws java.io.IOException | |
58 * @throws ClassNotFoundException | |
59 */ | |
60 public static Object getService(String lusHost,Class serviceClass, | |
61 String serviceName) | |
62 throws java.net.MalformedURLException,java.io.IOException, | |
63 ClassNotFoundException{ | |
64 | |
65 Class [] types=new Class[]{serviceClass}; | |
66 Entry [] entry=null; | |
67 | |
68 if(serviceName!=null){ | |
69 entry=new Entry[]{new Name(serviceName)}; | |
70 } | |
71 | |
72 ServiceTemplate _template = new ServiceTemplate(null,types,entry); | |
73 LookupLocator loc=new LookupLocator("jini://"+lusHost); | |
74 ServiceRegistrar reggie=loc.getRegistrar(); | |
75 | |
76 return reggie.lookup(_template); | |
77 } | |
78 /** | |
79 * Locates the first matching service via multicast discovery | |
80 * @param serviceClass The class object representing the interface of the service | |
81 * @throws IOException | |
82 * @throws InterruptedException | |
83 * @return */ | |
84 public static Object getService(Class serviceClass) | |
85 throws java.io.IOException,InterruptedException{ | |
86 | |
87 return getService(serviceClass,null,Long.MAX_VALUE); | |
88 } | |
89 /** | |
90 * Locates the first matching service via multicast discovery | |
91 * @param serviceClass The class object representing the interface of the service | |
92 * @param waitTime How to wait for the service to be discovered | |
93 * @throws IOException | |
94 * @throws InterruptedException | |
95 * @return */ | |
96 public static Object getService(Class serviceClass,long waitTime) | |
97 throws java.io.IOException,InterruptedException{ | |
98 | |
99 return getService(serviceClass,null,waitTime); | |
100 } | |
101 /** | |
102 * Locates the first matching service via multicast discovery | |
103 * @param serviceClass The class object representing the interface of the service | |
104 * @param serviceName The Name attribute of the service | |
105 * @throws IOException | |
106 * @throws InterruptedException | |
107 * @return */ | |
108 public static Object getService(Class serviceClass,String serviceName,long waitTime) | |
109 throws java.io.IOException,InterruptedException{ | |
110 | |
111 ServiceLocator sl=new ServiceLocator(); | |
112 return sl.getServiceImpl(serviceClass,serviceName,waitTime); | |
113 } | |
114 | |
115 | |
116 private Object getServiceImpl(Class serviceClass,String serviceName,long waitTime) | |
117 throws java.io.IOException,InterruptedException{ | |
118 | |
119 | |
120 Class [] types=new Class[]{serviceClass}; | |
121 Entry [] entry=null; | |
122 | |
123 if(serviceName!=null){ | |
124 entry=new Entry[]{new Name(serviceName)}; | |
125 } | |
126 | |
127 _template=new ServiceTemplate(null,types,entry); | |
128 | |
129 LookupDiscovery disco= | |
130 new LookupDiscovery(LookupDiscovery.ALL_GROUPS); | |
131 | |
132 disco.addDiscoveryListener(new Listener()); | |
133 | |
134 | |
135 | |
136 synchronized(_lock){ | |
137 _lock.wait(waitTime); | |
138 } | |
139 | |
140 disco.terminate(); | |
141 if(_proxy==null){ | |
142 throw new InterruptedException("Service not found within wait time"); | |
143 } | |
144 return _proxy; | |
145 | |
146 } | |
147 class Listener implements DiscoveryListener { | |
148 //invoked when a LUS is discovered | |
149 public void discovered(DiscoveryEvent ev) { | |
150 ServiceRegistrar[] reg = ev.getRegistrars(); | |
151 for (int i=0 ; i<reg.length && _proxy==null ; i++) { | |
152 findService(reg[i]); | |
153 } | |
154 } | |
155 | |
156 public void discarded(DiscoveryEvent ev) { | |
157 } | |
158 } | |
159 private void findService(ServiceRegistrar lus) { | |
160 | |
161 try { | |
162 synchronized(_lock){ | |
163 _proxy=lus.lookup(_template); | |
164 if(_proxy!=null){ | |
165 _lock.notifyAll(); | |
166 } | |
167 } | |
168 }catch(RemoteException ex) { | |
169 ex.printStackTrace(System.err); | |
170 } | |
171 } | |
172 } |