Skip to content

Commit 276b97e

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 query all models for an id
1 parent 9851080 commit 276b97e

File tree

1 file changed

+27
-6
lines changed
  • ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core

1 file changed

+27
-6
lines changed

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

Lines changed: 27 additions & 6 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,7 @@
5660
import org.osgi.framework.FrameworkUtil;
5761
import org.osgi.framework.ServiceReference;
5862
import org.osgi.framework.ServiceRegistration;
63+
import org.osgi.framework.Version;
5964
import org.osgi.util.tracker.ServiceTracker;
6065

6166
import aQute.bnd.build.Workspace;
@@ -109,7 +114,7 @@ public class PDECore extends Plugin implements DebugOptionsListener {
109114
*/
110115
private static PDEPreferencesManager fPreferenceManager;
111116

112-
private Map<String, IPluginModelBase> fHostPlugins;
117+
private Map<String, List<IPluginModelBase>> fHostPlugins;
113118

114119
public static PDECore getDefault() {
115120
return inst;
@@ -221,15 +226,20 @@ public PDECore() {
221226
inst = this;
222227
}
223228

224-
public synchronized IPluginModelBase findPluginInHost(String id) {
229+
public IPluginModelBase findPluginInHost(String id) {
230+
return findPluginsInHost(id).max(Comparator.comparing(plugin -> {
231+
return getOSGiVersion(plugin);
232+
})).orElse(null);
233+
}
234+
235+
public synchronized Stream<IPluginModelBase> findPluginsInHost(String id) {
225236
if (fHostPlugins == null) {
226237
fHostPlugins = new HashMap<>();
227-
228238
ITargetDefinition defaultTarget = TargetPlatformService.getDefault().newDefaultTarget();
229239
IStatus status = defaultTarget.resolve(new NullProgressMonitor());
230240
if (!status.isOK()) {
231241
log(status);
232-
return null;
242+
return Stream.empty();
233243
}
234244

235245
URI[] pluginPaths = Arrays.stream(defaultTarget.getBundles()) //
@@ -240,11 +250,22 @@ public synchronized IPluginModelBase findPluginInHost(String id) {
240250
state.resolveState(false);
241251

242252
for (IPluginModelBase plugin : state.getTargetModels()) {
243-
fHostPlugins.put(plugin.getPluginBase().getId(), plugin);
253+
fHostPlugins.computeIfAbsent(plugin.getPluginBase().getId(), nil -> new ArrayList<>(1)).add(plugin);
244254
}
245255
}
256+
List<IPluginModelBase> list = fHostPlugins.get(id);
257+
if (list == null) {
258+
return Stream.empty();
259+
}
260+
return list.stream();
261+
}
246262

247-
return fHostPlugins.get(id);
263+
public static Version getOSGiVersion(IPluginModelBase plugin) {
264+
BundleDescription description = plugin.getBundleDescription();
265+
if (description == null) {
266+
return Version.emptyVersion;
267+
}
268+
return description.getVersion();
248269
}
249270

250271
public PluginModelManager getModelManager() {

0 commit comments

Comments
 (0)