org.osgi.framework.wiring.BundleCapability Java Examples

The following examples show how to use org.osgi.framework.wiring.BundleCapability. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConciergeJvmOptions.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
private void checkEECaps(List<BundleCapability> caps) throws Exception {
	String propVersion = "java.specification.version";
	String propVersionValue = System.getProperty(propVersion);
	String propName = "java.specification.name";
	String propNameValue = System.getProperty(propName);
	// now check if we see the expected capabilities for "osgi.ee"
	// this test does only work on for JavaSE 7/8 Full-JRE yet
	// for other JVM to run test on we have to add expected values
	if ("1.8".equals(propVersionValue)) {
		checkEECapsForJava8(caps);
	} else if ("1.7".equals(propVersionValue)) {
		checkEECapsForJava7(caps);
	} else {
		Assert.fail("Test not yet supported on JavaVM " + propNameValue
				+ " " + propVersionValue);
	}
}
 
Example #2
Source File: PackageAdminImpl.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
private void getExportedPackages0(final Bundle bundle, final String name, final ArrayList<ExportedPackage> result) {
	if (bundle == null) {
		throw new IllegalArgumentException("bundle==null");
	}
	if (result == null) {
		throw new IllegalArgumentException("result==null");
	}

	final List<BundleRevision> revs = bundle.adapt(BundleRevisions.class).getRevisions();

	if (revs.isEmpty()) {
		return;
	}

	for (final BundleRevision r : revs) {
		final BundleWiring wiring = r.getWiring();
		if (wiring != null && wiring.isInUse()) {
			for (final Capability cap : wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
				if (name == null || name.equals(cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
					result.add(new ExportedPackageImpl((BundleCapability) cap));
				}
			}
		}
	}
}
 
Example #3
Source File: NPEfix8_eigth_s.java    From coming with MIT License 6 votes vote down vote up
public List<BundleCapability> getDeclaredCapabilities(String namespace)
{
    List<BundleCapability> result = m_declaredCaps;
    if (namespace != null)
    {
        result = new ArrayList<BundleCapability>();
        for (BundleCapability cap : m_declaredCaps)
        {
            if (cap.getNamespace().equals(namespace))
            {
                result.add(cap);
            }
        }
    }
    return result;
}
 
Example #4
Source File: NPEfix8_eigth_t.java    From coming with MIT License 6 votes vote down vote up
public List<BundleCapability> getDeclaredCapabilities(String namespace)
{
    List<BundleCapability> result = m_declaredCaps;
    if (namespace != null)
    {
        result = new ArrayList<BundleCapability>();
        for (BundleCapability cap : m_declaredCaps)
        {
            if (cap.getNamespace().equals(namespace))
            {
                result.add(cap);
            }
        }
    }
    return result;
}
 
Example #5
Source File: FuchsiaUtils.java    From fuchsia with Apache License 2.0 6 votes vote down vote up
/**
 * Return the BundleCapability of a bundle exporting the package packageName.
 *
 * @param context     The BundleContext
 * @param packageName The package name
 * @return the BundleCapability of a bundle exporting the package packageName
 */
private static BundleCapability getExportedPackage(BundleContext context, String packageName) {
    List<BundleCapability> packages = new ArrayList<BundleCapability>();
    for (Bundle bundle : context.getBundles()) {
        BundleRevision bundleRevision = bundle.adapt(BundleRevision.class);
        for (BundleCapability packageCapability : bundleRevision.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE)) {
            String pName = (String) packageCapability.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE);
            if (pName.equalsIgnoreCase(packageName)) {
                packages.add(packageCapability);
            }
        }
    }

    Version max = Version.emptyVersion;
    BundleCapability maxVersion = null;
    for (BundleCapability aPackage : packages) {
        Version version = (Version) aPackage.getAttributes().get("version");
        if (max.compareTo(version) <= 0) {
            max = version;
            maxVersion = aPackage;
        }
    }

    return maxVersion;
}
 
