Skip to content

Commit

Permalink
Make the PlatformConfigurationFactory a component
Browse files Browse the repository at this point in the history
Currently the activator created the factory (but only register it as a
plain service) and on the other hand the BundleGroupComponent then
access the activator in a static way.

This now decouples the Factory and BundleGroupComponent from the
activator by making them components.

Fix #1572
  • Loading branch information
laeubi committed Dec 15, 2024
1 parent 5d8ee71 commit 8b8afca
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 133 deletions.
5 changes: 3 additions & 2 deletions update/org.eclipse.update.configurator/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.update.configurator; singleton:=true
Bundle-Version: 3.5.500.qualifier
Bundle-Version: 3.5.600.qualifier
Bundle-Activator: org.eclipse.update.internal.configurator.ConfigurationActivator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand All @@ -17,6 +17,7 @@ Import-Package: javax.xml.parsers,
org.w3c.dom,
org.xml.sax,
org.xml.sax.helpers
Service-Component: OSGI-INF/org.eclipse.update.internal.configurator.BundleGroupComponent.xml
Bundle-ActivationPolicy: lazy
Automatic-Module-Name: org.eclipse.update.configurator
Service-Component: OSGI-INF/org.eclipse.update.internal.configurator.BundleGroupComponent.xml,
OSGI-INF/org.eclipse.update.internal.configurator.PlatformConfigurationFactory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,58 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.update.internal.configurator;

import java.util.ArrayList;

