Skip to content

Commit

Permalink
Migrate p2 engine to agent properties
Browse files Browse the repository at this point in the history
Now that agent properties are possible we should use them in the engine,
to not rely on only static OSGi context properties.
  • Loading branch information
laeubi committed Feb 10, 2024
1 parent fa81fb9 commit 4fc3821
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 71 deletions.
47 changes: 47 additions & 0 deletions bundles/org.eclipse.equinox.p2.engine/.settings/.api_filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.equinox.p2.engine" version="2">
<resource path="src/org/eclipse/equinox/p2/engine/PhaseSetFactory.java" type="org.eclipse.equinox.p2.engine.PhaseSetFactory">
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_CHECK_TRUST"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_COLLECT"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_CONFIGURE"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_INSTALL"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_PROPERTY"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_UNCONFIGURE"/>
</message_arguments>
</filter>
<filter id="388100214">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.engine.PhaseSetFactory"/>
<message_argument value="PHASE_UNINSTALL"/>
</message_arguments>
</filter>
</resource>
</component>
2 changes: 1 addition & 1 deletion bundles/org.eclipse.equinox.p2.engine/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.equinox.p2.engine;singleton:=true
Bundle-Version: 2.9.100.qualifier
Bundle-Version: 2.10.0.qualifier
Bundle-Activator: org.eclipse.equinox.internal.p2.engine.EngineActivator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static String formatInstallableUnitOperand(InstallableUnitOperand iuOpera

public static String formatPhaseSet(PhaseSet phaseSet) {
StringBuilder buffer = new StringBuilder(phaseSet.getClass().getName());
buffer.append(DebugHelper.formatArray(Arrays.asList(phaseSet.getPhases()), false, false));
buffer.append(DebugHelper.formatArray(Arrays.asList(phaseSet.getPhaseIds()), false, false));
return buffer.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public IStatus perform(IProfile iprofile, IPhaseSet phases, Operand[] operands,
String property = context.getProperty(ProvisioningContext.CHECK_AUTHORITIES);
if (property == null) {
// Allow a system property to force the property.
property = EngineActivator.getContext().getProperty(ProvisioningContext.CHECK_AUTHORITIES);
property = EngineActivator.getProperty(ProvisioningContext.CHECK_AUTHORITIES, agent);
if (property == null) {
// Otherwise, if we are checking trust, also check the authorities.
if (Arrays.asList(phases.getPhaseIds()).contains(PhaseSetFactory.PHASE_CHECK_TRUST)) {
Expand Down Expand Up @@ -136,7 +136,7 @@ protected IStatus validate(IProfile iprofile, PhaseSet phaseSet, Operand[] opera
monitor = new NullProgressMonitor();

ActionManager actionManager = agent.getService(ActionManager.class);
return phaseSet.validate(actionManager, iprofile, operands, context, monitor);
return phaseSet.validate(actionManager, iprofile, operands, context, agent, monitor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
* Red Hat, Inc - fragments support added
Expand All @@ -22,6 +22,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -175,4 +176,15 @@ private static ArrayList<File> getInfoFilesFromLocation(String locationToCheck)
}
return result;
}

public static String getProperty(String key, IProvisioningAgent agent) {
if (agent != null) {
return agent.getProperty(key);
}
BundleContext bc = EngineActivator.getContext();
if (bc != null) {
return bc.getProperty(key);
}
return System.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,51 @@

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.engine.phases.*;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.osgi.util.NLS;

public class PhaseSet implements IPhaseSet {

private final Phase[] phases;
private static final Set<String> SUPPORTED_PHASES = Set.of(PhaseSetFactory.PHASE_COLLECT,
PhaseSetFactory.PHASE_UNCONFIGURE, PhaseSetFactory.PHASE_UNINSTALL, PhaseSetFactory.PHASE_PROPERTY,
PhaseSetFactory.PHASE_CHECK_TRUST, PhaseSetFactory.PHASE_INSTALL, PhaseSetFactory.PHASE_CONFIGURE);

private Phase[] phases;
private boolean isRunning = false;
private boolean isPaused = false;
private String[] phaseIds;

public PhaseSet(Phase[] phases) {
if (phases == null)
if (phases == null) {
throw new IllegalArgumentException(Messages.null_phases);

}
this.phases = phases;
}

public PhaseSet(String[] phases) {
if (phases == null) {
throw new IllegalArgumentException(Messages.null_phases);
}
this.phaseIds = Arrays.stream(phases).filter(SUPPORTED_PHASES::contains).toArray(String[]::new);
}

public final MultiStatus perform(EngineSession session, Operand[] operands, IProgressMonitor monitor) {
MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
int[] weights = getProgressWeights(operands);
Phase[] array = getPhases(session.getAgent());
int[] weights = getProgressWeights(operands, array);
int totalWork = getTotalWork(weights);
SubMonitor pm = SubMonitor.convert(monitor, totalWork);
try {
isRunning = true;
for (int i = 0; i < phases.length; i++) {
for (int i = 0; i < array.length; i++) {
if (pm.isCanceled()) {
status.add(Status.CANCEL_STATUS);
return status;
}
Phase phase = phases[i];
Phase phase = array[i];
phase.actionManager = session.getAgent().getService(ActionManager.class);
try {
phase.perform(status, session, operands, pm.newChild(weights[i]));
Expand Down Expand Up @@ -80,9 +95,9 @@ public final MultiStatus perform(EngineSession session, Operand[] operands, IPro
}

public synchronized boolean pause() {
if (isRunning && !isPaused) {
if (isRunning && !isPaused && this.phases != null) {
isPaused = true;
for (Phase phase : phases) {
for (Phase phase : this.phases) {
phase.setPaused(isPaused);
}
return true;
Expand All @@ -91,19 +106,20 @@ public synchronized boolean pause() {
}

public synchronized boolean resume() {
if (isRunning && isPaused) {
if (isRunning && isPaused && this.phases != null) {
isPaused = false;
for (Phase phase : phases) {
for (Phase phase : this.phases) {
phase.setPaused(isPaused);
}
return true;
}
return false;
}

public final IStatus validate(ActionManager actionManager, IProfile profile, Operand[] operands, ProvisioningContext context, IProgressMonitor monitor) {
public final IStatus validate(ActionManager actionManager, IProfile profile, Operand[] operands,
ProvisioningContext context, IProvisioningAgent agent, IProgressMonitor monitor) {
Set<MissingAction> missingActions = new HashSet<>();
for (Phase phase2 : phases) {
for (Phase phase2 : getPhases(agent)) {
Phase phase = phase2;
phase.actionManager = actionManager;
try {
Expand Down Expand Up @@ -151,14 +167,14 @@ private int getTotalWork(int[] weights) {
return sum;
}

private int[] getProgressWeights(Operand[] operands) {
int[] weights = new int[phases.length];
for (int i = 0; i < phases.length; i += 1) {
private int[] getProgressWeights(Operand[] operands, Phase[] array) {
int[] weights = new int[array.length];
for (int i = 0; i < array.length; i += 1) {
if (operands.length > 0)
//alter weights according to the number of operands applicable to that phase
weights[i] = (phases[i].weight * countApplicable(phases[i], operands) / operands.length);
weights[i] = (array[i].weight * countApplicable(array[i], operands) / operands.length);
else
weights[i] = phases[i].weight;
weights[i] = array[i].weight;
}
return weights;
}
Expand All @@ -174,14 +190,37 @@ private int countApplicable(Phase phase, Operand[] operands) {

@Override
public String[] getPhaseIds() {
String[] ids = new String[phases.length];
for (int i = 0; i < ids.length; i++) {
ids[i] = phases[i].phaseId;
if (phaseIds != null) {
return phaseIds.clone();
}
return ids;
if (phases != null) {
return Arrays.stream(phases).map(p -> p.phaseId).toArray(String[]::new);
}
return new String[0];
}

public Phase[] getPhases() {
private Phase[] getPhases(IProvisioningAgent agent) {
if (phases == null) {
boolean forcedUninstall = Boolean
.parseBoolean(EngineActivator.getProperty("org.eclipse.equinox.p2.engine.forcedUninstall", agent)); //$NON-NLS-1$
List<String> includeList = Arrays.asList(phaseIds);
List<Phase> list = new ArrayList<>();
if (includeList.contains(PhaseSetFactory.PHASE_COLLECT))
list.add(new Collect(100));
if (includeList.contains(PhaseSetFactory.PHASE_CHECK_TRUST))
list.add(new CheckTrust(10));
if (includeList.contains(PhaseSetFactory.PHASE_UNCONFIGURE))
list.add(new Unconfigure(10, forcedUninstall));
if (includeList.contains(PhaseSetFactory.PHASE_UNINSTALL))
list.add(new Uninstall(50, forcedUninstall));
if (includeList.contains(PhaseSetFactory.PHASE_PROPERTY))
list.add(new Property(1));
if (includeList.contains(PhaseSetFactory.PHASE_INSTALL))
list.add(new Install(50));
if (includeList.contains(PhaseSetFactory.PHASE_CONFIGURE))
list.add(new Configure(10));
this.phases = list.toArray(Phase[]::new);
}
return phases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ public void setEventBus(IProvisioningEventBus bus) {
*/
private boolean shouldGzipFile(Profile profile) {
//check system property controlling compression
String format = EngineActivator.getContext().getProperty(EngineActivator.PROP_PROFILE_FORMAT);
String format = EngineActivator.getProperty(EngineActivator.PROP_PROFILE_FORMAT, agent);
if (format != null && format.equals(EngineActivator.PROFILE_FORMAT_UNCOMPRESSED))
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public class SurrogateProfileHandler implements ISurrogateProfileHandler {

private SoftReference<IProfile> cachedProfile;

private static void addSharedProfileBaseIUs(final IProfile sharedProfile, final Profile userProfile) {
private static void addSharedProfileBaseIUs(final IProfile sharedProfile, final Profile userProfile,
IProvisioningAgent agent) {
IQuery<IInstallableUnit> rootIUQuery = QueryUtil.createMatchQuery( //
"profileProperties[$0] == 'true' || (touchpointType != null && touchpointType.id == $1)", //$NON-NLS-1$
IProfile.PROP_PROFILE_ROOT_IU, NATIVE_TOUCHPOINT_TYPE);
IQueryResult<IInstallableUnit> rootIUs = sharedProfile.query(rootIUQuery, null);
for (IInstallableUnit iu : rootIUs) {
userProfile.addInstallableUnit(iu);
userProfile.addInstallableUnitProperties(iu, sharedProfile.getInstallableUnitProperties(iu));
String profileLockedIUSystemProperty = EngineActivator.getContext()
.getProperty(IProfile.PROP_PROFILE_LOCKED_IU);
String profileLockedIUSystemProperty = EngineActivator.getProperty(IProfile.PROP_PROFILE_LOCKED_IU, agent);
if (profileLockedIUSystemProperty == null) {
userProfile.setInstallableUnitProperty(iu, IProfile.PROP_PROFILE_LOCKED_IU, IU_LOCKED);
} else {
Expand Down Expand Up @@ -150,7 +150,7 @@ public SurrogateProfileHandler(IProvisioningAgent agent) {

private synchronized SimpleProfileRegistry getProfileRegistry() {
if (profileRegistry == null) {
String installArea = EngineActivator.getContext().getProperty(OSGI_INSTALL_AREA);
String installArea = EngineActivator.getProperty(OSGI_INSTALL_AREA, agent);
try {
URL registryURL = new URL(installArea + P2_ENGINE_DIR + SimpleProfileRegistry.DEFAULT_STORAGE_DIR);
File sharedRegistryDirectory = URIUtil.toFile(URIUtil.toURI(registryURL));
Expand Down Expand Up @@ -233,7 +233,7 @@ public IProfile createProfile(String id) {
userProfile.setProperty(PROP_SURROGATE, Boolean.TRUE.toString());
userProfile.setSurrogateProfileHandler(this);
updateProperties(sharedProfile, userProfile);
addSharedProfileBaseIUs(sharedProfile, userProfile);
addSharedProfileBaseIUs(sharedProfile, userProfile, agent);

return userProfile;
}
Expand Down Expand Up @@ -279,7 +279,7 @@ public IProfile createProfile(String id) {
userProfile.setProperty(PROP_SURROGATE, Boolean.TRUE.toString());
userProfile.setSurrogateProfileHandler(this);
updateProperties(sharedProfile, userProfile);
addSharedProfileBaseIUs(sharedProfile, userProfile);
addSharedProfileBaseIUs(sharedProfile, userProfile, agent);

return userProfile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public Set<URI> getPreferenceTrustedAuthorities() {
var result = new LinkedHashSet<URI>();
var preferences = getEnngineProfilePreferences();
if (preferences != null) {
String defaultValue = EngineActivator.getContext().getProperty("p2.trustedAuthorities");
String defaultValue = EngineActivator.getProperty("p2.trustedAuthorities", agent);
if (defaultValue == null) {
defaultValue = "https://download.eclipse.org https://archive.eclipse.org";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public Set<? extends Certificate> getPreferenceTrustedCertificates() {
* Return the policy on unsigned content.
*/
private String getUnsignedContentPolicy() {
String policy = EngineActivator.getContext().getProperty(EngineActivator.PROP_UNSIGNED_POLICY);
String policy = EngineActivator.getProperty(EngineActivator.PROP_UNSIGNED_POLICY, agent);
if (policy == null)
policy = EngineActivator.UNSIGNED_PROMPT;
return policy;
Expand Down
Loading

0 comments on commit 4fc3821

Please sign in to comment.