use of org.osgi.service.indexer.Capability in project bnd by bndtools.
the class TestBundleAnalyzer method testPackageExports.
public void testPackageExports() throws Exception {
BundleAnalyzer a = new BundleAnalyzer(new XNullLogSvc());
LinkedList<Capability> caps = new LinkedList<Capability>();
LinkedList<Requirement> reqs = new LinkedList<Requirement>();
a.analyzeResource(new JarResource(new File("testdata/03-export.jar")), caps, reqs);
Capability export = findCaps("osgi.wiring.package", caps).get(0);
assertEquals("org.example.a", export.getAttributes().get("osgi.wiring.package"));
assertEquals(new Version(1, 0, 0), export.getAttributes().get("version"));
}
use of org.osgi.service.indexer.Capability in project bndtools by bndtools.
the class BuiltBundleIndexer method builtBundles.
@Override
public void builtBundles(final IProject project, IPath[] paths) {
IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
final URI workspaceRootUri = wsroot.getLocationURI();
Set<File> files = new HashSet<File>();
for (IPath path : paths) {
try {
IFile ifile = wsroot.getFile(path);
IPath location = ifile.getLocation();
if (location != null)
files.add(location.toFile());
} catch (IllegalArgumentException e) {
System.err.println("### Error processing path: " + path);
e.printStackTrace();
}
}
// Generate the index file
File indexFile;
try {
Project model = Central.getProject(project);
File target = model.getTarget();
indexFile = new File(target, INDEX_FILENAME);
IFile indexPath = wsroot.getFile(Central.toPath(indexFile));
// Create the indexer and add ResourceAnalyzers from plugins
RepoIndex indexer = new RepoIndex(logAdapter);
List<ResourceAnalyzer> analyzers = Central.getWorkspace().getPlugins(ResourceAnalyzer.class);
for (ResourceAnalyzer analyzer : analyzers) {
indexer.addAnalyzer(analyzer, null);
}
// Use an analyzer to add a marker capability to workspace resources
indexer.addAnalyzer(new ResourceAnalyzer() {
@Override
public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
Capability cap = new Builder().setNamespace("bndtools.workspace").addAttribute("bndtools.workspace", workspaceRootUri.toString()).addAttribute("project.path", project.getFullPath().toString()).buildCapability();
capabilities.add(cap);
}
}, null);
Map<String, String> config = new HashMap<String, String>();
config.put(ResourceIndexer.REPOSITORY_NAME, project.getName());
config.put(ResourceIndexer.ROOT_URL, project.getLocation().toFile().toURI().toString());
config.put(ResourceIndexer.PRETTY, "true");
try (OutputStream output = IO.outputStream(indexFile)) {
indexer.index(files, output, config);
}
indexPath.refreshLocal(IResource.DEPTH_ZERO, null);
if (indexPath.exists())
indexPath.setDerived(true, null);
} catch (Exception e) {
logger.logError(MessageFormat.format("Failed to generate index file for bundles in project {0}.", project.getName()), e);
return;
}
// Parse the index and add to the workspace repository
try (InputStream input = IO.stream(indexFile)) {
WorkspaceR5Repository workspaceRepo = Central.getWorkspaceR5Repository();
workspaceRepo.loadProjectIndex(project, input, project.getLocation().toFile().toURI());
} catch (Exception e) {
logger.logError("Failed to update workspace index.", e);
}
}
use of org.osgi.service.indexer.Capability in project bnd by bndtools.
the class OSGiFrameworkAnalyzer method analyzeResource.
public void analyzeResource(Resource resource, List<Capability> caps, List<Requirement> reqs) throws Exception {
Resource fwkFactorySvc = resource.getChild(SERVICE_FRAMEWORK_FACTORY);
if (fwkFactorySvc != null) {
Builder builder = new Builder().setNamespace(Namespaces.NS_CONTRACT).addAttribute(Namespaces.NS_CONTRACT, Namespaces.CONTRACT_OSGI_FRAMEWORK);
Version specVersion = null;
StringBuilder uses = new StringBuilder();
boolean firstPkg = true;
for (Capability cap : caps) {
if (Namespaces.NS_WIRING_PACKAGE.equals(cap.getNamespace())) {
// Add to the uses directive
if (!firstPkg)
uses.append(',');
String pkgName = (String) cap.getAttributes().get(Namespaces.NS_WIRING_PACKAGE);
uses.append(pkgName);
firstPkg = false;
// map to OSGi spec version
if (FRAMEWORK_PACKAGE.equals(pkgName)) {
Version frameworkPkgVersion = (Version) cap.getAttributes().get(Namespaces.ATTR_VERSION);
specVersion = mapFrameworkPackageVersion(frameworkPkgVersion);
}
}
}
if (specVersion != null)
builder.addAttribute(Namespaces.ATTR_VERSION, specVersion);
builder.addDirective(Namespaces.DIRECTIVE_USES, uses.toString());
caps.add(builder.buildCapability());
}
}
use of org.osgi.service.indexer.Capability in project bnd by bndtools.
the class TestBundleAnalyzer method testFragmentHost.
public void testFragmentHost() throws Exception {
BundleAnalyzer a = new BundleAnalyzer(new XNullLogSvc());
LinkedList<Capability> caps = new LinkedList<Capability>();
LinkedList<Requirement> reqs = new LinkedList<Requirement>();
a.analyzeResource(new JarResource(new File("testdata/08-fragmenthost.jar")), caps, reqs);
Requirement req = findReqs("osgi.wiring.host", reqs).get(0);
assertEquals("(&(osgi.wiring.host=org.example.a)(bundle-version>=0.0.0))", req.getDirectives().get("filter"));
}
use of org.osgi.service.indexer.Capability in project bnd by bndtools.
the class TestBundleAnalyzer method testPackageImportOptional.
public void testPackageImportOptional() throws Exception {
BundleAnalyzer a = new BundleAnalyzer(new XNullLogSvc());
LinkedList<Capability> caps = new LinkedList<Capability>();
LinkedList<Requirement> reqs = new LinkedList<Requirement>();
a.analyzeResource(new JarResource(new File("testdata/07-optionalimport.jar")), caps, reqs);
Requirement pkgImport = findReqs("osgi.wiring.package", reqs).get(0);
assertEquals("(&(osgi.wiring.package=org.example.a)(version>=1.0.0)(!(version>=2.0.0)))", pkgImport.getDirectives().get("filter"));
assertEquals("optional", pkgImport.getDirectives().get("resolution"));
}
Aggregations