blob: c42f0d3552fe3a361a9cc16ff29acd592dd5b085 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.systemextension;
import java.util.Collection;
import java.util.Iterator;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRequirement;
import org.osgi.framework.wiring.BundleRevision;
public class ResolverHook implements ResolverHookFactory {
@Override
public org.osgi.framework.hooks.resolver.ResolverHook begin(
final Collection<BundleRevision> triggers) {
return new org.osgi.framework.hooks.resolver.ResolverHook() {
@Override
public void filterSingletonCollisions(BundleCapability singleton,
Collection<BundleCapability> collisionCandidates) {
}
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
}
@Override
public void filterMatches(BundleRequirement requirement,
Collection<BundleCapability> candidates) {
if (requirement.getNamespace().equals("osgi.wiring.bundle")) {
if (candidates.size() > 1 && containsGuava(candidates)) {
filterHighestGuava(candidates);
}
}
// if (requirement.getNamespace().equals("osgi.wiring.package")) {
//
// if (candidates.size() > 1
// && containsJavaxAnnotation(candidates)) {
// removeJavaxAnnotation120(candidates);
// }
// }
}
@Override
public void end() {
}
};
}
/**
* Returns true, if the candiates contain guava.
*
* @param candidates
* @return
*/
protected boolean containsGuava(Collection<BundleCapability> candidates) {
for (BundleCapability cap : candidates) {
if (cap.getResource().getSymbolicName().equals("com.google.guava")) {
return true;
}
}
return false;
}
// /**
// * Returns true if the candiates contain javax.annotation.
// *
// * @param candidates
// * @return
// */
// protected boolean containsJavaxAnnotation(
// Collection<BundleCapability> candidates) {
// for (BundleCapability cap : candidates) {
// if (cap.getResource().getSymbolicName().equals("javax.annotation")) {
// return true;
//
// }
// }
// return false;
// }
// /**
// * Handle javax.annotation bundle. Do not use version 1.2.0.
// *
// * @param candidates
// */
// private void removeJavaxAnnotation120(
// Collection<BundleCapability> candidates) {
// for (Iterator<BundleCapability> iterator = candidates.iterator(); iterator
// .hasNext();) {
// BundleCapability cap = iterator.next();
// if (cap.getResource().getVersion().toString().startsWith("1.2.0")) {
// iterator.remove();
// }
// }
// }
/**
* Handle guava bundle. Only use the highest supported version.
*
* @param candidates
*/
private void filterHighestGuava(Collection<BundleCapability> candidates) {
BundleCapability highest = getHighestGuava(candidates);
if (highest != null) {
for (Iterator<BundleCapability> iterator = candidates.iterator(); iterator
.hasNext();) {
BundleCapability cap = iterator.next();
if (cap != highest) {
iterator.remove();
}
}
}
}
/**
* Filters the highest available guava from candidates.
*
* @param candidates
* @return
*/
private BundleCapability getHighestGuava(
Collection<BundleCapability> candidates) {
BundleCapability highest = null;
for (BundleCapability cap : candidates) {
if (cap.getResource().getSymbolicName().equals("com.google.guava")) {
if (highest != null) {
if (highest.getResource().getVersion()
.compareTo(cap.getResource().getVersion()) < 1) {
highest = cap;
}
} else {
highest = cap;
}
}
}
return highest;
}
}