How do I easily query a running Eclipse for its installed Features?

我是研究僧i 提交于 2019-12-23 03:42:36

问题


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


回答1:


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);
    }

}

}




回答2:


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. :)



来源:https://stackoverflow.com/questions/9927400/how-do-i-easily-query-a-running-eclipse-for-its-installed-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!