代码之家  ›  专栏  ›  技术社区  ›  positron

以编程方式获取已安装的功能

  •  1
  • positron  · 技术社区  · 11 年前

    我正在运行以下代码来迭代已安装的功能

    IBundleGroup[] bundleGroups = Platform.getBundleGroupProviders()[0].getBundleGroups();
    

    但是,此代码不会返回产品初始运行后安装的功能。我在“安装详细信息”对话框的“功能”选项卡下看不到这些已安装的功能(帮助->关于->安装详细信息),但在“已安装的软件”选项卡下看到了这些功能。是否有其他API来获取这些功能?

    1 回复  |  直到 11 年前
        1
  •  3
  •   Ilya Shinkarenko    11 年前

    最好使用P2 API。下面是一个工作原理示例(未经测试,但您会明白):

    Set<IInstallableUnit> findFeatures() throws ProvisionException {
    
        Set<IInstallableUnit> result = Sets.newHashSet();
    
        // 1. initialize necessary p2 services
        BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext();
        ServiceReference<IProvisioningAgentProvider> ref = ctx.getServiceReference(IProvisioningAgentProvider.class);
    
        IProvisioningAgentProvider agentProvider = ctx.getService(ref);
    
        String profileId = IProfileRegistry.SELF; // the profile id for the currently running system
        URI location = null; // the location for the currently running system is null
    
        IProvisioningAgent provisioningAgent = agentProvider.createAgent(location);
        IProfileRegistry profileRegistry = (IProfileRegistry) provisioningAgent.getService(IProfileRegistry.SERVICE_NAME);
        IProfile p2Profile = profileRegistry.getProfile(profileId);
    
        // 2. create a query (check QueryUtil for options)
        IQuery<IInstallableUnit> query = QueryUtil.createIUGroupQuery();
    
        // 3. perform query
        IQueryResult<IInstallableUnit> queryResult = p2Profile.query(query, null);
        result = queryResult.toSet();
    
        return result;
    }