Set<XBundle> getBundles() {
   Set<XBundle> result = new HashSet<XBundle>();
   XEnvironment env = injectedEnvironment.getValue();
   for (Resource aux : env.getResources(XEnvironment.ALL_IDENTITY_TYPES)) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (bundle.getState() != Bundle.UNINSTALLED) result.add(bundle);
   }
   return Collections.unmodifiableSet(result);
 }
 @Override
 public Set<XBundle> getBundles(Integer states) {
   Set<XBundle> result = new HashSet<XBundle>();
   XEnvironment env = injectedEnvironment.getValue();
   for (Resource aux : env.getResources(XEnvironment.ALL_IDENTITY_TYPES)) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (states == null || (bundle.getState() & states.intValue()) != 0) result.add(bundle);
   }
   return Collections.unmodifiableSet(result);
 }
 @Override
 public XBundle getBundleByLocation(String location) {
   assert location != null : "Null location";
   for (XBundle aux : getBundles()) {
     String auxLocation = aux.getLocation();
     if (location.equals(auxLocation)) {
       return aux;
     }
   }
   return null;
 }
 @Override
 public Set<XBundle> getBundles(String symbolicName, String versionRange) {
   Set<XBundle> resultSet = new HashSet<XBundle>();
   for (XBundle aux : getBundles(null)) {
     if (symbolicName == null || symbolicName.equals(aux.getSymbolicName())) {
       if (versionRange == null || VersionRange.parse(versionRange).isInRange(aux.getVersion())) {
         resultSet.add(aux);
       }
     }
   }
   return Collections.unmodifiableSet(resultSet);
 }
 @Override
 public XBundle getBundleById(long bundleId) {
   if (bundleId == 0) {
     return getFrameworkState().getSystemBundle();
   }
   XEnvironment env = injectedEnvironment.getValue();
   Collection<XResource> resources = env.getResources(XEnvironment.ALL_IDENTITY_TYPES);
   for (Resource aux : resources) {
     XBundle bundle = ((XBundleRevision) aux).getBundle();
     if (bundle.getBundleId() == bundleId) {
       return bundle;
     }
   }
   return null;
 }
Exemplo n.º 6
0
  Class<?> loadClass(String className) throws ClassNotFoundException {

    if (testClasses.contains(className) == false)
      throw new ClassNotFoundException("Class '" + className + "' not found in: " + testClasses);

    XBundle bundle = depUnit.getAttachment(OSGiConstants.INSTALLED_BUNDLE_KEY);
    Module module = depUnit.getAttachment(Attachments.MODULE);
    if (bundle == null && module == null)
      throw new IllegalStateException("Cannot determine deployment type: " + depUnit);

    Class<?> testClass;
    if (bundle != null) {
      testClass = bundle.loadClass(className);
      BundleAssociation.setBundle(bundle);
    } else {
      testClass = module.getClassLoader().loadClass(className);
    }

    return testClass;
  }
  @Test
  public void testInstallModule() throws Exception {

    // Try to start the bundle and verify the expected ResolutionException
    XBundle bundleA = (XBundle) installBundle(getBundleA());
    try {
      bundleA.start();
      Assert.fail("BundleException expected");
    } catch (BundleException ex) {
      ResolutionException cause = (ResolutionException) ex.getCause();
      Collection<Requirement> reqs = cause.getUnresolvedRequirements();
      Assert.assertEquals(1, reqs.size());
      Requirement req = reqs.iterator().next();
      String namespace = req.getNamespace();
      Assert.assertEquals(PackageNamespace.PACKAGE_NAMESPACE, namespace);
    }

    Module moduleB = loadModule(getModuleB());
    XBundleRevision brevB = installResource(moduleB);
    try {
      XBundle bundleB = brevB.getBundle();
      Assert.assertEquals(bundleA.getBundleId() + 1, bundleB.getBundleId());

      bundleA.start();
      assertLoadClass(bundleA, ModuleServiceX.class.getName(), bundleB);

      // verify wiring for A
      XBundleRevision brevA = bundleA.getBundleRevision();
      Assert.assertSame(bundleA, brevA.getBundle());
      BundleWiring wiringA = brevA.getWiring();
      List<BundleWire> requiredA = wiringA.getRequiredWires(null);
      Assert.assertEquals(1, requiredA.size());
      BundleWire wireA = requiredA.get(0);
      Assert.assertSame(brevA, wireA.getRequirer());
      Assert.assertSame(bundleB, wireA.getProvider().getBundle());
      List<BundleWire> providedA = wiringA.getProvidedWires(null);
      Assert.assertEquals(0, providedA.size());

      // verify wiring for B
      Assert.assertSame(bundleB, brevB.getBundle());
      BundleWiring wiringB = brevB.getWiring();
      List<BundleWire> requiredB = wiringB.getRequiredWires(null);
      Assert.assertEquals(0, requiredB.size());
      List<BundleWire> providedB = wiringB.getProvidedWires(null);
      Assert.assertEquals(1, providedB.size());
      BundleWire wireB = providedB.get(0);
      Assert.assertSame(brevA, wireB.getRequirer());
      Assert.assertSame(bundleB, wireB.getProvider().getBundle());
    } finally {
      removeModule(moduleB);
    }
  }