private Item lookupDo(Template tmpl){
  if (isEmpty(tmpl.serviceTypes) || tmpl.serviceID != null) {
    ItemIter iter=matchingItems(tmpl);
    if (iter.hasNext())     return iter.next();
    return null;
  }
  List services=matchingServices(tmpl.serviceTypes);
  long now=SystemTime.timeMillis();
  int slen=services.size();
  if (slen == 0)   return null;
  int srand=random.nextInt(Integer.MAX_VALUE) % slen;
  for (int i=0; i < slen; i++) {
    SvcReg reg=(SvcReg)services.get((i + srand) % slen);
    if (reg.leaseExpiration > now && matchAttributes(tmpl,reg.item))     return reg.item;
  }
  return null;
}

private Item lookupDo ( Template tmpl ) { if ( isEmpty ( tmpl . serviceTypes ) || tmpl . serviceID != null ) { ItemIter iter = matchingItems ( tmpl ) ; if ( iter . hasNext ( ) ) return iter . next ( ) ; return null ; } List services = matchingServices ( tmpl . serviceTypes ) ; long now = SystemTime . timeMillis ( ) ; int slen = services . size ( ) ; if ( slen == 0 ) return null ; int srand = random . nextInt ( Integer . MAX_VALUE ) % slen ; for ( int i = 0 ; i < slen ; i ++ ) { SvcReg reg = ( SvcReg ) services . get ( ( i + srand ) % slen ) ; if ( reg . leaseExpiration > now && matchAttributes ( tmpl , reg . item ) ) return reg . item ; } return null ; }
the code that does the real work of lookup . as a special case , if the template specifies at least one service type to match , and there are multiple items that match the template , then we make a random pick among them , in an attempt to load balance use of " equivalent " services and avoid starving any of them .
