Search in sources :

Example 6 with VersionRange

use of org.osgi.framework.VersionRange in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method updateOverrides.

/**
 * <p>Updates existing <code>etc/overrides.properties</code> after installing single {@link PatchKind#NON_ROLLUP}
 * patch.</p>
 * @param workTree
 * @param patchData
 */
private void updateOverrides(File workTree, PatchData patchData) throws IOException {
    File overrides = new File(workTree, "etc/overrides.properties");
    List<String> currentOverrides = overrides.isFile() ? FileUtils.readLines(overrides) : new LinkedList<String>();
    for (String bundle : patchData.getBundles()) {
        Artifact artifact = mvnurlToArtifact(bundle, true);
        if (artifact == null) {
            continue;
        }
        // Compute patch bundle version and range
        VersionRange range;
        Version oVer = Utils.getOsgiVersion(artifact.getVersion());
        String vr = patchData.getVersionRange(bundle);
        String override;
        if (vr != null && !vr.isEmpty()) {
            override = bundle + ";range=" + vr;
            range = new VersionRange(vr);
        } else {
            override = bundle;
            Version v1 = new Version(oVer.getMajor(), oVer.getMinor(), 0);
            Version v2 = new Version(oVer.getMajor(), oVer.getMinor() + 1, 0);
            range = new VersionRange(VersionRange.LEFT_CLOSED, v1, v2, VersionRange.RIGHT_OPEN);
        }
        // Process overrides.properties
        boolean matching = false;
        boolean added = false;
        for (int i = 0; i < currentOverrides.size(); i++) {
            String line = currentOverrides.get(i).trim();
            if (!line.isEmpty() && !line.startsWith("#")) {
                Artifact overrideArtifact = mvnurlToArtifact(line, true);
                if (overrideArtifact != null) {
                    Version ver = Utils.getOsgiVersion(overrideArtifact.getVersion());
                    if (isSameButVersion(artifact, overrideArtifact) && range.includes(ver)) {
                        matching = true;
                        if (ver.compareTo(oVer) < 0) {
                            // Replace old override with the new one
                            currentOverrides.set(i, override);
                            added = true;
                        }
                    }
                }
            }
        }
        // If there was not matching bundles, add it
        if (!matching) {
            currentOverrides.add(override);
        }
    }
    FileUtils.writeLines(overrides, currentOverrides, IOUtils.LINE_SEPARATOR_UNIX);
}
Also used : Version(org.osgi.framework.Version) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) VersionRange(org.osgi.framework.VersionRange) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) Artifact(io.fabric8.patch.management.Artifact)

Example 7 with VersionRange

use of org.osgi.framework.VersionRange in project fuse-karaf by jboss-fuse.

the class GitPatchManagementServiceImpl method updateOverrides.

/**
 * <p>Updates existing <code>etc/org.apache.karaf.features.xml</code> after installing single {@link PatchKind#NON_ROLLUP}
 * patch. Both bundle and feature replacements are taken into account.</p>
 * @param workTree
 * @param patches
 */