import org.eclipse.core.runtime.IBundleGroup;
import org.eclipse.core.runtime.IBundleGroupProvider;
import org.eclipse.update.configurator.IPlatformConfiguration;
import org.eclipse.update.configurator.IPlatformConfiguration.IFeatureEntry;
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* Declarative services component that provides an implementation of
* {@link IBundleGroupProvider}. This allows the bundle group provider to be
* made available in the service registry before this bundle has started.
*/
@Component
@Component(service = IBundleGroupProvider.class)
@SuppressWarnings("deprecation")
public class BundleGroupComponent implements IBundleGroupProvider {


private IPlatformConfigurationFactory factory;

@Activate
public BundleGroupComponent(@Reference IPlatformConfigurationFactory factory) {
this.factory = factory;
}

@Override
public IBundleGroup[] getBundleGroups() {
ConfigurationActivator activator = ConfigurationActivator.getConfigurator();
if (activator.bundleGroupProviderSR != null)
// we manually registered the group in the activator; return no groups
// the manually registered service will handle the groups we know about
IPlatformConfiguration configuration = factory.getCurrentPlatformConfiguration();
if (configuration == null) {
return new IBundleGroup[0];
return activator.getBundleGroups();
}
IPlatformConfiguration.IFeatureEntry[] features = configuration.getConfiguredFeatureEntries();
ArrayList<IBundleGroup> bundleGroups = new ArrayList<>(features.length);
for (IFeatureEntry feature : features) {
if (feature instanceof FeatureEntry && ((FeatureEntry) feature).hasBranding())
bundleGroups.add((IBundleGroup) feature);
}
return bundleGroups.toArray(new IBundleGroup[bundleGroups.size()]);
}

@Override
public String getName() {
return ConfigurationActivator.getConfigurator().getName();
return Messages.BundleGroupProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,13 @@
*******************************************************************************/
package org.eclipse.update.internal.configurator;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.IBundleGroup;
import org.eclipse.core.runtime.IBundleGroupProvider;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.util.NLS;
import org.eclipse.update.configurator.IPlatformConfiguration;
import org.eclipse.update.configurator.IPlatformConfiguration.IFeatureEntry;
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

public class ConfigurationActivator implements BundleActivator, IBundleGroupProvider, IConfigurationConstants {
public class ConfigurationActivator implements BundleActivator, IConfigurationConstants {

public static String PI_CONFIGURATOR = "org.eclipse.update.configurator"; //$NON-NLS-1$
public static final String LAST_CONFIG_STAMP = "last.config.stamp"; //$NON-NLS-1$
Expand All @@ -46,91 +32,22 @@ public class ConfigurationActivator implements BundleActivator, IBundleGroupProv
public static boolean DEBUG = false;

private static BundleContext context;
private ServiceRegistration<IPlatformConfigurationFactory> configurationFactorySR;
ServiceRegistration<?> bundleGroupProviderSR;
private PlatformConfiguration configuration;

// Location of the configuration data
private Location configLocation;

// Singleton
private static ConfigurationActivator configurator;

public ConfigurationActivator() {
configurator = this;
}

@Override
public void start(BundleContext ctx) throws Exception {
context = ctx;
loadOptions();
acquireFrameworkLogService();
try {
initialize();
} catch (Exception e) {
//we failed to start, so make sure Utils closes its service trackers
Utils.shutdown();
throw e;
}

Utils.debug("Starting update configurator..."); //$NON-NLS-1$
}

private void initialize() throws Exception {

configLocation = Utils.getConfigurationLocation();
// create the name space directory for update (configuration/org.eclipse.update)
if (!configLocation.isReadOnly()) {
try {
URL privateURL = new URL(configLocation.getURL(), NAME_SPACE);
File f = new File(privateURL.getFile());
if (!f.exists())
f.mkdirs();
} catch (MalformedURLException e1) {
// ignore
}
}
configurationFactorySR = context.registerService(IPlatformConfigurationFactory.class, new PlatformConfigurationFactory(), null);
configuration = getPlatformConfiguration(Utils.getInstallURL(), configLocation);
if (configuration == null)
throw Utils.newCoreException(NLS.bind(Messages.ConfigurationActivator_createConfig, (new String[] {configLocation.getURL().toExternalForm()})), null);

}

@Override
public void stop(BundleContext ctx) throws Exception {
// quick fix (hack) for bug 47861
try {
PlatformConfiguration.shutdown();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
configurationFactorySR.unregister();
if (bundleGroupProviderSR != null)
bundleGroupProviderSR.unregister();
Utils.shutdown();
}

/**
* Creates and starts the platform configuration.
* @return the just started platform configuration
*/
private PlatformConfiguration getPlatformConfiguration(URL installURL, Location configLocation) {
try {
PlatformConfiguration.startup(installURL, configLocation);
} catch (Exception e) {
String message = e.getMessage();
if (message == null)
message = ""; //$NON-NLS-1$
Utils.log(Utils.newStatus(message, e));
}
return PlatformConfiguration.getCurrent();

}

private void loadOptions() {
// all this is only to get the application args
// all this is only to get the application args
DebugOptions service = null;
ServiceReference<DebugOptions> reference = context.getServiceReference(DebugOptions.class);
if (reference != null)
Expand All @@ -149,29 +66,6 @@ public static BundleContext getBundleContext() {
return context;
}

@Override
public String getName() {
return Messages.BundleGroupProvider;
}

@Override
public IBundleGroup[] getBundleGroups() {
if (configuration == null)
return new IBundleGroup[0];

IPlatformConfiguration.IFeatureEntry[] features = configuration.getConfiguredFeatureEntries();
ArrayList<IBundleGroup> bundleGroups = new ArrayList<>(features.length);
for (IFeatureEntry feature : features) {
if (feature instanceof FeatureEntry && ((FeatureEntry) feature).hasBranding())
bundleGroups.add((IBundleGroup) feature);
}
return bundleGroups.toArray(new IBundleGroup[bundleGroups.size()]);
}

public static ConfigurationActivator getConfigurator() {
return configurator;
}

private void acquireFrameworkLogService() {
ServiceReference<FrameworkLog> logServiceReference = context.getServiceReference(FrameworkLog.class);
if (logServiceReference == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public URL[] getPluginPath() {
public Set<String> getPluginPaths() {

HashSet<String> paths = new HashSet<>();

for (ISiteEntry site : getConfiguredSites()) {
for (String plugin : site.getPlugins()) {
paths.add(plugin);
Expand All @@ -429,12 +429,12 @@ public PluginEntry[] getPlugins() {
Utils.debug("computed plug-ins:"); //$NON-NLS-1$

ISiteEntry[] sites = getConfiguredSites();
for (int i = 0; i < sites.length; i++) {
if (!(sites[i] instanceof SiteEntry)) {
Utils.debug("Site " + sites[i].getURL() + " is not a SiteEntry"); //$NON-NLS-1$ //$NON-NLS-2$
for (ISiteEntry site : sites) {

Check warning on line 432 in update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/PlatformConfiguration.java

View check run for this annotation

Jenkins - Eclipse Platform / Compiler and API Tools

Deprecation

NORMAL: The type IPlatformConfiguration.ISiteEntry is deprecated
if (!(site instanceof SiteEntry)) {
Utils.debug("Site " + site.getURL() + " is not a SiteEntry"); //$NON-NLS-1$ //$NON-NLS-2$

Check warning on line 434 in update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/PlatformConfiguration.java

View check run for this annotation

Jenkins - Eclipse Platform / Compiler and API Tools

Deprecation

NORMAL: The method getURL() from the type IPlatformConfiguration.ISiteEntry is deprecated
continue;
}
for (PluginEntry plugin : ((SiteEntry) sites[i]).getPluginEntries()) {
for (PluginEntry plugin : ((SiteEntry) site).getPluginEntries()) {
allPlugins.add(plugin);
Utils.debug(" " + plugin.getURL()); //$NON-NLS-1$
}
Expand Down Expand Up @@ -494,7 +494,7 @@ public synchronized void save(URL url) throws IOException {
// not a file protocol - attempt to save to the URL
URLConnection uc = url.openConnection();
uc.setDoOutput(true);

try(OutputStream os = uc.getOutputStream()) {
saveAsXML(os);
config.setDirty(false);
Expand Down Expand Up @@ -595,9 +595,7 @@ public static PlatformConfiguration getCurrent() {
/**
* Starts a platform installed at specified installURL using configuration located at platformConfigLocation.
*/
public static synchronized void startup(URL installURL, Location platformConfigLocation) throws Exception {
PlatformConfiguration.installURL = installURL;

public static synchronized void startup(Location platformConfigLocation) throws Exception {
// create current configuration
if (currentPlatformConfiguration == null) {
currentPlatformConfiguration = new PlatformConfiguration(platformConfigLocation);
Expand Down Expand Up @@ -645,7 +643,7 @@ private synchronized void initializeCurrent(Location platformConfigLocation) thr

// try loading the configuration
try {
config = loadConfig(configFileURL, installURL);
config = loadConfig(configFileURL, getInstallURL());
Utils.debug("Using configuration " + configFileURL.toString()); //$NON-NLS-1$
} catch (Exception e) {
// failed to load, see if we can find pre-initialized configuration.
Expand All @@ -655,7 +653,7 @@ private synchronized void initializeCurrent(Location platformConfigLocation) thr
throw new IOException(); // no platform.xml found, need to create default site

URL sharedConfigFileURL = new URL(parentLocation.getURL(), CONFIG_NAME);
config = loadConfig(sharedConfigFileURL, installURL);
config = loadConfig(sharedConfigFileURL, getInstallURL());

// pre-initialized config loaded OK ... copy any remaining update metadata
// Only copy if the default config location is not the install location
Expand All @@ -667,7 +665,7 @@ private synchronized void initializeCurrent(Location platformConfigLocation) thr
return;
} catch (Exception ioe) {
Utils.debug("Creating default configuration from " + configFileURL.toExternalForm()); //$NON-NLS-1$
createDefaultConfiguration(configFileURL, installURL);
createDefaultConfiguration(configFileURL, getInstallURL());
}
} finally {
// if config == null an unhandled exception has been thrown and we allow it to propagate
Expand Down Expand Up @@ -951,6 +949,9 @@ private URL getBasePathLocation(URL url, URL installLocation, URL configLocation
}

public static URL getInstallURL() {
if (installURL == null) {
installURL = Utils.getInstallURL();
}
return installURL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,36 @@
*******************************************************************************/
package org.eclipse.update.internal.configurator;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.update.configurator.IPlatformConfiguration;
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

@SuppressWarnings("deprecation")
@Component(service = IPlatformConfigurationFactory.class)
public class PlatformConfigurationFactory implements IPlatformConfigurationFactory {
private Location configLocation;

@Override
public IPlatformConfiguration getCurrentPlatformConfiguration() {
try {
PlatformConfiguration.startup(configLocation);
} catch (Exception e) {
String message = e.getMessage();
if (message == null)
message = ""; //$NON-NLS-1$
Utils.log(Utils.newStatus(message, e));
}
return PlatformConfiguration.getCurrent();
}

@Override
public IPlatformConfiguration getPlatformConfiguration(URL url) throws IOException {
try {
Expand All @@ -35,7 +53,7 @@ public IPlatformConfiguration getPlatformConfiguration(URL url) throws IOExcepti
throw new IOException(e.getMessage());
}
}

@Override
public IPlatformConfiguration getPlatformConfiguration(URL url, URL loc) throws IOException {
try {
Expand All @@ -46,5 +64,29 @@ public IPlatformConfiguration getPlatformConfiguration(URL url, URL loc) throws
throw new IOException(e.getMessage());
}
}


@Activate
public void startup() {
configLocation = Utils.getConfigurationLocation();
// create the name space directory for update (configuration/org.eclipse.update)
if (!configLocation.isReadOnly()) {
try {
URL privateURL = new URL(configLocation.getURL(), ConfigurationActivator.NAME_SPACE);
File f = new File(privateURL.getFile());
if (!f.exists())
f.mkdirs();
} catch (MalformedURLException e1) {
// ignore
}
}
}

@Deactivate
public void shutdown() {
try {
PlatformConfiguration.shutdown();
} catch (IOException e) {
}
}

}

0 comments on commit 8b8afca

Please sign in to comment.