Skip to content

Commit 548dd58

Browse files
author
jantje
committed
Get the bundle context in a not depreacated way #1126
needs code activation and testing to be completed but I need Sloeber for that.
1 parent 12e7c15 commit 548dd58

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

io.sloeber.autoBuild/src/io/sloeber/autoBuild/core/Activator.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
public class Activator implements BundleActivator {
88
public static final String PLUGIN_ID = "io.sloeber.autoBuild"; //$NON-NLS-1$
9+
private static BundleContext myBundleContext = null;
910

11+
public static BundleContext getBundleContext() {
12+
return myBundleContext;
13+
}
1014

1115
@Override
1216
public void start(BundleContext context) throws Exception {
13-
// TODO Auto-generated method stub
17+
myBundleContext = context;
1418

1519
}
1620

io.sloeber.autoBuild/src/io/sloeber/autoBuild/integration/AutoBuildConfigurationDescription.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@
1414
import org.eclipse.cdt.core.settings.model.extension.CBuildData;
1515
import org.eclipse.cdt.core.settings.model.extension.CTargetPlatformData;
1616
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
17-
import org.eclipse.core.internal.registry.osgi.OSGIUtils;
1817
import org.eclipse.core.resources.IFile;
1918
import org.eclipse.core.resources.IFolder;
2019
import org.eclipse.core.resources.IProject;
2120
import org.eclipse.core.resources.IResource;
22-
import org.eclipse.core.runtime.spi.RegistryContributor;
23-
import org.eclipse.core.runtime.spi.RegistryStrategy;
2421
import org.osgi.framework.Bundle;
25-
2622
import io.sloeber.autoBuild.api.AutoBuildConfigurationExtensionDescription;
2723
import io.sloeber.autoBuild.api.IAutoBuildConfigurationDescription;
2824
import io.sloeber.autoBuild.api.IBuildRunner;
@@ -38,6 +34,10 @@
3834
import io.sloeber.schema.api.IToolChain;
3935
import io.sloeber.schema.internal.Configuration;
4036
import io.sloeber.schema.internal.Tool;
37+
//The following lines give warning. See TOFIX below on why
38+
import org.osgi.framework.BundleContext;
39+
import org.osgi.framework.FrameworkUtil;
40+
import io.sloeber.autoBuild.core.Activator;
4141

4242
public class AutoBuildConfigurationDescription extends AutoBuildResourceData
4343
implements IAutoBuildConfigurationDescription {
@@ -497,7 +497,7 @@ public AutoBuildConfigurationDescription(ICConfigurationDescription cfgDescripti
497497
if (autoCfgExtentionDesc != null && autoCfgExtentionBundel != null && (!autoCfgExtentionDesc.isBlank())
498498
&& (!autoCfgExtentionBundel.isBlank())) {
499499
try {
500-
Bundle contributingBundle = OSGIUtils.getDefault().getBundle(autoCfgExtentionBundel);
500+
Bundle contributingBundle = getBundle(autoCfgExtentionBundel);
501501
Class<?> autoCfgExtentionDescClass = contributingBundle.loadClass(autoCfgExtentionDesc);
502502
Constructor<?> ctor = autoCfgExtentionDescClass.getDeclaredConstructor(
503503
IAutoBuildConfigurationDescription.class, String.class, String.class, String.class);
@@ -508,6 +508,7 @@ public AutoBuildConfigurationDescription(ICConfigurationDescription cfgDescripti
508508
} catch (NoSuchMethodException e) {
509509
System.err.println(
510510
"Classes derived from AutoBuildConfigurationExtensionDescription need to implement constructor IAutoBuildConfigurationDescription, String, String, String"); //$NON-NLS-1$
511+
e.printStackTrace();
511512
} catch (SecurityException | ClassNotFoundException | InstantiationException
512513

513514
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
@@ -517,6 +518,26 @@ public AutoBuildConfigurationDescription(ICConfigurationDescription cfgDescripti
517518

518519
}
519520

521+
private static Bundle getBundle(String symbolicName) {
522+
return org.eclipse.core.internal.registry.osgi.OSGIUtils.getDefault().getBundle(symbolicName);
523+
//TOFIX Below is an alternative but as I could not test at the time ...
524+
// BundleContext bundleContext = Activator.getBundleContext();
525+
// if (bundleContext == null) {
526+
// System.err.println("Failed to get the bundleContext");
527+
// System.err.println("Could not load the plugin this project is created with.");
528+
// return null;
529+
// }
530+
// Bundle result = null;
531+
// for (Bundle candidate : bundleContext.getBundles()) {
532+
// if (candidate.getSymbolicName().equals(symbolicName)) {
533+
// if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0) {
534+
// result = candidate;
535+
// }
536+
// }
537+
// }
538+
// return result;
539+
}
540+
520541
/**
521542
* get the default options and update the combined options
522543
*/

0 commit comments

Comments
 (0)