Example #6
Source File: ResolverHooks.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void filterSingletonCollisions(BundleCapability singleton , Collection<BundleCapability> candidates) throws BundleException {
  if (hasHooks()) {
    Collection<BundleCapability> c = new RemoveOnlyCollection<BundleCapability>(candidates);
    blockResolveForHooks();
    try {
      for (TrackedResolverHookFactory rhf : active) {
        final ResolverHook rh = checkActiveRemoved(rhf);
        try {
          rh.filterSingletonCollisions(singleton, c);
        } catch (RuntimeException re) {
          throw new BundleException("Resolver hook throw an exception, bid="
                                    + rhf.bundle.getBundleId(),
                                    BundleException.REJECTED_BY_HOOK, re);
        }
      }
    } finally {
      unblockResolveForHooks();
    }
  }
}
 
Example #7
Source File: ResolverHooks.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void filterMatches(BundleRequirement requirement , Collection<? extends BundleCapability> candidates) throws BundleException {
  if (hasHooks()) {
    @SuppressWarnings("unchecked")
    Collection<BundleCapability> c = new RemoveOnlyCollection<BundleCapability>((Collection<BundleCapability>) candidates);
    blockResolveForHooks();
    try {
      for (TrackedResolverHookFactory rhf : active) {
        final ResolverHook rh = checkActiveRemoved(rhf);
        try {
          rh.filterMatches(requirement, c);
        } catch (RuntimeException re) {
          throw new BundleException("Resolver hook throw an exception, bid="
                                    + rhf.bundle.getBundleId(),
                                    BundleException.REJECTED_BY_HOOK, re);
        }
      }
    } finally {
      unblockResolveForHooks();
    }
  }
}
 
Example #8
Source File: BundleImpl.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
private MultiMap<String, BundleCapability> parseCapabilities(
		final String str) throws BundleException {
	final MultiMap<String, BundleCapability> result = new MultiMap<String, BundleCapability>();

	if (str == null) {
		return result;
	}

	final String[] reqStrs = Utils.splitString(str, ',');
	for (int i = 0; i < reqStrs.length; i++) {
		final BundleCapabilityImpl cap = new BundleCapabilityImpl(this,
				reqStrs[i]);
		final String namespace = cap.getNamespace();
		if(namespace.equals(NativeNamespace.NATIVE_NAMESPACE)){
			throw new BundleException("Only the system bundle can provide a native capability", BundleException.MANIFEST_ERROR);
		}
		result.insert(namespace, cap);
	}
	return result;
}
 
Example #9
Source File: BundleRevisionImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
BundleRevisionDTO getDTO() {
  BundleRevisionDTO res = new BundleRevisionDTO();
  res.symbolicName = gen.symbolicName;
  res.type = getTypes();
  res.version = gen.version.toString();
  res.bundle = gen.bundle.id;
  res.id = dtoId;
  res.capabilities = new ArrayList<CapabilityDTO>();
  for (BundleCapability bc :  getDeclaredCapabilities(null)) {
    res.capabilities.add(BundleCapabilityImpl.getDTO(bc, this));
  }
  res.requirements = new ArrayList<RequirementDTO>();
  for (BundleRequirement br :  getDeclaredRequirements(null)) {
    res.requirements.add(BundleRequirementImpl.getDTO(br, this));
  }
  return res;
}
 
Example #10
Source File: ConciergeJvmOptions.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Test if startup of framework does work when java.specification.name is
 * NOT null. This is the standard case for JavaSE 7/8 etc JVMs.
 */
@Test
public void testJavaSpecificationNameNotNull() throws Exception {
	String propName = "java.specification.name";
	String propValue = System.getProperty(propName);
	Assert.assertNotNull(propValue);
	try {
		startFramework();

		Assert.assertEquals(0, framework.getBundleId());
		BundleWiring w = framework.adapt(BundleWiring.class);
		List<BundleCapability> caps = w.getCapabilities("osgi.ee");
		checkEECaps(caps);
	} finally {
		stopFramework();
	}
}
 
Example #11
Source File: ConciergeJvmOptions.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Test if startup of framework does work when java.specification.name IS
 * null. This will for example happen when running on CEE-J.
 */
