NullPointerException in create ProvisioningJob for eclipse r



我正在实现 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;
}
}

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.

最新更新