Bug 563987 - Use ServiceCaller instead of ServiceTracker in common
bundle.

Change-Id: I34936ccc46c35610e9d06065768c1b581e006c7b
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/internal/runtime/Activator.java b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/internal/runtime/Activator.java
index 578c365..a2e8f3c 100644
--- a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/internal/runtime/Activator.java
+++ b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/internal/runtime/Activator.java
@@ -19,6 +19,7 @@
 import org.eclipse.core.internal.boot.PlatformURLBaseConnection;
 import org.eclipse.core.internal.boot.PlatformURLHandler;
 import org.eclipse.core.runtime.IAdapterManager;
+import org.eclipse.core.runtime.ServiceCaller;
 import org.eclipse.equinox.log.ExtendedLogReaderService;
 import org.eclipse.equinox.log.ExtendedLogService;
 import org.eclipse.osgi.framework.log.FrameworkLog;
@@ -50,13 +51,15 @@
 	private static Activator singleton;
 	private ServiceRegistration<URLConverter> platformURLConverterService = null;
 	private ServiceRegistration<IAdapterManager> adapterManagerService = null;
-	private ServiceTracker<Object, Location> installLocationTracker = null;
-	private ServiceTracker<Object, Location> instanceLocationTracker = null;
-	private ServiceTracker<Object, Location> configLocationTracker = null;
-	private ServiceTracker<Object, PackageAdmin> bundleTracker = null;
-	private ServiceTracker<Object, DebugOptions> debugTracker = null;
-	private ServiceTracker<Object, FrameworkLog> logTracker = null;
-	private ServiceTracker<Object, BundleLocalization> localizationTracker = null;
+	private final ServiceCaller<Location> installLocationTracker = new ServiceCaller<>(getClass(), Location.class, Location.INSTALL_FILTER);
+	private final ServiceCaller<Location> instanceLocationTracker = new ServiceCaller<>(getClass(), Location.class, Location.INSTANCE_FILTER);
+	private final ServiceCaller<Location> configLocationTracker = new ServiceCaller<>(getClass(), Location.class, Location.CONFIGURATION_FILTER);
+	@SuppressWarnings("deprecation")
+	private final ServiceCaller<PackageAdmin> bundleTracker = new ServiceCaller<>(getClass(), PackageAdmin.class);
+	private final ServiceCaller<DebugOptions> debugTracker = new ServiceCaller<>(getClass(), DebugOptions.class);
+	private final ServiceCaller<FrameworkLog> logTracker = new ServiceCaller<>(getClass(), FrameworkLog.class);
+	private final ServiceCaller<BundleLocalization> localizationTracker = new ServiceCaller<>(getClass(), BundleLocalization.class);
+
 	private ServiceRegistration<DebugOptionsListener> debugRegistration;
 
 	/*
@@ -72,14 +75,6 @@
 		bundleContext = context;
 		singleton = this;
 
-		installLocationTracker = openServiceTracker(Location.INSTALL_FILTER);
-		instanceLocationTracker = openServiceTracker(Location.INSTANCE_FILTER);
-		configLocationTracker = openServiceTracker(Location.CONFIGURATION_FILTER);
-		bundleTracker = openServiceTracker(PackageAdmin.class);
-		debugTracker = openServiceTracker(DebugOptions.class);
-		logTracker = openServiceTracker(FrameworkLog.class);
-		localizationTracker = openServiceTracker(BundleLocalization.class);
-
 		RuntimeLog.setLogWriter(getPlatformWriter(context));
 		Dictionary<String, Object> urlProperties = new Hashtable<>();
 		urlProperties.put("protocol", "platform"); //$NON-NLS-1$ //$NON-NLS-2$
@@ -111,28 +106,28 @@
 	 * Return the configuration location service, if available.
 	 */
 	public Location getConfigurationLocation() {
-		return configLocationTracker.getService();
+		return configLocationTracker.current().orElse(null);
 	}
 
 	/*
 	 * Return the debug options service, if available.
 	 */
 	public DebugOptions getDebugOptions() {
-		return debugTracker.getService();
+		return debugTracker.current().orElse(null);
 	}
 
 	/*
 	 * Return the framework log service, if available.
 	 */
 	public FrameworkLog getFrameworkLog() {
-		return logTracker.getService();
+		return logTracker.current().orElse(null);
 	}
 
 	/*
 	 * Return the instance location service, if available.
 	 */
 	public Location getInstanceLocation() {
-		return instanceLocationTracker.getService();
+		return instanceLocationTracker.current().orElse(null);
 	}
 
 	/**
@@ -160,7 +155,7 @@
 	 * Return the package admin service, if available.
 	 */
 	private PackageAdmin getBundleAdmin() {
-		return bundleTracker.getService();
+		return bundleTracker.current().orElse(null);
 	}
 
 	/*
@@ -177,20 +172,7 @@
 	 * Return the install location service if available.
 	 */
 	public Location getInstallLocation() {
-		return installLocationTracker.getService();
-	}
-
-	private <T> ServiceTracker<Object, T> openServiceTracker(String filterString) throws InvalidSyntaxException {
-		Filter filter = bundleContext.createFilter(filterString);
-		ServiceTracker<Object, T> tracker = new ServiceTracker<>(bundleContext, filter, null);
-		tracker.open();
-		return tracker;
-	}
-
-	private <T> ServiceTracker<Object, T> openServiceTracker(Class<?> clazz) {
-		ServiceTracker<Object, T> tracker = new ServiceTracker<>(bundleContext, clazz.getName(), null);
-		tracker.open();
-		return tracker;
+		return installLocationTracker.current().orElse(null);
 	}
 
 	/**
@@ -218,7 +200,7 @@
 		if (localizationTracker == null) {
 			throw new MissingResourceException(CommonMessages.activator_resourceBundleNotStarted, bundle.getSymbolicName(), ""); //$NON-NLS-1$
 		}
-		BundleLocalization location = localizationTracker.getService();
+		BundleLocalization location = localizationTracker.current().orElse(null);
 		ResourceBundle result = null;
 		if (location != null)
 			result = location.getLocalization(bundle, locale);
@@ -238,27 +220,6 @@
 			adapterManagerService.unregister();
 			adapterManagerService = null;
 		}
-		if (installLocationTracker != null) {
-			installLocationTracker.close();
-		}
-		if (configLocationTracker != null) {
-			configLocationTracker.close();
-		}
-		if (bundleTracker != null) {
-			bundleTracker.close();
-		}
-		if (debugTracker != null) {
-			debugTracker.close();
-		}
-		if (logTracker != null) {
-			logTracker.close();
-		}
-		if (instanceLocationTracker != null) {
-			instanceLocationTracker.close();
-		}
-		if (localizationTracker != null) {
-			localizationTracker.close();
-		}
 		if (debugRegistration != null) {
 			debugRegistration.unregister();
 			debugRegistration = null;
diff --git a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
index bd472f5..7afe74e 100644
--- a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
+++ b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/ServiceCaller.java
@@ -78,9 +78,8 @@
  * }
  * </pre>
  * 
- * Note that this class is intended for simple service usage patterns only.  More advanced cases that
- * require tracking of service ranking or additional service property matching must use other
- * mechanisms such as the {@link ServiceTracker} or declarative services.
+ * Note that this class is intended for simple service usage patterns only.  More advanced cases 
+ * should use other mechanisms such as the {@link ServiceTracker} or declarative services.
  * 
  * @param <Service> the service type for this caller
  * @since 3.13