org.osgi.resource.Capability Java Examples

The following examples show how to use org.osgi.resource.Capability. 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: ResolveContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int insertHostedCapability(List<Capability> caps,
		HostedCapability hc) {
	List<Resource> resources = explicitResources;
	int index = resources.indexOf(hc.getResource());
	for (int i = 0; i < caps.size(); ++i) {
		Capability c = caps.get(i); 
		int otherIndex = resources.indexOf(c.getResource());
		if (otherIndex > index ) {
			caps.add(i, hc);
			return i;
		}
	}
	caps.add(hc);
	return caps.size()-1;
}
 
Example #2
Source File: NsToHtmlEE.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String toHTML(Capability 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);
  }

  return sb.toString();
}
 
Example #3
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
protected List<Capability> findProviders(Requirement requirement){
	final String filterStr = requirement.getDirectives()
			.get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);

	final List<Capability> providers;
	if (filterStr == null) {
		providers = capabilityRegistry
				.getAll(requirement.getNamespace());
	} else {
		try {
			providers = RFC1960Filter.filterWithIndex(
					requirement, filterStr, capabilityRegistry);
		} catch (final InvalidSyntaxException ise) {
			// TODO: debug output
			ise.printStackTrace();
			return Collections.emptyList();
		}
	}
	
	return providers;
}
 
Example #4
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the location to use when installing this resource.
 *
 * If available, the resource URL will be used as the location. Otherwise we
 * simply use the hash code of the resource.
 *
 * @param resource
 *          the resource to determine the installation location for.
 * @return location to use when installing this resource or {@code null} if
 *         location is available for this resource.
 */
public static String getLocation(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    if (supportedMimeTypes.contains(attrs
        .get(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE))) {
      final String url =
        (String) attrs.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
      if (url != null) {
        return url;
      }
    }
  }
  return null;
}
 
Example #5
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
static boolean matches0(final String namespace, final Requirement req,
		final Capability cap, final String filterStr) {
	if (!namespace.startsWith("osgi.wiring.")) {
		return true;
	}

	final String mandatory = cap.getDirectives()
			.get(Namespace.RESOLUTION_MANDATORY);

	if (mandatory == null) {
		return true;
	}

	final Set<String> mandatoryAttributes = new HashSet<String>(
			Arrays.asList(Utils.splitString(
					Utils.unQuote(mandatory).toLowerCase(), ',')));
	final Matcher matcher = FILTER_ASSERT_MATCHER
			.matcher(filterStr == null ? "" : filterStr);
	while (matcher.find()) {
		mandatoryAttributes.remove(matcher.group(1));
	}
	return mandatoryAttributes.isEmpty();
}
 
Example #6
Source File: NsToHtmlBundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String toHTML(Capability 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);
  }

  return sb.toString();
}
 
Example #7
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get Fragments matching a host bundle.
 * 
 * @param hostBundle
 *            , the host bundle, for which fragments should be found
 * @return an array of fragments, which should be attached to the host
 *         bundle
 */
List<Revision> getFragments(final BundleRevision hostBundle) {
	final List<Revision> candidates = new ArrayList<Revision>(
			fragmentIndex.lookup(hostBundle.getSymbolicName()));

	if (!candidates.isEmpty()) {
		final Capability cap = hostBundle
				.getCapabilities(HostNamespace.HOST_NAMESPACE).get(0);
		for (final Iterator<Revision> iter = candidates.iterator(); iter
				.hasNext();) {
			final Requirement req = iter.next()
					.getRequirements(HostNamespace.HOST_NAMESPACE).get(0);
			if (!matches(req, cap)) {
				iter.remove();
			}
		}
	}

	return candidates;
}
 
Example #8
Source File: RepositoryDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * BND generated XML repository files never contains a value for the
 * optional attributes in the identity name space, also look for it in the
 * KF-name space.
 *
 * @param key
 *          Attribute key to look up.
 *
 * @return the attribute value of the resource that this node represents.
 */
private String getIdAttribute(final String key)
{
  for (final Capability idCap : idCaps) {
    final Map<String, Object> attrs = idCap.getAttributes();

    final String description = (String) attrs.remove(key);
    if (description != null && description.length() > 0) {
      return description;
    }
  }
  if (kfExtraCaps != null && kfExtraCaps.size() > 0) {
    return (String) kfExtraCaps.iterator().next().getAttributes()
        .get(key);
  }
  return null;
}
 
Example #9
Source File: ResolveContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void addToProvidersIfMatching(Resource res, List<Capability> providers, Requirement req) {
	String f = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
	Filter filter = null;
	if(f != null) {
	  try {
	    filter = bc.createFilter(f);
	  } catch (InvalidSyntaxException e) {
	    // TODO log filter failure, skip
	    System.err.println("Failed, " + f + ". " + e);
	    return;
	  }
	}
	for(Capability c : res.getCapabilities(req.getNamespace())) {
	  if(filter != null && !filter.matches(c.getAttributes())) {
	    continue;
	  }
	  providers.add(c);
	}
}
 
