RCP P2在线更新

1、新建feature工程,如下图:
RCP P2在线更新_第1张图片

2、修改feature.xml文件
2.1、点击Plug-ins选项卡,点击Add按钮添加插件(RCP运行基础插件、P2更新插件、开发的插件),如下图:
RCP P2在线更新_第2张图片

需要依赖的插件清单:
RCP P2在线更新_第3张图片

2.2、点击Included Features选项卡,点击Add按钮,添加Feature插件(org.eclipse.rcp),如下图:
RCP P2在线更新_第4张图片

2.3、其他选项卡可以根据自身需要进行设置

3、设置默认P2更新首选项
3.1、在 RCP主工程下添加plugin_customization.ini文件
RCP P2在线更新_第5张图片
// plugin-customization.ini文件内容

automatic update options are defined in org.eclipse.equinox.p2.sdk.scheduler.PreferenceConstants

check for updates on startup

org.eclipse.equinox.p2.ui.sdk.scheduler/enabled=true
org.eclipse.equinox.p2.ui.sdk.scheduler/schedule=on-startup

remind the user every 4 hours

org.eclipse.equinox.p2.ui.sdk.scheduler/remindOnSchedule=true

see AutomaticUpdatesPopup, values can be “30 minutes”, “Hour”, “4 Hours”

org.eclipse.equinox.p2.ui.sdk.scheduler/remindElapsedTime=4 Hours

download updates before notifying the user

org.eclipse.equinox.p2.ui.sdk.scheduler/download=true
org.eclipse.ui/SHOW_PROGRESS_ON_STARTUP=false
org.eclipse.ui/SHOW_TRADITIONAL_STYLE_TABS=false
org.eclipse.ui/DOCK_PERSPECTIVE_BAR=topRight

3.2、关联设置plugin-customization.ini文件
3.2.1、打开主工程下的MANIFEST.MF文件,点击Extension选项卡,添加org.eclipse.core.runtime.products扩展点,增加preferenceCustomization属性,设置值为plugin_customization.ini,如下图:
RCP P2在线更新_第6张图片

3.2.2、新增首选项加载类,如下图:
RCP P2在线更新_第7张图片

// PreferenceInitializer.java文件内容
public class PreferenceInitializer extends AbstractPreferenceInitializer {

@Override
public void initializeDefaultPreferences() {
    Preferences node = DefaultScope.INSTANCE.getNode(Activator.PLUGIN_ID);
    node.putBoolean(PreferenceConstants.REPOSITORIES_VISIBLE, false);
    node.putBoolean(PreferenceConstants.SHOW_LATEST_VERSION_ONLY, true);
    node.putBoolean(PreferenceConstants.AVAILABLE_SHOW_ALL_BUNDLES, false);
    node.putBoolean(PreferenceConstants.INSTALLED_SHOW_ALL_BUNDLES, false);
    node.putBoolean(PreferenceConstants.AVAILABLE_GROUP_BY_CATEGORY, true);
    node.putBoolean(PreferenceConstants.SHOW_DRILLDOWN_REQUIREMENTS, false);
    node.putInt(PreferenceConstants.UPDATE_WIZARD_STYLE, Policy.UPDATE_STYLE_MULTIPLE_IUS);
    node.putBoolean(PreferenceConstants.FILTER_ON_ENV, false);
    node.putInt(PreferenceConstants.UPDATE_DETAILS_HEIGHT, SWT.DEFAULT);
    node.putInt(PreferenceConstants.UPDATE_DETAILS_WIDTH, SWT.DEFAULT);
}

}

//PreferenceConstants文件内容
public interface PreferenceConstants {

public static final String SHOW_LATEST_VERSION_ONLY = "showLatestVersionOnly";
public static final String AVAILABLE_GROUP_BY_CATEGORY = "groupByCategory";
public static final String AVAILABLE_SHOW_ALL_BUNDLES = "showAllBundlesAvailable";
public static final String INSTALLED_SHOW_ALL_BUNDLES = "showAllBundlesInstalled";
public static final String REPOSITORIES_VISIBLE = "repositoriesVisible";
public static final String SHOW_DRILLDOWN_REQUIREMENTS = "showDrilldownRequirements";
public static final String UPDATE_WIZARD_STYLE = "updateWizardStyle";
public static final String UPDATE_DETAILS_WIDTH = "updateDetailsWidth";
public static final String UPDATE_DETAILS_HEIGHT = "updateDetailsHeight";
public static final String FILTER_ON_ENV = "filterOnEnv";

}

