Search in sources :

Example 1 with BundleRevisionDTO

use of org.osgi.framework.wiring.dto.BundleRevisionDTO in project bnd by bndtools.

the class RemoteCommand method _distro.

public void _distro(DistroOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String bsn;
    String version;
    bsn = args.remove(0);
    if (!Verifier.isBsn(bsn)) {
        error("Not a bundle symbolic name %s", bsn);
    }
    if (args.isEmpty())
        version = "0";
    else {
        version = args.remove(0);
        if (!Version.isVersion(version)) {
            error("Invalid version %s", version);
        }
    }
    File output = getFile(opts.output("distro.jar"));
    if (output.getParentFile() == null || !output.getParentFile().isDirectory()) {
        error("Cannot write to %s because parent not a directory", output);
    }
    if (output.isFile() && !output.canWrite()) {
        error("Cannot write to %s", output);
    }
    logger.debug("Starting distro {};{}", bsn, version);
    List<BundleRevisionDTO> bundleRevisons = agent.getBundleRevisons();
    logger.debug("Found {} bundle revisions", bundleRevisons.size());
    Parameters packages = new Parameters();
    List<Parameters> provided = new ArrayList<>();
    for (BundleRevisionDTO brd : bundleRevisons) {
        for (CapabilityDTO c : brd.capabilities) {
            CapabilityBuilder cb = new CapabilityBuilder(c.namespace);
            for (Entry<String, Object> e : c.attributes.entrySet()) {
                String key = e.getKey();
                Object value = e.getValue();
                if (key.equals("version")) {
                    if (value instanceof Collection || value.getClass().isArray())
                        value = Converter.cnv(tref, value);
                    else
                        value = new Version((String) value);
                }
                cb.addAttribute(key, value);
            }
            cb.addDirectives(c.directives);
            Attrs attrs = cb.toAttrs();
            if (cb.isPackage()) {
                attrs.remove(Constants.BUNDLE_SYMBOLIC_NAME_ATTRIBUTE);
                attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
                String pname = attrs.remove(PackageNamespace.PACKAGE_NAMESPACE);
                if (pname == null) {
                    warning("Invalid package capability found %s", c);
                } else
                    packages.put(pname, attrs);
                logger.debug("P: {};{}", pname, attrs);
            } else if (NativeNamespace.NATIVE_NAMESPACE.equals(c.namespace)) {
                Attrs newAttrs = new Attrs();
                for (Entry<String, String> entry : attrs.entrySet()) {
                    if (entry.getKey().startsWith(NativeNamespace.NATIVE_NAMESPACE)) {
                        newAttrs.put(entry.getKey(), entry.getValue());
                    }
                }
                Parameters p = new Parameters();
                p.put(c.namespace, newAttrs);
                provided.add(p);
            } else if (!IGNORED_NAMESPACES.contains(c.namespace)) {
                logger.debug("C {};{}", c.namespace, attrs);
                Parameters p = new Parameters();
                p.put(c.namespace, attrs);
                provided.add(p);
            }
        }
    }
    if (isOk()) {
        Manifest m = new Manifest();
        Attributes main = m.getMainAttributes();
        main.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
        main.putValue(Constants.BUNDLE_SYMBOLICNAME, bsn);
        main.putValue(Constants.BUNDLE_VERSION, version);
        main.putValue(Constants.EXPORT_PACKAGE, packages.toString());
        // Make distro unresolvable
        Parameters unresolveable = new Parameters("osgi.unresolvable; filter:='(&(must.not.resolve=*)(!(must.not.resolve=*)))'");
        main.putValue(Constants.REQUIRE_CAPABILITY, unresolveable.toString());
        provided.add(new Parameters("osgi.unresolvable"));
        StringBuilder sb = new StringBuilder();
        for (Parameters parameter : provided) {
            sb.append(parameter.toString());
            sb.append(",");
        }
        String capabilities = sb.toString().substring(0, sb.length() - 1);
        main.putValue(Constants.PROVIDE_CAPABILITY, capabilities);
        if (opts.description() != null)
            main.putValue(Constants.BUNDLE_DESCRIPTION, opts.description());
        if (opts.license() != null)
            main.putValue(Constants.BUNDLE_LICENSE, opts.license());
        if (opts.copyright() != null)
            main.putValue(Constants.BUNDLE_COPYRIGHT, opts.copyright());
        if (opts.vendor() != null)
            main.putValue(Constants.BUNDLE_VENDOR, opts.vendor());
        Jar jar = new Jar("distro");
        jar.setManifest(m);
        Verifier v = new Verifier(jar);
        v.setProperty(Constants.FIXUPMESSAGES, "osgi.* namespace must not be specified with generic capabilities");
        v.verify();
        v.getErrors();
        if (isFailOk() || v.isOk()) {
            jar.updateModified(System.currentTimeMillis(), "Writing distro jar");
            jar.write(output);
        } else
            getInfo(v);
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) ArrayList(java.util.ArrayList) Attrs(aQute.bnd.header.Attrs) Attributes(java.util.jar.Attributes) CapabilityBuilder(aQute.bnd.osgi.resource.CapabilityBuilder) Manifest(java.util.jar.Manifest) Verifier(aQute.bnd.osgi.Verifier) Entry(java.util.Map.Entry) CapabilityDTO(org.osgi.resource.dto.CapabilityDTO) Version(aQute.bnd.version.Version) Collection(java.util.Collection) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Example 2 with BundleRevisionDTO

use of org.osgi.framework.wiring.dto.BundleRevisionDTO in project bnd by bndtools.

the class AgentServer method toDTO.

/*
	 * Turn a bundle in a Bundle Revision dto. On a r6 framework we could do
	 * this with adapt but on earlier frameworks we're on our own
	 */
private BundleRevisionDTO toDTO(BundleRevision resource) {
    BundleRevisionDTO brd = new BundleRevisionDTO();
    brd.bundle = resource.getBundle().getBundleId();
    brd.id = sequence.getAndIncrement();
    brd.symbolicName = resource.getSymbolicName();
    brd.type = resource.getTypes();
    brd.version = resource.getVersion().toString();
    brd.requirements = new ArrayList<RequirementDTO>();
    for (Requirement r : resource.getRequirements(null)) {
        brd.requirements.add(toDTO(brd.id, r));
    }
    brd.capabilities = new ArrayList<CapabilityDTO>();
    for (Capability c : resource.getCapabilities(null)) {
        brd.capabilities.add(toDTO(brd.id, c));
    }
    return brd;
}
Also used : Requirement(org.osgi.resource.Requirement) RequirementDTO(org.osgi.resource.dto.RequirementDTO) Capability(org.osgi.resource.Capability) BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) CapabilityDTO(org.osgi.resource.dto.CapabilityDTO)

