Search in sources :

Example 11 with Builder

use of org.osgi.service.indexer.Builder in project bnd by bndtools.

the class BundleAnalyzer method doFragment.

private void doFragment(Resource resource, List<? super Requirement> reqs) throws Exception {
    Manifest manifest = resource.getManifest();
    String fragmentHost = manifest.getMainAttributes().getValue(Constants.FRAGMENT_HOST);
    if (fragmentHost != null) {
        StringBuilder filter = new StringBuilder();
        Map<String, Map<String, String>> fragmentList = OSGiHeader.parseHeader(fragmentHost);
        if (fragmentList.size() != 1)
            throw new IllegalArgumentException("Invalid Fragment-Host header: cannot contain multiple entries");
        Entry<String, Map<String, String>> entry = fragmentList.entrySet().iterator().next();
        String bsn = entry.getKey();
        filter.append("(&(osgi.wiring.host=").append(bsn).append(")");
        String versionStr = entry.getValue().get(Constants.BUNDLE_VERSION_ATTRIBUTE);
        VersionRange version = versionStr != null ? new VersionRange(versionStr) : new VersionRange(Version.emptyVersion.toString());
        Util.addVersionFilter(filter, version, VersionKey.BundleVersion);
        filter.append(")");
        Builder builder = new Builder().setNamespace(Namespaces.NS_WIRING_HOST).addDirective(Namespaces.DIRECTIVE_FILTER, filter.toString());
        reqs.add(builder.buildRequirement());
    }
}
Also used : Builder(org.osgi.service.indexer.Builder) VersionRange(org.osgi.service.indexer.impl.types.VersionRange) Manifest(java.util.jar.Manifest) Map(java.util.Map)

Example 12 with Builder

use of org.osgi.service.indexer.Builder in project bnd by bndtools.

the class SCRAnalyzer method createRequirement.

private static Requirement createRequirement(VersionRange range) {
    Builder builder = new Builder().setNamespace(Namespaces.NS_EXTENDER);
    StringBuilder filter = new StringBuilder();
    filter.append('(').append(Namespaces.NS_EXTENDER).append('=').append(Namespaces.EXTENDER_SCR).append(')');
    filter.insert(0, "(&");
    Util.addVersionFilter(filter, range, VersionKey.PackageVersion);
    filter.append(')');
    builder.addDirective(Namespaces.DIRECTIVE_FILTER, filter.toString()).addDirective(Namespaces.DIRECTIVE_EFFECTIVE, Namespaces.EFFECTIVE_ACTIVE);
    Requirement requirement = builder.buildRequirement();
    return requirement;
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Builder(org.osgi.service.indexer.Builder)

Example 13 with Builder

use of org.osgi.service.indexer.Builder 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);
    }
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) Builder(org.osgi.service.indexer.Builder) OutputStream(java.io.OutputStream) URI(java.net.URI) WorkspaceR5Repository(bndtools.central.WorkspaceR5Repository) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) Capability(org.osgi.service.indexer.Capability) InputStream(java.io.InputStream) Resource(org.osgi.service.indexer.Resource) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) Project(aQute.bnd.build.Project) Requirement(org.osgi.service.indexer.Requirement) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IFile(org.eclipse.core.resources.IFile) File(java.io.File) RepoIndex(org.osgi.service.indexer.impl.RepoIndex)

Example 14 with Builder

use of org.osgi.service.indexer.Builder in project bnd by bndtools.

the class KnownBundleAnalyzer method processClause.

private static void processClause(String bundleRef, String clauseStr, List<Capability> caps, List<Requirement> reqs) {
    Map<String, Map<String, String>> header = OSGiHeader.parseHeader(clauseStr);
    for (Entry<String, Map<String, String>> entry : header.entrySet()) {
        String indicator = OSGiHeader.removeDuplicateMarker(entry.getKey());
        IndicatorType type;
        String namespace;
        if (indicator.startsWith(IndicatorType.Capability.getPrefix())) {
            type = IndicatorType.Capability;
            namespace = indicator.substring(IndicatorType.Capability.getPrefix().length());
        } else if (indicator.startsWith(IndicatorType.Requirement.getPrefix())) {
            type = IndicatorType.Requirement;
            namespace = indicator.substring(IndicatorType.Requirement.getPrefix().length());
        } else {
            throw new IllegalArgumentException(MessageFormat.format("Invalid indicator format in known-bundle parsing for bundle  \"{0}\", expected cap=namespace or req=namespace, found \"{1}\".", bundleRef, indicator));
        }
        Builder builder = new Builder().setNamespace(namespace);
        Map<String, String> attribs = entry.getValue();
        Util.copyAttribsToBuilder(builder, attribs);
        if (type == IndicatorType.Capability)
            caps.add(builder.buildCapability());
        else if (type == IndicatorType.Requirement)
            reqs.add(builder.buildRequirement());
    }
}
Also used : Builder(org.osgi.service.indexer.Builder) Map(java.util.Map)

Example 15 with Builder

use of org.osgi.service.indexer.Builder 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());
    }
}
Also used : Capability(org.osgi.service.indexer.Capability) Version(org.osgi.framework.Version) Builder(org.osgi.service.indexer.Builder) Resource(org.osgi.service.indexer.Resource)

Aggregations

Builder (org.osgi.service.indexer.Builder)18 Map (java.util.Map)10 Manifest (java.util.jar.Manifest)5 Version (org.osgi.framework.Version)5 VersionRange (org.osgi.service.indexer.impl.types.VersionRange)4 Requirement (org.osgi.service.indexer.Requirement)3 SymbolicName (org.osgi.service.indexer.impl.types.SymbolicName)3 Capability (org.osgi.service.indexer.Capability)2 Resource (org.osgi.service.indexer.Resource)2 Project (aQute.bnd.build.Project)1 WorkspaceR5Repository (bndtools.central.WorkspaceR5Repository)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1