question: private MarshalledWrapper lookupDo ( Template tmpl ) { if ( isEmpty ( tmpl . serviceTypes ) || tmpl . serviceID != null ) { ItemIter iter = matchingItems ( tmpl ) ; if ( iter . hasNext ( ) ) return iter . next ( ) . service ; return null ; } List services = matchingServices ( tmpl . serviceTypes ) ; long now = System . currentTimeMillis ( ) ; int slen = services . size ( ) ; if ( slen == 0 ) return null ; int srand = Math . abs ( random . nextInt ( ) % 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 . service ; } return null ; }
target:  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.