Example 3 with BundleRevisionDTO

use of org.osgi.framework.wiring.dto.BundleRevisionDTO in project bnd by bndtools.

the class AgentServer method getBundleRevisons.

/**
	 * Return the bundle revisions
	 */
@Override
public List<BundleRevisionDTO> getBundleRevisons(long... bundleId) throws Exception {
    Bundle[] bundles;
    if (bundleId.length == 0) {
        bundles = context.getBundles();
    } else {
        bundles = new Bundle[bundleId.length];
        for (int i = 0; i < bundleId.length; i++) {
            bundles[i] = context.getBundle(bundleId[i]);
        }
    }
    List<BundleRevisionDTO> revisions = new ArrayList<BundleRevisionDTO>(bundles.length);
    for (Bundle b : bundles) {
        BundleRevision resource = b.adapt(BundleRevision.class);
        BundleRevisionDTO bwd = toDTO(resource);
        revisions.add(bwd);
    }
    return revisions;
}
Also used : BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 4 with BundleRevisionDTO

use of org.osgi.framework.wiring.dto.BundleRevisionDTO in project bnd by bndtools.

the class RemoteTest method testBRD.

/*
	 * Test if we can get the BundleRevisionDTOs
	 */
public void testBRD() throws Exception {
    LauncherSupervisor supervisor = new LauncherSupervisor();
    supervisor.connect("localhost", Agent.DEFAULT_PORT);
    assertNotNull(supervisor);
    Agent agent = supervisor.getAgent();
    assertNotNull(agent);
    List<BundleRevisionDTO> bundleRevisons = agent.getBundleRevisons();
    assertNotNull(bundleRevisons);
}
Also used : Agent(aQute.remote.api.Agent) BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) LauncherSupervisor(aQute.remote.plugin.LauncherSupervisor)

Aggregations

BundleRevisionDTO (org.osgi.framework.wiring.dto.BundleRevisionDTO)4 ArrayList (java.util.ArrayList)2 CapabilityDTO (org.osgi.resource.dto.CapabilityDTO)2 Attrs (aQute.bnd.header.Attrs)1 Parameters (aQute.bnd.header.Parameters)1 Jar (aQute.bnd.osgi.Jar)1 Verifier (aQute.bnd.osgi.Verifier)1 CapabilityBuilder (aQute.bnd.osgi.resource.CapabilityBuilder)1 Version (aQute.bnd.version.Version)1 Agent (aQute.remote.api.Agent)1 LauncherSupervisor (aQute.remote.plugin.LauncherSupervisor)1 File (java.io.File)1 Collection (java.util.Collection)1 Entry (java.util.Map.Entry)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 Bundle (org.osgi.framework.Bundle)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1