下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.resource.Capability 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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));
}
}
}
}
}
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;
}
}
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;
}
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();
}
/**
* 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;
}
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;
}
}
@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(" ");
sb.append(version);
}
if (!attrs.isEmpty()) {
sb.append(" ");
sb.append(attrs);
}
return sb.toString();
}
/**
* 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;
}
@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();
}
@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(" ");
sb.append(version);
}
if (!attrs.isEmpty()) {
sb.append(" ");
sb.append(attrs);
}
return sb.toString();
}
/**
* 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;
}
/**
* 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;
}
@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(" ");
sb.append(versions);
}
if (!attrs.isEmpty()) {
sb.append(" ");
sb.append(attrs);
}
return sb.toString();
}
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);
}
}
@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;
}
private void addWire(final Wire wire) {
if (wire.getProvider() == resource) {
final Capability cap = wire.getCapability();
capabilities.insertUnique(cap.getNamespace(), cap);
providedWires.insert(cap.getNamespace(), wire);
} else {
final Requirement req = wire.getRequirement();
requirements.insertUnique(req.getNamespace(), req);
requiredWires.insert(req.getNamespace(), wire);
}
}
protected final BundleRevisionDTO getBundleRevisionDTO(BundleRevision revision){
BundleRevisionDTO dto = new BundleRevisionDTO();
dto.bundle = revision.getBundle().getBundleId();
dto.id = revision.hashCode();
dto.symbolicName = revision.getSymbolicName();
dto.type = revision.getTypes();
dto.version = getVersion().toString();
// add requirement/capabilities
List<Capability> caps = revision.getCapabilities(null);
dto.capabilities = new ArrayList<CapabilityDTO>(caps.size());
for(Capability c : caps){
CapabilityDTO capDTO = new CapabilityDTO();
capDTO.id = c.hashCode();
capDTO.namespace = c.getNamespace();
capDTO.resource = c.getResource().hashCode();
capDTO.attributes = getDTOMap(c.getAttributes());
capDTO.directives = new HashMap<String, String>(c.getDirectives());
dto.capabilities.add(capDTO);
}
List<Requirement> reqs = revision.getRequirements(null);
dto.requirements = new ArrayList<RequirementDTO>(reqs.size());
for(Requirement r : reqs){
RequirementDTO reqDTO = new RequirementDTO();
reqDTO.id = r.hashCode();
reqDTO.namespace = r.getNamespace();
reqDTO.resource = r.getResource().hashCode();
reqDTO.attributes = getDTOMap(r.getAttributes());
reqDTO.directives = new HashMap<String, String>(r.getDirectives());
dto.requirements.add(reqDTO);
}
return dto;
}
public int compare(final Capability cap1, final Capability cap2) {
final Version cap1Version = (Version) cap1.getAttributes().get(
AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
final Version cap2Version = (Version) cap2.getAttributes().get(
AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
return cap2Version.compareTo(cap1Version);
}
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);
}
/**
* @see org.osgi.framework.wiring.FrameworkWiring#findProviders(Requirement requirement)
*/
public Collection<BundleCapability> findProviders(Requirement requirement) {
final List<Capability> providers = resolver.findProviders(requirement);
final List<BundleCapability> result = new ArrayList<BundleCapability>(providers.size());
for(int i=0;i<providers.size();i++){
Capability cap = providers.get(i);
if(cap instanceof BundleCapability){
result.add((BundleCapability) cap);
}
}
return result;
}
private void hostFragment(final ResolveContext context,
final BundleRevision fragment, final BundleRevision host,
final MultiMap<Resource, Wire> solution) {
// host the capabilities
for (final Capability cap : fragment.getCapabilities(null)) {
if (!IdentityNamespace.IDENTITY_NAMESPACE
.equals(cap.getNamespace())) {
final HostedBundleCapability hostedCap = new HostedBundleCapability(
host, cap);
context.insertHostedCapability(
new ArrayList<Capability>(host.getCapabilities(
PackageNamespace.PACKAGE_NAMESPACE)),
hostedCap);
}
}
// create host wire
final Capability hostCapability = host
.getCapabilities(HostNamespace.HOST_NAMESPACE).get(0);
final Requirement hostRequirement = fragment
.getRequirements(HostNamespace.HOST_NAMESPACE).get(0);
final Wire wire = Resources.createWire(hostCapability,
hostRequirement);
solution.insert(fragment, wire);
solution.insert(host, wire);
}
static boolean matches(final Requirement req, final Capability cap) {
final String reqNamespace = req.getNamespace();
final String capNamespace = cap.getNamespace();
if (!reqNamespace.equals(capNamespace)) {
return false;
}
/*
* final String effective = req.getDirectives().get(
* Namespace.REQUIREMENT_EFFECTIVE_DIRECTIVE); if (!(effective == null
* || effective .equals(Namespace.EFFECTIVE_RESOLVE))) { return false; }
*/
final String filter = req.getDirectives()
.get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
try {
if (!(filter == null || RFC1960Filter.fromString(filter)
.matches(cap.getAttributes()))) {
return false;
}
return matches0(capNamespace, req, cap, filter);
} catch (final InvalidSyntaxException e) {
// TODO: to log
e.printStackTrace();
return false;
}
}
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);
}
}
public List<Capability> getByValue(final String namespace,
final String value) {
final MultiMap<String, Capability> attributeIndex = defaultAttributeIndex
.get(namespace);
final List<Capability> result = attributeIndex == null ? null : attributeIndex.get(value);
return result == null ? Collections.<Capability> emptyList()
: new ArrayList<Capability>(result);
}
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);
}
@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(" 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();
}
/**
* 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();
}
/**
* 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);
}
/**
* 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 "—";
}
/**
* 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;
}