@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);
  }
Exemplo n.º 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;
  }
Exemplo n.º 3
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);
  }