@Test
public void testJavaSpecificationNameNull() throws Exception {
	String propName = "java.specification.name";
	String savedPropValue = System.getProperty(propName);
	try {
		System.clearProperty(propName);
		String propValue = System.getProperty(propName);
		Assert.assertNull(propValue);
		startFramework();

		Assert.assertEquals(0, framework.getBundleId());
		BundleWiring w = framework.adapt(BundleWiring.class);
		List<BundleCapability> caps = w.getCapabilities("osgi.ee");
		checkEECaps(caps);
	} finally {
		stopFramework();
		System.setProperty(propName, savedPropValue);
	}
}
 
Example #12
Source File: BundleUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static Bundle[] getBundles(String symbolicName, FrameworkWiring fwkWiring) {
	BundleContext context = fwkWiring.getBundle().getBundleContext();
	if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName)) {
		symbolicName = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getSymbolicName();
	}
	StringBuilder filter = new StringBuilder();
	filter.append('(')
		.append(IdentityNamespace.IDENTITY_NAMESPACE)
		.append('=')
		.append(symbolicName)
		.append(')');
	Map<String, String> directives = Collections.singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter.toString());
	Collection<BundleCapability> matchingBundleCapabilities = fwkWiring.findProviders(ModuleContainer.createRequirement(IdentityNamespace.IDENTITY_NAMESPACE, directives, Collections.emptyMap()));
	if (matchingBundleCapabilities.isEmpty()) {
		return null;
	}
	Bundle[] results = matchingBundleCapabilities.stream().map(c -> c.getRevision().getBundle())
			// Remove all the bundles that are uninstalled
			.filter(bundle -> (bundle.getState() & (Bundle.UNINSTALLED)) == 0)
			.sorted((b1, b2) -> b2.getVersion().compareTo(b1.getVersion())) // highest version first
			.toArray(Bundle[]::new);
	return results.length > 0 ? results : null;
}
 
Example #13
Source File: NativeRequirement.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean matches(BundleCapability capability) {
  if (BundleRevision.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
    for (NativeCodeEntry e : entries) {
      if (e.filter == null || e.filter.matches(capability.getAttributes())) {
        return true;
      }
    }
  }
  return false;
}
 
Example #14
Source File: BundleRequirementImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean matches(BundleCapability capability) {
  if (namespace.equals(capability.getNamespace())) {
    return null==filter ? true : filter.matches(capability.getAttributes());
  }
  return false;
}
 
Example #15
Source File: ImportPkg.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean matches(BundleCapability capability) {
  if (BundleRevision.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
    return toFilter().matches(capability.getAttributes());
  }
  return false;
}
 
Example #16
Source File: FrameworkWiringImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Collection<BundleCapability> findProviders(Requirement requirement) {
  final String namespace = requirement.getNamespace();
  final String filterStr = requirement.getDirectives().get("filter");
  Filter filter;
  if (filterStr != null) {
    try {
      filter = FrameworkUtil.createFilter(filterStr);
    } catch (InvalidSyntaxException ise) {
      final String msg = "Invalid filter directive '" + filterStr + "': " + ise;
      throw new IllegalArgumentException(msg, ise);
    }
  } else {
    filter = null;
  }
  HashSet<BundleCapability> res = new HashSet<BundleCapability>();
  for (BundleGeneration bg : fwCtx.bundles.getBundleGenerations(null)) {
    BundleRevisionImpl bri = bg.bundleRevision;
    if (bri != null) {
      for (BundleCapability bc : bri.getDeclaredCapabilities(namespace)) {
        if (null == filter || filter.matches(bc.getAttributes())) {
          res.add(bc);
        }
      }
    }
  }
  return res;
}
 
Example #17
Source File: RequireBundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean matches(BundleCapability capability)
{
  if (BundleRevision.BUNDLE_NAMESPACE.equals(capability.getNamespace())) {
    return toFilter().matches(capability.getAttributes());
  }
  return false;
}
 