4、P2更新
4.1、自动更新
重写ApplicationWorkbenchAdvisor.java类的postStartup方法,如下所示:
@Override

public void postStartup() {
    super.postStartup();
    ProvisioningUI provisioningUI = ProvisioningUI.getDefaultUI();
    IMetadataRepositoryManager metadataRepositoryManager = ProvUI.getMetadataRepositoryManager(provisioningUI.getSession());
    IArtifactRepositoryManager artifactRepositoryManager = ProvUI.getArtifactRepositoryManager(provisioningUI.getSession());
    try {
        URI uri = new URI(System.getProperty("update url."));
        metadataRepositoryManager.addRepository(uri);
        artifactRepositoryManager.addRepository(uri);
    } catch (Exception e) {
        LoggerUtil.error("add respository uri error.", e);
    }
}

4.2、手动更新

说明:UpdateHandler类为菜单栏响应类
//PreloadingRepositoryHandler.java文件内容
public abstract class PreloadingRepositoryHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    doExecuteAndLoad();
    return null;
}

private void doExecuteAndLoad() {
    if (preloadRepositories()) {
        Job.getJobManager().cancel(LoadMetadataRepositoryJob.LOAD_FAMILY);
        final LoadMetadataRepositoryJob loadJob = new LoadMetadataRepositoryJob(getProvisioningUI());
        setLoadJobProperties(loadJob);
        if (waitForPreload()) {
            loadJob.addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    if (PlatformUI.isWorkbenchRunning()) {
                        if (event.getResult().isOK()) {
                            PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    doExecute(loadJob);
                                }
                            });
                        }
                    }
                }
            });
            loadJob.setUser(true);
            loadJob.schedule();
        } else {
            loadJob.setSystem(true);
            loadJob.setUser(false);
            loadJob.schedule();
            doExecute(null);
        }
    } else {
        doExecute(null);
    }
}

protected abstract void doExecute(LoadMetadataRepositoryJob job);

protected boolean preloadRepositories() {
    return true;
}

protected boolean waitForPreload() {
    return true;
}

protected void setLoadJobProperties(Job loadJob) {
    loadJob.setProperty(LoadMetadataRepositoryJob.ACCUMULATE_LOAD_ERRORS, Boolean.toString(true));
}

protected ProvisioningUI getProvisioningUI() {
    return ProvisioningUI.getDefaultUI();
}

protected Shell getShell() {
    return PlatformUI.getWorkbench().getModalDialogShellProvider().getShell();
}

}

//UpdateHandler文件内容
public class UpdateHandler extends PreloadingRepositoryHandler {

@Override
protected void doExecute(LoadMetadataRepositoryJob job) {
    UpdateOperation updateOperation = getProvisioningUI().getUpdateOperation(null, null);
    updateOperation.resolveModal(null);
    if (getProvisioningUI().getPolicy().continueWorkingWithOperation(updateOperation, getShell())) {
        if (UpdateSingleIUWizard.validFor(updateOperation)) {
            UpdateSingleIUWizard wizard = new UpdateSingleIUWizard(getProvisioningUI(), updateOperation);
            WizardDialog dialog = new WizardDialog(getShell(), wizard);
            dialog.create();
            dialog.open();
        } else {
            getProvisioningUI().openUpdateWizard(false, updateOperation, job);
        }
    }
}

@Override
protected boolean preloadRepositories() {
    IMetadataRepositoryManager metadataRepositoryManager = ProvUI.getMetadataRepositoryManager(getProvisioningUI().getSession());
    IArtifactRepositoryManager artifactRepositoryManager = ProvUI.getArtifactRepositoryManager(getProvisioningUI().getSession());
    try {
        URI[] uris = metadataRepositoryManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
        for (URI uri : uris) {
            metadataRepositoryManager.loadRepository(uri, 0, null);
            artifactRepositoryManager.loadRepository(uri, 0, null);
        }
    } catch (Exception e) {
    }
    return super.preloadRepositories();
}

}

你可能感兴趣的:(RCP P2在线更新)