1 7 package org.objectweb.proactive.p2p.registry; 8 9 import java.io.*; 10 import java.rmi.*; 11 import java.util.*; 12 import net.jini.discovery.*; 13 import net.jini.core.lookup.*; 14 import net.jini.core.entry.*; 15 16 public class ServiceFinder implements DiscoveryListener { 17 private static String [] publicGroup = new String [] { "" }; 18 private Vector returnObject = new Vector(); 19 20 private LookupDiscovery reg; 21 private ServiceTemplate template; 22 23 public ServiceFinder(Class serviceInterface) throws IOException { 24 this(publicGroup, serviceInterface, (Entry[])null); 25 } 26 27 public ServiceFinder(Class serviceInterface, Entry attribute) 28 throws IOException { 29 this(publicGroup, serviceInterface, new Entry[] { attribute }); 30 } 31 32 public ServiceFinder(Class serviceInterface, Entry[] attributes) 33 throws IOException { 34 this(publicGroup, serviceInterface, attributes); 35 } 36 37 public ServiceFinder(String [] groups, Class serviceInterface, 38 Entry[] attributes) throws IOException { 39 Class [] name = new Class [] { serviceInterface }; 42 template = new ServiceTemplate(null, name, attributes); 43 44 reg = new LookupDiscovery(groups); 47 reg.addDiscoveryListener(this); 48 } 49 50 public synchronized void discovered(DiscoveryEvent dev) { 53 ServiceRegistrar[] lookup = dev.getRegistrars(); 54 for (int i = 0; i < lookup.length; i++) { 56 try { 57 ServiceMatches items = 58 lookup[i].lookup(template, Integer.MAX_VALUE); 59 for (int j = 0; j < items.items.length; j++) { 62 if (items.items[j].service != null) 63 returnObject.addElement(items.items[j]); 65 } 68 notifyAll(); 69 } catch (RemoteException ex) { 70 System.err.println("[Service Finder] ServiceFinder Error: " + ex); 71 } 72 } 73 } 74 75 76 public synchronized void discarded(DiscoveryEvent dev) { 77 } 78 79 80 81 82 public synchronized Object getObject() { 85 while (returnObject.size() == 0) { 86 try { 87 wait(); 88 } catch (InterruptedException ex) { 89 }; 90 } 91 return ((ServiceItem)returnObject.elementAt(0)).service; 93 } 94 95 96 97 public synchronized void errored(Object obj) { 101 if ((obj != null) && (returnObject.size() != 0)) { 102 if (obj.equals(((ServiceItem)returnObject.elementAt(0)).service)) { 103 returnObject.removeElementAt(0); 104 } 105 } 106 } 107 } 108 109 | Popular Tags |