Skip to content

Commit

Permalink
Use RepositoryTracker for KnownRepositories to be consistent with Update
Browse files Browse the repository at this point in the history
Currently if the user choose 'Check for Updates' the handler uses the
ProvisioningUI (that uses RepositoryTracker internally) to choose the
initial items to offer. The UpdateChecker (aka automatically search for
updates) on the other hand uses a (potentially) different set of items.

This now adds a new method that allow to pass the repository flags to be
used when search for an update and delegate it on the callers side to
use the Provisioning UI.
  • Loading branch information
laeubi committed May 14, 2024
1 parent 0f3af2c commit b46100f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,27 @@ default String getProperty(String key, String defaultValue) {
return property;
}

/**
* Returns an agent bound property as a boolean, the default implementation
* delegates to {@link IProvisioningAgent#getBooleanProperty(String, boolean)}
* with <code>false</code> as the default.
*
* @since 2.11
*/
default boolean getBooleanProperty(String key) {
return getBooleanProperty(key, false);
}

/**
* Returns an agent bound property as a boolean, the default implementation
* delegates to {@link IProvisioningAgent#getProperty(String)} with the given
* default as a String and parses the result as by
* {@link Boolean#parseBoolean(String)}.
*
* @since 2.11
*/
default boolean getBooleanProperty(String key, boolean defaultValue) {
return Boolean.parseBoolean(getProperty(key, Boolean.toString(defaultValue)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public void publishRepositoryReferences() {
return;
}
List<IRepositoryReference> repositoriesSnapshot = createRepositoriesSnapshot();
boolean referenceAsSystem = Boolean
.parseBoolean(agent.getProperty("p2.metadata.repository.reference.system", "true")); //$NON-NLS-1$ //$NON-NLS-2$
boolean referenceAsSystem = agent.getBooleanProperty("p2.metadata.repository.reference.system", true); //$NON-NLS-1$
for (IRepositoryReference reference : repositoriesSnapshot) {
RepositoryEvent event = RepositoryEvent.newDiscoveryEvent(reference.getLocation(), reference.getNickname(),
reference.getType(), reference.isEnabled(), referenceAsSystem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.equinox.p2.engine.query.IUProfilePropertyQuery;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.repository.IRepositoryManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.PlatformUI;
Expand Down Expand Up @@ -175,7 +176,13 @@ public void checkingForUpdates() {
StatusManager.getManager().handle(status, StatusManager.LOG);
return;
}
checker.addUpdateCheck(IProfileRegistry.SELF, getProfileQuery(), delay, poll, listener);
checker.addUpdateCheck(IProfileRegistry.SELF, getProfileQuery(), delay, poll, () -> {
if (pagent.getBooleanProperty("p2.ui.sdk.scheduler.update.useProvisioningUI", true)) { //$NON-NLS-1$
return AutomaticUpdatePlugin.getDefault().getAutomaticUpdater().getProvisioningUI()
.getRepositoryTracker().getMetadataRepositoryFlags();
}
return IRepositoryManager.REPOSITORIES_ALL;
}, listener);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.equinox.p2.updatechecker" version="2">
<resource path="src/org/eclipse/equinox/internal/p2/updatechecker/UpdateChecker.java" type="org.eclipse.equinox.internal.p2.updatechecker.UpdateChecker">
<filter id="640712815">
<message_arguments>
<message_argument value="IPlanner"/>
<message_argument value="UpdateChecker"/>
<message_argument value="updatesFor(IInstallableUnit, ProvisioningContext, IProgressMonitor)"/>
</message_arguments>
</filter>
</resource>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Import-Package: org.eclipse.equinox.internal.p2.core.helpers,
org.eclipse.equinox.p2.core.spi;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.engine;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.operations;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.planner;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.query;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.repository;version="[2.0.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.IntSupplier;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
Expand Down Expand Up @@ -57,12 +58,16 @@ private class UpdateCheckThread extends Thread {
IUpdateListener listener;
String profileId;
IQuery<IInstallableUnit> query;
private IntSupplier repositoryFlags;

UpdateCheckThread(String profileId, IQuery<IInstallableUnit> query, long delay, long poll, IUpdateListener listener) {
UpdateCheckThread(String profileId, IQuery<IInstallableUnit> query, long delay, long poll,
IntSupplier repositoryFlags,
IUpdateListener listener) {
this.poll = poll;
this.delay = delay;
this.profileId = profileId;
this.query = query;
this.repositoryFlags = repositoryFlags;
this.listener = listener;
}

Expand All @@ -75,7 +80,8 @@ public void run() {
while (!done) {
listener.checkingForUpdates();
trace("Checking for updates for " + profileId + " at " + getTimeStamp()); //$NON-NLS-1$ //$NON-NLS-2$
Collection<IInstallableUnit> iusWithUpdates = checkForUpdates(profileId, query);
Collection<IInstallableUnit> iusWithUpdates = checkForUpdates(profileId, query,
repositoryFlags.getAsInt());
if (iusWithUpdates.size() > 0) {
trace("Notifying listener of available updates"); //$NON-NLS-1$
UpdateEvent event = new UpdateEvent(profileId, iusWithUpdates);
Expand Down Expand Up @@ -104,10 +110,17 @@ public UpdateChecker(IProvisioningAgent agent) {

@Override
public void addUpdateCheck(String profileId, IQuery<IInstallableUnit> query, long delay, long poll, IUpdateListener listener) {
if (checkers.containsKey(listener))
addUpdateCheck(profileId, query, delay, poll, () -> IRepositoryManager.REPOSITORIES_ALL, listener);
}

@Override
public void addUpdateCheck(String profileId, IQuery<IInstallableUnit> query, long delay, long poll,
IntSupplier repositoryFlags, IUpdateListener listener) {
if (checkers.containsKey(listener)) {
return;
}
trace("Adding update checker for " + profileId + " at " + getTimeStamp()); //$NON-NLS-1$ //$NON-NLS-2$
UpdateCheckThread thread = new UpdateCheckThread(profileId, query, delay, poll, listener);
UpdateCheckThread thread = new UpdateCheckThread(profileId, query, delay, poll, repositoryFlags, listener);
checkers.put(listener, thread);
thread.start();
}
Expand All @@ -121,18 +134,17 @@ public void removeUpdateCheck(IUpdateListener listener) {
* Return the array of ius in the profile that have updates
* available.
*/
Collection<IInstallableUnit> checkForUpdates(String profileId, IQuery<IInstallableUnit> query) {
Collection<IInstallableUnit> checkForUpdates(String profileId, IQuery<IInstallableUnit> query,
int repositoryFlags) {
IProfile profile = getProfileRegistry().getProfile(profileId);
ArrayList<IInstallableUnit> iusWithUpdates = new ArrayList<>();
if (profile == null)
return Collections.emptyList();
ProvisioningContext context = new ProvisioningContext(agent);
context.setMetadataRepositories(getAvailableRepositories());
context.setMetadataRepositories(getAvailableRepositories(repositoryFlags));
if (query == null)
query = QueryUtil.createIUAnyQuery();
Iterator<IInstallableUnit> iter = profile.query(query, null).iterator();
while (iter.hasNext()) {
IInstallableUnit iu = iter.next();
for (IInstallableUnit iu : profile.query(query, null)) {
IQueryResult<IInstallableUnit> replacements = getPlanner().updatesFor(iu, context, null);
if (!replacements.isEmpty())
iusWithUpdates.add(iu);
Expand All @@ -143,9 +155,9 @@ Collection<IInstallableUnit> checkForUpdates(String profileId, IQuery<IInstallab
/**
* Returns the list of metadata repositories that are currently available.
*/
private URI[] getAvailableRepositories() {
private URI[] getAvailableRepositories(int repositoryFlags) {
IMetadataRepositoryManager repoMgr = agent.getService(IMetadataRepositoryManager.class);
URI[] repositories = repoMgr.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
URI[] repositories = repoMgr.getKnownRepositories(repositoryFlags);
ArrayList<URI> available = new ArrayList<>();
for (URI repositorie : repositories) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.internal.provisional.p2.updatechecker;

import java.util.function.IntSupplier;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.query.IQuery;

Expand All @@ -22,40 +23,51 @@
* whether to retrieve the updates, inform the user, etc.
*/
public interface IUpdateChecker {
public static final String SERVICE_NAME = IUpdateChecker.class.getName();
public static long ONE_TIME_CHECK = -1L;
String SERVICE_NAME = IUpdateChecker.class.getName();
long ONE_TIME_CHECK = -1L;

/**
* Adds an update listener that will be notified when updates are available for all
* installable units that satisfy the given query. The listener will remain
* registered until removed using the {@link #removeUpdateCheck(IUpdateListener)}
* method. Adding a listener that is identical to a listener that is already registered
* has no effect.
* Adds an update listener that will be notified when updates are available for
* all installable units that satisfy the given query. The listener will remain
* registered until removed using the
* {@link #removeUpdateCheck(IUpdateListener)} method. Adding a listener that is
* identical to a listener that is already registered has no effect.
* <p>
* Once the listener is registered, it will continue to receive notification of updates
* based on the specified polling frequency. However, if a delay value of {@link #ONE_TIME_CHECK}
* is used, only a single update check will occur for that listener. If this delay value
* is used, the specified polling frequency is ignored.
*
* @param profileId The profile id to check for updates
* @param iusToCheckQuery An installable unit query that matches the units to check for updates
* @param delay The delay in milliseconds before the first query should occur, or {@link #ONE_TIME_CHECK}
* to indicate that a single update check should occur immediately
* @param poll The polling frequency, in milliseconds, between checks for updates
* @param listener The listener to be notified of updates
* Once the listener is registered, it will continue to receive notification of
* updates based on the specified polling frequency. However, if a delay value
* of {@link #ONE_TIME_CHECK} is used, only a single update check will occur for
* that listener. If this delay value is used, the specified polling frequency
* is ignored.
*
* @param profileId The profile id to check for updates
* @param iusToCheckQuery An installable unit query that matches the units to
* check for updates
* @param delay The delay in milliseconds before the first query
* should occur, or {@link #ONE_TIME_CHECK} to indicate
* that a single update check should occur immediately
* @param poll The polling frequency, in milliseconds, between checks
* for updates
* @param repositoryFlagsSupplier the repository flags to be used for the update check
* @param listener The listener to be notified of updates
* @see #removeUpdateCheck(IUpdateListener)
*/
public abstract void addUpdateCheck(String profileId, IQuery<IInstallableUnit> iusToCheckQuery, long delay, long poll, IUpdateListener listener);
default void addUpdateCheck(String profileId, IQuery<IInstallableUnit> iusToCheckQuery, long delay, long poll,
IntSupplier repositoryFlagsSupplier, IUpdateListener listener) {
addUpdateCheck(profileId, iusToCheckQuery, delay, poll, listener);
}

void addUpdateCheck(String profileId, IQuery<IInstallableUnit> iusToCheckQuery, long delay, long poll,
IUpdateListener listener);

/**
* Removes an update listener from the set of listeners registered with this update
* checker. If an update check is currently in progress the listener may still receive
* events after this method returns. Removing a listener that is not registered has
* no effect.
*
*
* @param listener The listener to remove
* @see #addUpdateCheck(String, IQuery, long, long, IUpdateListener)
*/
public abstract void removeUpdateCheck(IUpdateListener listener);
void removeUpdateCheck(IUpdateListener listener);

}

0 comments on commit b46100f

Please sign in to comment.