/** Look for persistence unit bundles that were already installed when we came along. */
  public void lookForExistingBundles() {

    // Look at the bundles that are already installed
    Bundle[] installedBundles = mgr.getBundleContext().getBundles();
    debug("Extender.lookForExistingBundles: ", installedBundles);

    // Check if any are p-unit bundles
    for (Bundle b : installedBundles) {
      if (isPersistenceUnitBundle(b)) {
        // We found a persistence unit bundle.
        if (GeminiSystemProperties.refreshPersistenceBundles()) {
          // If bundle is active then refresh it and push it through the life cycle again
          // so it will go through resolving and we can assign it a provider, etc.
          //  if ((b.getState() != Bundle.INSTALLED) && (b.getState() != Bundle.UNINSTALLED)) {
          if (b.getState() == Bundle.ACTIVE) {
            if (isAssigned(b)) {
              debug(
                  "Found existing installed bundle " + b.getSymbolicName(),
                  " but it was already assigned");
            } else {
              debug(
                  "Found active bundle ",
                  b,
                  " - refreshing to push it back to resolve for processing");
              refreshBundle(b);
            }
          }
        } else {
          // Refreshing is disabled - go through assigning and registering process w/o events
          if (b.getState() != Bundle.UNINSTALLED) {
            // Assign the p-unit
            // NOTE: With no refresh, assigning may be happening after the bundle has been resolved
            warning(
                "Extender - Refreshing disabled - entities in bundle " + b.getSymbolicName(),
                " may not be woven");
            tryAssigningPersistenceUnitsInBundle(b);
            // Now if bundle is starting or active then register the p-units in it
            if ((b.getState() == Bundle.STARTING) || (b.getState() == Bundle.ACTIVE)) {
              registerPersistenceUnitsInBundle(b);
            } // Otherwise just let future events take their course
          }
        }
      }
    }
  }
  public void bundleChanged(BundleEvent event) {

    // Only continue if it is a persistence unit bundle
    Bundle b = event.getBundle();
    debug("Extender - bundle event: ", event);
    if (!isPersistenceUnitBundle(b)) return;

    // Process each event
    int eventType = event.getType();

    if (eventType == BundleEvent.INSTALLED) {
      tryAssigningPersistenceUnitsInBundle(b);

    } else if (eventType == BundleEvent.LAZY_ACTIVATION) {
      if (isAssigned(b)) {
        lazyBundles.add(b.getBundleId());
        registerPersistenceUnitsInBundle(b);
      }
    } else if (eventType == BundleEvent.STARTING) {
      if (!isAssigned(b) && !GeminiSystemProperties.refreshPersistenceBundles()) {
        warning(
            "Refreshing disabled - Bundle " + b.getSymbolicName(),
            " starting - entities may not be woven");
        tryAssigningPersistenceUnitsInBundle(b);
      }
      if (isAssigned(b)) {
        if (!isLazy(b)) {
          registerPersistenceUnitsInBundle(b);
        }
      }
    } else if (eventType == BundleEvent.STARTED) {
      // If not assigned and not in limbo then this must be the fist time we
      // have seen it. We need to refresh it to get through our system
      if (!isAssigned(b) && !isInLimbo(b.getSymbolicName())) {
        if (GeminiSystemProperties.refreshPersistenceBundles()) {
          refreshBundle(b);
        } else {
          warning(
              "Refreshing disabled - Bundle " + b.getSymbolicName(),
              " started - entities may not be woven");
          tryAssigningPersistenceUnitsInBundle(b);
          if (isAssigned(b) && !isLazy(b)) {
            registerPersistenceUnitsInBundle(b);
          }
        }
      }
    } else if (eventType == BundleEvent.STOPPING) {
      if (isAssigned(b)) {
        // Fix for bug #342996
        if (isLazy(b)) {
          removeFromLazyBundles(b);
        }
        unregisterPersistenceUnitsInBundle(b);
      }
    } else if (eventType == BundleEvent.UNINSTALLED) {
      if (isAssigned(b)) {
        unassignPersistenceUnitsInBundle(b);
      }
    } else if (eventType == BundleEvent.UPDATED) {
      if (isAssigned(b)) {
        unassignPersistenceUnitsInBundle(b);
      }
      tryAssigningPersistenceUnitsInBundle(b);

    } else if (eventType == BundleEvent.UNRESOLVED) {
      if (isRefreshing(b)) { // assign refreshing bundles
        tryAssigningPersistenceUnitsInBundle(b);
        removeFromRefreshingBundles(b);
      }
    } else { // RESOLVED, STARTED, STOPPED
      // Do nothing.
    }
  }