Example #10
Source File: Resources.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
void addWire(final BundleWire wire) {
	final Capability cap = wire.getCapability();
	final Requirement req = wire.getRequirement();
	if (wire.getProvider() == revision) {
		providedWires.insert(cap.getNamespace(), wire);
		inUseSet.add(wire.getRequirer());
		((ConciergeBundleWire) wire).providerWiring = this;
	} else {
		requiredWires.insert(req.getNamespace(), wire);
		if (HostNamespace.HOST_NAMESPACE.equals(wire.getRequirement()
				.getNamespace())) {
			inUseSet.add(wire.getProvider());
		}
		((ConciergeBundleWire) wire).requirerWiring = this;
	}
}
 
Example #11
Source File: NsToHtmlGeneric.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String toHTML(Capability capability)
{
  final StringBuffer sb = new StringBuffer(100);
  sb.append("<table border='0'>\n");
  // Generic HTML presentation
  for (final Entry<String, Object> attr : capability.getAttributes()
      .entrySet()) {
    Util.toHTMLtr13_3(sb, "=", attr.getKey(), attr.getValue());
  }
  for (final Entry<String, String> dir : capability.getDirectives()
      .entrySet()) {
    Util.toHTMLtr13_3(sb, ":=", dir.getKey(), dir.getValue());
  }
  sb.append("</table>\n");
  return sb.toString();
}
 
Example #12
Source File: NsToHtmlHost.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String toHTML(Capability 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);
  }

  return sb.toString();
}
 
Example #13
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
boolean remove(final Capability cap) {
	final String namespace = cap.getNamespace();
	capabilities.remove(namespace, cap);

	final Object defaultAttribute = cap.getAttributes().get(namespace);

	final MultiMap<String, Capability> attributeIndex = defaultAttributeIndex
			.get(namespace);
	if (attributeIndex == null) {
		return false;
	}

	if (defaultAttribute != null
			&& defaultAttribute instanceof String) {
		final boolean success = attributeIndex.remove(defaultAttribute,
				cap);
		if (success) {
			if (attributeIndex.isEmpty()) {
				defaultAttributeIndex.remove(namespace);
			}
		}
		return success;
	} else {
		return false;
	}
}
 
Example #14
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 #15
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the MIME-type of a repository resource.
 *
 * @param resource
 *          The resource to get the MIME type for.
 *
 * @return Resource MIME type or {@code null} if no MIME-type available.
 */
public static String getResourceMime(Resource resource)
{
  String res = null;
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final String mime =
      (String) attrs.get(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE);
    if (mime != null) {
      res = mime;
      break;
    }
  }
  return res;
}
 
Example #16
Source File: NsToHtmlContent.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toHTML(Capability capability)
{
  // mime, url, size, sha

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

  final StringBuffer sb = new StringBuffer(50);

  Object val;

  val = attrs.remove(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
  if (val != null) {
    sb.append(Util.toHTML(val)).append("<br>");
  }

  val = attrs.remove(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE);
  if (val != null) {
    sb.append("Size: ").append(val).append("&nbsp;bytes.<br>");
  }

  val = attrs.remove(ContentNamespace.CONTENT_NAMESPACE);
  if (val != null) {
    sb.append("SHA-256: ").append(val).append("<br>");
  }

  val = attrs.remove(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE);
  if (val != null) {
    sb.append("Content-Type: ").append(val).append("<br>");
  }

  if (!attrs.isEmpty()) {
    sb.append("Other attributes: ");
    sb.append(attrs);
  }

  return sb.toString();
}
 
Example #17
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void ensureOsgiContentUrlsAreAbsolute(URL repositoryUrl, Collection<Resource> rs) throws Exception {
  for(Resource r : rs) {
    for(Capability c : r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
      String url = (String)c.getAttributes().get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
      url = new URL(repositoryUrl, url).toExternalForm();
      c.getAttributes().put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, url);
    }
  }
}
 
Example #18
Source File: ResourceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<Capability> getCapabilities(String namespace) {
  List<Capability> result = cs;
  if (namespace != null) {
    result = new ArrayList<Capability>();
    for (Capability c : cs) {
      if (namespace.equalsIgnoreCase(c.getNamespace())) {
        result.add(c);
      }
    }
  }
  return Collections.unmodifiableList(result);
}
 
