@Test
  public void testAttachedFragment() throws Exception {
    Bundle hostA = installBundle(getHostA());
    assertBundleState(Bundle.INSTALLED, hostA.getState());

    Bundle fragA = installBundle(getFragmentA());
    assertBundleState(Bundle.INSTALLED, fragA.getState());

    hostA.start();
    assertBundleState(Bundle.ACTIVE, hostA.getState());
    assertBundleState(Bundle.RESOLVED, fragA.getState());

    URL entryURL = hostA.getEntry("resource.txt");
    assertNull("Entry URL null", entryURL);

    URL resourceURL = hostA.getResource("resource.txt");
    assertEquals("bundle", resourceURL.getProtocol());
    assertEquals("/resource.txt", resourceURL.getPath());

    BufferedReader br = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
    assertEquals("fragA", br.readLine());

    // Load class provided by the fragment
    assertLoadClass(hostA, FragBeanA.class.getName());

    // Load a private class from host
    assertLoadClass(hostA, SubBeanA.class.getName());

    // PackageAdmin.getBundleType
    PackageAdmin pa = getPackageAdmin();
    assertEquals("Bundle type", 0, pa.getBundleType(hostA));
    assertEquals("Bundle type", PackageAdmin.BUNDLE_TYPE_FRAGMENT, pa.getBundleType(fragA));

    // PackageAdmin.getHosts
    Bundle[] hosts = pa.getHosts(hostA);
    assertNull("Not a fragment", hosts);

    hosts = pa.getHosts(fragA);
    assertNotNull("Hosts not null", hosts);
    assertEquals("Hosts length", 1, hosts.length);
    assertEquals("Hosts equals", hostA, hosts[0]);

    // PackageAdmin.getFragments
    Bundle[] fragments = pa.getFragments(fragA);
    assertNull("Not a host", fragments);

    fragments = pa.getFragments(hostA);
    assertNotNull("Fragments not null", fragments);
    assertEquals("Fragments length", 1, fragments.length);
    assertEquals("Fragments equals", fragA, fragments[0]);

    hostA.uninstall();
    assertBundleState(Bundle.UNINSTALLED, hostA.getState());
    assertBundleState(Bundle.RESOLVED, fragA.getState());

    fragA.uninstall();
    assertBundleState(Bundle.UNINSTALLED, fragA.getState());
  }
Esempio n. 2
0
  /**
   * Stop the bundles in the supplied crates. Inter-crate dependencies are NOT expanded.
   *
   * @param context a bundle context
   * @param crates the list of crates to be stopped
   * @return the list of stopped Bundles
   * @throws CrateException wrapping any other exceptions that happen
   */
  public static List<Bundle> stopBundles(BundleContext context, Crate... crates)
      throws CrateException {
    Logger logger = Logger.getLogger(CrateStarterElf.class);

    // get the set of currently installed bundles...
    Map<String, Bundle> installed = new HashMap<String, Bundle>();
    for (Bundle bundle : context.getBundles()) {
      installed.put(bundle.getSymbolicName(), bundle);
    }

    ServiceReference reference = context.getServiceReference(PackageAdmin.class.getName());
    PackageAdmin packageAdmin = (PackageAdmin) context.getService(reference);

    List<Bundle> stoppedBundles = new ArrayList<Bundle>();

    // Shutdown in reverse order...
    List<Crate> cratesReversed = Arrays.asList(crates);
    Collections.reverse(cratesReversed);

    for (Crate crate : cratesReversed) {
      // Shutdown in reverse order...
      List<CrateBundle> bundles = crate.getBundles();
      Collections.reverse(bundles);

      for (CrateBundle cb : bundles) {
        Bundle bundle = installed.get(cb.getId());
        if (bundle != null && !bundle.getSymbolicName().startsWith("org.eclipse")) {
          try {
            Dictionary<?, ?> dict = bundle.getHeaders();
            String bundleVersion = (String) dict.get("Bundle-Version");
            if (bundleVersion != null && bundleVersion.equals(cb.getVersion())) {
              if (packageAdmin.getBundleType(bundle) != PackageAdmin.BUNDLE_TYPE_FRAGMENT) {
                logger.debug("Stopping bundle " + bundle.getSymbolicName() + " " + bundleVersion);
                bundle.stop();
              }
            }
          } catch (BundleException be) {
            logger.warn("Error stopping bundle " + cb.getId(), be);
          }
        }
      }
    }

    return stoppedBundles;
  }
Esempio n. 3
0
 private boolean isFragment(Bundle b) {
   return padmin != null && padmin.getBundleType(b) == PackageAdmin.BUNDLE_TYPE_FRAGMENT;
 }
Esempio n. 4
0
 public boolean isFragment(Bundle bundle) {
   PackageAdmin packageAdmin = getPackageAdmin();
   if (packageAdmin == null) return false;
   return (packageAdmin.getBundleType(bundle) & PackageAdmin.BUNDLE_TYPE_FRAGMENT) > 0;
 }