How do I easily query a running Eclipse for its in

2019-06-06 06:32发布

I'm building an Eclipse Feature that has a requirement that amounts to this... The Feature must be able to be uninstalled automatically if the user has his license revoked.

If you want more background information about that, Here is another question that I've asked on this topic.

Thanks to the answers on that question, I've been trying to learn the Eclipse p2 director api. I've found some classes that look useful here

I've been trying to instantiate one of these classes in my code, but with no luck yet. I've been reading the help documentation, and I'm getting kind of lost in it all.

I'm stuck because I have a need to supply the OperationFactory with a collection of IInstallableUnit objects.

private void scheduleUninstallOperationJob(
        Collection<? extends IVersionedId> toUninstall) 
{
    OperationFactory oFactory = new OperationFactory();
    Collection<URI> repos = null;
    UninstallOperation op = null;
    try {
        op = oFactory.createUninstallOperation(toUninstall, repos, null);
    } catch (ProvisionException e) {
        e.printStackTrace();
    }
    IStatus result = op.resolveModal(null);
    if (result.isOK()) {
        op.getProvisioningJob(null).schedule();
    }
}

I don't see any way to easily ask the running instance of Eclipse to give me the collection of currently installed InstallableUnits so that I can easily pass the one I want to uninstall to the OperationFactory.createUninstallOperation() method.

I've tried using Eclipse source code as an example, but the code that I have found is the org.eclipse.equinox.p2.ui.ProvisioningUI, and it's tightly coupled to the UI that is used when manually uninstalling InstallableUnits. This code also uses code that is in the dreaded Eclipse internal packages which I would like to avoid using if possible.

Thank you for your consideration, Trace

2条回答
三岁会撩人
2楼-- · 2019-06-06 07:15

There was very little traffic on this question. I attribute that to the odd nature of the requirement that is driving this development.

Well, it seems that those requirements have changed, and I will no longer be needing to programmatically uninstall Eclipse Features based on a revoked license. Therefore I will stop researching how to do this for now.

If you're looking to solve this issue to, feel free to contact me by leaving a comment here, or answering this question with another question. :)

查看更多
ら.Afraid
3楼-- · 2019-06-06 07:39

This code gets the collection of IUs which are managed by the profile of the currently running system:

/**
 * This Activator informs user about the IUs which are currently installed in
 * the running environment.
 * 
 * This code is intended for demo only and should be much more defensive for
 * production use.
 * 
 * @author Ilya Shinkarenko
 * 
 */
public class SelfInformerActivator extends Plugin {

@Override
public void start(final BundleContext ctx) throws Exception {
    super.start(ctx);

    ServiceReference<IProvisioningAgentProvider> sr = ctx.getServiceReference(IProvisioningAgentProvider.class);
    IProvisioningAgentProvider agentProvider = ctx.getService(sr);
    URI p2InstanceURI = null; // myself
    final IProvisioningAgent agent = agentProvider.createAgent(p2InstanceURI);

    IProfileRegistry regProfile = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);

    IProfile profileSelf = regProfile.getProfile(IProfileRegistry.SELF);

    IQuery<IInstallableUnit> query = QueryUtil.createIUAnyQuery();

    //This is what you need:
    IQueryResult<IInstallableUnit> allIUs = profileSelf.query(query, new NullProgressMonitor());

    //Let's output it:
    Iterator<IInstallableUnit> iterator = allIUs.iterator();
    while (iterator.hasNext()) {
        IInstallableUnit iu = iterator.next();
        System.out.println(iu);
    }

}

}

查看更多
登录 后发表回答