Example #19
Source File: ResourceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public InputStream getContent() {
  try {
    Capability c = getCapabilities(ContentNamespace.CONTENT_NAMESPACE).get(0);
    return new URL((String) c.getAttributes().get(
        ContentNamespace.CAPABILITY_URL_ATTRIBUTE)).openStream();
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}
 
Example #20
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
public List<Capability> getByKey(final String namespace,
		final String value) {
	final MultiMap<String, Capability> caps = defaultAttributeIndex
			.get(namespace);
	return caps == null ? Collections.<Capability> emptyList()
			: caps.get(value);
}
 
Example #21
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
public int compare(final Capability c1, final Capability c2) {
	if (!(c1 instanceof BundleCapability
			&& c2 instanceof BundleCapability)) {
		return 0;
	}

	final BundleCapability cap1 = (BundleCapability) c1;
	final BundleCapability cap2 = (BundleCapability) c2;

	final int cap1Resolved = cap1.getResource().getWiring() == null ? 0
			: 1;
	final int cap2Resolved = cap2.getResource().getWiring() == null ? 0
			: 1;
	int score = cap2Resolved - cap1Resolved;
	if (score != 0) {
		return score;
	}

	final Version cap1Version = (Version) cap1.getAttributes()
			.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
	final Version cap2Version = (Version) cap2.getAttributes()
			.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);

	score = cap2Version.compareTo(cap1Version);

	if (score != 0) {
		return score;
	}

	final long cap1BundleId = cap1.getRevision().getBundle()
			.getBundleId();
	final long cap2BundleId = cap2.getRevision().getBundle()
			.getBundleId();

	return (int) (cap1BundleId - cap2BundleId);
}
 
Example #22
Source File: NsToHtmlPackage.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toHTML(Capability 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);
  }

  return sb.toString();
}
 
Example #23
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the icon for a repository resource.
 *
 * The icon is specified as the value of the bundle manifest entry named
 * {@code Bundle-Icon}.
 *
 * @param resource
 *          The resource to get the icon for.
 *
 * @return Resource icon string or {@code null} if icon information is
 *         not available.
 */
public static String getResourceIcon(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("icon");
    if (val != null) {
      return (String) val;
    }
  }
  return null;
}
 
Example #24
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
void add(final Capability cap) {
	final String namespace = cap.getNamespace();
	capabilities.insert(namespace, cap);

	final Object defaultAttribute = cap.getAttributes().get(namespace);
	if (defaultAttribute instanceof String) {
		MultiMap<String, Capability> attributeIndex = defaultAttributeIndex
				.get(namespace);
		if (attributeIndex == null) {
			attributeIndex = new MultiMap<String, Capability>();
			defaultAttributeIndex.put(namespace, attributeIndex);
		}
		attributeIndex.insert((String) defaultAttribute, cap);
	}
}
 
Example #25
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the vendor of a repository resource.
 *
 * The vendor is specified as the value of the bundle manifest entry named
 * {@link Constants#BUNDLE_VENDOR}.
 *
 * @param resource
 *          The resource to get the vendor for.
 *
 * @return Resource vendor or {@code "[no vendor]"} if vendor information is
 *         not available.
 */
public static String getResourceVendor(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("vendor");
    if (val != null) {
      return (String) val;
    }
  }
  return "[no vendor]";
}
 
Example #26
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the name of a repository resource from the identity name space."
 */
public static String getResourceName(Resource resource)
{
  final List<Capability> idCaps =
    resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
  if (idCaps.size() != 1) {
    Activator.log.error("Found " + idCaps.size()
                        + " identity capabilites expected one: " + idCaps);
    return resource.toString();
  }
  final Capability idCap = idCaps.get(0);
  return idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE)
      .toString();
}
 
Example #27
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get version of a repository resource form the identity name space.
 */
public static Version getResourceVersion(Resource resource)
{
  final List<Capability> idCaps =
    resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
  if (idCaps.size() != 1) {
    Activator.log.error("Found " + idCaps.size()
                        + " identity capabilites expected one: " + idCaps);
    return Version.emptyVersion;
  }
  final Capability idCap = idCaps.get(0);
  return (Version) idCap.getAttributes()
      .get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
}
 
Example #28
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the type of a repository resource from the identity name space."
 *
 * @param resource
 *          The resource to get the type for.
 *
 * @return Type as a string, one of {@link IdentityNamespace#TYPE_BUNDLE},
 *         {@link IdentityNamespace#TYPE_FRAGMENT}, and
 *         {@link IdentityNamespace#TYPE_UNKNOWN}.
 */
public static String getResourceType(Resource resource)
{
  final List<Capability> idCaps =
    resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
  for (final Capability idCap : idCaps) {
    final String type =
      (String) idCap.getAttributes()
          .get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE);
    if (type != null) {
      return type;
    }
  }
  return "&mdash;";
}
 
Example #29
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the size in bytes of a repository resource.
 *
 * @param resource
 *          The resource to get the size of.
 *
 * @return Resource size in byte or {@code -1} if no size available.
 */
public static long getResourceSize(Resource resource)
{
  long res = -1;
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Long size =
      (Long) attrs.get(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE);
    if (size != null) {
      res = size.longValue();
    }
  }
  return res;
}
 
Example #30
Source File: Util.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the category of a repository resource.
 *
 * The category is specified as the value of the bundle manifest entry named
 * {@link Constants#BUNDLE_CATEGORY}.
 *
 * @param resource
 *          The resource to get the category for.
 *
 * @return Resource category or {@code "[no category]"} if no category is available.
 */
public static String getResourceCategory(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("category");
    if (val != null) {
      return (String) val;
    }
  }
  return "[no category]";
}