Bug 570345 - equinox.console remove usage of deprecated services

Change-Id: Idd50fb772be21ed95fc55b612786ca08eb497990
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.equinox.console/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.console/META-INF/MANIFEST.MF
index db06185..7909bce 100755
--- a/bundles/org.eclipse.equinox.console/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.console/META-INF/MANIFEST.MF
@@ -2,21 +2,22 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %bundleName
 Bundle-SymbolicName: org.eclipse.equinox.console
-Bundle-Version: 1.4.200.qualifier
+Bundle-Version: 1.4.300.qualifier
 Bundle-Activator: org.eclipse.equinox.console.command.adapter.Activator
 Bundle-Vendor: %bundleVendor
 Bundle-Localization: plugin
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Import-Package: org.eclipse.osgi.framework.console,
- org.eclipse.osgi.report.resolution; version="[1.0,2.0)",
+ org.eclipse.osgi.report.resolution;version="[1.0,2.0)",
  org.eclipse.osgi.service.environment,
  org.eclipse.osgi.util,
  org.osgi.framework,
- org.osgi.framework.hooks.resolver; version="[1.0, 2.0)",
+ org.osgi.framework.hooks.resolver;version="[1.0,2.0)",
  org.osgi.framework.namespace;version="1.0.0",
+ org.osgi.framework.startlevel;version="[1.0,2.0)",
  org.osgi.framework.wiring;version="1.1.0",
  org.osgi.resource;version="1.0.0",
- org.osgi.service.cm; version="[1.4, 2.0)"; resolution:=optional,
+ org.osgi.service.cm;version="[1.4,2.0)";resolution:=optional,
  org.osgi.service.condpermadmin,
  org.osgi.service.packageadmin,
  org.osgi.service.permissionadmin,
diff --git a/bundles/org.eclipse.equinox.console/pom.xml b/bundles/org.eclipse.equinox.console/pom.xml
index 3b28d69..7e3b6ab 100644
--- a/bundles/org.eclipse.equinox.console/pom.xml
+++ b/bundles/org.eclipse.equinox.console/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.console</artifactId>
-  <version>1.4.200-SNAPSHOT</version>
+  <version>1.4.300-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java
index e2a6a43..d8611fd 100644
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/command/adapter/Activator.java
@@ -20,9 +20,12 @@
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.felix.service.command.CommandProcessor;
 import org.apache.felix.service.command.CommandSession;
@@ -42,9 +45,14 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.framework.startlevel.FrameworkStartLevel;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.FrameworkWiring;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
 import org.osgi.service.packageadmin.PackageAdmin;
-import org.osgi.service.startlevel.StartLevel;
 import org.osgi.service.permissionadmin.PermissionAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
@@ -53,9 +61,11 @@
  * The activator class controls the plug-in life cycle
  */
 public class Activator implements BundleActivator {
-	private ServiceTracker<StartLevel, StartLevel> startLevelManagerTracker;
 	private ServiceTracker<ConditionalPermissionAdmin, ConditionalPermissionAdmin> condPermAdminTracker;
 	private ServiceTracker<PermissionAdmin, PermissionAdmin> permissionAdminTracker;
+	private FrameworkStartLevel frameworkStartLevel;
+	private FrameworkWiring frameworkWiring;
+	@SuppressWarnings("deprecation")
 	private ServiceTracker<PackageAdmin, PackageAdmin> packageAdminTracker;
 	private static boolean isFirstProcessor = true;
 	private static TelnetCommand telnetConnection = null;
@@ -217,8 +227,13 @@
 
 	}
 
+	@SuppressWarnings("deprecation")
 	@Override
 	public void start(BundleContext context) throws Exception {
+		Bundle systemBundle = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION);
+		frameworkStartLevel = systemBundle.adapt(FrameworkStartLevel.class);
+		frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
+
 		commandProviderTracker = new ServiceTracker<>(context, CommandProvider.class, new CommandCustomizer(context));
 		commandProviderTracker.open();
 		commandProcessorTracker = new ServiceTracker<>(context, CommandProcessor.class, new ProcessorCustomizer(context));
@@ -231,9 +246,6 @@
 		permissionAdminTracker = new ServiceTracker<>(context, PermissionAdmin.class, null);
 		permissionAdminTracker.open();
 
-		startLevelManagerTracker = new ServiceTracker<>(context, StartLevel.class, null);
-		startLevelManagerTracker.open();
-
 		packageAdminTracker = new ServiceTracker<>(context, PackageAdmin.class, null);
 		packageAdminTracker.open();
 		
