Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.equinox.p2.operations;bundle-version="2.7.100",
org.eclipse.equinox.p2.repository;bundle-version="2.7.100",
org.eclipse.equinox.p2.engine;bundle-version="2.10.600",
org.eclipse.equinox.p2.garbagecollector;bundle-version="1.3.0",
org.eclipse.equinox.p2.ui;bundle-version="2.8.100",
org.eclipse.chemclipse.progress;bundle-version="0.9.0",
net.openchrom.feature.branding;bundle-version="1.6.13",
Expand Down
5 changes: 1 addition & 4 deletions openchrom/plugins/net.openchrom.installer.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
point="org.eclipse.ui.startup">
<startup
class="net.openchrom.installer.ui.FeatureCheck"></startup>
</extension>
<extension
point="org.eclipse.ui.startup">
<startup
class="net.openchrom.installer.ui.RepositoryCleanup"></startup>
class="net.openchrom.installer.ui.P2Cleanup"></startup>
</extension>
<extension
id="com.lablicate.installer.ui.fragment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import java.net.URI;

import org.eclipse.chemclipse.logging.core.Logger;
import org.eclipse.equinox.internal.p2.garbagecollector.GarbageCollector;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.IProfileRegistry;
import org.eclipse.equinox.p2.repository.IRepository;
import org.eclipse.equinox.p2.repository.IRepositoryManager;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
Expand All @@ -26,20 +30,67 @@
import org.osgi.framework.Version;
import org.osgi.util.tracker.ServiceTracker;

public class RepositoryCleanup implements IStartup {
public class P2Cleanup implements IStartup {

private static final Logger logger = Logger.getLogger(RepositoryCleanup.class);
private static final Logger logger = Logger.getLogger(P2Cleanup.class);

@Override
public void earlyStartup() {

BundleContext context = FrameworkUtil.getBundle(RepositoryCleanup.class).getBundleContext();
BundleContext context = FrameworkUtil.getBundle(P2Cleanup.class).getBundleContext();
ServiceTracker<IProvisioningAgent, IProvisioningAgent> tracker = new ServiceTracker<>(context, IProvisioningAgent.class, null);
tracker.open();
cleanRepositories(tracker.getService(), context.getBundle().getVersion());
IProvisioningAgent agent = tracker.getService();
cleanRepositories(agent, context.getBundle().getVersion());
cleanProfileStates(agent);
tracker.close();
}

/*
* Delete old snapshots of the running profile so bundles referenced in it are removed from disk.
*/
private static void cleanProfileStates(IProvisioningAgent agent) {

if(agent == null) {
return;
}
IProfileRegistry profileRegistry = agent.getService(IProfileRegistry.class);
if(profileRegistry == null) {
return;
}
IProfile profile = profileRegistry.getProfile(IProfileRegistry.SELF);
if(profile == null) {
return;
}
String profileId = profile.getProfileId();
long[] timestamps = profileRegistry.listProfileTimestamps(profileId);
if(timestamps.length <= 1) {
return;
}
boolean removed = false;
// The last timestamp belongs to the currently running profile and must be kept.
for(int i = 0; i < timestamps.length - 1; i++) {
try {
profileRegistry.removeProfile(profileId, timestamps[i]);
removed = true;
} catch(ProvisionException e) {
logger.warn(e);
}
}
if(removed) {
runGarbageCollector(agent, profile);
}
}

@SuppressWarnings("restriction")
private static void runGarbageCollector(IProvisioningAgent agent, IProfile profile) {

GarbageCollector garbageCollector = agent.getService(GarbageCollector.class);
if(garbageCollector != null) {
garbageCollector.runGC(profile);
}
}

private static void cleanRepositories(IProvisioningAgent agent, Version version) {

var currentVersion = version.getMajor() + "." + version.getMinor() + "." + version.getMicro();
Expand Down