public void start(int options) throws BundleException {
    if (getState() == UNINSTALLED) throw new IllegalStateException("Bundle.UNINSTALLED");

    BundleStartLevel bundleStartLevel = adapt(BundleStartLevel.class);
    FrameworkStartLevel frameworkStartLevel = getFramework().adapt(FrameworkStartLevel.class);

    if ((bundleStartLevel != null)
        && (bundleStartLevel.getStartLevel() > frameworkStartLevel.getStartLevel())) {
      if ((options & START_TRANSIENT) == START_TRANSIENT) throw new BundleException("startLevel");
      else return;
    }

    if (getState() == ACTIVE) return;

    if (getState() == INSTALLED) setState(RESOLVED);

    setState(STARTING);

    String location = getLocation();

    if (location != null) {
      BundleActivator bundleActivator = null;
      Throwable exception = null;

      try {
        bundleActivator = (BundleActivator) loadClass(location.replace('/', '.')).newInstance();

        bundleActivator.start(getBundleContext());
      } catch (Throwable t) {
        logger.log(Level.SEVERE, "Error starting bundle: " + bundleActivator, t);

        if (t instanceof ThreadDeath) throw (ThreadDeath) t;
        else exception = t;
      }

      if (exception == null) this.bundleActivator = bundleActivator;
      else {
        setState(STOPPING);
        setState(RESOLVED);
        getFramework().fireBundleEvent(BundleEvent.STOPPED, this);
        throw new BundleException("BundleActivator.start", exception);
      }
    }

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

    setState(ACTIVE);
  }
  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;
    }
  }