P2从Web存储库自动更新

时间:2012-03-07 10:12:21

标签: eclipse repository eclipse-rcp p2

我的P2自动更新有一些问题,当我在本地执行时,从本地存储库一切正常,我可以在重启时进行自动更新,但是当我尝试从网络仓库更新时,我遇到了这些错误:

An internal error occurred during: "Install download0".
org.eclipse.swt.SWTException: Invalid thread access

真的,我不明白其中的区别。 在我的ApplicationWindowWorkbenchAdvisor中,我有这个帖子窗口打开方法:

private static final String JUSTUPDATED = "justUpdated";

public void postWindowOpen() {
    final IProvisioningAgent agent = (IProvisioningAgent) ServiceHelper
            .getService(Activator.bundleContext,
                    IProvisioningAgent.SERVICE_NAME);
    if (agent == null) {
        LogHelper
                .log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        "No provisioning agent found.  This application is not set up for updates."));
    }
    final IPreferenceStore prefStore = Activator.getDefault()
            .getPreferenceStore();
    if (prefStore.getBoolean(JUSTUPDATED)) {
        prefStore.setValue(JUSTUPDATED, false);
        return;
    }

    IRunnableWithProgress runnable = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor)
                throws InvocationTargetException, InterruptedException {
            IStatus updateStatus = P2Util.checkForUpdates(agent, monitor);
            if (updateStatus.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
                PlatformUI.getWorkbench().getDisplay()
                        .asyncExec(new Runnable() {
                            public void run() {
                                MessageDialog.openInformation(null,
                                        "Updates", "No updates were found");
                            }
                        });
            } else if (updateStatus.getSeverity() != IStatus.ERROR) {
                prefStore.setValue(JUSTUPDATED, true);
                PlatformUI.getWorkbench().restart();
            } else {
                LogHelper.log(updateStatus);
            }
        }
    };
    try {
        new ProgressMonitorDialog(null).run(true, true, runnable);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }

}

在我的P2Util中:

    public static IStatus checkForUpdates(IProvisioningAgent agent,
        IProgressMonitor monitor) {
    ProvisioningSession session = new ProvisioningSession(agent);
    UpdateOperation operation = new UpdateOperation(session);
    SubMonitor sub = SubMonitor.convert(monitor,
            "Checking for application updates...", 200);
    IStatus status = operation.resolveModal(sub.newChild(100));
    if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
        return status;
    }
    if (status.getSeverity() == IStatus.CANCEL)
        throw new OperationCanceledException();

    if (status.getSeverity() != IStatus.ERROR) {
        ProvisioningJob job = operation.getProvisioningJob(null);
        status = job.runModal(sub.newChild(100));
        if (status.getSeverity() == IStatus.CANCEL)
            throw new OperationCanceledException();
    } 
    return status;
}

在我的PreferenceInitializer中:

node.putBoolean(PreferenceConstants.PREF_AUTO_UPDATE_ENABLED, true);
node.putBoolean(PreferenceConstants.PREF_AUTO_UPDATE_SCHEDULE, true);
node.putBoolean(PreferenceConstants.PREF_DOWNLOAD_ONLY, true);

我重复一遍:从本地回购我可以执行自动更新,但从网络回购我不能。 感谢阅读。

1 个答案:

答案 0 :(得分:4)

有问题的代码是

new ProgressMonitorDialog(null).run(true, true, runnable);

这是在ui-thread之外调用的,它会抛出错误...你还必须在asyncExec调用中包装它