use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.
the class ResourceTest method assertResourceEquals.
void assertResourceEquals(boolean expected, String a, String... b) throws Exception {
ResourceBuilder rba = new ResourceBuilder();
if (a != null) {
CapReqBuilder cap = new CapReqBuilder("osgi.content");
cap.addAttribute("url", a);
cap.addAttribute("osgi.content", defaultSHA);
rba.addCapability(cap);
}
ResourceBuilder rbb = new ResourceBuilder();
int n = 0;
for (String bb : b) {
CapReqBuilder cap = new CapReqBuilder("osgi.content");
cap.addAttribute("url", bb);
String sha;
if (b.length > 1 && b.length == n + 1)
sha = alternativeSHA;
else
sha = defaultSHA;
cap.addAttribute("osgi.content", sha);
rbb.addCapability(cap);
n++;
}
assertEquals(expected, rba.build().equals(rbb.build()));
}
use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.
the class ResourceTest method testImportExportService.
public void testImportExportService() throws Exception {
ResourceBuilder rb = new ResourceBuilder();
rb.addImportServices(new Parameters(is));
rb.addExportServices(new Parameters(es));
Resource build = rb.build();
assertConfigAdminServices(build);
}
use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.
the class ResourceTest method testSnapshotResourceToVersionedClause.
public void testSnapshotResourceToVersionedClause() throws Exception {
ResourceBuilder rb = new ResourceBuilder();
rb.addManifest(Domain.domain(IO.getFile("../demo-fragment/generated/demo-fragment.jar")));
Attrs attrs = new Attrs();
attrs.put("bnd.workspace.project", "demo-fragment");
rb.addCapability(CapabilityBuilder.createCapReqBuilder("bnd.workspace.project", attrs));
Resource resource = rb.build();
VersionedClause versionClause = ResourceUtils.toVersionClause(resource, "[===,==+)");
StringBuilder sb = new StringBuilder();
versionClause.formatTo(sb);
assertEquals("demo-fragment;version=snapshot", sb.toString());
}
use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.
the class P2Indexer method readRepository.
private Repository readRepository() throws Exception {
P2Impl p2 = new P2Impl(client, this.url, Processor.getExecutor());
List<Artifact> artifacts = p2.getArtifacts();
List<Promise<Resource>> fetched = new ArrayList<>(artifacts.size());
Set<URI> visitedURIs = new HashSet<>(artifacts.size());
Set<ArtifactID> visitedArtifacts = new HashSet<>(artifacts.size());
Map<ArtifactID, Resource> knownResources = new HashMap<>();
if (getBridge() != null) {
for (Capability capability : getBridge().getRepository().findProviders(singleton(MD5_REQUIREMENT)).get(MD5_REQUIREMENT)) {
Resource resource = capability.getResource();
IdentityCapability identity = ResourceUtils.getIdentityCapability(resource);
ArtifactID artifact = new ArtifactID(identity.osgi_identity(), identity.version(), (String) capability.getAttributes().get(MD5_ATTRIBUTE));
knownResources.put(artifact, resource);
}
}
for (final Artifact a : artifacts) {
if (!visitedURIs.add(a.uri))
continue;
if (a.md5 != null) {
ArtifactID id = new ArtifactID(a.id, toVersion(a.version), a.md5);
if (!visitedArtifacts.add(id))
continue;
if (knownResources.containsKey(id)) {
fetched.add(Promises.resolved(knownResources.get(id)));
continue;
}
}
Promise<Resource> promise = client.build().useCache(MAX_STALE).async(a.uri.toURL()).map(new Function<File, Resource>() {
@Override
public Resource apply(File file) {
try {
ResourceBuilder rb = new ResourceBuilder();
rb.addFile(file, a.uri);
if (a.md5 != null)
rb.addCapability(new CapabilityBuilder(P2_CAPABILITY_NAMESPACE).addAttribute(MD5_ATTRIBUTE, a.md5));
return rb.build();
} catch (Exception e) {
logger.debug("{}: Failed to create resource for %s from {}", name, a, file, e);
return RECOVERY;
}
}
}).recover(new Function<Promise<?>, Resource>() {
@Override
public Resource apply(Promise<?> failed) {
try {
logger.debug("{}: Failed to create resource for {}", name, a, failed.getFailure());
} catch (InterruptedException e) {
// impossible
}
return RECOVERY;
}
});
fetched.add(promise);
}
Promise<List<Resource>> all = Promises.all(fetched);
return all.map(new Function<List<Resource>, ResourcesRepository>() {
@Override
public ResourcesRepository apply(List<Resource> resources) {
ResourcesRepository rr = new ResourcesRepository();
for (Resource resource : resources) {
if (resource != RECOVERY) {
rr.add(resource);
}
}
return rr;
}
}).getValue();
}
use of aQute.bnd.osgi.resource.ResourceBuilder in project bnd by bndtools.
the class IndexerMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
logger.debug("skip project as configured");
return;
}
if (scopes == null || scopes.isEmpty()) {
scopes = Arrays.asList("compile", "runtime");
}
logger.debug("Indexing dependencies with scopes: {}", scopes);
logger.debug("Including Transitive dependencies: {}", includeTransitive);
logger.debug("Local file URLs permitted: {}", localURLs);
logger.debug("Adding mvn: URLs as alternative content: {}", addMvnURLs);
DependencyResolver dependencyResolver = new DependencyResolver(project, session, resolver, system, scopes, includeTransitive, new RemotePostProcessor(session, system, metadataReader, localURLs));
Map<File, ArtifactResult> dependencies = dependencyResolver.resolve();
Map<String, ArtifactRepository> repositories = new HashMap<>();
for (ArtifactRepository artifactRepository : project.getRemoteArtifactRepositories()) {
logger.debug("Located an artifact repository {}", artifactRepository.getId());
repositories.put(artifactRepository.getId(), artifactRepository);
}
ArtifactRepository deploymentRepo = project.getDistributionManagementArtifactRepository();
if (deploymentRepo != null) {
logger.debug("Located a deployment repository {}", deploymentRepo.getId());
if (repositories.get(deploymentRepo.getId()) == null) {
repositories.put(deploymentRepo.getId(), deploymentRepo);
} else {
logger.info("The configured deployment repository {} has the same id as one of the remote artifact repositories. It is assumed that these repositories are the same.", deploymentRepo.getId());
}
}
RepositoryURLResolver repositoryURLResolver = new RepositoryURLResolver(repositories);
MavenURLResolver mavenURLResolver = new MavenURLResolver();
ResourcesRepository resourcesRepository = new ResourcesRepository();
XMLResourceGenerator xmlResourceGenerator = new XMLResourceGenerator();
logger.debug("Indexing artifacts: {}", dependencies.keySet());
try {
IO.mkdirs(outputFile.getParentFile());
for (Entry<File, ArtifactResult> entry : dependencies.entrySet()) {
File file = entry.getKey();
ResourceBuilder resourceBuilder = new ResourceBuilder();
resourceBuilder.addFile(entry.getKey(), repositoryURLResolver.resolver(file, entry.getValue()));
if (addMvnURLs) {
CapabilityBuilder c = new CapabilityBuilder(ContentNamespace.CONTENT_NAMESPACE);
c.addAttribute(ContentNamespace.CONTENT_NAMESPACE, SHA256.digest(file).asHex());
c.addAttribute(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, mavenURLResolver.resolver(file, entry.getValue()));
c.addAttribute(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE, file.length());
c.addAttribute(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE, MavenURLResolver.MIME);
resourceBuilder.addCapability(c);
}
resourcesRepository.add(resourceBuilder.build());
}
if (includeJar && project.getPackaging().equals("jar")) {
File current = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".jar");
if (current.exists()) {
ResourceBuilder resourceBuilder = new ResourceBuilder();
resourceBuilder.addFile(current, current.toURI());
resourcesRepository.add(resourceBuilder.build());
}
}
xmlResourceGenerator.repository(resourcesRepository).save(outputFile);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
if (fail) {
throw new MojoExecutionException("One or more URI lookups failed");
}
attach(outputFile, "osgi-index", "xml");
if (includeGzip) {
File gzipOutputFile = new File(outputFile.getPath() + ".gz");
try {
xmlResourceGenerator.save(gzipOutputFile);
} catch (Exception e) {
throw new MojoExecutionException("Unable to create the gzipped output file");
}
attach(gzipOutputFile, "osgi-index", "xml.gz");
}
}
Aggregations