@Test
  public void testStartLevel() throws Exception {

    assertNotNull("StartLevel injected", startLevel);
    int initialStartLevel = startLevel.getInitialBundleStartLevel();
    assertEquals("Initial bundle start level", 1, initialStartLevel);

    assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());
    assertEquals("arq465-bundle", bundle.getSymbolicName());

    int bundleStartLevel = startLevel.getBundleStartLevel(bundle);
    assertEquals("Bundle start level", 3, bundleStartLevel);

    try {
      bundle.start(Bundle.START_TRANSIENT);
      fail("Bundle cannot be started due to the Framework's current start level");
    } catch (BundleException ex) {
      // expected
    }
    assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());

    // The bundle should not be started
    bundle.start();
    assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());

    // Change the frameworkj start level and wait for the changed event
    final CountDownLatch latch = new CountDownLatch(1);
    context.addFrameworkListener(
        new FrameworkListener() {
          public void frameworkEvent(FrameworkEvent event) {
            if (event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) latch.countDown();
          }
        });
    startLevel.setStartLevel(3);
    latch.await(3, TimeUnit.SECONDS);

    // The bundle should now be started
    assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());

    bundle.stop();
    assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());

    bundle.uninstall();
    assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState());
  }
  public void execute(String s, PrintStream out, PrintStream err) {
    // Get start level service.
    ServiceReference ref =
        m_context.getServiceReference(org.osgi.service.startlevel.StartLevel.class.getName());
    if (ref == null) {
      out.println("StartLevel service is unavailable.");
      return;
    }

    StartLevel sl = (StartLevel) m_context.getService(ref);
    if (sl == null) {
      out.println("StartLevel service is unavailable.");
      return;
    }

    // Parse command line.
    StringTokenizer st = new StringTokenizer(s, " ");

    // Ignore the command name.
    st.nextToken();

    // If there is only one token, then assume it is
    // a bundle ID for which we must retrieve the bundle
    // level.
    if (st.countTokens() == 1) {
      // Get the bundle and display start level.
      Bundle bundle = null;
      String token = null;
      try {
        token = st.nextToken();
        long id = Long.parseLong(token);
        bundle = m_context.getBundle(id);
        if (bundle != null) {
          out.println("Bundle " + token + " is level " + sl.getBundleStartLevel(bundle));
        } else {
          err.println("Bundle ID " + token + " is invalid.");
        }
      } catch (NumberFormatException ex) {
        err.println("Unable to parse integer '" + token + "'.");
      } catch (Exception ex) {
        err.println(ex.toString());
      }
    }
    // If there is more than one token, assume the first
    // token is the new start level and the remaining
    // tokens are the bundle IDs whose start levels should
    // be changed.
    else if (st.countTokens() > 1) {
      // Get the bundle.
      Bundle bundle = null;
      String token = null;
      int startLevel = -1;

      try {
        token = st.nextToken();
        startLevel = Integer.parseInt(token);
      } catch (NumberFormatException ex) {
        err.println("Unable to parse start level '" + token + "'.");
      }

      // Ignore invalid start levels.
      if (startLevel > 0) {
        // Set the start level for each specified bundle.
        while (st.hasMoreTokens()) {
          try {
            token = st.nextToken();
            long id = Long.parseLong(token);
            bundle = m_context.getBundle(id);
            if (bundle != null) {
              sl.setBundleStartLevel(bundle, startLevel);
            } else {
              err.println("Bundle ID '" + token + "' is invalid.");
            }
          } catch (NumberFormatException ex) {
            err.println("Unable to parse bundle ID '" + token + "'.");
          } catch (Exception ex) {
            err.println(ex.toString());
          }
        }
      } else {
        err.println("Invalid start level.");
      }
    } else {
      err.println("Incorrect number of arguments.");
    }
  }