最近基于eclipse 3.6开发RCP程序,不借用eclipse的更新UI,而是在程序启动时自动检查更新,也就是所谓的headless update.
其实这是很简单的,利用eclipse wiki上的一段代码就可以搞定,如下:
public class P2Util {
static final String JUSTUPDATED = "update_flag"; //$NON-NLS-1$
public static void headlessUpdate() {
// update when startup
final IProvisioningAgent agent = (IProvisioningAgent) ServiceHelper
.getService(Activator.bundleContext,
IProvisioningAgent.SERVICE_NAME);
addSites(agent);
if (agent == null) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
Messages.getString("UpdateChecker.1"))); //$NON-NLS-1$
}
// XXX if we're restarting after updating, don't check again.
final IPreferenceStore prefStore = Activator.getDefault()
.getPreferenceStore();
if (prefStore.getBoolean(JUSTUPDATED)) {
prefStore.setValue(JUSTUPDATED, false);
return;
}
// XXX check for updates before starting up.
// If an update is performed, restart. Otherwise log
// the status.
final 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);
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
PlatformUI.getWorkbench().restart();
}
});
} else {
LogHelper.log(updateStatus);
}
}
};
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
new ProgressMonitorDialog(null).run(true, true, runnable);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
}
});
}
// XXX Check for updates to this application and return a status.
public 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,
Messages.getString("P2Util.0"), 200); //$NON-NLS-1$
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.
try {
ProvisioningJob job = operation.getProvisioningJob(null);
status = job.runModal(sub.newChild(100));
} catch (Exception e) {
throw new OperationCanceledException();
}
if (status.getSeverity() == IStatus.CANCEL)
throw new OperationCanceledException();
}
return status;
}
}
为了让程序在启动时执行这段更新代码,我们可以实现IStartup扩展点,来启动更新检查:
public class UpdateChecker implements IStartup {
@Override
public void earlyStartup() {
P2Util.headlessUpdate();
}
}
最后说一下更新站点地址的配置,一般情况下,eclipse是在一个p2.inf文件中配置的,这里就不多说了,因为这种方式不够灵活,p2.inf文件是在插件jar包里的,不便于用户修改,并且一旦更新地址发生变化,也不变程序自动适配。
这里我们用xml配置更新地址,可以随意添加多个,兼容性更加强一些。
xml格式很简单,如下:
<?xml version="1.0" encoding="UTF-8"?>
<sites>
<site name="sample1" url="http://popjxc.iteye.com/update1"/>
<site name="sample2" url="http://popjxc.iteye.com/update2"/>
</sites>
然后读取xml,并添加更新站点,需要在P2Util类里添加下面两个函数:
private static void addSites(IProvisioningAgent provisioningAgent) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(new File(getUpdateSiteFile()));
Element root = document.getRootElement();
Iterator<?> it = root.elementIterator("site");
String name;
String url;
while(it.hasNext()) {
Element e = (Element)it.next();
name = e.attributeValue("name");
url = e.attributeValue("url");
addUpdateSite(provisioningAgent, url, name);
}
} catch (Exception e) {
e.printStackTrace();
ExceptionManager.logError(e, e.getMessage());
}
}
private static void addUpdateSite(IProvisioningAgent provisioningAgent, String url, String name)
throws InvocationTargetException {
// Load repository manager
IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) provisioningAgent
.getService(IMetadataRepositoryManager.SERVICE_NAME);
if (metadataManager == null) {
LogMessage.logInfo("Metadata manager was null");
Throwable throwable = new Throwable(
"Could not load Metadata Repository Manager");
throwable.fillInStackTrace();
throw new InvocationTargetException(throwable);
}
// Load artifact manager
IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) provisioningAgent
.getService(IArtifactRepositoryManager.SERVICE_NAME);
if (artifactManager == null) {
LogMessage.logInfo("Artifact manager was null");
Throwable throwable = new Throwable(
"Could not load Artifact Repository Manager");
throwable.fillInStackTrace();
throw new InvocationTargetException(throwable);
}
// Load repo
try {
URI repoLocation = new URI(url);
LogMessage.logInfo("Adding repository " + repoLocation);
metadataManager.loadRepository(repoLocation, null);
artifactManager.loadRepository(repoLocation, null);
} catch (ProvisionException pe) {
LogMessage.logError("Caught provisioning exception " + pe.getMessage(), pe);
throw new InvocationTargetException(pe);
} catch (URISyntaxException e) {
LogMessage.logError("Caught URI syntax exception " + e.getMessage(), e);
throw new InvocationTargetException(e);
}
}
private static String getUpdateSiteFile() {
String path = System.getProperty("user.dir");
path += "/configuration/UpdateSite.xml";
return path;
}