Example #18
Source File: BundleCapabilityImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static CapabilityDTO getDTO(BundleCapability bc, BundleRevisionImpl bri) {
  CapabilityDTO res = new CapabilityDTO();
  res.id = ((DTOId)bc).dtoId;
  res.namespace = bc.getNamespace();
  res.directives = new HashMap<String, String>(bc.getDirectives());
  res.attributes = Util.safeDTOMap(bc.getAttributes());
  res.resource = bri.dtoId;
  return res;
}
 
Example #19
Source File: ResolverHooks.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
boolean filterMatches(BundleRequirement requirement , BundleCapability candidate) throws BundleException {
  if (hasHooks()) {
    Collection<BundleCapability> c = new ShrinkableSingletonCollection<BundleCapability>(candidate);
    filterMatches(requirement, c);
    return !c.isEmpty();
  }
  return true;
}
 
Example #20
Source File: Fragment.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean matches(BundleCapability capability) {
  if (BundleRevision.HOST_NAMESPACE.equals(capability.getNamespace())) {
    return toFilter().matches(capability.getAttributes());
  }
  return false;
}
 
Example #21
Source File: BundleCapabilityImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public BundleCapabilityImpl(BundleCapability bc, BundleGeneration bg) {
  gen = bg;
  owner = ((BundleCapabilityImpl)bc).owner;
  namespace = bc.getNamespace();
  attributes = bc.getAttributes();
  directives = bc.getDirectives();
}
 
Example #22
Source File: WiringHTMLDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
String getCapName(final BundleCapability capability)
{
  final StringBuffer sb = new StringBuffer(50);

  // Make a modifiable clone of the capability attributes.
  final Map<String, Object> attrs
    = new HashMap<String, Object>(capability.getAttributes());

  sb.append(attrs.remove(BundleRevision.PACKAGE_NAMESPACE));

  final Version version = (Version) attrs
      .remove(Constants.VERSION_ATTRIBUTE);
  if (version!=null) {
    sb.append("&nbsp;");
    sb.append(version);
  }

  attrs.remove(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE);
  attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
  if (!attrs.isEmpty()) {
    sb.append("&nbsp;");
    sb.append(attrs);
  }

  final BundleWiring capWiring = capability.getRevision().getWiring();
  appendPendingRemovalOnRefresh(sb, capWiring);

  return sb.toString();
}
 
Example #23
Source File: WiringHTMLDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
String getCapName(final BundleCapability capability)
{
  // Make a modifiable clone of the attributes.
  final Map<String, Object> attrs =
    new HashMap<String, Object>(capability.getAttributes());

  final StringBuffer sb = new StringBuffer(50);
  sb.append(attrs.remove(BundleRevision.BUNDLE_NAMESPACE));

  final Version version =
    (Version) attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
  if (version != null) {
    sb.append("&nbsp;");
    sb.append(version);
  }

  if (!attrs.isEmpty()) {
    sb.append("&nbsp;");
    sb.append(attrs);
  }

  final BundleWiring capWiring = capability.getRevision().getWiring();
  appendPendingRemovalOnRefresh(sb, capWiring);

  return sb.toString();
}
 
Example #24
Source File: WiringHTMLDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
String getCapName(final BundleCapability capability)
{
  // Make a modifiable clone of the attributes.
  final Map<String, Object> attrs
    = new HashMap<String, Object>(capability.getAttributes());

  final StringBuffer sb = new StringBuffer(50);
  sb.append(attrs.remove(BundleRevision.HOST_NAMESPACE));

  final Version version =
    (Version) attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
  if (version != null) {
    sb.append("&nbsp;");
    sb.append(version);
  }

  if (!attrs.isEmpty()) {
    sb.append("&nbsp;");
    sb.append(attrs);
  }

  final BundleWiring capWiring = capability.getRevision().getWiring();
  appendPendingRemovalOnRefresh(sb, capWiring);

  return sb.toString();
}
 
