/**
   * Call bundle's BundleActivator.stop() This method is called by Bundle.stopWorker to stop the
   * bundle.
   *
   * @exception BundleException if the bundle has a class that implements the BundleActivator
   *     interface, and the BundleActivator.stop() method failed
   */
  protected void stop() throws BundleException {
    try {
      AccessController.doPrivileged(
          new PrivilegedExceptionAction<Object>() {
            public Object run() throws Exception {
              if (activator != null) {
                // make sure the context class loader is set correctly
                Object previousTCCL = setContextFinder();
                try {
                  /* Stop the bundle synchronously */
                  activator.stop(BundleContextImpl.this);
                } finally {
                  if (previousTCCL != Boolean.FALSE)
                    Thread.currentThread().setContextClassLoader((ClassLoader) previousTCCL);
                }
              }
              return null;
            }
          });
    } catch (Throwable t) {
      if (t instanceof PrivilegedActionException) {
        t = ((PrivilegedActionException) t).getException();
      }

      if (Debug.DEBUG_GENERAL) {
        Debug.printStackTrace(t);
      }

      String clazz = (activator == null) ? "" : activator.getClass().getName(); // $NON-NLS-1$

      throw new BundleException(
          NLS.bind(
              Msg.BUNDLE_ACTIVATOR_EXCEPTION,
              new Object[] {
                clazz,
                "stop",
                bundle.getSymbolicName() == null
                    ? "" + bundle.getBundleId()
                    : bundle.getSymbolicName()
              }),
          BundleException.ACTIVATOR_ERROR,
          t); //$NON-NLS-1$ //$NON-NLS-2$
    } finally {
      activator = null;
    }
  }
  /**
   * Call bundle's BundleActivator.start() This method is called by Bundle.startWorker to start the
   * bundle.
   *
   * @exception BundleException if the bundle has a class that implements the BundleActivator
   *     interface, but Framework couldn't instantiate it, or the BundleActivator.start() method
   *     failed
   */
  protected void start() throws BundleException {
    activator = bundle.loadBundleActivator();

    if (activator != null) {
      try {
        startActivator(activator);
      } catch (BundleException be) {
        activator = null;
        throw be;
      }
    }

    /* activator completed successfully. We must use this
    same activator object when we stop this bundle. */
  }
  /**
   * Calls the start method of a BundleActivator.
   *
   * @param bundleActivator that activator to start
   */
  protected void startActivator(final BundleActivator bundleActivator) throws BundleException {
    if (Profile.PROFILE && Profile.STARTUP)
      Profile.logEnter("BundleContextImpl.startActivator()", null); // $NON-NLS-1$
    try {
      AccessController.doPrivileged(
          new PrivilegedExceptionAction<Object>() {
            public Object run() throws Exception {
              if (bundleActivator != null) {
                if (Profile.PROFILE && Profile.STARTUP)
                  Profile.logTime(
                      "BundleContextImpl.startActivator()",
                      "calling "
                          + bundle.getLocation()
                          + " bundle activator"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                // make sure the context class loader is set correctly
                Object previousTCCL = setContextFinder();
                /* Start the bundle synchronously */
                try {
                  bundleActivator.start(BundleContextImpl.this);
                } finally {
                  if (previousTCCL != Boolean.FALSE)
                    Thread.currentThread().setContextClassLoader((ClassLoader) previousTCCL);
                }
                if (Profile.PROFILE && Profile.STARTUP)
                  Profile.logTime(
                      "BundleContextImpl.startActivator()",
                      "returned from "
                          + bundle.getLocation()
                          + " bundle activator"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
              }
              return null;
            }
          });
    } catch (Throwable t) {
      if (t instanceof PrivilegedActionException) {
        t = ((PrivilegedActionException) t).getException();
      }

      if (Debug.DEBUG_GENERAL) {
        Debug.printStackTrace(t);
      }

      String clazz = null;
      clazz = bundleActivator.getClass().getName();

      throw new BundleException(
          NLS.bind(
              Msg.BUNDLE_ACTIVATOR_EXCEPTION,
              new Object[] {
                clazz,
                "start",
                bundle.getSymbolicName() == null
                    ? "" + bundle.getBundleId()
                    : bundle.getSymbolicName()
              }),
          BundleException.ACTIVATOR_ERROR,
          t); //$NON-NLS-1$ //$NON-NLS-2$
    } finally {
      if (Profile.PROFILE && Profile.STARTUP)
        Profile.logExit("BundleContextImpl.startActivator()"); // $NON-NLS-1$
    }
  }