NullPointerException在创建ProvisioningJob以进行无头更新Eclipse RCP应用程序时

时间:2018-08-23 20:02:25

标签: java eclipse eclipse-rcp

我正在实施Eclipse应用程序的无头强制更新。我使用了https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fp2_startup.htm中的P2Util类,但是我的代码正在ProvisioningJob job = operation.getProvisioningJob(null);返回空指针异常,而作业对象即将到null。有谁知道这个空对象的可能原因。

public class P2Util {
    // XXX Check for updates to this application and return a status.
    static IStatus checkForUpdates(IProvisioningAgent agent, IProgressMonitor monitor) throws OperationCanceledException {
        ProvisioningSession session = new ProvisioningSession(agent);
        // the default update operation looks for updates to the currently
        // running profile, using the default profile root marker. To change
        // which installable units are being updated, use the more detailed
        // constructors.
        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) {
            // More complex status handling might include showing the user what updates
            // are available if there are multiples, differentiating patches vs. updates, etc.
            // In this example, we simply update as suggested by the operation.
            ProvisioningJob job = operation.getProvisioningJob(null);
            status = job.runModal(sub.newChild(100));//null pointer here
            if (status.getSeverity() == IStatus.CANCEL)
                throw new OperationCanceledException();
        }
        return status;
    }
}

我正在按以下方式调用此方法。

private Integer checkUpdate(final String updateServerURL, final IProvisioningAgent provisioningAgent, ProgressMonitorSplash monitor) {
    returnValue = IApplication.EXIT_OK;

    final IRunnableWithProgress runnable = new IRunnableWithProgress() {
        @Override
        public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            P2Util.addRepository(provisioningAgent, updateServerURL);
            final IStatus updateStatus = P2Util.checkForUpdates(provisioningAgent, monitor);
            if (updateStatus.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
                logger.debug("No Updates");
            } else if (updateStatus.getSeverity() != IStatus.ERROR) {
                logger.debug("Updates applied, attempting restart");
                returnValue = IApplication.EXIT_RESTART;
            } else {
                logger.error(updateStatus.getMessage());
            }
        }
    };
    try {
        monitor.run(true, runnable);
    } catch (final InvocationTargetException e) {
        e.printStackTrace();
    } catch (final InterruptedException e) {
        logger.error("interrupted: " + e.getMessage());
    }
    return returnValue;
}

我正在使用EclipseContext创建ProvisingAgent

            final IEclipseContext localContext = EclipseContextFactory.getServiceContext(Activator.getContext());
            final IProvisioningAgent provisioningAgent = getService(localContext, IProvisioningAgent.class);
            String env = System.getProperty("env").toLowerCase();
            String repo = System.getProperty("validUrl." + env);
            if ( repo == null ){
                repo = System.getProperty("validUrl");
            }
            if ( repo != null ){
                ret = checkUpdate(repo, provisioningAgent, sp);
                if ( ret == IApplication.EXIT_RESTART ){
                    logger.info("Update successful, restarting...");
                    return ret;
                }
            }

1 个答案:

答案 0 :(得分:0)

UpdateOperation#getProvisioningJob(IProgressMonitor)上的Javadoc说:

 * @return a job that can be used to perform the provisioning operation.  This may be <code>null</code> 
 * if the operation has not been resolved, or if a plan could not be obtained when attempting to
 * resolve.  If the job is null and the operation has been resolved, then the resolution result 
 * will explain the problem.