Exemplo n.º 1
0
  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);
  }
Exemplo n.º 2
0
  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;
    }
  }
  /**
   * 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;
    }
  }
  /**
   * 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$
    }
  }