use of org.apache.felix.utils.version.VersionRange in project karaf by apache.
the class AbstractFeatureMojo method getMatchingFeature.
private Feature getMatchingFeature(Map<String, Feature> featuresMap, String feature, String version) {
Feature f = null;
if (version != null && !version.equals(Feature.DEFAULT_VERSION)) {
// looking for a specific feature with name and version
f = featuresMap.get(feature + "/" + version);
if (f == null) {
//it's probably is a version range so try to use VersionRange Utils
VersionRange versionRange = new VersionRange(version);
for (String key : featuresMap.keySet()) {
String[] nameVersion = key.split("/");
if (feature.equals(nameVersion[0])) {
String verStr = featuresMap.get(key).getVersion();
Version ver = VersionTable.getVersion(verStr);
if (versionRange.contains(ver)) {
if (f == null || VersionTable.getVersion(f.getVersion()).compareTo(VersionTable.getVersion(featuresMap.get(key).getVersion())) < 0) {
f = featuresMap.get(key);
}
}
}
}
}
} else {
// looking for the first feature name (whatever the version is)
for (String key : featuresMap.keySet()) {
String[] nameVersion = key.split("/");
if (feature.equals(nameVersion[0])) {
f = featuresMap.get(key);
break;
}
}
}
if (f == null) {
throw new IllegalArgumentException("Unable to find the feature '" + feature + "'");
}
return f;
}
use of org.apache.felix.utils.version.VersionRange in project karaf by apache.
the class FeaturesServiceImpl method getFeatureMatching.
private Feature getFeatureMatching(Map<String, Feature> versions, String version) {
if (version != null) {
version = version.trim();
if (version.equals(org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION)) {
version = "";
}
} else {
version = "";
}
if (versions == null || versions.isEmpty()) {
return null;
} else {
Feature feature = version.isEmpty() ? null : versions.get(version);
if (feature == null) {
// Compute version range. If an version has been given, assume exact range
VersionRange versionRange = version.isEmpty() ? new VersionRange(Version.emptyVersion) : new VersionRange(version, true, true);
Version latest = Version.emptyVersion;
for (String available : versions.keySet()) {
Version availableVersion = VersionTable.getVersion(available);
if (availableVersion.compareTo(latest) >= 0 && versionRange.contains(availableVersion)) {
feature = versions.get(available);
latest = availableVersion;
}
}
}
return feature;
}
}
use of org.apache.felix.utils.version.VersionRange in project karaf by apache.
the class DeployerTest method testInstallSimpleFeature.
@Test
public void testInstallSimpleFeature() throws Exception {
IMocksControl c = EasyMock.createControl();
String dataDir = "data1";
TestDownloadManager manager = new TestDownloadManager(getClass(), dataDir);
RepositoryImpl repo = new RepositoryImpl(getClass().getResource(dataDir + "/features.xml").toURI());
repo.load(true);
Feature f100 = repo.getFeatures()[0];
Feature f101 = repo.getFeatures()[1];
Deployer.DeployCallback callback = c.createMock(Deployer.DeployCallback.class);
BundleInstallSupport installSupport = c.createMock(BundleInstallSupportImpl.class);
Deployer deployer = new Deployer(manager, resolver, installSupport, callback);
callback.print(EasyMock.anyString(), EasyMock.anyBoolean());
EasyMock.expectLastCall().anyTimes();
callback.callListeners(DeploymentEvent.DEPLOYMENT_STARTED);
EasyMock.expectLastCall();
installSupport.replaceDigraph(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall();
callback.saveState(EasyMock.anyObject());
EasyMock.expectLastCall();
installSupport.installConfigs(f100);
EasyMock.expectLastCall();
installSupport.installLibraries(f100);
EasyMock.expectLastCall();
callback.callListeners(DeploymentEvent.BUNDLES_INSTALLED);
EasyMock.expectLastCall();
installSupport.resolveBundles(EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall();
callback.callListeners(DeploymentEvent.BUNDLES_RESOLVED);
EasyMock.expectLastCall();
callback.callListeners(EasyMock.<FeatureEvent>anyObject());
EasyMock.expectLastCall();
callback.callListeners(DeploymentEvent.DEPLOYMENT_FINISHED);
EasyMock.expectLastCall();
Bundle bundleA = createTestBundle(1, Bundle.ACTIVE, dataDir, "a100");
EasyMock.expect(installSupport.installBundle(EasyMock.eq(ROOT_REGION), EasyMock.eq("a100"), EasyMock.anyObject())).andReturn(bundleA);
c.replay();
Deployer.DeploymentState dstate = new Deployer.DeploymentState();
dstate.state = new State();
dstate.bundles = new HashMap<>();
dstate.bundlesPerRegion = new HashMap<>();
dstate.features = new HashMap<>();
dstate.features.put(f100.getId(), f100);
dstate.features.put(f101.getId(), f101);
dstate.filtersPerRegion = new HashMap<>();
dstate.filtersPerRegion.put(ROOT_REGION, new HashMap<>());
Deployer.DeploymentRequest request = new Deployer.DeploymentRequest();
request.bundleUpdateRange = DEFAULT_BUNDLE_UPDATE_RANGE;
request.featureResolutionRange = DEFAULT_FEATURE_RESOLUTION_RANGE;
request.globalRepository = null;
request.options = EnumSet.noneOf(Option.class);
request.overrides = Collections.emptySet();
request.stateChanges = Collections.emptyMap();
request.updateSnaphots = UPDATE_SNAPSHOTS_NONE;
request.requirements = new HashMap<>();
addToMapSet(request.requirements, ROOT_REGION, f100.getName() + "/" + new VersionRange(f100.getVersion(), true));
deployer.deploy(dstate, request);
c.verify();
}
use of org.apache.felix.utils.version.VersionRange in project karaf by apache.
the class ResourceUtils method addIdentityRequirement.
public static void addIdentityRequirement(ResourceImpl resource, Resource required, boolean mandatory) {
for (Capability cap : required.getCapabilities(null)) {
if (cap.getNamespace().equals(IDENTITY_NAMESPACE)) {
Map<String, Object> attributes = cap.getAttributes();
Map<String, String> dirs = new HashMap<>();
dirs.put(REQUIREMENT_RESOLUTION_DIRECTIVE, mandatory ? RESOLUTION_MANDATORY : RESOLUTION_OPTIONAL);
Map<String, Object> attrs = new HashMap<>();
attrs.put(IDENTITY_NAMESPACE, attributes.get(IDENTITY_NAMESPACE));
attrs.put(CAPABILITY_TYPE_ATTRIBUTE, attributes.get(CAPABILITY_TYPE_ATTRIBUTE));
Version version = (Version) attributes.get(CAPABILITY_VERSION_ATTRIBUTE);
if (version != null) {
attrs.put(CAPABILITY_VERSION_ATTRIBUTE, new VersionRange(version, true));
}
resource.addRequirement(new RequirementImpl(resource, IDENTITY_NAMESPACE, dirs, attrs));
}
}
}
use of org.apache.felix.utils.version.VersionRange in project karaf by apache.
the class SimpleFilter method convert.
/**
* Converts a attribute map to a filter. The filter is created by iterating
* over the map's entry set. If ordering of attributes is important (e.g.,
* for hitting attribute indices), then the map's entry set should iterate
* in the desired order. Equality testing is assumed for all attribute types
* other than version ranges, which are handled appropriated. If the attribute
* map is empty, then a filter that matches anything is returned.
*
* @param attrs Map of attributes to convert to a filter.
* @return A filter corresponding to the attributes.
*/
@SuppressWarnings("unchecked")
public static SimpleFilter convert(Map<String, Object> attrs) {
// Rather than building a filter string to be parsed into a SimpleFilter,
// we will just create the parsed SimpleFilter directly.
List<SimpleFilter> filters = new ArrayList<>(attrs.size());
for (Entry<String, Object> entry : attrs.entrySet()) {
if (entry.getValue() instanceof VersionRange) {
VersionRange vr = (VersionRange) entry.getValue();
if (!vr.isOpenFloor()) {
filters.add(new SimpleFilter(entry.getKey(), vr.getFloor().toString(), SimpleFilter.GTE));
} else {
SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
((List<Object>) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getFloor().toString(), SimpleFilter.LTE));
filters.add(not);
}
if (vr.getCeiling() != null) {
if (!vr.isOpenCeiling()) {
filters.add(new SimpleFilter(entry.getKey(), vr.getCeiling().toString(), SimpleFilter.LTE));
} else if (!vr.getCeiling().equals(VersionRange.INFINITE_VERSION)) {
SimpleFilter not = new SimpleFilter(null, new ArrayList(), SimpleFilter.NOT);
((List<Object>) not.getValue()).add(new SimpleFilter(entry.getKey(), vr.getCeiling().toString(), SimpleFilter.GTE));
filters.add(not);
}
}
} else {
List<String> values = SimpleFilter.parseSubstring(entry.getValue().toString());
if (values.size() > 1) {
filters.add(new SimpleFilter(entry.getKey(), values, SimpleFilter.SUBSTRING));
} else {
filters.add(new SimpleFilter(entry.getKey(), values.get(0), SimpleFilter.EQ));
}
}
}
SimpleFilter sf = null;
if (filters.size() == 1) {
sf = filters.get(0);
} else if (attrs.size() > 1) {
sf = new SimpleFilter(null, filters, SimpleFilter.AND);
} else if (filters.isEmpty()) {
sf = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
}
return sf;
}
Aggregations