org.osgi.resource.Resource Java Examples

The following examples show how to use org.osgi.resource.Resource. 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: 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 #2
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 #3
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
protected void filterResources(final Collection<ResolverHook> hooks,
		final Collection<Resource> resources,
		final Collection<Resource> removed) {
	final ArrayList<BundleRevision> revisions = new ArrayList<BundleRevision>();
	removed.addAll(resources);
	for (final Iterator<Resource> iter = resources.iterator(); iter
			.hasNext();) {
		final Resource res = iter.next();
		if (res instanceof BundleRevision) {
			revisions.add((BundleRevision) res);
			iter.remove();
		}
	}

	final ConciergeCollections.RemoveOnlyList<BundleRevision> filteredResources = new ConciergeCollections.RemoveOnlyList<BundleRevision>(
			revisions);

	for (final ResolverHook hook : resolver.hooks.keySet()) {
		hook.filterResolvable(filteredResources);
	}

	resources.addAll(filteredResources);
	removed.removeAll(filteredResources);
}
 
Example #4
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
public synchronized Map<Resource, List<Wire>> resolve(
		final ResolveContext context) throws ResolutionException {
	if (context == null) {
		throw new IllegalArgumentException("context is null");
	}

	final MultiMap<Resource, Wire> solution = new MultiMap<Resource, Wire>();
	final ArrayList<Requirement> unresolvedRequirements = new ArrayList<Requirement>();
	final ArrayList<Resource> unresolvedResources = new ArrayList<Resource>();

	resolve0(context, solution, unresolvedRequirements,
			unresolvedResources, true);

	if (!unresolvedRequirements.isEmpty()
			|| !unresolvedResources.isEmpty()) {
		throw new ResolutionException("Could not resolve.", null,
				unresolvedRequirements);
	}

	return solution.getFlatMap();
}
 
Example #5
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 #6
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 #7
Source File: RepositoryCommandGroup.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void printBundleResources(PrintWriter out, List<Resource> resources, boolean verbose) {
  out.println("I Bundle resource");
  out.println("- --------------------");
  for (Resource r : resources) {
    Map<String, Object> identity = r.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).iterator().next().getAttributes();
    out.print(isInstalled(r) ? "* " : "  ");
    out.print(identity.get(IdentityNamespace.IDENTITY_NAMESPACE));
    out.print(", version=");
    out.println(identity.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE));
    if (verbose) {
      Map<String, Object> content = r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).iterator().next().getAttributes();
      out.println("    Type: " + identity.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE));
      out.println("    URL:  " + content.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE));
      out.println("    Size: " + content.get(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE));
    }
  }
}
 
Example #8
Source File: RepositoryCommandGroup.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean isInstalled(Resource r) {
  Map<String, Object> identity = r.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).iterator().next().getAttributes();
  String name = (String) identity.get(IdentityNamespace.IDENTITY_NAMESPACE);
  Version version = (Version) identity.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
  Map<String, Object> content = r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).iterator().next().getAttributes();
  Bundle lb = bc.getBundle((String)content.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE));
  if (lb != null && name.equals(lb.getSymbolicName()) && version.equals(lb.getVersion())) {
    return true;
  }
  for (Bundle b : bc.getBundles()) {
    if (name.equals(b.getSymbolicName()) && version.equals(b.getVersion())) {
      return true;
    }
  }
  return false;
}
 
Example #9
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
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);
}
 
Example #10
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 #11
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]";
}
 
Example #12
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 #13
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 #14
Source File: ResolveContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public synchronized Map<Resource, Wiring> getWirings() { 
	if(wirings == null) {
		Map<Resource, Wiring> ws = new HashMap<Resource, Wiring>();
		Bundle[] bs = bc.getBundles();
		for(Bundle b : bs) {
			BundleRevision br = b.adapt(BundleRevision.class);
			ws.put(br, br.getWiring());
		}
		wirings = Collections.unmodifiableMap(ws);
	}
	return wirings;
}
 
Example #15
Source File: ExpressionResolver.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void run() {
  try {
    Collection<Resource> resolved = resolve(expression);
    if (resolved instanceof NegativeCollection) {
      RequirementBuilder rb = repository.newRequirementBuilder(ContentNamespace.CONTENT_NAMESPACE);
      Collection<Resource> rs = getResources(rb.build());
      rs.retainAll(resolved);
      resolved = rs;
    }
    deferred.resolve(resolved);
  } catch (Exception e) {
    deferred.fail(e);
  }
}
 
