Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated PackageAdmin service with Wiring-API #47

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bundles/org.eclipse.equinox.app/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ Export-Package:
org.osgi.service.application;version="1.1"
Import-Package: org.eclipse.osgi.framework.console;resolution:=optional,
org.eclipse.osgi.framework.log,
org.eclipse.osgi.framework.util,
org.eclipse.osgi.service.datalocation,
org.eclipse.osgi.service.debug,
org.eclipse.osgi.service.environment; version="1.1",
org.eclipse.osgi.service.environment;version="1.1",
org.eclipse.osgi.service.runnable,
org.eclipse.osgi.storagemanager,
org.eclipse.osgi.util,
org.osgi.framework;version="1.3",
org.osgi.service.condpermadmin; resolution:=optional,
org.osgi.service.condpermadmin;resolution:=optional,
org.osgi.service.event;version="1.0.0";resolution:=optional,
org.osgi.service.packageadmin;version="1.2",
org.osgi.util.tracker
Require-Bundle: org.eclipse.equinox.registry;bundle-version="[3.4.0,4.0.0)",
org.eclipse.equinox.common;bundle-version="[3.2.0,4.0.0)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*******************************************************************************
* Copyright (c) 2005, 2018 IBM Corporation and others.
* Copyright (c) 2005, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
Expand All @@ -21,20 +21,17 @@
import org.eclipse.core.runtime.spi.RegistryContributor;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.framework.util.Wirings;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class Activator implements BundleActivator, ServiceTrackerCustomizer {
public static final String PI_APP = "org.eclipse.equinox.app"; //$NON-NLS-1$
public static boolean DEBUG = false;
private volatile static BundleContext _context;
// PackageAdmin is a system service that never goes away as long
// as the framework is active. No need to track it!!
private volatile static PackageAdmin _packageAdmin;
private volatile static EclipseAppContainer container;
// tracks the FrameworkLog service
private volatile static ServiceTracker _frameworkLogTracker;
Expand All @@ -45,10 +42,6 @@ public class Activator implements BundleActivator, ServiceTrackerCustomizer {
@Override
public void start(BundleContext bc) {
_context = bc;
// doing simple get service here because we expect the PackageAdmin service to always be available
ServiceReference ref = bc.getServiceReference(PackageAdmin.class.getName());
if (ref != null)
_packageAdmin = (PackageAdmin) bc.getService(ref);
_frameworkLogTracker = new ServiceTracker(bc, FrameworkLog.class.getName(), null);
_frameworkLogTracker.open();
getDebugOptions(bc);
Expand Down Expand Up @@ -83,7 +76,6 @@ public void stop(BundleContext bc) {
_frameworkLogTracker.close();
_frameworkLogTracker = null;
}
_packageAdmin = null; // we do not unget PackageAdmin here; let the framework do it for us
_context = null;
}

Expand Down Expand Up @@ -218,19 +210,8 @@ static Bundle getBundle(IContributor contributor) {
// try using the name of the contributor below
}
}
PackageAdmin packageAdmin = _packageAdmin;
if (packageAdmin == null)
return null;
Bundle[] bundles = packageAdmin.getBundles(contributor.getName(), null);
if (bundles == null)
return null;
//Return the first bundle that is not installed or uninstalled
for (Bundle bundle : bundles) {
if ((bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundle;
}
}
return null;
// Return the first bundle that is not installed or uninstalled
return Wirings.getAtLeastResolvedBundle(contributor.getName()).orElse(null);
}

static BundleContext getContext() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.builder.annotationPath.allLocations=disabled
org.eclipse.jdt.core.builder.cleanOutputFolder=clean
org.eclipse.jdt.core.builder.duplicateResourceTask=warning
org.eclipse.jdt.core.builder.invalidClasspath=abort
Expand All @@ -25,6 +26,7 @@ org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.doc.comment.support=enabled
org.eclipse.jdt.core.compiler.maxProblemPerUnit=1000
org.eclipse.jdt.core.compiler.problem.APILeak=warning
org.eclipse.jdt.core.compiler.problem.annotatedTypeArgumentToUnannotated=info
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
Expand All @@ -33,7 +35,7 @@ org.eclipse.jdt.core.compiler.problem.deadCode=warning
org.eclipse.jdt.core.compiler.problem.deprecation=warning
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.equinox.cm.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Bundle-SymbolicName: org.eclipse.equinox.cm.test
Bundle-Version: 1.1.200.qualifier
Bundle-Activator: org.eclipse.equinox.cm.test.Activator
Import-Package: org.eclipse.equinox.log,
org.eclipse.osgi.framework.util,
org.junit;version="4.12.0",
org.junit.runner;version="4.12.0",
org.junit.runners;version="4.12.0",
org.osgi.framework;version="1.3.0",
org.osgi.service.cm;version="1.2.0",
org.osgi.service.event;version="1.1.0",
org.osgi.service.log;version="1.3.0",
org.osgi.service.packageadmin;version="1.2.0"
org.osgi.service.log;version="1.3.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Automatic-Module-Name: org.eclipse.equinox.cm.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation and others
* Copyright (c) 2007, 2022 IBM Corporation and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,51 +14,28 @@
package org.eclipse.equinox.cm.test;

import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;

public class Activator implements BundleActivator {

private static PackageAdmin packageAdmin;
private static BundleContext bundleContext;
private ServiceReference<PackageAdmin> packageAdminRef;

public void start(BundleContext context) throws Exception {
packageAdminRef = context.getServiceReference(PackageAdmin.class);
setPackageAdmin(context.getService(packageAdminRef));
setBundleContext(context);
}

public void stop(BundleContext context) throws Exception {
setBundleContext(null);
setPackageAdmin(null);
context.ungetService(packageAdminRef);
}

private static synchronized void setBundleContext(BundleContext context) {
bundleContext = context;
}

private static synchronized void setPackageAdmin(PackageAdmin service) {
packageAdmin = service;
}

static synchronized BundleContext getBundleContext() {
return bundleContext;
}

static synchronized Bundle getBundle(String symbolicName) {
if (packageAdmin == null)
return null;

Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
if (bundles == null)
return null;
//Return the first bundle that is not installed or uninstalled
for (Bundle bundle : bundles) {
if ((bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundle;
}
}
return null;
static Bundle getBundle(String symbolicName) {
return org.eclipse.osgi.framework.util.Wirings.getBundle(symbolicName).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*******************************************************************************/
package org.eclipse.core.internal.runtime;

import static java.util.function.Predicate.not;
import static org.eclipse.osgi.framework.util.Wirings.inState;

import java.net.URL;
import java.util.*;
import org.eclipse.core.internal.boot.PlatformURLBaseConnection;
Expand All @@ -23,14 +26,14 @@
import org.eclipse.equinox.log.ExtendedLogReaderService;
import org.eclipse.equinox.log.ExtendedLogService;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.framework.util.Wirings;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.debug.DebugOptionsListener;
import org.eclipse.osgi.service.localization.BundleLocalization;
import org.eclipse.osgi.service.urlconversion.URLConverter;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.service.url.URLConstants;
import org.osgi.service.url.URLStreamHandlerService;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -55,7 +58,6 @@ public class Activator implements BundleActivator {
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);
Expand Down Expand Up @@ -93,15 +95,15 @@ public void start(BundleContext context) throws Exception {
private PlatformLogWriter getPlatformWriter(BundleContext context) {
ServiceReference<ExtendedLogService> logRef = context.getServiceReference(ExtendedLogService.class);
ServiceReference<ExtendedLogReaderService> readerRef = context.getServiceReference(ExtendedLogReaderService.class);
ServiceReference<PackageAdmin> packageAdminRef = context.getServiceReference(PackageAdmin.class);
if (logRef == null || readerRef == null || packageAdminRef == null)
if (logRef == null || readerRef == null) {
return null;
}
ExtendedLogService logService = context.getService(logRef);
ExtendedLogReaderService readerService = context.getService(readerRef);
PackageAdmin packageAdmin = context.getService(packageAdminRef);
if (logService == null || readerService == null || packageAdmin == null)
if (logService == null || readerService == null) {
return null;
PlatformLogWriter writer = new PlatformLogWriter(logService, packageAdmin, context.getBundle());
}
PlatformLogWriter writer = new PlatformLogWriter(logService, context.getBundle());
readerService.addLogListener(writer, writer);
return writer;
}
Expand Down Expand Up @@ -136,40 +138,18 @@ public Location getInstanceLocation() {

/**
* Return the resolved bundle with the specified symbolic name.
*
* @see PackageAdmin#getBundles(String, String)
*/
public Bundle getBundle(String symbolicName) {
PackageAdmin admin = getBundleAdmin();
if (admin == null)
return null;
Bundle[] bundles = admin.getBundles(symbolicName, null);
if (bundles == null)
return null;
//Return the first bundle that is not installed or uninstalled
for (Bundle bundle : bundles) {
if ((bundle.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundle;
}
}
return null;
}

/*
* Return the package admin service, if available.
*/
private PackageAdmin getBundleAdmin() {
return bundleTracker.current().orElse(null);
return Wirings.getBundles(symbolicName) //
.filter(not(inState(Bundle.INSTALLED, Bundle.UNINSTALLED))) //
.findFirst().orElse(null);
}

/*
* Return an array of fragments for the given bundle host.
*/
public Bundle[] getFragments(Bundle host) {
PackageAdmin admin = getBundleAdmin();
if (admin == null)
return new Bundle[0];
return admin.getFragments(host);
public static List<Bundle> getFragments(Bundle host) {
return Wirings.getFragments(host);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.*;
import org.eclipse.core.runtime.*;
import org.osgi.framework.Bundle;

Expand Down Expand Up @@ -243,13 +242,13 @@ private static URL findInFragments(Bundle b, IPath filePath, ArrayList<URL> mult
Activator activator = Activator.getDefault();
if (activator == null)
return null;
Bundle[] fragments = activator.getFragments(b);
if (fragments == null)
List<Bundle> fragments = Activator.getFragments(b);
if (fragments.isEmpty()) {
return null;

if (multiple != null)
multiple.ensureCapacity(fragments.length + 1);

}
if (multiple != null) {
multiple.ensureCapacity(fragments.size() + 1);
}
for (Bundle fragment : fragments) {
URL fileURL = fragment.getEntry(filePath.toString());
if (fileURL != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
* Copyright (c) 2000, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -15,13 +15,14 @@
package org.eclipse.core.internal.runtime;

import java.util.ArrayList;
import java.util.Optional;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.log.*;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.framework.util.Wirings;
import org.osgi.framework.Bundle;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogService;
import org.osgi.service.packageadmin.PackageAdmin;

/**
* A log writer that writes log entries.
Expand All @@ -32,12 +33,10 @@
public class PlatformLogWriter implements SynchronousLogListener, LogFilter {
public static final String EQUINOX_LOGGER_NAME = "org.eclipse.equinox.logger"; //$NON-NLS-1$
private final ExtendedLogService logService;
private final PackageAdmin packageAdmin;
private final Bundle bundle;

public PlatformLogWriter(ExtendedLogService logService, PackageAdmin packageAdmin, Bundle bundle) {
public PlatformLogWriter(ExtendedLogService logService, Bundle bundle) {
this.logService = logService;
this.packageAdmin = packageAdmin;
this.bundle = bundle;
}

Expand Down Expand Up @@ -90,10 +89,11 @@ public static FrameworkLogEntry getLog(IStatus status) {

private Bundle getBundle(IStatus status) {
String pluginID = status.getPlugin();
if (pluginID == null)
if (pluginID == null) {
return bundle;
Bundle[] bundles = packageAdmin.getBundles(pluginID, null);
return bundles == null || bundles.length == 0 ? bundle : bundles[0];
}
Optional<Bundle> pluginBundle = Wirings.getBundle(pluginID);
return pluginBundle.orElse(bundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,7 @@ private static ClassLoader createTempClassloader(Bundle b) {
}

private static void addFragments(Bundle host, ArrayList<URL> classpath) {
Activator activator = Activator.getDefault();
if (activator == null)
return;
Bundle[] fragments = activator.getFragments(host);
if (fragments == null)
return;

for (Bundle fragment : fragments) {
for (Bundle fragment : Activator.getFragments(host)) {
addClasspathEntries(fragment, classpath);
addDevEntries(fragment, classpath);
}
Expand Down
Loading