FileDocCategorySizeDatePackage
ServiceFinder.javaAPI DocExample4898Thu Mar 16 11:52:46 GMT 2000None

ServiceFinder

public class ServiceFinder extends Object implements DiscoveryListener

Fields Summary
private static String[]
publicGroup
private Vector
returnObject
private LookupDiscovery
reg
private ServiceTemplate
template
Constructors Summary
public ServiceFinder(Class serviceInterface)


         
        this(publicGroup, serviceInterface, (Entry[])null);
    
public ServiceFinder(Class serviceInterface, Entry attribute)

        this(publicGroup, serviceInterface, new Entry[] { attribute });
    
public ServiceFinder(Class serviceInterface, Entry[] attributes)

        this(publicGroup, serviceInterface, attributes);
    
public ServiceFinder(String[] groups, Class serviceInterface, Entry[] attributes)

        // Construct the template here for matching in the lookup service
        // We don't use the template until we actually discover a service
        Class[] name = new Class[] { serviceInterface };
        template = new ServiceTemplate(null, name, attributes);

        // Create the facility to perform multicast discovery for all
        // lookup services
        reg = new LookupDiscovery(groups);
        reg.addDiscoveryListener(this);
    
Methods Summary
public synchronized voiddiscarded(DiscoveryEvent dev)

    
public synchronized voiddiscovered(DiscoveryEvent dev)

        ServiceRegistrar[] lookup = dev.getRegistrars();
        // We may have discovered one or more lookup services
        for (int i = 0; i < lookup.length; i++) {
            try {
                ServiceMatches items =
                    lookup[i].lookup(template, Integer.MAX_VALUE);
                // Each lookup service may have zero or more registered
                // servers that implement our desired template
                for (int j = 0; j < items.items.length; j++) {
                    if (items.items[j].service != null)
                        // Put each matching service into our vector
                        returnObject.addElement(items.items[j]);
		    // else the service item couldn't be deserialized
		    // so the lookup() method skipped it
                }
                    notifyAll();
            } catch (RemoteException ex) {
                System.out.println("ServiceFinder Error: " + ex);
            }
        }
    
public synchronized voiderrored(java.lang.Object obj)

        if ((obj != null) && (returnObject.size() != 0)) {
            if (obj.equals(((ServiceItem)returnObject.elementAt(0)).service)) {
                returnObject.removeElementAt(0);
            }
        }
    
public synchronized java.lang.ObjectgetObject()

        while (returnObject.size() == 0) {
            try {
                wait();
            } catch (InterruptedException ex) {};
        }
        return ((ServiceItem)returnObject.elementAt(0)).service;