读书人

(转)减低Eclipse RCP 项目 插件依赖度

发布时间: 2013-09-11 17:44:28 作者: rapoo

(转)降低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)

?

下面这篇文章思路颇有点 osgi service 意思, 有思想的可以看看

?

上图中, org.talend.designer.core插件项目要依赖于如此多的其它项目。从整个系统的体系结构看,org.talend.designer.core对于红线连接的插件的依赖是不符合逻辑的。?另外,其他插件之间也存在着相互依赖的情况,例如Runprocess就要依赖于Repository。?随着以后插件项目的增多,这么复杂的依赖型将导致软件的维护越来越困难。?解决的方案是提供一个服务的注册,提供机制,所有的服务在一个地方统一注册,其他插件到这里取用。?以Repository模块为例:可以体现为如下设计方案。Repository作为服务的提供者,其他使用该功能的插件作为消费者。?(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)??实际实现1 在基础项目org.talend.core中提供统一的服务注册接口程序如下:?(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)package?org.talend.core;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?java.util.HashMap;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?java.util.Map;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?org.eclipse.core.runtime.CoreException;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?org.eclipse.core.runtime.IConfigurationElement;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?org.eclipse.core.runtime.IExtensionRegistry;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?org.eclipse.core.runtime.Platform;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)import?org.talend.commons.exception.ExceptionHandler;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)/**
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?*?DOC?qian?class?global?comment.?A?global?service?register?provides?the?service?registration?and?acquirement.?<br/>
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?*?
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?*?$Id:?talend-code-templates.xml?1?2006-09-29?17:06:40?+0000?(星期五,?29?九月?2006)?nrousseau?$
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?*?
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?*/
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)public?class?GlobalServiceRegister?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????//?The?shared?instance
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????private?static?GlobalServiceRegister?instance?=?new?GlobalServiceRegister();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????private?static?IConfigurationElement[]?configurationElements;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?static?GlobalServiceRegister?getDefault()?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?instance;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????private?Map<Class,?IService>?services?=?new?HashMap<Class,?IService>();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????static?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????IExtensionRegistry?registry?=?Platform.getExtensionRegistry();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????configurationElements?=?registry.getConfigurationElementsFor("org.talend.core.service");
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????/**
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?DOC?qian?Comment?method?"getService".Gets?the?specific?IService.
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?@param?klass?the?Service?type?you?want?to?get
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?@return?IService?IService
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*/
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IService?getService(Class?klass)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????IService?service?=?services.get(klass);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????if?(service?==?null)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????service?=?findService(klass);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????if?(service?==?null)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????throw?new?RuntimeException("The?service?"?+?klass.getName()?+?"?has?not?been?registered.");
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????services.put(klass,?service);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?service;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????/**
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?DOC?qian?Comment?method?"findService".Finds?the?specific?service?from?the?list.
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?@param?klass?the?interface?type?want?to?find.
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?@return?IService
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*/
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????private?IService?findService(Class?klass)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????String?key?=?klass.getName();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????for?(int?i?=?0;?i?<?configurationElements.length;?i++)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????IConfigurationElement?element?=?configurationElements[i];
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????String?id?=?element.getAttribute("serviceId");
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????if?(!key.endsWith(id))?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????continue;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????try?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????Object?service?=?element.createExecutableExtension("class");
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????if?(klass.isInstance(service))?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????????return?(IService)?service;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????}?catch?(CoreException?e)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????????ExceptionHandler.process(e);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?null;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)}??2 服务接口为:?(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)public?interface?IService?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)}3 插件扩展点定义为;org.talend.core/plugin.xml(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)<extension-point?id="service"?name="Service?Registration"?schema="schema/service.exsd"/>
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)4对于希望提供服务插件需要声明自己的服务类型。例如org.talend.repository插件希望提供服务。定义IRepositoryService.javapublic?interface?IRepositoryService?extends?IService?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IComponentsFactory?getComponentsFactory();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IPath?getPathFileName(String?folderName,?String?fileName);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IProxyRepositoryFactory?getProxyRepositoryFactory();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IPath?getRepositoryPath(RepositoryNode?node);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)}?(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)public?class?RepositoryService?implements?IRepositoryService?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IComponentsFactory?getComponentsFactory()?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?ComponentsFactoryProvider.getInstance();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IPath?getPathFileName(String?folderName,?String?fileName)?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?RepositoryPathProvider.getPathFileName(folderName,?fileName);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IProxyRepositoryFactory?getProxyRepositoryFactory()?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?ProxyRepositoryFactory.getInstance();
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IPath?getRepositoryPath(RepositoryNode?node){
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?RepositoryNodeUtilities.getPath(node);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)}?5 org.talend.repository使用扩展点,使自己注册到org.talend.core中.org.talend.repository/plugin.xml(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)<extension
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????????point="org.talend.core.service">
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)??????<Service
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????serviceId="IRepositoryService"
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????????class="org.talend.repository.RepositoryService"/>
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?</extension>
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)??6 消费者使用org.talend.repository提供的服务时,只需要调用?(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)/**
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?DOC?get?a?implement?of?IRepositoryService.
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*?@return?a?implement?of?IRepositoryService
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)?????*/
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????public?IRepositoryService?getRepositoryService()?{
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????IService?service?=?GlobalServiceRegister.getDefault().getService(IRepositoryService.class);
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????????return?(IRepositoryService)?service;
(转)减低Eclipse RCP 项目 插件依赖度(OSGI 服务的理解)????}?经过这样的改造,所有的插件都只要依赖org.talend.core即可,解决了插件间依赖结构混乱的问题。而且系统具有很好的开放性,很容易的加入其他服务的注册,有利于今后的扩展。?P.S. 一开始想利用Eclipse 的getAdaptable(Class) 机制来实现此功能,虽然也能实现,但是使用者还要自己写Facotry类,增加了扩展的难度,而且getAdaptable(Class)机制也不是用来解决这种问题的。而且自己控制服务的加载可以避免很多额外的麻烦。

?


我的异常网推荐解决方案:org.eclipse.core.runtime.CoreException,http://www.myexception.cn/eclipse/org.eclipse.core.runtime.CoreException.html

读书人网 >操作系统

热点推荐