private void updateOverrides(File workTree, List<PatchData> patches) throws IOException {
    File overrides = new File(workTree, "etc/" + featureProcessing);
    File versions = new File(workTree, "etc/" + featureProcessingVersions);
    // we need two different versions to detect whether the version is externalized in etc/versions.properties
    FeaturesProcessing fp1;
    FeaturesProcessing fp2;
    if (overrides.isFile()) {
        fp1 = InternalUtils.loadFeatureProcessing(overrides, versions);
        fp2 = InternalUtils.loadFeatureProcessing(overrides, null);
    } else {
        fp1 = fp2 = new FeaturesProcessing();
    }
    List<BundleReplacements.OverrideBundle> br1 = fp1.getBundleReplacements().getOverrideBundles();
    List<BundleReplacements.OverrideBundle> br2 = fp2.getBundleReplacements().getOverrideBundles();
    org.apache.felix.utils.properties.Properties props = null;
    boolean propertyChanged = false;
    if (versions.isFile()) {
        props = new org.apache.felix.utils.properties.Properties(versions);
    }
    for (PatchData patchData : patches) {
        for (String bundle : patchData.getBundles()) {
            Artifact artifact = mvnurlToArtifact(bundle, true);
            if (artifact == null) {
                continue;
            }
            // Compute patch bundle version and range
            Version oVer = Utils.getOsgiVersion(artifact.getVersion());
            String vr = patchData.getVersionRange(bundle);
            if (vr != null && !vr.isEmpty()) {
                artifact.setVersion(vr);
            } else {
                Version v1 = new Version(oVer.getMajor(), oVer.getMinor(), 0);
                Version v2 = new Version(oVer.getMajor(), oVer.getMinor() + 1, 0);
                artifact.setVersion(new VersionRange(VersionRange.LEFT_CLOSED, v1, v2, VersionRange.RIGHT_OPEN).toString());
            }
            // features processing file may contain e.g.,:
            // <bundle originalUri="mvn:org.jboss.fuse/fuse-zen/[1,2)/war"
            // replacement="mvn:org.jboss.fuse/fuse-zen/${version.test2}/war" mode="maven" />
            // patch descriptor contains e.g.,:
            // bundle.0 = mvn:org.jboss.fuse/fuse-zen/1.2.0/war
            // bundle.0.range = [1.1,1.2)
            // 
            // we will always match by replacement attribute, ignoring originalUri - the patch descriptor must be
            // prepared correctly
            int idx = 0;
            BundleReplacements.OverrideBundle existing = null;
            // we'll examine model with resolved property placeholders, but modify the other one
            for (BundleReplacements.OverrideBundle override : br1) {
                LocationPattern lp = new LocationPattern(artifact.getCanonicalUri());
                if (lp.matches(override.getReplacement())) {
                    // we've found existing override in current etc/org.apache.karaf.features.xml
                    existing = br2.get(idx);
                    break;
                }
                idx++;
            }
            if (existing == null) {
                existing = new BundleReplacements.OverrideBundle();
                br2.add(existing);
            }
            // either update existing override or configure a new one
            existing.setMode(BundleReplacements.BundleOverrideMode.MAVEN);
            existing.setOriginalUri(artifact.getCanonicalUri());
            String replacement = existing.getReplacement();
            if (replacement != null && replacement.contains("${")) {
                // assume that we have existing replacement="mvn:org.jboss.fuse/fuse-zen/${version.test2}/war"
                // so we can't change the replacement and instead we have to update properties
                String property = null;
                String value = null;
                if (replacement.startsWith("mvn:")) {
                    LocationPattern existingReplacement = new LocationPattern(replacement);
                    property = existingReplacement.getVersionString().substring(existingReplacement.getVersionString().indexOf("${") + 2);
                    if (property.contains("}")) {
                        // it should...
                        property = property.substring(0, property.indexOf("}"));
                    }
                    LocationPattern newReplacement = new LocationPattern(bundle);
                    value = newReplacement.getVersionString();
                } else {
                // non-mvn? then we can't determine the version from non-mvn: URI...
                }
                // we are not changing replacement - we'll have to update properties
                if (props != null && property != null) {
                    props.setProperty(property, value);
                    propertyChanged = true;
                }
            } else {
                existing.setReplacement(bundle);
            }
        }
        // feature overrides
        File featureOverridesLocation = new File(patchData.getPatchDirectory(), "org.apache.karaf.features.xml");
        if (featureOverridesLocation.isFile()) {
            FeaturesProcessing featureOverrides = InternalUtils.loadFeatureProcessing(featureOverridesLocation, null);
            Map<String, FeatureReplacements.OverrideFeature> patchedFeatures = new LinkedHashMap<>();
            List<FeatureReplacements.OverrideFeature> mergedOverrides = new LinkedList<>();
            featureOverrides.getFeatureReplacements().getReplacements().forEach(of -> patchedFeatures.put(of.getFeature().getId(), of));
            fp2.getFeatureReplacements().getReplacements().forEach(of -> {
                FeatureReplacements.OverrideFeature override = patchedFeatures.remove(of.getFeature().getId());
                mergedOverrides.add(override == null ? of : override);
            });
            // add remaining
            mergedOverrides.addAll(patchedFeatures.values());
            fp2.getFeatureReplacements().getReplacements().clear();
            fp2.getFeatureReplacements().getReplacements().addAll(mergedOverrides);
        }
    }
    if (propertyChanged) {
        props.save();
    }
    InternalUtils.saveFeatureProcessing(fp2, overrides, versions);
}
Also used : BundleReplacements(org.apache.karaf.features.internal.model.processing.BundleReplacements) VersionRange(org.osgi.framework.VersionRange) FeatureReplacements(org.apache.karaf.features.internal.model.processing.FeatureReplacements) FeaturesProcessing(org.apache.karaf.features.internal.model.processing.FeaturesProcessing) LinkedHashMap(java.util.LinkedHashMap) Version(org.osgi.framework.Version) PatchData(org.jboss.fuse.patch.management.PatchData) Artifact(org.jboss.fuse.patch.management.Artifact) LinkedList(java.util.LinkedList) LocationPattern(org.apache.karaf.features.LocationPattern) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 8 with VersionRange

use of org.osgi.framework.VersionRange in project tycho by eclipse.

the class AbstractJUnitProvider method isEnabled.