Example #16
Source File: RepositoryDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * If possible, get the bundle for a resource.
 *
 * @param resource
 *          the resource to search for
 * @return an installed bundle, if the resource seem to be installed
 */
static Bundle getBundle(Resource resource)
{
  final Bundle[] bl = bc.getBundles();

  for (int i = 0; bl != null && i < bl.length; i++) {
    if (Util.isBundleFromResource(bl[i], resource)) {
      return bl[i];
    }
  }
  return null;
}
 
Example #17
Source File: RepositoryCommandGroup.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int cmdShow(Dictionary<String,?> opts, Reader in, PrintWriter out,
    Session session) {
  final String namespace = (String) opts.get("namespace");
  final String filterStr = (String) opts.get("filter");

  BasicRequirement requirement;
  requirement = new BasicRequirement(namespace);
  if (filterStr != null) {
    try {
      FrameworkUtil.createFilter(filterStr);
    } catch (InvalidSyntaxException e) {
      out.println("Invalid filter: " + e.getMessage());
      return 1;
    }
    requirement.addDirective("filter", filterStr); 
  }
  List<Capability> cs = getRepositoryManager().findProviders(requirement);
  if (cs.isEmpty()) {
    out.println("No matching resources found!");
    return 0;
  }
  List<Resource> resources = new ArrayList<Resource>();
  for (Capability c : cs) {
    resources.add(c.getResource());
  }
  printResources(out, resources, namespace, opts.get("-t") == null);
  return 0;
}
 
Example #18
Source File: RepositoryCommandGroup.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int cmdBundle(Dictionary<String,?> opts, Reader in, PrintWriter out,
    Session session) {
  final boolean verbose = (opts.get("-l") != null);
  final String bsn = (String) opts.get("symbolicname");
  final String ver = (String) opts.get("versionRange");
  BasicRequirement requirement;
  if (bsn != null) {
    requirement = new BasicRequirement(IdentityNamespace.IDENTITY_NAMESPACE, bsn);
  } else {
    requirement = new BasicRequirement(IdentityNamespace.IDENTITY_NAMESPACE);
  }
  if (ver != null) {
    requirement.addVersionRangeFilter(new VersionRange(ver)); 
  }
  requirement.addBundleIdentityFilter();
  List<Resource> resources = new ArrayList<Resource>();
  for (Capability c : getRepositoryManager().findProviders(requirement)) {
    resources.add(c.getResource());
  }
  if (resources.isEmpty()) {
    out.println("No bundles found!");
    return 1;
  } else {
    printBundleResources(out, resources, verbose);
  }
  return 0;
}
 
Example #19
Source File: RepositoryDisplayer.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int compare(Resource r1, Resource r2)
{
  final String s1 = Util.getResourceName(r1).toLowerCase();
  final String s2 = Util.getResourceName(r2).toLowerCase();
  int n = 0;

  try {
    n = s1.compareTo(s2);
    if (n == 0) {
      final Version v1 = Util.getResourceVersion(r1);
      final Version v2 = Util.getResourceVersion(r2);
      n = v1.compareTo(v2);

      if (n == 0) {
        final String loc1 = Util.getLocation(r1);
        final String loc2 = Util.getLocation(r2);
        if (loc1 != null && loc2 != null) {
          n = loc1.compareTo(loc2);
        } else if (loc1 == null) {
          n = -1;
        } else {
          n = loc2 == null ? 0 : 1;
        }
      }
    }
  } catch (final Exception e) {
  }
  return n;
}
 
Example #20
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 #21
Source File: ExpressionResolver.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Collection<Resource> getResources(Requirement req)
{
  Collection<Capability> cs = repository.findProviders(Collections.singleton(req)).get(req);
  Collection<Resource> res = new ArrayList<Resource>(cs.size());
  for (Capability c : cs) {
    // TODO could resource be null?
    res.add(c.getResource());
  }
  return res;
}
 
