了解 Eclipse 插件如何使用 OSGiEclipse 和 OSGi 的關(guān)系,從 plugin.xml 到 manifest.mf ![]() |
![]() | 級別: 中級 Scott Delap (scott@clientjava.com), Desktop/Enterprise Java 顧問(wèn) 2006 年 9 月 21 日 Eclipse 集成開(kāi)發(fā)環(huán)境(IDE)和 Eclipse Rich Client Platform(RCP)應用程序的核心由 Open Services Gateway Initiative(OSGi)規范的實(shí)現驅動(dòng)。本文通過(guò)描述對 Eclipse 平臺而言插件是什么,并跟蹤從 Eclipse V2.1 到今天基于 OSGi 的實(shí)現中插件的發(fā)展,闡明了 Eclipse 與 OSGi 的關(guān)系。還解釋了 OSGi manifest.mf 文件選項以及通過(guò) Eclipse 提供的添加項。 大多數 Java™ 編程語(yǔ)言開(kāi)發(fā)人員通過(guò)作為 IDE 的功能認識了 Eclipse。Eclipse IDE 實(shí)際上由叫做插件 的交互式組件的集合組成。這些插件組成了 IDE 的基礎,它們還可用于創(chuàng )建其他桌面應用程序。創(chuàng )建基于 Eclipse 的應用程序所需的最小插件集稱(chēng)為 Eclipse Rich Client Platform(RCP)。但是,插件本身不能啟動(dòng)。它們需要在一個(gè)環(huán)境中啟動(dòng)和操作。Eclipse 使用 OSGi R4 規范的實(shí)現提供了該環(huán)境。 因為 Eclipse 在本質(zhì)上是由 OSGi 驅動(dòng)的,因此必須了解 Eclipse 插件的概念與 OSGi 框架有什么關(guān)系。在本文中,我將通過(guò)描述對 Eclipse 平臺而言插件是什么來(lái)詳細解釋這種關(guān)系。然后,將描述在 Eclipse V2.1 平臺到今天基于 OSGi 的實(shí)現中插件的發(fā)展。最后,將詳細介紹應用于 Eclipse 插件的 OSGi 提供的 manifest.mf 選項。 Eclipse 聯(lián)機幫助將插件定義為: “插件是為系統提供功能的代碼和/或數據的結構化包??梢砸源a庫(帶有公共 [應用程序接口] API 的 Java 類(lèi))、平臺擴展甚至文檔的形式來(lái)提供功能。插件可以定義擴展點(diǎn)、定義良好的位置,其他插件可以在這些位置添加功能。” 要注意的一個(gè)重點(diǎn)是插件以結構化方式提供功能。它們可以提供服務(wù)(比如日志)或可用于用戶(hù)界面(UI)的功能,比如編輯器。不管什么功能,所有插件都以相同的結構化方式來(lái)定義。
如前所述,Eclipse 使用 OSGi 作為插件系統的基礎。但并非總是如此。早期版本的 Eclipse 也設計為插件集合,而且 Eclipse 包括自己專(zhuān)用的插件系統來(lái)管理交互。但是,隨著(zhù) Eclipse IDE 要求的增長(cháng),必須需要一個(gè)更強壯的解決方案。這個(gè)新系統的基本要求包括動(dòng)態(tài)添加新插件和停止現有插件的能力。經(jīng)過(guò)大量研究之后,Eclipse 創(chuàng )建者決定通過(guò)實(shí)現 OSGi 框架規范替換專(zhuān)用的插件框架。 OSGi 是服務(wù)平臺的規范。Eclipse 提供了該規范的許多可用實(shí)現之一,并用作最新 OSGi R4 規范的參考實(shí)現。OSGi 是基于 Java 的框架,旨在用于需要長(cháng)運行時(shí)間、動(dòng)態(tài)更新和對運行環(huán)境破壞最小的系統。起初,OSGi 旨在用于家庭自動(dòng)化和家庭網(wǎng)關(guān)設備。最近,從手機到汽車(chē)都發(fā)現了它的蹤跡。 在核心,OSGi 是一個(gè)組件和服務(wù)模型,如圖 1 所示。OSGi 規范定義了一個(gè)叫做綁定包 的模塊化單位。(在下文中,除非特別指明,Eclipse 術(shù)語(yǔ)插件 和 OSGi 術(shù)語(yǔ)綁定包 可交換使用,因為所有 Eclipse 插件現在都是 OSGi 綁定包。)OSGi 還提供了 Java Virtual Machine(JVM)級別的服務(wù)注冊,該綁定包可用于發(fā)布、發(fā)現和綁定至服務(wù)。 圖 1. 主機操作系統、Java 和 OSGi 中層的交互 ![]() OSGi 規范定義了綁定包生命周期的基礎架構和綁定包的交互方式。這些規則通過(guò)使用特殊 Java 類(lèi)加載器來(lái)強制執行。在一般 Java 應用程序中,CLASSPATH 中的所有類(lèi)都對所有其他類(lèi)可見(jiàn)。相反,OSGi 類(lèi)加載器基于 OSGi 規范和每個(gè)綁定包的 manifest.mf 文件中指定的選項(稍后將詳細介紹)來(lái)限制類(lèi)交互。 Eclipse IDE 使用圍繞模塊化和綁定包生命周期的一個(gè) OSGi 子集【Eclipse用到了一個(gè)OSGI的子集,這個(gè)子集的核心是1.模塊化2.綁定申明周期】。但是,它最低限度地使用了 OSGi 提供的服務(wù)支持。相反,Eclipse 提供自己的擴展點(diǎn)系統來(lái)啟用綁定包交互。綁定包將功能暴露給其他擴展。綁定包還定義自己的擴展點(diǎn),允許其他綁定包向其貢獻功能。使用 Eclipse 中擴展點(diǎn)的一個(gè)示例是 Preferences 窗口。核心 Eclipse 插件提供中央窗口,并暴露擴展點(diǎn)以允許其他首選項頁(yè)面的貢獻。當插件添加到 Eclipse 中時(shí),它們可以貢獻它們自己的頁(yè)面。Eclipse 中擴展點(diǎn)的模型不同于基本的 OSGi 服務(wù)。綁定包擴展點(diǎn)由定義綁定包擁有;其他綁定包只對這些點(diǎn)做貢獻。相反,任何綁定包可以實(shí)現和使用 OSGi 服務(wù)。
在 3.1 之前版本的 Eclipse 中,在每個(gè)插件的 plugin.xml 文件中定義插件依賴(lài)關(guān)系以及擴展和擴展點(diǎn)。在使用 OSGi 的新版本 Eclipse 中,依賴(lài)關(guān)系信息被分解到 manifest.mf 文件中,而 plugin.xml 文件只包含擴展和擴展點(diǎn)的 XML 定義??匆粋€(gè)演示該發(fā)展的生動(dòng)的工作示例十分有用。清單 1 展示了 Eclipse V3.0 中 org.eclipse.pde.ui 插件的代碼段。 清單 1. org.eclipse.pde 插件中的代碼段
接下來(lái)兩部分定義了 org.eclipse.pde.ui 可用于其他插件的擴展點(diǎn)以及它對其他插件的貢獻。在本例中,可以看到自定義 Eclipse Plug-in Development Environment(PDE)視圖的定義。 下面來(lái)看 Eclipse V3.1 中的同一插件定義。清單 2 展示了 plugin.xml 文件。 清單 2. Plugin.xml
注意,導出和導入信息不見(jiàn)了。該信息現在位于清單 3 所示的 manifest.mf 文件中。 清單 3. Manifest.mf
各種插件導入現在被指定為必需的綁定包,* 包導出已經(jīng)替換為顯式導出的包列表。 插件級的依賴(lài)關(guān)系改為需要顯式導出和導入包的依賴(lài)關(guān)系,當 Eclipse 宣布這個(gè)消息時(shí),曾引起大量騷動(dòng)。主要抱怨的是缺乏已經(jīng)存在于 Eclipse 早期版本中的
OSGi R4 框架核心目前的規范草案幾乎有 PDF 格式的 300 頁(yè)。介紹該規范的每個(gè)部分超出了本文范圍,但我將討論 Eclipse 插件開(kāi)發(fā)人員特別感興趣的 OSGi manifest.mf 選項:
OSGi 規范包括的 manifest.mf 配置選項不提供 Eclipse 平臺需要的所有功能。因此,Eclipse 創(chuàng )建者添加了多個(gè)擴展(還建議將它們包括在未來(lái)版本的 OSGi 規范中):
Eclipse 已經(jīng)從使用 OSGi 中大大受益,獲得了以動(dòng)態(tài)方式管理組件生命周期的一個(gè)健壯的系統。新的使用方法每天都在被發(fā)掘,比如服務(wù)器層特征 servlet、JavaServer Pages 以及 Eclipse 樣式插件中的其他 HTTP 資源。 Eclipse Foundation 已經(jīng)決定在驅動(dòng) OSGi 規范向前發(fā)展的過(guò)程中扮演關(guān)鍵角色,以便于自己和其他人利用 OSGi。在從專(zhuān)用 Eclipse 插件框架轉換到 OSGi 的過(guò)程中,對 OSGi 規范進(jìn)行了許多添加,這些添加成了 OSGi R4 規范發(fā)行版的一部分。因此,Eclipse Equinox 項目已經(jīng)成為不斷發(fā)展的 OSGi 參考實(shí)現。該實(shí)現以及用于管理發(fā)展 OSGi 的 Java Specification Request(JSR) 291 的創(chuàng )建,保證了 Eclipse/OSGi 合作伙伴關(guān)系將在未來(lái)幾年里不斷取得成功。 學(xué)習
獲得產(chǎn)品和技術(shù)
討論
| ||||||||||||||||
Understanding how Eclipse plug-ins work with OSGiEclipse and OSGi from A to Z, or in this case, plugin.xml to manifest.mf ![]() | ![]() |
![]() |
Level: Intermediate Scott Delap (scott@clientjava.com), Desktop/Enterprise Java Consultant 06 Jun 2006 The core of the Eclipse integrated development environment (IDE) and Eclipse Rich Client Platform (RCP) applications is driven by an implementation of the Open Services Gateway Initiative (OSGi) specification. This article illustrates Eclipse's relationship with OSGi by describing what a plug-in is in terms of the Eclipse platform and traces the evolution of plug-ins from Eclipse V2.1 through today's OSGi-based implementation. It also explains the OSGi manifest.mf file options, along with the additions provided through Eclipse. Most Java™ programming language developers are introduced to Eclipse through its function as an IDE. The Eclipse IDE actually consists of a collection of interacting components called plug-ins. These plug-ins, which make up the base of the IDE, can also be used to create other desktop applications. The minimal set of plug-ins needed to create an Eclipse-based application is referred to as the Eclipse Rich Client Platform (RCP). Plug-ins don't just start themselves, however. They require an environment in which to start and operate. Eclipse provides this environment with an implementation of the OSGi R4 specification. Because Eclipse is at its core driven by OSGi, it's important to understand how the concept of an Eclipse plug-in relates to the OSGi framework. In this article, I will explain this relationship in detail by describing what a plug-in is in terms of the Eclipse platform. Then I'll describe the evolution of plug-ins in the Eclipse V2.1 platform through today's OSGi-based implementation. Finally, the OSGi-provided manifest.mf options that apply to Eclipse plug-ins will be covered in detail. The Eclipse online help defines a plug-in as follows: "Plug-ins are structured bundles of code and/or data that contribute function to the system. Function can be contributed in the form of code libraries (Java classes with public [application program interfaces] APIs), platform extensions, or even documentation. Plug-ins can define extension points, well-defined places where other plug-ins can add functionality." The key point to focus on is that plug-ins contribute function in a structured manner. They may provide a service such as logging or piece functionality available in the user interface (UI), such as an editor. Regardless of their function, all plug-ins are defined in the same structured way.
As mentioned, Eclipse uses OSGi as the basis for its plug-in system. This wasn't always the case, however. Early versions of Eclipse were also designed as a collection of plug-ins, and Eclipse included its own propriety plug-in system to manage the interaction. However, as the requirements of the Eclipse IDE grew, it became apparent that a more robust solution was required. Basic requirements of this new system included the ability to dynamically handle the addition of new plug-ins and stopping existing plug-ins. After considerable research, the Eclipse creators decided to replace the proprietary plug-in framework by implementing the OSGi framework specification. OSGi is a specification of a service platform. Eclipse provides one of many available implementations of this specification and serves as the reference implementation of the latest OSGi R4 specification. OSGi is a Java-based framework targeted for use by systems that require long running times, dynamic updates, and minimal disruptions to the running environment. Initially, OSGi was targeted at home automation and residential gateway devices. More recently, it has found use in everything from mobile phones to cars. At its core, OSGi is a component and service model, as illustrated in Figure 1. The OSGi specification defines a unit of modularization called a bundle. (Unless explicitly noted during the rest of this article, the Eclipse term plug-in and the OSGi term bundle are used interchangeably because all Eclipse plug-ins are now OSGi bundles.) OSGi also provides a Java Virtual Machine (JVM)-level service registry that bundles can use to publish, discover, and bind to services. Figure 1. The interaction of the layers in the host operating system, Java, and OSGi ![]() The OSGi specification defines an infrastructure 【基礎結構,基礎設施】for a bundle's life cycle and how bundles interact. These rules are enforced through the use of special Java class loaders. In an average Java application, all classes in the CLASSPATH are visible to all other classes. In contrast, the OSGi class loaders restrict class interaction among bundles based on the OSGi specification and the options specified (covered in detail later in this article) in the manifest.mf file for each bundle. 【OSGI中Bundle的相互作用跟Average java Application之間的區別,前者類(lèi)與類(lèi)之間發(fā)生作用,需要指定ClassPath,后者則通過(guò)Manifest來(lái)指定bundle之間怎么作用】 The Eclipse IDE uses a subset of OSGi centered around modularization and bundle life cycle. However, it makes minimal use of the service support OSGi provides. Instead, Eclipse provides its own extension point system to enable bundle interaction. Bundles expose functionality as contributions to other extensions. Bundles also define their own extension points, to which other bundles may contribute. An example of using extension points in Eclipse is the Preferences window. A core Eclipse plug-in provides the central window and exposes an extension point to allow the contribution of additional preference pages. As new plug-ins are added to Eclipse, they can contribute their own pages. The model of extension points in Eclipse is different from basic OSGi services. Bundle extension points are owned by the defining bundle; other bundles simply contribute to them. In comparison, any bundle can implement and use an OSGi service.
Implementing Eclipse using OSGi In versions of Eclipse before 3.1, you defined plug-in dependencies, as well as extensions and extension points, in each plug-in's plugin.xml file. In the newer versions of Eclipse that use OSGi, dependency information has been broken out into the manifest.mf file, leaving the plugin.xml file containing only XML definitions of extensions and extension points. It is useful to look at a live, working example of this evolution. Listing 1 shows an excerpt from the org.eclipse.pde.ui plug-in from Eclipse V3.0. Listing 1. Excerpt from the org.eclipse.pde plug-in
The The next two sections define the extension points org.eclipse.pde.ui makes available to other plug-ins, as well as the contributions it makes to them. In this case, you can see the definition of a custom Eclipse Plug-in Development Environment (PDE) perspective. Let's look at the same plug-in definition in Eclipse V3.1. Listing 2 shows the plugin.xml file. Listing 2. Plugin.xml
Notice that the export and import information is gone. This information now resides in the manifest.mf file shown in Listing 3. Listing 3. Manifest.mf
The various plug-in imports are now specified as required bundles, and the * package export has been replaced with a list of explicitly exported packages. The move away from plug-in-level dependencies to dependencies that required the explicit exporting and importing of packages generated a lot of commotion when Eclipse announced the news. The primary complaint was the lack of an equivalent of
The current draft specification for the OSGi R4 framework core is almost 300 pages in PDF form. Covering every portion of this specification is outside the scope of this article, but I do discuss the OSGi manifest.mf options of particular interest to Eclipse plug-in developers:
Additional manifest options provided by Eclipse
The manifest.mf configuration options that the OSGi specification includes don't provide all the functionality that the Eclipse platform requires. As a result, the Eclipse creators have added several extensions (and also proposed them for inclusion in future versions of the OSGi specification):
Future directions of Eclipse and OSGi Eclipse has benefited greatly in its use of OSGi, gaining a robust system managing the life cycle of components in a dynamic manner. New uses, such as dynamic Web Archive (WAR) files on the server tier featuring servlets, JavaServer Pages, and other HTTP resources in Eclipse-style plug-ins are being explored every day. The Eclipse Foundation has positioned itself to play a key role in driving the OSGi specification forward, both for its own uses and for those of other parties leveraging OSGi. During the transition from the propriety Eclipse plug-in framework to OSGi, many additions were made to the OSGi specification that became part of the OSGi R4 specification release. The Eclipse Equinox project has, as a result, become the OSGi reference implementation moving forward. This involvement, as well as the creation of Java Specification Request (JSR) 291 to manage the evolution OSGi, guarantees that the Eclipse/OSGi partnership will continue to be successful in the years ahead. Learn
Get products and technologies
Discuss
|
聯(lián)系客服