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

ServiceFinder

public class ServiceFinder extends Object implements DiscoveryListener

Fields Summary
private static String[]
publicGroup
private ServiceItem
returnObject
private Hashtable
items
private Hashtable
leases
private LeaseRenewalManager
lrm
private ServiceFinderListener
sfl
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);

        // Initialize for receiving events from the lookup service
        lrm = new LeaseRenewalManager();
        sfl = new ServiceFinderListener(this);

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

        items.put(item.serviceID, item);
        notifyAll();
    
public synchronized voiddiscarded(DiscoveryEvent dev)

        ServiceRegistrar[] lookup = dev.getRegistrars();
        for (int i = 0; i < lookup.length; i++) {
            try {
                EventRegistration reg = (EventRegistration) leases.get(lookup[i]);
                if (reg != null) {
                    leases.remove(lookup[i]);
                    lrm.remove(reg.getLease());
                }
            } catch (UnknownLeaseException ule) {}
        }
    
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 {
                EventRegistration reg = lookup[i].notify(template,
                		ServiceRegistrar.TRANSITION_NOMATCH_MATCH,
                		sfl, null, Lease.FOREVER);
		lrm.renewUntil(reg.getLease(), Lease.FOREVER, null);
		leases.put(lookup[i], reg);
	    } catch (RemoteException rex) {
	        System.out.println("Registration error " + rex);
	    }
	    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
                        addServiceItem(items.items[j]);
                    // else the service item couldn't be deserialized
                    // so the lookup() method skipped it
                }
            } catch (RemoteException ex) {
                System.out.println("ServiceFinder Error: " + ex);
            }
        }
    
public synchronized voiderrored(java.lang.Object obj)

        if ((obj != null) && (returnObject != null)) {
            if (obj.equals(returnObject.service)) {
                items.remove(returnObject.serviceID);
		returnObject = null;
            }
        }
    
public synchronized java.lang.ObjectgetObject()

        if (returnObject == null) {
            while (items.isEmpty()) {
                try {
                    wait();
	        } catch (InterruptedException ie) {}
	    }
	    returnObject = (ServiceItem) items.elements().nextElement();
        }
        return returnObject.service;