Buggy code:
public class PersistenceBundleManager implements BundleActivator , BundleTrackerCustomizer , ServiceTrackerCustomizer { private static final String QUIESCE_PARTICIPANT_CLASS = "org.apache.aries.quiesce.participant.QuiesceParticipant" ; private static final Logger _logger = LoggerFactory . getLogger ( "org.apache.aries.jpa.container" ) ; private BundleContext ctx = null ; private final Map < Bundle , EntityManagerFactoryManager > bundleToManagerMap = new HashMap < Bundle , EntityManagerFactoryManager > ( ) ; private Set < ServiceReference > persistenceProviders = new HashSet < ServiceReference > ( ) ; private Collection < EntityManagerFactoryManager > managersAwaitingProviders = new ArrayList < EntityManagerFactoryManager > ( ) ; private ManagedPersistenceUnitInfoFactory persistenceUnitFactory ; private PersistenceDescriptorParser parser ; private ServiceRegistration parserReg ; private Properties config ; private RecursiveBundleTracker tracker ; private ServiceTracker serviceTracker ; private ServiceRegistration quiesceReg ; private DestroyCallback quiesceParticipant ; private AtomicBoolean quiesce = new AtomicBoolean ( false ) ; @ SuppressWarnings ( "unchecked" ) private void open ( ) { } private void close ( ) { } public Object addingBundle ( Bundle bundle , BundleEvent event ) { } public Object addingService ( ServiceReference ref ) { if ( quiesce . get ( ) ) { return null ; } Map < EntityManagerFactoryManager , ServiceReference > managersToManage = new HashMap < EntityManagerFactoryManager , ServiceReference > ( ) ; synchronized ( this ) { if ( PersistenceBundleManager . _logger . isDebugEnabled ( ) ) PersistenceBundleManager . _logger . debug ( "Adding<seq2seq4repair_space>a<seq2seq4repair_space>provider:<seq2seq4repair_space>{}" , new Object [ ] { ref } ) ; persistenceProviders . add ( ref ) ; Iterator < EntityManagerFactoryManager > it = managersAwaitingProviders . iterator ( ) ; while ( it . hasNext ( ) ) { EntityManagerFactoryManager mgr = it . next ( ) ; ServiceReference reference = getProviderServiceReference ( mgr . getParsedPersistenceUnits ( ) ) ; <START_BUG> if ( ref != null ) { <END_BUG> managersToManage . put ( mgr , reference ) ; it . remove ( ) ; } } } for ( Map . Entry < EntityManagerFactoryManager , ServiceReference > entry : managersToManage . entrySet ( ) ) { EntityManagerFactoryManager mgr = entry . getKey ( ) ; ServiceReference reference = entry . getValue ( ) ; Collection < ? extends ManagedPersistenceUnitInfo > infos = null ; try { infos = persistenceUnitFactory . createManagedPersistenceUnitMetadata ( ctx , mgr . getBundle ( ) , reference , mgr . getParsedPersistenceUnits ( ) ) ; mgr . manage ( reference , infos ) ; mgr . bundleStateChange ( ) ; } catch ( Exception e ) { if ( e instanceof InvalidPersistenceUnitException ) { logInvalidPersistenceUnitException ( mgr . getBundle ( ) , ( ( InvalidPersistenceUnitException ) ( e ) ) ) ; } else { PersistenceBundleManager . _logger . warn ( MESSAGES . getMessage ( "unable.to.manage.pu" , mgr . getBundle ( ) . getSymbolicName ( ) , mgr . getBundle ( ) . getVersion ( ) ) , e ) ; } mgr . destroy ( ) ; if ( infos != null ) persistenceUnitFactory . destroyPersistenceBundle ( ctx , mgr . getBundle ( ) ) ; setupManager ( mgr . getBundle ( ) , mgr , false ) ; } } return ref ; } public void removedService ( ServiceReference ref , Object o ) { } private void initConfig ( ) { } public void modifiedBundle ( Bundle bundle , BundleEvent event , Object object ) { } public void removedBundle ( Bundle bundle , BundleEvent event , Object object ) { } private Collection < ParsedPersistenceUnit > parseBundle ( Bundle b ) { } private EntityManagerFactoryManager setupManager ( Bundle bundle , EntityManagerFactoryManager mgr , boolean reParse ) { } private synchronized ServiceReference getProviderServiceReference ( Collection < ParsedPersistenceUnit > parsedPersistenceUnits ) { } private VersionRange combineVersionRanges ( List < VersionRange > versionRanges ) throws InvalidRangeCombination { } @ SuppressWarnings ( "unchecked" ) private synchronized ServiceReference getBestProvider ( String providerClass , VersionRange matchingCriteria , Bundle bundle ) { } private static class ProviderServiceComparator implements Comparator < ServiceReference > { public int compare ( ServiceReference object1 , ServiceReference object2 ) { } } private void logInvalidPersistenceUnitException ( Bundle bundle , InvalidPersistenceUnitException e ) { } public void modifiedService ( ServiceReference reference , Object service ) { } public void start ( BundleContext context ) throws Exception { } private void initParser ( ) { } public void stop ( BundleContext context ) throws Exception { } public BundleContext getCtx ( ) { } public void quiesceBundle ( Bundle bundleToQuiesce , final DestroyCallback callback ) { } }

Fix:
if ( reference != null ) {