Example #25
Source File: WiringHTMLDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
String getCapName(final BundleCapability capability)
{
  // Make a modifiable clone of the attributes.
  final Map<String, Object> attrs
    = new HashMap<String, Object>(capability.getAttributes());

  final StringBuffer sb = new StringBuffer(50);
  sb.append(attrs.remove(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE));

  @SuppressWarnings("unchecked")
  final List<Version> versions = (List<Version>) attrs
      .remove(Constants.VERSION_ATTRIBUTE);
  if (versions!=null) {
    sb.append("&nbsp;");
    sb.append(versions);
  }

  if (!attrs.isEmpty()) {
    sb.append("&nbsp;");
    sb.append(attrs);
  }

  final BundleWiring capWiring = capability.getRevision().getWiring();
  appendPendingRemovalOnRefresh(sb, capWiring);

  return sb.toString();
}
 
Example #26
Source File: WiringHTMLDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get a HTML-formated presentation string for the capability described by
 * the given {@code capability}.
 * @param capability capability to be named.
 * @return
 */
String getCapName(final BundleCapability capability)
{
  final StringBuffer sb = new StringBuffer(50);
  sb.append(capability.getAttributes());

  final BundleWiring capWiring = capability.getRevision().getWiring();
  appendPendingRemovalOnRefresh(sb, capWiring);

  return sb.toString();
}
 
Example #27
Source File: ManifestHelper.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public List<String> getExportedPackages() {
    MutableList<String> result = MutableList.of();
    for (BundleCapability c : parse.getCapabilities()) {
        if (WIRING_PACKAGE.equals(c.getNamespace())) {
            result.add((String) c.getAttributes().get(WIRING_PACKAGE));
        }
    }
    return result;
}
 
Example #28
Source File: FuchsiaUtils.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
/**
 * Load the Class of name <code>klassName</code>.
 * TODO : handle class version
 *
 * @param context   The BundleContext
 * @param klassName The Class name
 * @return The Class of name <code>klassName</code>
 * @throws ClassNotFoundException if we can't load the Class of name <code>klassName</code>
 */
public static Class<?> loadClassNew(BundleContext context, String klassName) throws ClassNotFoundException {
    // extract package
    String packageName = klassName.substring(0, klassName.lastIndexOf('.'));
    BundleCapability exportedPackage = getExportedPackage(context, packageName);
    if (exportedPackage == null) {
        throw new ClassNotFoundException("No package found with name " + packageName + " while trying to load the class "
                + klassName + ".");
    }
    return exportedPackage.getRevision().getBundle().loadClass(klassName);
}
 
Example #29
Source File: ConciergeJvmOptions.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
private void checkEECapsForJava7(List<BundleCapability> caps)
		throws Exception {
	// we expect for "JavaSE 7" 2 capabilities:
	// JavaSE, OSGi/Minimum
	Assert.assertEquals(2, caps.size());
	Iterator<BundleCapability> iter = caps.iterator();
	while (iter.hasNext()) {
		BundleCapability cap = iter.next();

		// get the details for a capability
		String ee = (String) cap.getAttributes().get("osgi.ee");
		@SuppressWarnings("unchecked")
		List<String> version = (List<String>) cap.getAttributes()
				.get("version");

		if (ee.equals("JavaSE")) {
			// version=[1.7.0, 1.6.0, 1.5.0, 1.4.0, 1.3.0,
			// 1.2.0, 1.1.0]
			Assert.assertEquals(7, version.size());
		} else if (ee.equals("OSGi/Minimum")) {
			// version=[1.2.0, 1.1.0, 1.0.0]
			Assert.assertEquals(3, version.size());
		} else {
			Assert.fail("Unknown capability namespace " + ee + " found");
		}
	}
}
 
Example #30
Source File: BundleRevisionImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
List<CapabilityRefDTO> getCapabilityRefDTOs() {
  List<CapabilityRefDTO> res = new ArrayList<CapabilityRefDTO>();
  for (BundleCapability bc :  getDeclaredCapabilities(null)) {
    res.add(BundleCapabilityImpl.getRefDTO(bc, this));
  }
  return res;
}