Skip to content

Commit 8b8afca

Browse files
committed
Make the PlatformConfigurationFactory a component
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
1 parent 5d8ee71 commit 8b8afca

File tree

5 files changed

+92
-133
lines changed

5 files changed

+92
-133
lines changed

update/org.eclipse.update.configurator/META-INF/MANIFEST.MF

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.update.configurator; singleton:=true
5-
Bundle-Version: 3.5.500.qualifier
5+
Bundle-Version: 3.5.600.qualifier
66
Bundle-Activator: org.eclipse.update.internal.configurator.ConfigurationActivator
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin
@@ -17,6 +17,7 @@ Import-Package: javax.xml.parsers,
1717
org.w3c.dom,
1818
org.xml.sax,
1919
org.xml.sax.helpers
20-
Service-Component: OSGI-INF/org.eclipse.update.internal.configurator.BundleGroupComponent.xml
2120
Bundle-ActivationPolicy: lazy
2221
Automatic-Module-Name: org.eclipse.update.configurator
22+
Service-Component: OSGI-INF/org.eclipse.update.internal.configurator.BundleGroupComponent.xml,
23+
OSGI-INF/org.eclipse.update.internal.configurator.PlatformConfigurationFactory.xml

update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/BundleGroupComponent.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,58 @@
77
* https://www.eclipse.org/legal/epl-2.0/
88
*
99
* SPDX-License-Identifier: EPL-2.0
10-
*
10+
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
1313
*******************************************************************************/
1414
package org.eclipse.update.internal.configurator;
1515

16+
import java.util.ArrayList;
17+
1618
import org.eclipse.core.runtime.IBundleGroup;
1719
import org.eclipse.core.runtime.IBundleGroupProvider;
20+
import org.eclipse.update.configurator.IPlatformConfiguration;
21+
import org.eclipse.update.configurator.IPlatformConfiguration.IFeatureEntry;
22+
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
23+
import org.osgi.service.component.annotations.Activate;
1824
import org.osgi.service.component.annotations.Component;
25+
import org.osgi.service.component.annotations.Reference;
1926

2027
/**
2128
* Declarative services component that provides an implementation of
2229
* {@link IBundleGroupProvider}. This allows the bundle group provider to be
2330
* made available in the service registry before this bundle has started.
2431
*/
25-
@Component
32+
@Component(service = IBundleGroupProvider.class)
33+
@SuppressWarnings("deprecation")
2634
public class BundleGroupComponent implements IBundleGroupProvider {
2735

36+
37+
private IPlatformConfigurationFactory factory;
38+
39+
@Activate
40+
public BundleGroupComponent(@Reference IPlatformConfigurationFactory factory) {
41+
this.factory = factory;
42+
}
43+
2844
@Override
2945
public IBundleGroup[] getBundleGroups() {
30-
ConfigurationActivator activator = ConfigurationActivator.getConfigurator();
31-
if (activator.bundleGroupProviderSR != null)
32-
// we manually registered the group in the activator; return no groups
33-
// the manually registered service will handle the groups we know about
46+
IPlatformConfiguration configuration = factory.getCurrentPlatformConfiguration();
47+
if (configuration == null) {
3448
return new IBundleGroup[0];
35-
return activator.getBundleGroups();
49+
}
50+
IPlatformConfiguration.IFeatureEntry[] features = configuration.getConfiguredFeatureEntries();
51+
ArrayList<IBundleGroup> bundleGroups = new ArrayList<>(features.length);
52+
for (IFeatureEntry feature : features) {
53+
if (feature instanceof FeatureEntry && ((FeatureEntry) feature).hasBranding())
54+
bundleGroups.add((IBundleGroup) feature);
55+
}
56+
return bundleGroups.toArray(new IBundleGroup[bundleGroups.size()]);
3657
}
3758

3859
@Override
3960
public String getName() {
40-
return ConfigurationActivator.getConfigurator().getName();
61+
return Messages.BundleGroupProvider;
4162
}
4263

4364
}

