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.
  • Loading branch information
laeubi committed Dec 15, 2024
1 parent 5d8ee71 commit 9cf50f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 58 deletions.
3 changes: 2 additions & 1 deletion update/org.eclipse.update.configurator/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -17,23 +17,16 @@
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,20 +39,11 @@ 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;
Expand All @@ -75,7 +59,7 @@ public void start(BundleContext ctx) throws Exception {

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

private void initialize() throws Exception {

configLocation = Utils.getConfigurationLocation();
Expand All @@ -90,7 +74,6 @@ private void initialize() throws Exception {
// 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);
Expand All @@ -106,9 +89,6 @@ public void stop(BundleContext ctx) throws Exception {
// TODO Auto-generated catch block
e.printStackTrace();
}
configurationFactorySR.unregister();
if (bundleGroupProviderSR != null)
bundleGroupProviderSR.unregister();
Utils.shutdown();
}

Expand All @@ -130,7 +110,7 @@ private PlatformConfiguration getPlatformConfiguration(URL installURL, Location
}

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 +129,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 @@ -18,13 +18,16 @@

import org.eclipse.update.configurator.IPlatformConfiguration;
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
import org.osgi.service.component.annotations.Component;

@SuppressWarnings("deprecation")
@Component(service = IPlatformConfigurationFactory.class)
public class PlatformConfigurationFactory implements IPlatformConfigurationFactory {
@Override
public IPlatformConfiguration getCurrentPlatformConfiguration() {
return PlatformConfiguration.getCurrent();
}

@Override
public IPlatformConfiguration getPlatformConfiguration(URL url) throws IOException {
try {
Expand All @@ -35,7 +38,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 +49,5 @@ public IPlatformConfiguration getPlatformConfiguration(URL url, URL loc) throws
throw new IOException(e.getMessage());
}
}

}

0 comments on commit 9cf50f4

Please sign in to comment.