Search in sources :

Example 61 with Version

use of org.osgi.framework.Version in project bndtools by bndtools.

the class ResolutionWizard method resourceToRunBundle.

private static VersionedClause resourceToRunBundle(Resource resource) {
    Capability idCap = ResourceUtils.getIdentityCapability(resource);
    String identity = ResourceUtils.getIdentity(idCap);
    // Map version range string, using "latest" for any workspace resources
    Attrs attribs = new Attrs();
    String versionRangeStr;
    if (isWorkspace(resource)) {
        versionRangeStr = VERSION_SNAPSHOT;
    } else {
        Version version = ResourceUtils.getVersion(idCap);
        VersionRange versionRange = createVersionRange(version);
        versionRangeStr = versionRange.toString();
    }
    attribs.put(Constants.VERSION_ATTRIBUTE, versionRangeStr);
    return new VersionedClause(identity, attribs);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) VersionRange(aQute.bnd.version.VersionRange)

Example 62 with Version

use of org.osgi.framework.Version in project bndtools by bndtools.

the class ResolutionSuccessPanel method resourceToRequirement.

private static Requirement resourceToRequirement(Resource resource) {
    Capability identity = ResourceUtils.getIdentityCapability(resource);
    String id = ResourceUtils.getIdentity(identity);
    Version version = ResourceUtils.getVersion(identity);
    Version dropQualifier = new Version(version.getMajor(), version.getMinor(), version.getMicro());
    AndFilter filter = new AndFilter();
    filter.addChild(new SimpleFilter(IdentityNamespace.IDENTITY_NAMESPACE, id));
    filter.addChild(new LiteralFilter(Filters.fromVersionRange(dropQualifier.toString())));
    Requirement req = new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter.toString()).buildSyntheticRequirement();
    return req;
}
Also used : LiteralFilter(aQute.libg.filters.LiteralFilter) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) AndFilter(aQute.libg.filters.AndFilter) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) SimpleFilter(aQute.libg.filters.SimpleFilter)

Example 63 with Version

use of org.osgi.framework.Version in project sling by apache.

the class DeployPckTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    // get and check symbolic name
    final String symbolicName = (String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_SYMBOLICMAME);
    if (symbolicName == null) {
        logger.error("Resource {} has no symbolic name - ignoring.", tr);
        this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        return;
    }
    // get package if available
    final DeploymentPackage dp = this.deploymentAdmin.getDeploymentPackage(symbolicName);
    if (tr.getState() == ResourceState.INSTALL) {
        InputStream is = null;
        try {
            is = tr.getInputStream();
            if (is == null) {
                // something went wrong
                logger.error("Resource {} does not provide an input stream!", tr);
                this.getResourceGroup().setFinishState(ResourceState.IGNORED);
            } else {
                final Version newVersion = new Version((String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_VERSION));
                // check version
                if (dp != null) {
                    final int compare = dp.getVersion().compareTo(newVersion);
                    if (compare < 0) {
                        // installed version is lower -> update
                        this.deploymentAdmin.installDeploymentPackage(is);
                        ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                        this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                    } else if (compare >= 0) {
                        logger.debug("Deployment package " + symbolicName + " " + newVersion + " is not installed, package with higher or same version is already installed.");
                    }
                } else {
                    this.deploymentAdmin.installDeploymentPackage(is);
                    ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                }
            }
        } catch (final DeploymentException e) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } catch (final IOException ioe) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
        }
    } else {
        // uninstall
        if (dp != null) {
            try {
                dp.uninstall();
            } catch (final DeploymentException e) {
                logger.error("Unable to uninstall deployment package {} from resource {}", symbolicName, tr);
            }
        } else {
            logger.info("Unable to find deployment package with symbolic name {} for uninstalling.", symbolicName);
        }
        this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Version(org.osgi.framework.Version) InputStream(java.io.InputStream) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) IOException(java.io.IOException)

Example 64 with Version

use of org.osgi.framework.Version in project sling by apache.

the class DeploymentPackageInstaller method transform.

/**
     * @see org.apache.sling.installer.api.tasks.ResourceTransformer#transform(org.apache.sling.installer.api.tasks.RegisteredResource)
     */
