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

为EclipseRCP应用程序的无头更新创建ProvisioningJob时出现NullPointerException

  •  0
  • Ashish  · 技术社区  · 7 年前

    我正在实现Eclipse应用程序的无头强制更新。我使用了来自的p2util类 https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fp2_startup.htm 但我的代码返回空指针异常 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 回复  |  直到 7 年前
        1
  •  0
  •   Brian de Alwis    7 年前

    上的JavaDoc UpdateOperation#getProvisioningJob(IProgressMonitor) 说:

     * @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.