Skip to content

Commit 9560528

Browse files
committed
Add support to search bundles in the host by version range
Currently PDE#findPluginInHost assumes that a bundle is unique by its id what is not true in all cases. This now do the following: - index the plugins by id but keep them in a list to support multiple - if more than one version is found return the highest - allow to further restrict by version range
1 parent 9851080 commit 9560528

File tree

1 file changed

+35
-4
lines changed
  • ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core

1 file changed

+35
-4
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDECore.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
import java.lang.reflect.InvocationTargetException;
1717
import java.net.URI;
18+
import java.util.ArrayList;
1819
import java.util.Arrays;
20+
import java.util.Comparator;
1921
import java.util.HashMap;
2022
import java.util.Hashtable;
23+
import java.util.List;
2124
import java.util.Map;
2225
import java.util.stream.Stream;
2326

@@ -37,6 +40,7 @@
3740
import org.eclipse.osgi.service.debug.DebugOptions;
3841
import org.eclipse.osgi.service.debug.DebugOptionsListener;
3942
import org.eclipse.osgi.service.debug.DebugTrace;
43+
import org.eclipse.osgi.service.resolver.BundleDescription;
4044
import org.eclipse.pde.core.IBundleClasspathResolver;
4145
import org.eclipse.pde.core.IClasspathContributor;
4246
import org.eclipse.pde.core.plugin.IPluginModelBase;
@@ -56,6 +60,8 @@
5660
import org.osgi.framework.FrameworkUtil;
5761
import org.osgi.framework.ServiceReference;
5862
import org.osgi.framework.ServiceRegistration;
63+
import org.osgi.framework.Version;
64+
import org.osgi.framework.VersionRange;
5965
import org.osgi.util.tracker.ServiceTracker;
6066

6167
import aQute.bnd.build.Workspace;
@@ -109,7 +115,7 @@ public class PDECore extends Plugin implements DebugOptionsListener {
109115
*/
110116
private static PDEPreferencesManager fPreferenceManager;
111117

112-
private Map<String, IPluginModelBase> fHostPlugins;
118+
private Map<String, List<IPluginModelBase>> fHostPlugins;
113119

114120
public static PDECore getDefault() {
115121
return inst;
@@ -222,9 +228,12 @@ public PDECore() {
222228
}
223229

224230
public synchronized IPluginModelBase findPluginInHost(String id) {
231+
return findPluginInHost(id, null);
232+
}
233+
234+
public synchronized IPluginModelBase findPluginInHost(String id, VersionRange version) {
225235
if (fHostPlugins == null) {
226236
fHostPlugins = new HashMap<>();
227-
228237
ITargetDefinition defaultTarget = TargetPlatformService.getDefault().newDefaultTarget();
229238
IStatus status = defaultTarget.resolve(new NullProgressMonitor());
230239
if (!status.isOK()) {
@@ -240,11 +249,33 @@ public synchronized IPluginModelBase findPluginInHost(String id) {
240249
state.resolveState(false);
241250

242251
for (IPluginModelBase plugin : state.getTargetModels()) {
243-
fHostPlugins.put(plugin.getPluginBase().getId(), plugin);
252+
fHostPlugins.computeIfAbsent(plugin.getPluginBase().getId(), nil -> new ArrayList<>(1)).add(plugin);
244253
}
245254
}
255+
List<IPluginModelBase> list = fHostPlugins.get(id);
256+
if (list.isEmpty()) {
257+
return null;
258+
}
259+
if (version == null) {
260+
if (list.size() == 1) {
261+
return list.get(0);
262+
}
263+
return list.stream().max(Comparator.comparing(plugin -> {
264+
return getOSGiVersion(plugin);
265+
})).get();
266+
}
267+
return list.stream().filter(plugin -> version.includes(getOSGiVersion(plugin)))
268+
.max(Comparator.comparing(plugin -> {
269+
return getOSGiVersion(plugin);
270+
})).orElse(null);
271+
}
246272

247-
return fHostPlugins.get(id);
273+
public static Version getOSGiVersion(IPluginModelBase plugin) {
274+
BundleDescription description = plugin.getBundleDescription();
275+
if (description == null) {
276+
return Version.emptyVersion;
277+
}
278+
return description.getVersion();
248279
}
249280

250281
public PluginModelManager getModelManager() {

0 commit comments

Comments
 (0)