@@ -258,20 +270,40 @@
 	}
 
 	private void startBundle(String bsn, boolean required) throws BundleException {
-		PackageAdmin pa = packageAdminTracker.getService();
-		if (pa != null) {
-			@SuppressWarnings("deprecation")
-			Bundle[] shells = pa.getBundles(bsn, null);
-			if (shells != null && shells.length > 0) {
-				shells[0].start(Bundle.START_TRANSIENT);
-			} else if (required) {
-				throw new BundleException("Missing required bundle: " + bsn);
+		Collection<BundleCapability> found = frameworkWiring.findProviders(new Requirement() {
+			
+			@Override
+			public Resource getResource() {
+				return null;
 			}
+			
+			@Override
+			public String getNamespace() {
+				return IdentityNamespace.IDENTITY_NAMESPACE;
+			}
+			
+			@Override
+			public Map<String, String> getDirectives() {
+				return Collections.singletonMap("filter", "("+ getNamespace() + "=" + bsn + ")");
+			}
+			
+			@Override
+			public Map<String, Object> getAttributes() {
+				return Collections.emptyMap();
+			}
+		});
+
+		if (found.isEmpty()) {
+			if (required) {
+				throw new BundleException("Missing required gogo bundle: " + bsn);
+			}
+		} else {
+			found.iterator().next().getRevision().getBundle().start(Bundle.START_TRANSIENT);
 		}
 	}
 
-	public StartLevel getStartLevel() {
-		return getServiceFromTracker(startLevelManagerTracker, StartLevel.class);
+	public FrameworkStartLevel getStartLevel() {
+		return frameworkStartLevel;
 	}
 
 	public PermissionAdmin getPermissionAdmin() {
@@ -282,6 +314,7 @@
 		return getServiceFromTracker(condPermAdminTracker, ConditionalPermissionAdmin.class);
 	}
 
+	@SuppressWarnings("deprecation")
 	public PackageAdmin getPackageAdmin() {
 		return getServiceFromTracker(packageAdminTracker, PackageAdmin.class);
 	}
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMessages.properties b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMessages.properties
index 8b24115..c77f34d 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMessages.properties
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMessages.properties
@@ -40,7 +40,6 @@
 CONSOLE_FREE_MEMORY_AFTER_GARBAGE_COLLECTION_MESSAGE=Free memory after GC:
 CONSOLE_MEMORY_GAINED_WITH_GARBAGE_COLLECTION_MESSAGE=Memory gained with GC:
 CONSOLE_FRAMEWORK_LAUNCHED_PLEASE_SHUTDOWN_MESSAGE=Framework is launched. Please shutdown framework first.
-CONSOLE_CAN_NOT_REFRESH_NO_PACKAGE_ADMIN_ERROR=Cannot refresh [PackageAdmin service is not registered]
 CONSOLE_NO_COMMAND_SPECIFIED_ERROR=No command specified
 CONSOLE_EXECUTED_RESULT_CODE_MESSAGE=Executed ({0}); result code = {1}
 CONSOLE_STARTED_IN_MESSAGE=Started({0}) in {1}
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMsg.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMsg.java
index 59a7716..d0b48e3 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMsg.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/ConsoleMsg.java
@@ -63,7 +63,6 @@
 	public static String CONSOLE_FREE_MEMORY_AFTER_GARBAGE_COLLECTION_MESSAGE;
 	public static String CONSOLE_MEMORY_GAINED_WITH_GARBAGE_COLLECTION_MESSAGE;
 	public static String CONSOLE_FRAMEWORK_LAUNCHED_PLEASE_SHUTDOWN_MESSAGE;
-	public static String CONSOLE_CAN_NOT_REFRESH_NO_PACKAGE_ADMIN_ERROR;
 	public static String CONSOLE_NO_COMMAND_SPECIFIED_ERROR;
 	public static String CONSOLE_STARTED_IN_MESSAGE;
 	public static String CONSOLE_EXECUTED_RESULT_CODE_MESSAGE;
diff --git a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
index 1833407..c835dbd 100755
--- a/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
+++ b/bundles/org.eclipse.equinox.console/src/org/eclipse/equinox/console/commands/EquinoxCommandProvider.java
@@ -61,6 +61,7 @@
 import org.osgi.framework.namespace.BundleNamespace;
 import org.osgi.framework.namespace.HostNamespace;
 import org.osgi.framework.namespace.PackageNamespace;
+import org.osgi.framework.startlevel.BundleStartLevel;
 import org.osgi.framework.wiring.BundleCapability;
 import org.osgi.framework.wiring.BundleRequirement;
 import org.osgi.framework.wiring.BundleRevision;
@@ -72,10 +73,10 @@
 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
 import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
 import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;
+//import org.osgi.service.packageadmin.ExportedPackage;
 import org.osgi.service.packageadmin.ExportedPackage;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.service.permissionadmin.PermissionAdmin;
-import org.osgi.service.startlevel.StartLevel;
 
 /**
  * This class provides methods to execute commands from the command line.  It registers
@@ -1108,24 +1109,19 @@
 	 *
 	 *  @param bundles bundle(s) to be refreshed
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_REFRESH_COMMAND_DESCRIPTION)
 	public void refresh(
 			@Descriptor(ConsoleMsg.CONSOLE_HELP_REFRESH_ALL_OPTION_DESCRIPTION)
 			@Parameter(absentValue = "false", presentValue = "true", names = { "-all" })
 			boolean shouldRefreshAll,
 			@Descriptor(ConsoleMsg.CONSOLE_HELP_REFRESH_COMMAND_ARGUMENT_DESCRIPTION) Bundle... bundles) throws Exception {
-		PackageAdmin packageAdmin = activator.getPackageAdmin();
-		if (packageAdmin != null) {
-			if(bundles != null && bundles.length > 0) {
-				packageAdmin.refreshPackages(bundles);
-			} else if (shouldRefreshAll == true) {
-				packageAdmin.refreshPackages(context.getBundles());
-			} else {
-				packageAdmin.refreshPackages(null);
-			}
+		FrameworkWiring frameworkWiring = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(FrameworkWiring.class);
+		if(bundles != null && bundles.length > 0) {
+			frameworkWiring.refreshBundles(Arrays.asList(bundles));
+		} else if (shouldRefreshAll == true) {
+			frameworkWiring.refreshBundles(Arrays.asList(context.getBundles()));
 		} else {
-			System.out.println(ConsoleMsg.CONSOLE_CAN_NOT_REFRESH_NO_PACKAGE_ADMIN_ERROR);
+			frameworkWiring.refreshBundles(null);
 		}
 	}
 
@@ -1266,7 +1262,6 @@
 	 *
 	 * @param arguments
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_SS_COMMAND_DESCRIPTION)
 	public void ss(@Descriptor(ConsoleMsg.CONSOLE_HELP_STATUS_ARGUMENT_DESCRIPTION) String... arguments) throws Exception {
 		if (context.getBundle(0).getState() == Bundle.ACTIVE) {
@@ -1322,22 +1317,22 @@
 				else
 					label = label + "_" + b.getVersion(); //$NON-NLS-1$
 				System.out.println(b.getBundleId() + "\t" + getStateName(b) + label); //$NON-NLS-1$
-				PackageAdmin packageAdmin = activator.getPackageAdmin();
-				if ((packageAdmin.getBundleType(b) & PackageAdmin.BUNDLE_TYPE_FRAGMENT) != 0) {
-					Bundle[] hosts = packageAdmin.getHosts(b);
-					if (hosts != null)
-						for (Bundle host : hosts) {
-							System.out.println("\t            Master=" + host.getBundleId()); //$NON-NLS-1$
+				BundleRevision revision = b.adapt(BundleRevision.class);
+				BundleWiring wiring = b.adapt(BundleWiring.class);
+				if (revision != null && wiring != null) {
+					if ((revision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
+						for (BundleWire hostWire : wiring.getRequiredWires(HostNamespace.HOST_NAMESPACE)) {
+							System.out.println("\t            Master=" + hostWire.getProvider().getBundle().getBundleId()); //$NON-NLS-1$
 						}
-				} else {
-					Bundle[] fragments = packageAdmin.getFragments(b);
-					if (fragments != null) {
-						System.out.print("\t            Fragments="); //$NON-NLS-1$
-						for (int f = 0; f < fragments.length; f++) {
-							Bundle fragment = fragments[f];
-							System.out.print((f > 0 ? ", " : "") + fragment.getBundleId()); //$NON-NLS-1$ //$NON-NLS-2$
+					} else {
+						List<BundleWire> fragWires = wiring.getProvidedWires(HostNamespace.HOST_NAMESPACE);
+						if (!fragWires.isEmpty()) {
+							System.out.print("\t            Fragments="); //$NON-NLS-1$
+							Iterator<BundleWire> itr = fragWires.iterator();
+							System.out.print(itr.next().getRequirer().getBundle().getBundleId());
+							itr.forEachRemaining(w -> System.out.print(", " + w.getRequirer().getBundle().getBundleId()));
+							System.out.println();
 						}
-						System.out.println();
 					}
 				}
 			}
@@ -1486,19 +1481,15 @@
 	 *
 	 * @param bundle bundle to display startlevel for; if no bundle is specified, the framework startlevel is displayed
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_SL_COMMAND_DESCRIPTION)
 	public void sl(@Descriptor(ConsoleMsg.CONSOLE_HELP_SL_COMMAND_ARGUMENT_DESCRIPTION) Bundle... bundle) throws Exception {
-		StartLevel startLevel = activator.getStartLevel();
-		if (startLevel != null) {
-			int value = 0;
-			if (bundle == null || bundle.length == 0) { // must want framework startlevel
-				value = startLevel.getStartLevel();
-				System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
-			} else { // must want bundle startlevel
-				value = startLevel.getBundleStartLevel(bundle[0]);
-				System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, Long.valueOf(bundle[0].getBundleId()), Integer.valueOf(value)));
-			}
+		int value = 0;
+		if (bundle == null || bundle.length == 0) { // must want framework startlevel
+			value = activator.getStartLevel().getStartLevel();
+			System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(value)));
+		} else { // must want bundle startlevel
+			value = bundle[0].adapt(BundleStartLevel.class).getStartLevel();
+			System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, Long.valueOf(bundle[0].getBundleId()), Integer.valueOf(value)));
 		}
 	}
 
@@ -1507,18 +1498,14 @@
 	 *
 	 * @param newSL new value for the framewrok start level
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_SETFWSL_COMMAND_DESCRIPTION)
 	public void setfwsl(@Descriptor(ConsoleMsg.CONSOLE_HELP_SETFWSL_COMMAND_ARGUMENT_DESCRIPTION) int newSL) throws Exception {
-		StartLevel startLevel = activator.getStartLevel();
-		if (startLevel != null) {
 			try {
-				startLevel.setStartLevel(newSL);
+				activator.getStartLevel().setStartLevel(newSL);
 				System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_FRAMEWORK_ACTIVE_STARTLEVEL, String.valueOf(newSL)));
 			} catch (IllegalArgumentException e) {
 				System.out.println(e.getMessage());
 			}
-		}
 	}
 
 	/**
@@ -1527,24 +1514,20 @@
 	 * @param newSL new value for bundle start level
 	 * @param bundles bundles whose start value will be changed
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_SETBSL_COMMAND_DESCRIPTION)
 	public void setbsl(
 			@Descriptor(ConsoleMsg.CONSOLE_HELP_SETFWSL_COMMAND_ARGUMENT_DESCRIPTION)int newSL,
 			@Descriptor(ConsoleMsg.CONSOLE_HELP_SETBSL_COMMAND_ARGUMENT_DESCRIPTION) Bundle... bundles) throws Exception {
-		StartLevel startLevel = activator.getStartLevel();
-		if (startLevel != null) {
-			if (bundles == null) {
-				System.out.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_OR_BUNDLE_GIVEN);
-				return;
-			}
-			for (Bundle bundle : bundles) {
-				try {
-					startLevel.setBundleStartLevel(bundle, newSL);
-					System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, Long.valueOf(bundle.getBundleId()), Integer.valueOf(newSL)));
-				} catch (IllegalArgumentException e) {
-					System.out.println(e.getMessage());
-				}
+		if (bundles == null) {
+			System.out.println(ConsoleMsg.STARTLEVEL_NO_STARTLEVEL_OR_BUNDLE_GIVEN);
+			return;
+		}
+		for (Bundle bundle : bundles) {
+			try {
+				bundle.adapt(BundleStartLevel.class).setStartLevel(newSL);
+				System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_BUNDLE_STARTLEVEL, Long.valueOf(bundle.getBundleId()), Integer.valueOf(newSL)));
+			} catch (IllegalArgumentException e) {
+				System.out.println(e.getMessage());
 			}
 		}
 	}
@@ -1554,17 +1537,13 @@
 	 *
 	 * @param newInitialSL new value for initial start level
 	 */
-	@SuppressWarnings("deprecation")
 	@Descriptor(ConsoleMsg.CONSOLE_HELP_SETIBSL_COMMAND_DESCRIPTION)
 	public void setibsl(@Descriptor(ConsoleMsg.CONSOLE_HELP_SETFWSL_COMMAND_ARGUMENT_DESCRIPTION) int newInitialSL) throws Exception {
-		StartLevel startLevel = activator.getStartLevel();
-		if (startLevel != null) {
-			try {
-				startLevel.setInitialBundleStartLevel(newInitialSL);
-				System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(newInitialSL)));
-			} catch (IllegalArgumentException e) {
-				System.out.println(e.getMessage());
-			}
+		try {
+			activator.getStartLevel().setInitialBundleStartLevel(newInitialSL);
+			System.out.println(NLS.bind(ConsoleMsg.STARTLEVEL_INITIAL_BUNDLE_STARTLEVEL, String.valueOf(newInitialSL)));
+		} catch (IllegalArgumentException e) {
+			System.out.println(e.getMessage());
 		}
 	}