public void stop(int options) throws BundleException {
    boolean wasActive = false;

    switch (getState()) {
      case ACTIVE:
        wasActive = true;
      case STARTING:
        setState(STOPPING);

        Throwable exception = null;

        if (wasActive && (bundleActivator != null)) {
          try {
            bundleActivator.stop(getBundleContext());
          } catch (Throwable t) {
            if (t instanceof ThreadDeath) throw (ThreadDeath) t;
            else exception = t;
          }
          this.bundleActivator = null;
        }

        if (getState() == UNINSTALLED) throw new BundleException("Bundle.UNINSTALLED");

        setState(RESOLVED);
        getFramework().fireBundleEvent(BundleEvent.STOPPED, this);

        if (exception != null) throw new BundleException("BundleActivator.stop", exception);
        break;

      case UNINSTALLED:
        throw new IllegalStateException("Bundle.UNINSTALLED");
      default:
        break;
    }
  }