@ SuppressWarnings ( "unchecked" ) private T getService ( boolean throwException ) { T svc = null ; ReferenceTuple < T > current = null ; ReferenceTuple < T > newTuple = null ; do { // Get the current tuple current = tuple . get ( ) ; // We have both a context and a service reference.. svc = current . locatedService ; if ( svc != null ) { break ; // break out. We know the answer, yes } // If we're missing the required bits, bail... if ( current . context == null || current . serviceRef == null ) { if ( throwException ) throw new IllegalStateException ( "Required attribute is null," + toString ( ) ) ; break ; // break out. Nothing more to do here } // We have to locate / resolve the service from the reference SecurityManager sm = System . getSecurityManager ( ) ; if ( sm != null ) { final ReferenceTuple < T > finalCurrent = current ; svc = AccessController . doPrivileged ( new PrivilegedAction < T > ( ) { @ Override public T run ( ) { return finalCurrent . context . locateService ( referenceName , finalCurrent . serviceRef ) ; } } ) ; } else { svc = current . context . locateService ( referenceName , current . serviceRef ) ; } // if we're asked to throw, throw if we couldn't find the service if ( svc == null ) { if ( throwException ) throw new IllegalStateException ( "Located service is null," + toString ( ) ) ; break ; // break out. Nothing more to do here } // Create a new tuple: keep the context and reference, set the cached service newTuple = new ReferenceTuple < T > ( current . context , current . serviceRef , svc ) ; // Try to save the new tuple: retry if someone changed the value meanwhile } while ( ! tuple . compareAndSet ( current , newTuple ) ) ; return svc ; }
Try to locate the service