Example #22
Source File: RepositoryManagerImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Set<Resource> findResolution(List<Resource> resources)
    throws Exception {
  Resolver resolver = resolverTracker.getService();
  if (resolver == null) {
    throw new Exception(
        "Unable to find resolution: No Resolver service available!");
  }

  ResolveContextImpl rc = new ResolveContextImpl(bc, resources,
      new ArrayList<Resource>());
  Map<Resource, List<Wire>> resolution = resolver.resolve(rc);
  return resolution.keySet();
}
 
Example #23
Source File: RepositoryImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Promise<Collection<Resource>> findProviders(RequirementExpression expression) {
  Deferred<Collection<Resource>> d = new Deferred<Collection<Resource>>();
  try {
    new ExpressionResolver(this, expression, d).start();
  } catch (Exception e) {
    d.fail(e);
  }
  return d.getPromise();
}
 
Example #24
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static ParseResult parseRepository(XmlPullParser p)
    throws XmlPullParserException, IOException, Exception {
  if(!"repository".equals(p.getName())) return null;
  
  ParseResult pr = new ParseResult();
  
  for(int i = 0; i < p.getAttributeCount(); ++i) {
    if("name".equalsIgnoreCase(p.getAttributeName(i))) {
      pr.name = p.getAttributeValue(i);
      continue;
    }
    if("increment".equalsIgnoreCase(p.getAttributeName(i))) {
      pr.increment = Long.parseLong(p.getAttributeValue(i).trim());
      continue;
    }
  }
  
  for (int  e = p.getEventType();
            e != XmlPullParser.END_DOCUMENT;
            e = p.next()) {
    Resource r = null;
    if (e == XmlPullParser.START_TAG) {
      r = parseResource(p);
    }
    if (r != null) {
      pr.resources.add(r);
    }
  }
  return pr;
}
 
Example #25
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 #26
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Resource parseResource(XmlPullParser p) throws Exception {
  if ("resource".equalsIgnoreCase(p.getName())) {
    return parseReqsAndCaps(p);
  } else {
    return null;
  }
}
 
Example #27
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Resource parseReqsAndCaps(XmlPullParser p) throws Exception {
  if (p.isEmptyElementTag()) {
    p.next();
    return null;
  }
  ResourceImpl r = new ResourceImpl();
  int startDepth = p.getDepth();
  p.next();
  while( !(p.getEventType() == XmlPullParser.END_DOCUMENT)
      && !(p.getEventType() == XmlPullParser.END_TAG 
      && p.getDepth() == startDepth)) {
    if (p.getEventType() != XmlPullParser.START_TAG) {
      p.next();
      continue;
    } 
    if ("requirement".equalsIgnoreCase(p.getName())) {
      RequirementImpl req = parseReq(p);
      if(req != null) {
        req.d.resource = r;
        r.addReq(req);
      }
      p.next();
      continue;
    }
    if ("capability".equalsIgnoreCase(p.getName())) {
      CapabilityImpl cap = parseCap(p);
      if(cap != null) {
        cap.d.resource = r;
        r.addCap(cap);
      }
      p.next();
      continue;
    }
  }
  return r;
}
 
Example #28
Source File: RepositoryXmlParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void debug(ParseResult pr) {
  System.out.println("======= BEGIN PARSED REPOSITORY XML ======="); 
  for(Resource r : pr.resources) {
   System.out.println(r.toString()); 
  }
  System.out.println("======== END PARSED REPOSITORY XML ========");
  System.out.println("======== NAME: " + pr.name);
  System.out.println("======== INCREMENT: " + pr.increment);
  System.out.println("======== RESOURCES FOUND: " + pr.resources.size());
  System.out.flush();
}
 
Example #29
Source File: ExpressionResolver.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ExpressionResolver(Repository repository,
                          RequirementExpression expression,
                          Deferred<Collection<Resource>> deferred)
{
  this.repository = repository;
  this.expression = expression;
  this.deferred = deferred;
}
 
Example #30
Source File: ResourceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean equals(Object other) {
  if (this == other)
    return true;
  if (other == null)
    return false;
  if (!(other instanceof Resource))
    return false;
  Resource r = (Resource) other;
  return getCapabilities(null).equals(r.getCapabilities(null))
      && getRequirements(null).equals(r.getRequirements(null));
}