public TransformationResult[] transform(final RegisteredResource resource) {
    if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
        try {
            final Manifest m = getManifest(resource.getInputStream());
            if (m != null) {
                final String sn = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_SYMBOLICMAME);
                if (sn != null) {
                    final String v = m.getMainAttributes().getValue(DEPLOYMENTPACKAGE_VERSION);
                    if (v != null) {
                        final Map<String, Object> attr = new HashMap<String, Object>();
                        attr.put(DEPLOYMENTPACKAGE_SYMBOLICMAME, sn);
                        final TransformationResult tr = new TransformationResult();
                        tr.setId(sn);
                        tr.setVersion(new Version(v));
                        tr.setResourceType(TYPE_DP);
                        tr.setAttributes(attr);
                        return new TransformationResult[] { tr };
                    }
                }
            }
        } catch (final IOException ignore) {
        // ignore
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Version(org.osgi.framework.Version) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 65 with Version

use of org.osgi.framework.Version in project sling by apache.

the class ModelTransformer method transform.

@Override
public TransformationResult[] transform(final RegisteredResource resource) {
    Model model = null;
    File baseDir = null;
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".model")) {
        try (final Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
            model = ModelReader.read(reader, resource.getURL());
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (resource.getType().equals(InstallableResource.TYPE_FILE) && resource.getURL().endsWith(".mar")) {
        baseDir = this.bundleContext.getDataFile("");
        try (final InputStream is = resource.getInputStream()) {
            model = ModelArchiveReader.read(is, new ModelArchiveReader.ArtifactConsumer() {

                @Override
                public void consume(final Artifact artifact, final InputStream is) throws IOException {
                // nothing to do, install task does extraction
                }
            });
        } catch (final IOException ioe) {
            logger.info("Unable to read model from " + resource.getURL(), ioe);
        }
    }
    if (model != null) {
        Map<Traceable, String> errors = ModelUtility.validate(model);
        if (errors == null) {
            try {
                final Model effectiveModel = ModelUtility.getEffectiveModel(model);
                errors = ModelUtility.validateIncludingVersion(effectiveModel);
                if (errors == null) {
                    String modelTxt = null;
                    try (final StringWriter sw = new StringWriter()) {
                        ModelWriter.write(sw, effectiveModel);
                        modelTxt = sw.toString();
                    } catch (final IOException ioe) {
                        logger.info("Unable to read model from " + resource.getURL(), ioe);
                    }
                    if (modelTxt != null) {
                        final TransformationResult[] result = new TransformationResult[effectiveModel.getFeatures().size()];
                        int index = 0;
                        for (final Feature f : effectiveModel.getFeatures()) {
                            final TransformationResult tr = new TransformationResult();
                            tr.setResourceType(TYPE_PROV_MODEL);
                            tr.setId(f.getName());
                            tr.setVersion(new Version(f.getVersion()));
                            final Map<String, Object> attributes = new HashMap<>();
                            attributes.put(ATTR_MODEL, modelTxt);
                            attributes.put(ATTR_FEATURE_INDEX, index);
                            attributes.put(ATTR_FEATURE_NAME, f.getName() + "-" + f.getVersion());
                            if (baseDir != null) {
                                final File dir = new File(baseDir, f.getName() + "-" + f.getVersion());
                                attributes.put(ATTR_BASE_PATH, dir.getAbsolutePath());
                            }
                            tr.setAttributes(attributes);
                            result[index] = tr;
                            index++;
                        }
                        return result;
                    }
                }
            } catch (final IllegalArgumentException iae) {
                errors = Collections.singletonMap((Traceable) model, iae.getMessage());
            }
        }
        if (errors != null) {
            logger.warn("Errors during parsing model at {} : {}", resource.getURL(), errors.values());
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.sling.provisioning.model.Artifact) StringWriter(java.io.StringWriter) Version(org.osgi.framework.Version) Model(org.apache.sling.provisioning.model.Model) Traceable(org.apache.sling.provisioning.model.Traceable) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) File(java.io.File)

Aggregations

Version (org.osgi.framework.Version)222 Test (org.junit.Test)35 ArrayList (java.util.ArrayList)28 Bundle (org.osgi.framework.Bundle)27 File (java.io.File)23 HashMap (java.util.HashMap)21 Capability (org.osgi.resource.Capability)20 IOException (java.io.IOException)19 BundleException (org.osgi.framework.BundleException)15 Resource (org.osgi.resource.Resource)14 InputStream (java.io.InputStream)12 Map (java.util.Map)11 Manifest (java.util.jar.Manifest)11 List (java.util.List)10 URL (java.net.URL)8 HashSet (java.util.HashSet)8 Content (org.apache.aries.application.Content)8 AriesApplication (org.apache.aries.application.management.AriesApplication)8 VersionRange (org.apache.felix.utils.version.VersionRange)8 BundleRevision (org.osgi.framework.wiring.BundleRevision)8