Search in sources :

Example 1 with PersistentResource

use of aQute.bnd.osgi.resource.PersistentResource in project bnd by bndtools.

the class InfoRepositoryWrapper method init.

boolean init() {
    try {
        if (System.currentTimeMillis() < lastTime + 10000)
            return true;
    } finally {
        lastTime = System.currentTimeMillis();
    }
    Set<String> errors = new LinkedHashSet<String>();
    try {
        //
        // Get the current repo contents
        //
        Set<String> toBeDeleted = new HashSet<String>(persistent.keySet());
        Map<String, DownloadBlocker> blockers = new HashMap<String, DownloadBlocker>();
        for (InfoRepository repo : repos) {
            Map<String, ResourceDescriptor> map = collectKeys(repo);
            for (final Map.Entry<String, ResourceDescriptor> entry : map.entrySet()) {
                final String id = entry.getKey();
                toBeDeleted.remove(id);
                if (persistent.containsKey(id))
                    continue;
                final ResourceDescriptor rd = entry.getValue();
                DownloadBlocker blocker = new DownloadBlocker(null) {

                    //
                    // We steal the thread of the downloader to index
                    //
                    @Override
                    public void success(File file) throws Exception {
                        IndexResult index = null;
                        try {
                            index = repoIndexer.indexFile(file);
                            ResourceBuilder rb = new ResourceBuilder();
                            for (org.osgi.service.indexer.Capability capability : index.capabilities) {
                                CapReqBuilder cb = new CapReqBuilder(capability.getNamespace());
                                cb.addAttributes(capability.getAttributes());
                                cb.addDirectives(capability.getDirectives());
                                rb.addCapability(cb.buildSyntheticCapability());
                            }
                            for (org.osgi.service.indexer.Requirement requirement : index.requirements) {
                                CapReqBuilder cb = new CapReqBuilder(requirement.getNamespace());
                                cb.addAttributes(requirement.getAttributes());
                                cb.addDirectives(requirement.getDirectives());
                                rb.addRequirement(cb.buildSyntheticRequirement());
                            }
                            Resource resource = rb.build();
                            PersistentResource pr = new PersistentResource(resource);
                            persistent.put(id, pr);
                        } finally {
                            super.success(file);
                            if (index != null) {
                                index.resource.close();
                            }
                        }
                    }
                };
                blockers.put(entry.getKey(), blocker);
                repo.get(rd.bsn, rd.version, null, blocker);
            }
        }
        for (Entry<String, DownloadBlocker> entry : blockers.entrySet()) {
            String key = entry.getKey();
            DownloadBlocker blocker = entry.getValue();
            String reason = blocker.getReason();
            if (reason != null) {
                errors.add(key + ": " + reason);
            }
        }
        persistent.keySet().removeAll(toBeDeleted);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!errors.isEmpty())
        throw new IllegalStateException("Cannot index " + repos + " due to " + errors);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) IndexResult(org.osgi.service.indexer.ResourceIndexer.IndexResult) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) Resource(org.osgi.resource.Resource) DownloadBlocker(aQute.bnd.build.DownloadBlocker) InfoRepository(aQute.bnd.service.repository.InfoRepository) IOException(java.io.IOException) HashMap(java.util.HashMap) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) PersistentMap(aQute.lib.persistentmap.PersistentMap) File(java.io.File) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 2 with PersistentResource

use of aQute.bnd.osgi.resource.PersistentResource in project bnd by bndtools.

the class PersistentResourceTest method testSimple.

public void testSimple() throws Exception {
    ResourceBuilder rb = new ResourceBuilder();
    rb.addCapability(new CapReqBuilder("test").addAttribute("double", 3.0).addAttribute("long", 3L).addAttribute("string", "3.0").addAttribute("version", new Version("3.0")).buildSyntheticCapability());
    Resource r = rb.build();
    PersistentResource pr = new PersistentResource(r);
    String s = new JSONCodec().enc().put(pr).toString();
    PersistentResource pr2 = new JSONCodec().dec().from(s).get(PersistentResource.class);
    List<Capability> capabilities = pr.getResource().getCapabilities(null);
    List<Requirement> requirements = pr.getResource().getRequirements(null);
    assertEquals(1, capabilities.size());
    assertEquals(0, requirements.size());
    Capability capability = capabilities.get(0);
    assertEquals("test", capability.getNamespace());
    assertEquals(3.0, capability.getAttributes().get("double"));
    assertEquals(3L, capability.getAttributes().get("long"));
    assertEquals("3.0", capability.getAttributes().get("string"));
    assertEquals(new Version("3.0"), capability.getAttributes().get("version"));
    assertEquals(0, capability.getDirectives().size());
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) Requirement(org.osgi.resource.Requirement) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) Resource(org.osgi.resource.Resource) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) JSONCodec(aQute.lib.json.JSONCodec)

Aggregations

CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)2 PersistentResource (aQute.bnd.osgi.resource.PersistentResource)2 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)2 Resource (org.osgi.resource.Resource)2 DownloadBlocker (aQute.bnd.build.DownloadBlocker)1 InfoRepository (aQute.bnd.service.repository.InfoRepository)1 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)1 MultiMap (aQute.lib.collections.MultiMap)1 JSONCodec (aQute.lib.json.JSONCodec)1 PersistentMap (aQute.lib.persistentmap.PersistentMap)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Version (org.osgi.framework.Version)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1 IndexResult (org.osgi.service.indexer.ResourceIndexer.IndexResult)1