@Override
public boolean isEnabled(List<ClasspathEntry> testBundleClassPath, Properties surefireProperties) {
    Set<String> junitBundleNames = getJUnitBundleNames();
    VersionRange range = getJUnitVersionRange();
    for (ClasspathEntry classpathEntry : testBundleClassPath) {
        ArtifactKey artifactKey = classpathEntry.getArtifactKey();
        if (junitBundleNames.contains(artifactKey.getId())) {
            Version version = Version.parseVersion(artifactKey.getVersion());
            if (range.includes(version)) {
                return true;
            }
        }
    }
    return false;
}
Also used : ArtifactKey(org.eclipse.tycho.ArtifactKey) Version(org.osgi.framework.Version) VersionRange(org.osgi.framework.VersionRange) ClasspathEntry(org.eclipse.tycho.classpath.ClasspathEntry)

Example 9 with VersionRange

use of org.osgi.framework.VersionRange in project tycho by eclipse.

the class DefaultVersionRangeUpdateStrategy method handleMatchingBouds.

private VersionRange handleMatchingBouds(VersionRange versionRange, Version originalReferencedVersion, Version newReferencedVersion) {
    Version newLeft;
    if (versionRange.getLeft().equals(originalReferencedVersion)) {
        newLeft = newReferencedVersion;
    } else {
        newLeft = versionRange.getLeft();
    }
    Version newRight;
    if (versionRange.getRight() != null && versionRange.getRight().equals(originalReferencedVersion)) {
        newRight = newReferencedVersion;
    } else {
        newRight = versionRange.getRight();
    }
    return new VersionRange(versionRange.getLeftType(), newLeft, newRight, versionRange.getRightType());
}
Also used : Version(org.osgi.framework.Version) VersionRange(org.osgi.framework.VersionRange)

Example 10 with VersionRange

use of org.osgi.framework.VersionRange in project aries by apache.

the class AbstractClauseBasedHeader method parseVersionRange.

protected static VersionRange parseVersionRange(List<SimpleFilter> filters) {
    SimpleFilter floor = null;
    SimpleFilter ceiling = null;
    for (SimpleFilter filter : filters) {
        switch(filter.getOperation()) {
            case SimpleFilter.EQ:
            case SimpleFilter.GTE:
                floor = filter;
                break;
            case SimpleFilter.LTE:
                ceiling = filter;
                break;
            case SimpleFilter.NOT:
                SimpleFilter negated = ((List<SimpleFilter>) filter.getValue()).get(0);
                switch(negated.getOperation()) {
                    case SimpleFilter.EQ:
                    case SimpleFilter.GTE:
                        ceiling = filter;
                        break;
                    case SimpleFilter.LTE:
                        floor = filter;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid filter: " + filter);
                }
                break;
            case SimpleFilter.PRESENT:
                /* This can happen with version ranges of the form 
					 * (1.5.0,2.0.0). The filter form will be
					 * (&(version=*)(!(version<=1.5.0))(!(version>=2.0.0)). The
					 * presence operator is required because an absent version
					 * attribute would otherwise match. These should simply be
					 * ignored for the purposes of converting back into a
					 * version range.
					 */
                break;
            default:
                throw new IllegalArgumentException("Invalid filter: " + filter);
        }
    }
    if (ceiling == null) {
        return new VersionRange(String.valueOf(floor.getValue()));
    }
    String range = new StringBuilder().append(floor.getOperation() == SimpleFilter.NOT ? '(' : '[').append(floor.getOperation() == SimpleFilter.NOT ? ((List<SimpleFilter>) floor.getValue()).get(0).getValue() : floor.getValue()).append(',').append(ceiling.getOperation() == SimpleFilter.NOT ? ((List<SimpleFilter>) ceiling.getValue()).get(0).getValue() : ceiling.getValue()).append(ceiling.getOperation() == SimpleFilter.NOT ? ')' : ']').toString();
    return new VersionRange(range);
}
Also used : SimpleFilter(org.apache.aries.subsystem.core.capabilityset.SimpleFilter) List(java.util.List) VersionRange(org.osgi.framework.VersionRange)

Aggregations

VersionRange (org.osgi.framework.VersionRange)18 Version (org.osgi.framework.Version)7 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Map (java.util.Map)3 ManifestElement (org.eclipse.osgi.util.ManifestElement)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)2 Requirement (org.osgi.resource.Requirement)2 Artifact (io.fabric8.patch.management.Artifact)1 Artifact.isSameButVersion (io.fabric8.patch.management.Artifact.isSameButVersion)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 SimpleFilter (org.apache.aries.subsystem.core.capabilityset.SimpleFilter)1 BasicRequirement (org.apache.aries.subsystem.core.internal.BasicRequirement)1 LocationPattern (org.apache.karaf.features.LocationPattern)1 BundleReplacements (org.apache.karaf.features.internal.model.processing.BundleReplacements)1