1515
1616import java .lang .reflect .InvocationTargetException ;
1717import java .net .URI ;
18+ import java .util .ArrayList ;
1819import java .util .Arrays ;
20+ import java .util .Comparator ;
1921import java .util .HashMap ;
2022import java .util .Hashtable ;
23+ import java .util .List ;
2124import java .util .Map ;
2225import java .util .stream .Stream ;
2326
3740import org .eclipse .osgi .service .debug .DebugOptions ;
3841import org .eclipse .osgi .service .debug .DebugOptionsListener ;
3942import org .eclipse .osgi .service .debug .DebugTrace ;
43+ import org .eclipse .osgi .service .resolver .BundleDescription ;
4044import org .eclipse .pde .core .IBundleClasspathResolver ;
4145import org .eclipse .pde .core .IClasspathContributor ;
4246import org .eclipse .pde .core .plugin .IPluginModelBase ;
5660import org .osgi .framework .FrameworkUtil ;
5761import org .osgi .framework .ServiceReference ;
5862import org .osgi .framework .ServiceRegistration ;
63+ import org .osgi .framework .Version ;
64+ import org .osgi .framework .VersionRange ;
5965import org .osgi .util .tracker .ServiceTracker ;
6066
6167import 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