代码之家  ›  专栏  ›  技术社区  ›  Jonathan Holloway

Eclipse扩展和声明性服务

  •  3
  • Jonathan Holloway  · 技术社区  · 17 年前

    我对Eclipse体系结构中的扩展/服务方法有点困惑。

    1. Eclipse插件扩展的使用- http://www.eclipse.org/articles/Article-Plug-in-architecture/plugin_architecture.html
    2. 声明性服务的使用- http://www.eclipse.org/equinox/bundles/

    什么时候你会使用一个而不是另一个,有什么优点和缺点

    2 回复  |  直到 17 年前
        1
  •  4
  •   McDowell rahul gupta    17 年前

    在EclipseZone上有一个很好的比较(我想是从2007年开始的): A Comparison of Eclipse Extensions and OSGi Services .

    MANIFEST.MF 对于依赖项和 插件 目录来确认这一点。

        2
  •  2
  •   tkokasih    8 年前

    “当前”方法(pre3.5M5,与eclipse3.4类似)的问题在于,Eclipse插件扩展或OSGI DS(声明性服务)都需要一些特定于扩展或OSGI的API出现在插件中。


    Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO ,摘自2009年的《日食》。

    这里有一种味道:


    模块层允许最小化静态依赖关系,更少的静态依赖关系意味着更少的 东西 这必须存在,以便您的组件正常工作。
    服务允许您的组件与其他组件交互。

    胶合

    声明性服务 (DS)是OSGi纲要第112节中的规范。
    它是在4.0版中引入的,基于extender模型。

    与所有扩展器一样,DS执行任务 代表 其他包。
    扩展包本身称为 或SCR。

    术语DS和SCR有时会混淆:
    DS是规范,SCR是实现规范的实际捆绑包

    OSGiR4.2中的DS有显著的改进。

    • 创建组件。
    • 束缚
    • 管理组件的生命周期,以响应绑定服务的进出。
    • (可选)将组件本身发布为服务

    您仍然有MANIFEST.MF:

    Bundle-SymbolicName : mybundle
    Bundle-Version : 1.0.0
    Service-Component : OSGI-INF/minimal. xml
    

    您将使用:

    org.eclipse.equinox.ds <version>.jar
    org.eclipse.equinox.util <version>.jar
    

    SCR将自动找到激活/停用方法。
    我们可以通过向XML声明中添加属性来调用它们 .

    在R4.2之前,方法名无法更改,必须采用类型为的参数 ComponentContext ,来自DS API。这破坏了组件的性能。

    因此,与其拧一个“ BundleActivator “(组件中的OSGI特定API:BAD),您可以编写一个普通的POJO对象:

    public class PollingComponent {
        private static final int DEFAULT_PERIOD = 2000;
        private PollingThread thread ;
        protected void activate ( Map<String , Object> config ) {
            System.out.println( " Polling Component Activated " );
            Integer period = (Integer)config.get( " period " );
            thread = new PollingThread(
                period!=null ? period : DEFAULT_PERIOD);
            thread.start();
        }
        protected void deactivate() {
            System.out.println( " Polling Component Deactivated " );
            thread.interrupt();
        }
    }
    

    你只需要简单地 声明 您对其他服务的参考:

    <reference name="LOG"
        interface="org.osgi.service.log.LogService "
        bind="setLog" unbind="unsetLog"
        cardinality="0..1"/>
    

    您可以将组件作为服务本身发布:
    这是通过 <service>

    <service>
        <provide interface="net ... ContactRepository"/>
        <property name="foo" value="bar"
    </service>
    

    只需添加其他服务,即可提供多种服务 <provide> 元素。
    使用 <property>