@Test
  public void startAndStop() throws Exception {
    BundleContext bundleContext =
        new MockBundleContext() {

          @Override
          public ServiceRegistration registerService(
              String clazz, Object service, Dictionary properties) {
            if (service instanceof ContactDao) {
              daoRegistered++;
              return new MockServiceRegistration() {
                @Override
                public void unregister() {
                  daoRegistered--;
                };
              };
            } else {
              return super.registerService(clazz, service, properties);
            }
          }
        };

    BundleActivator bundleActivator = new DirectoryDaoBundleActivatorSimple();
    bundleActivator.start(bundleContext);
    Assert.assertEquals(1, daoRegistered);
    bundleActivator.stop(bundleContext);
    Assert.assertEquals(0, daoRegistered);
  }
Beispiel #2
0
  public int activate() throws Exception {
    Policy.setPolicy(new AllPolicy());

    systemBundle = createFramework();
    if (systemBundle == null) return LauncherConstants.ERROR;

    doTimeoutHandler();

    // Initialize this framework so it becomes STARTING
    systemBundle.start();
    trace("system bundle started ok");

    BundleContext systemContext = systemBundle.getBundleContext();
    ServiceReference ref = systemContext.getServiceReference(PackageAdmin.class.getName());
    if (ref != null) {
      padmin = (PackageAdmin) systemContext.getService(ref);
    } else trace("could not get package admin");

    systemContext.addServiceListener(this, "(&(objectclass=java.lang.Runnable)(main.thread=true))");

    update();

    if (parms.trace) {
      report(out);
    }

    // Start embedded activators
    trace("start embedded activators");
    if (parms.activators != null) {
      ClassLoader loader = getClass().getClassLoader();
      for (Object token : parms.activators) {
        try {
          Class<?> clazz = loader.loadClass((String) token);
          BundleActivator activator = (BundleActivator) clazz.newInstance();
          embedded.add(activator);
          trace("adding activator %s", activator);
        } catch (Exception e) {
          throw new IllegalArgumentException(
              "Embedded Bundle Activator incorrect: " + token + ", " + e);
        }
      }
    }
    int result = LauncherConstants.OK;
    for (BundleActivator activator : embedded)
      try {
        trace("starting activator %s", activator);
        activator.start(systemContext);
      } catch (Exception e) {
        error("Starting activator %s : %s", activator, e);
        result = LauncherConstants.ERROR;
      }

    return result;
  }
  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;
    }
  }
  /**
   * 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$
    }
  }