ITeye博客

eclipse RCP自动更新利器----p2

popjxc · 2011-02-09 14:47 · Eclipse插件开发 · 编程语言 · Eclipse · RCP · headless update · p2 · Markdown

最近基于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;
	}

评论(9)

  1. #9 楼 · kc284463759 · 2014-05-28 16:56

    jjyy552211@qq.com,希望能得到源码,多谢

  2. #8 楼 · peixingchen · 2012-12-03 09:27

    有些配置还没清楚,能给源代码源代码吗?QQ:117310722,请发QQ邮箱

  3. #7 楼 · popjxc · 2012-11-08 11:57

    chenqisdfx writes:

    好像不行啊,在new出agent后我调用addsites(),但是在IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) provisioningAgent
    .getService(IMetadataRepositoryManager.SERVICE_NAME); 出错,metaDataManager总是为空。敢问能给解释以下否???

    或者QQ : 36396743

    chenqisdfx@163.com

    抱歉各位, 本人已不做java好久了。 给各位两点建议:

    1. 确认getUpdateSiteFile()得到的configuration/UpdateSite.xml文件一定要存在。
    2. 在headlessUpdate()中得到agent对象后,要调用addSites(agent)。 千万不要new出agent!
  4. #6 楼 · liangqiming · 2012-11-03 18:09

    代码似乎不是很完整吧,可否看下完整代码,如方便可以加QQ 568428154,或者是发送完整源码到568628154@qq.com

  5. #5 楼 · qq123zhz · 2012-06-15 10:07

    我也没有成功,求源码例子,qq123zhz@gmail.com

  6. #4 楼 · chenqisdfx · 2011-09-29 20:29

    好像不行啊,在new出agent后我调用addsites(),但是在IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) provisioningAgent
    .getService(IMetadataRepositoryManager.SERVICE_NAME); 出错,metaDataManager总是为空。敢问能给解释以下否???

    或者QQ : 36396743

    chenqisdfx@163.com

  7. #3 楼 · liuhuanleijava · 2011-09-15 09:41

    加Q 308834143 验证身份那写:rcp ,详细讨论下

  8. #2 楼 · xiehongdong · 2011-05-04 17:31

    能不能把源码发出来看下啊,我的邮箱hongdongxie2008@163.com

    先谢谢了……

  9. #1 楼 · tianlihu · 2011-04-04 00:20

    能说的更清楚一些吗?

评论功能维护中,当前仅展示历史评论。