update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/ConfigurationActivator.java

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,13 @@
1313
*******************************************************************************/
1414
package org.eclipse.update.internal.configurator;
1515

16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.net.MalformedURLException;
19-
import java.net.URL;
20-
import java.util.ArrayList;
21-
22-
import org.eclipse.core.runtime.IBundleGroup;
23-
import org.eclipse.core.runtime.IBundleGroupProvider;
2416
import org.eclipse.osgi.framework.log.FrameworkLog;
25-
import org.eclipse.osgi.service.datalocation.Location;
2617
import org.eclipse.osgi.service.debug.DebugOptions;
27-
import org.eclipse.osgi.util.NLS;
28-
import org.eclipse.update.configurator.IPlatformConfiguration;
29-
import org.eclipse.update.configurator.IPlatformConfiguration.IFeatureEntry;
30-
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
3118
import org.osgi.framework.BundleActivator;
3219
import org.osgi.framework.BundleContext;
3320
import org.osgi.framework.ServiceReference;
34-
import org.osgi.framework.ServiceRegistration;
3521

36-
public class ConfigurationActivator implements BundleActivator, IBundleGroupProvider, IConfigurationConstants {
22+
public class ConfigurationActivator implements BundleActivator, IConfigurationConstants {
3723

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

4834
private static BundleContext context;
49-
private ServiceRegistration<IPlatformConfigurationFactory> configurationFactorySR;
50-
ServiceRegistration<?> bundleGroupProviderSR;
51-
private PlatformConfiguration configuration;
52-
53-
// Location of the configuration data
54-
private Location configLocation;
55-
56-
// Singleton
57-
private static ConfigurationActivator configurator;
58-
59-
public ConfigurationActivator() {
60-
configurator = this;
61-
}
6235

6336
@Override
6437
public void start(BundleContext ctx) throws Exception {
6538
context = ctx;
6639
loadOptions();
6740
acquireFrameworkLogService();
68-
try {
69-
initialize();
70-
} catch (Exception e) {
71-
//we failed to start, so make sure Utils closes its service trackers
72-
Utils.shutdown();
73-
throw e;
74-
}
75-
7641
Utils.debug("Starting update configurator..."); //$NON-NLS-1$
7742
}
78-
79-
private void initialize() throws Exception {
80-
81-
configLocation = Utils.getConfigurationLocation();
82-
// create the name space directory for update (configuration/org.eclipse.update)
83-
if (!configLocation.isReadOnly()) {
84-
try {
85-
URL privateURL = new URL(configLocation.getURL(), NAME_SPACE);
86-
File f = new File(privateURL.getFile());
87-
if (!f.exists())
88-
f.mkdirs();
89-
} catch (MalformedURLException e1) {
90-
// ignore
91-
}
92-
}
93-
configurationFactorySR = context.registerService(IPlatformConfigurationFactory.class, new PlatformConfigurationFactory(), null);
94-
configuration = getPlatformConfiguration(Utils.getInstallURL(), configLocation);
95-
if (configuration == null)
96-
throw Utils.newCoreException(NLS.bind(Messages.ConfigurationActivator_createConfig, (new String[] {configLocation.getURL().toExternalForm()})), null);
97-
98-
}
9943

10044
@Override
10145
public void stop(BundleContext ctx) throws Exception {
102-
// quick fix (hack) for bug 47861
103-
try {
104-
PlatformConfiguration.shutdown();
105-
} catch (IOException e) {
106-
// TODO Auto-generated catch block
107-
e.printStackTrace();
108-
}
109-
configurationFactorySR.unregister();
110-
if (bundleGroupProviderSR != null)
111-
bundleGroupProviderSR.unregister();
11246
Utils.shutdown();
11347
}
11448

115-
/**
116-
* Creates and starts the platform configuration.
117-
* @return the just started platform configuration
118-
*/
119-
private PlatformConfiguration getPlatformConfiguration(URL installURL, Location configLocation) {
120-
try {
121-
PlatformConfiguration.startup(installURL, configLocation);
122-
} catch (Exception e) {
123-
String message = e.getMessage();
124-
if (message == null)
125-
message = ""; //$NON-NLS-1$
126-
Utils.log(Utils.newStatus(message, e));
127-
}
128-
return PlatformConfiguration.getCurrent();
129-
130-
}
131-
13249
private void loadOptions() {
133-
// all this is only to get the application args
50+
// all this is only to get the application args
13451
DebugOptions service = null;
13552
ServiceReference<DebugOptions> reference = context.getServiceReference(DebugOptions.class);
13653
if (reference != null)
@@ -149,29 +66,6 @@ public static BundleContext getBundleContext() {
14966
return context;
15067
}
15168

152-
@Override
153-
public String getName() {
154-
return Messages.BundleGroupProvider;
155-
}
156-
157-
@Override
158-
public IBundleGroup[] getBundleGroups() {
159-
if (configuration == null)
160-
return new IBundleGroup[0];
161-
162-
IPlatformConfiguration.IFeatureEntry[] features = configuration.getConfiguredFeatureEntries();
163-
ArrayList<IBundleGroup> bundleGroups = new ArrayList<>(features.length);
164-
for (IFeatureEntry feature : features) {
165-
if (feature instanceof FeatureEntry && ((FeatureEntry) feature).hasBranding())
166-
bundleGroups.add((IBundleGroup) feature);
167-
}
168-
return bundleGroups.toArray(new IBundleGroup[bundleGroups.size()]);
169-
}
170-
171-
public static ConfigurationActivator getConfigurator() {
172-
return configurator;
173-
}
174-
17569
private void acquireFrameworkLogService() {
17670
ServiceReference<FrameworkLog> logServiceReference = context.getServiceReference(FrameworkLog.class);
17771
if (logServiceReference == null)

update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/PlatformConfiguration.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public URL[] getPluginPath() {
411411
public Set<String> getPluginPaths() {
412412

413413
HashSet<String> paths = new HashSet<>();
414-
414+
415415
for (ISiteEntry site : getConfiguredSites()) {
416416
for (String plugin : site.getPlugins()) {
417417
paths.add(plugin);
@@ -429,12 +429,12 @@ public PluginEntry[] getPlugins() {
429429
Utils.debug("computed plug-ins:"); //$NON-NLS-1$
430430

431431
ISiteEntry[] sites = getConfiguredSites();
432-
for (int i = 0; i < sites.length; i++) {
433-
if (!(sites[i] instanceof SiteEntry)) {
434-
Utils.debug("Site " + sites[i].getURL() + " is not a SiteEntry"); //$NON-NLS-1$ //$NON-NLS-2$
432+
for (ISiteEntry site : sites) {
433+
if (!(site instanceof SiteEntry)) {
434+
Utils.debug("Site " + site.getURL() + " is not a SiteEntry"); //$NON-NLS-1$ //$NON-NLS-2$
435435
continue;
436436
}
437-
for (PluginEntry plugin : ((SiteEntry) sites[i]).getPluginEntries()) {
437+
for (PluginEntry plugin : ((SiteEntry) site).getPluginEntries()) {
438438
allPlugins.add(plugin);
439439
Utils.debug(" " + plugin.getURL()); //$NON-NLS-1$
440440
}
@@ -494,7 +494,7 @@ public synchronized void save(URL url) throws IOException {
494494
// not a file protocol - attempt to save to the URL
495495
URLConnection uc = url.openConnection();
496496
uc.setDoOutput(true);
497-
497+
498498
try(OutputStream os = uc.getOutputStream()) {
499499
saveAsXML(os);
500500
config.setDirty(false);
@@ -595,9 +595,7 @@ public static PlatformConfiguration getCurrent() {
595595
/**
596596
* Starts a platform installed at specified installURL using configuration located at platformConfigLocation.
597597
*/
598-
public static synchronized void startup(URL installURL, Location platformConfigLocation) throws Exception {
599-
PlatformConfiguration.installURL = installURL;
600-
598+
public static synchronized void startup(Location platformConfigLocation) throws Exception {
601599
// create current configuration
602600
if (currentPlatformConfiguration == null) {
603601
currentPlatformConfiguration = new PlatformConfiguration(platformConfigLocation);
@@ -645,7 +643,7 @@ private synchronized void initializeCurrent(Location platformConfigLocation) thr
645643

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

657655
URL sharedConfigFileURL = new URL(parentLocation.getURL(), CONFIG_NAME);
658-
config = loadConfig(sharedConfigFileURL, installURL);
656+
config = loadConfig(sharedConfigFileURL, getInstallURL());
659657

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

953951
public static URL getInstallURL() {
952+
if (installURL == null) {
953+
installURL = Utils.getInstallURL();
954+
}
954955
return installURL;
955956
}
956957

update/org.eclipse.update.configurator/src/org/eclipse/update/internal/configurator/PlatformConfigurationFactory.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,36 @@
1313
*******************************************************************************/
1414
package org.eclipse.update.internal.configurator;
1515

16+
import java.io.File;
1617
import java.io.IOException;
18+
import java.net.MalformedURLException;
1719
import java.net.URL;
1820

21+
import org.eclipse.osgi.service.datalocation.Location;
1922
import org.eclipse.update.configurator.IPlatformConfiguration;
2023
import org.eclipse.update.configurator.IPlatformConfigurationFactory;
24+
import org.osgi.service.component.annotations.Activate;
25+
import org.osgi.service.component.annotations.Component;
26+
import org.osgi.service.component.annotations.Deactivate;
2127

28+
@SuppressWarnings("deprecation")
29+
@Component(service = IPlatformConfigurationFactory.class)
2230
public class PlatformConfigurationFactory implements IPlatformConfigurationFactory {
31+
private Location configLocation;
32+
2333
@Override
2434
public IPlatformConfiguration getCurrentPlatformConfiguration() {
35+
try {
36+
PlatformConfiguration.startup(configLocation);
37+
} catch (Exception e) {
38+
String message = e.getMessage();
39+
if (message == null)
40+
message = ""; //$NON-NLS-1$
41+
Utils.log(Utils.newStatus(message, e));
42+
}
2543
return PlatformConfiguration.getCurrent();
2644
}
27-
45+
2846
@Override
2947
public IPlatformConfiguration getPlatformConfiguration(URL url) throws IOException {
3048
try {
@@ -35,7 +53,7 @@ public IPlatformConfiguration getPlatformConfiguration(URL url) throws IOExcepti
3553
throw new IOException(e.getMessage());
3654
}
3755
}
38-
56+
3957
@Override
4058
public IPlatformConfiguration getPlatformConfiguration(URL url, URL loc) throws IOException {
4159
try {
@@ -46,5 +64,29 @@ public IPlatformConfiguration getPlatformConfiguration(URL url, URL loc) throws
4664
throw new IOException(e.getMessage());
4765
}
4866
}
49-
67+
68+
@Activate
69+
public void startup() {
70+
configLocation = Utils.getConfigurationLocation();
71+
// create the name space directory for update (configuration/org.eclipse.update)
72+
if (!configLocation.isReadOnly()) {
73+
try {
74+
URL privateURL = new URL(configLocation.getURL(), ConfigurationActivator.NAME_SPACE);
75+
File f = new File(privateURL.getFile());
76+
if (!f.exists())
77+
f.mkdirs();
78+
} catch (MalformedURLException e1) {
79+
// ignore
80+
}
81+
}
82+
}
83+
84+
@Deactivate
85+
public void shutdown() {
86+
try {
87+
PlatformConfiguration.shutdown();
88+
} catch (IOException e) {
89+
}
90+
}
91+
5092
}

0 commit comments

Comments
 (0)