欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
了解 Eclipse 插件如何使用 OSGi

了解 Eclipse 插件如何使用 OSGi

Eclipse 和 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)定義。





到 OSGi 的發(fā)展

如前所述,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ù)。





使用 OSGi 實(shí)現 Eclipse

在 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 插件中的代碼段
<?xml version="1.0" encoding="UTF-8"?>                        <?eclipse version="3.0"?>                        <plugin                        id="org.eclipse.pde.ui"                        name="%name"                        version="3.0.2"                        provider-name="%provider-name"                        class="org.eclipse.pde.internal.ui.PDEPlugin">                        <runtime>                        <library name="pdeui.jar">                        <export name="*"/>                        </library>                        </runtime>                        <requires>                        <import plugin="org.eclipse.core.runtime.compatibility"/>                        <import plugin="org.eclipse.ui.ide"/>                        <import plugin="org.eclipse.ui.views"/>                        <import plugin="org.eclipse.jface.text"/>                        <import plugin="org.eclipse.ui.workbench.texteditor"/>                        <import plugin="org.eclipse.ui.editors"/>                        <import plugin="org.eclipse.ant.core"/>                        <import plugin="org.eclipse.core.resources"/>                        <import plugin="org.eclipse.debug.core"/>                        <import plugin="org.eclipse.debug.ui"/>                        <import plugin="org.eclipse.help.base"/>                        <import plugin="org.eclipse.jdt.core"/>                        <import plugin="org.eclipse.jdt.debug.ui"/>                        <import plugin="org.eclipse.jdt.launching"/>                        <import plugin="org.eclipse.jdt.ui"/>                        <import plugin="org.eclipse.pde"/>                        <import plugin="org.eclipse.pde.build"/>                        <import plugin="org.eclipse.search"/>                        <import plugin="org.eclipse.team.core"/>                        <import plugin="org.eclipse.ui"/>                        <import plugin="org.eclipse.update.core"/>                        <import plugin="org.eclipse.ui.forms"/>                        <import plugin="org.eclipse.ant.ui"/>                        <import plugin="org.eclipse.jdt.junit"/>                        <import plugin="org.eclipse.ui.intro"/>                        <import plugin="org.eclipse.ui.cheatsheets"/>                        </requires>                        <!-- Extension points -->                        <extension-point id="pluginContent"                        name="%expoint.pluginContent.name"                        schema="schema/pluginContent.exsd"/>                        <extension-point id="newExtension"                        name="%expoint.newExtension.name"                        schema="schema/newExtension.exsd"/>                        <extension-point id="templates"                        name="%expoint.templates.name"                        schema="schema/templates.exsd"/>                        <extension-point id="samples"                        name="%expoint.samples.name"                        schema="schema/samples.exsd"/>                        <!-- Extensions -->                        <extension                        point="org.eclipse.ui.perspectives">                        <perspective                        name="%perspective.name"                        icon="icons/eview16/plugins.gif"                        class="org.eclipse.pde.internal.ui.PDEPerspective"                        id="org.eclipse.pde.ui.PDEPerspective">                        </perspective>                        </extension>                        

<export name="*"/> 聲明暴露了插件中的所有包以供其他插件使用。插件依賴(lài)關(guān)系導入部分列出了 org.eclipse.pde.ui 插件需要的必備插件。

接下來(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
<?xml version="1.0" encoding="UTF-8"?>                        <?eclipse version="3.0"?>                        <plugin>                        <!-- Extension points -->                        <extension-point id="pluginContent"                        name="%expoint.pluginContent.name"                        schema="schema/pluginContent.exsd"/>                        <extension-point id="newExtension"                        name="%expoint.newExtension.name"                        schema="schema/newExtension.exsd"/>                        <extension-point id="templates"                        name="%expoint.templates.name"                        schema="schema/templates.exsd"/>                        <extension-point id="samples"                        name="%expoint.samples.name"                        schema="schema/samples.exsd"/>                        <!-- Extensions -->                        <extension                        point="org.eclipse.ui.perspectives">                        <perspective                        name="%perspective.name"                        icon="icons/eview16/plugins.gif"                        class="org.eclipse.pde.internal.ui.PDEPerspective"                        id="org.eclipse.pde.ui.PDEPerspective">                        </perspective>                        

注意,導出和導入信息不見(jiàn)了。該信息現在位于清單 3 所示的 manifest.mf 文件中。


清單 3. Manifest.mf
Manifest-Version: 1.0                        Bundle-Name: %name                        Bundle-SymbolicName: org.eclipse.pde.ui; singleton:=true                        Bundle-Version: 3.1.0                        Bundle-ClassPath: org.eclipse.pde.ui_3.1.0.jar                        Bundle-Activator: org.eclipse.pde.internal.ui.PDEPlugin                        Bundle-Vendor: %provider-name                        Bundle-Localization: plugin                        Require-Bundle: org.eclipse.core.runtime,                        org.eclipse.ui.ide,                        org.eclipse.ui.views,                        org.eclipse.jface.text,                        org.eclipse.ui.workbench.texteditor,                        org.eclipse.ui.editors,                        org.eclipse.ant.core,                        org.eclipse.core.resources,                        org.eclipse.debug.core,                        org.eclipse.debug.ui,                        org.eclipse.jdt.core,                        org.eclipse.jdt.debug.ui,                        org.eclipse.jdt.launching,                        org.eclipse.jdt.ui,                        org.eclipse.pde,                        org.eclipse.pde.build,                        org.eclipse.search,                        org.eclipse.team.core,                        org.eclipse.ui,                        org.eclipse.update.core,                        org.eclipse.ui.forms,                        org.eclipse.ant.ui,                        org.eclipse.jdt.junit,                        org.eclipse.ui.intro,                        org.eclipse.ui.cheatsheets,                        org.eclipse.update.configurator,                        org.eclipse.help.base                        Bundle-ManifestVersion: 2                        Eclipse-AutoStart: true                        Export-Package: org.eclipse.pde.internal.ui;x-internal:=true,                        org.eclipse.pde.internal.ui.build;x-internal:=true,                        . . .                        org.eclipse.pde.ui,                        org.eclipse.pde.ui.internal.samples;x-internal:=true,                        org.eclipse.pde.ui.templates                        

各種插件導入現在被指定為必需的綁定包,* 包導出已經(jīng)替換為顯式導出的包列表。

插件級的依賴(lài)關(guān)系改為需要顯式導出和導入包的依賴(lài)關(guān)系,當 Eclipse 宣布這個(gè)消息時(shí),曾引起大量騷動(dòng)。主要抱怨的是缺乏已經(jīng)存在于 Eclipse 早期版本中的 <export name="*"/> 的替代物。但是,該省略有許多原因。最重要的原因是從顯式導入和導出中獲得的速度收益。早期版本的 Eclipse 必須打開(kāi)并瀏覽每個(gè)插件 jar 文件以確定它包含哪些類(lèi)。不包括 * 導出還提供了一級保護來(lái)避免插件暴露不必要的類(lèi)。插件開(kāi)發(fā)人員必須進(jìn)行專(zhuān)門(mén)選擇來(lái)使插件中的功能可供外部使用。該限制允許內部包保留在內部。





OSGi 清單選項

OSGi R4 框架核心目前的規范草案幾乎有 PDF 格式的 300 頁(yè)。介紹該規范的每個(gè)部分超出了本文范圍,但我將討論 Eclipse 插件開(kāi)發(fā)人員特別感興趣的 OSGi manifest.mf 選項:

Bundle-Activator
該類(lèi)用于啟動(dòng)和停止綁定包。在上面的示例插件中,指定了 org.eclipse.pde.internal.ui.PDEPlugin 類(lèi)。該類(lèi)擴展 org.eclipse.core.runtime.Plugin,實(shí)現了 BundleActivator 接口。
Bundle-ClassPath
該屬性指定要用于綁定包的 CLASSPATH。該屬性可以包含對綁定包 jar 文件中目錄或 jar 文件的引用??梢允褂镁潼c(diǎn)指明綁定包的根。在示例 Eclipse PDE 綁定包中,指定了綁定包 jar 文件中的 org.eclipse.pde.ui_3.1.0.jar。如果將插件的源版本導入工作區中,導入過(guò)程將更改綁定包 CLASSPATH 以顯示為 Bundle-ClassPath:,這允許插件的開(kāi)發(fā)版本挑選已編譯的綁定包類(lèi)。
Bundle-Version
該屬性指定綁定包的版本號。包導入和必需的綁定包規范可以包括綁定包版本號。
Export-Package
該屬性指定要公共暴露給其他插件的所有包。
Import-Package
該屬性指定要從必需插件中顯式導入的所有包。默認情況下,必須為要啟動(dòng)的綁定包解析所有包。還可以將包導入指定為可選項,以支持包不存在的情況。顯式導入的類(lèi)在 Require-Bundle 插件中的包之前解析。
Require-Bundle
該屬性指定要在給定綁定包中導入使用的綁定包及其已導出的包。指定的綁定包在顯式包導入之后解析。

Eclipse 提供的其他清單選項

伙伴類(lèi)加載器選項

首先為 Hibernate 創(chuàng )建插件。然后創(chuàng )建一個(gè)插件,其中包含與 Hibernate 有依賴(lài)關(guān)系的特定于域的類(lèi)。將下列行添加到 Hibernate 插件清單中:Eclipse-BuddyPolicy: registered。

將下列清單屬性添加到包含特定于域的類(lèi)或資源的插件清單中: Eclipse-RegisterBuddy: hibernate。

該行允許插件通過(guò)聲明將自己暴露給 Hibernate 插件,而它預先并不知道這些插件?,F在,Hibernate 插件可以看到需要的類(lèi),雖然它并沒(méi)有專(zhuān)門(mén)導入它們。

OSGi 規范包括的 manifest.mf 配置選項不提供 Eclipse 平臺需要的所有功能。因此,Eclipse 創(chuàng )建者添加了多個(gè)擴展(還建議將它們包括在未來(lái)版本的 OSGi 規范中):

Export-Package 頭擴展
Eclipse 具有兩個(gè) OSGi 解析器方法 —— defaultstrict,可以使用 osgi.resolver 屬性指定它們。Eclipse 還包括對 Export-Package 屬性的兩個(gè)擴展 —— x-internalx-friends,啟用 Strict 模式時(shí),會(huì )強制執行這兩個(gè)擴展。
x-internal
該屬性的默認值是 false。當使用該選項將內部包指定為 true 時(shí),Eclipse PDE 禁止其使用。
x-friends
該選項類(lèi)似于 x-internal,但允許特定綁定包使用具有該選項的已導出包。其他綁定包被禁止。x-internal 選項優(yōu)先于 x-friends。
Eclipse-AutoStart
默認情況下,Eclipse 根據需要加載綁定包。因此,當導入綁定包包含的第一個(gè)類(lèi)的綁定包需要這個(gè)類(lèi)時(shí),就會(huì )加載這些綁定包。將該值指定為 ?? 會(huì )導致 Eclipse 在啟動(dòng)時(shí)加載綁定包。還可以指定例外情況列表,它們是無(wú)需啟動(dòng)包含它們的綁定包就可以加載的類(lèi)和資源。
Eclipse-PlatformFilter
該屬性允許為要啟動(dòng)的綁定包指定必須等于 true 的條件??梢詫⑾铝行畔ㄔ谥付ǖ谋磉_式中:
  • osgi.nl,表示語(yǔ)言
  • osgi.os,表示操作系統
  • osgi.arch,表示架構
  • osgi.ws,表示窗口系統
展示如何使用該屬性的一個(gè)示例是,在啟動(dòng)使用 SWT_AWT 橋的插件之前驗證操作系統是否是 Mac OS X。(Standard Widget Toolkit(SWT)的 Mac OS X 實(shí)現當前不支持該功能。)
Eclipse-BuddyPolicy
該選項指定加載綁定包策略的類(lèi)。通常,綁定包只在其內部類(lèi)和從依賴(lài)綁定包中導入的內部類(lèi)中具有可見(jiàn)性。在 Eclipse 新聞組中用來(lái)解釋伙伴類(lèi)加載的流行示例是 Hibernate。Hibernate 框架必須查看用戶(hù)創(chuàng )建的而非 Hibernate 本身一部分的類(lèi)和資源。這樣的一種情況是當使用項目動(dòng)態(tài)填充來(lái)自 Hibernate Query Language(HQL)查詢(xún)的類(lèi)時(shí)。默認情況下,Hibernate 將無(wú)法查看位于包含 Hibernate jar 文件的插件外部的類(lèi),而需要修改 Hibernate 插件以創(chuàng )建包含 Hibernate 地圖不可接受的類(lèi)的每個(gè)插件。幸運的是,伙伴類(lèi)加載器選項 一節中介紹的伙伴類(lèi)加載器選項解決了這個(gè)問(wèn)題。




Eclipse 和 OSGi 的未來(lái)趨勢

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ù)
  • 請用 IBM 試用軟件 改進(jìn)您的下一個(gè)開(kāi)放源碼開(kāi)發(fā)項目,這些軟件可以下載或從 DVD 獲得。


討論


關(guān)于作者

Scott Delap 是專(zhuān)注于 Java EE 和富 Java 客戶(hù)機的獨立顧問(wèn)。他在 JavaOne 撰寫(xiě)文章并積極活躍于桌面 Java 社區。他還是 ClientJava.com 的管理員,該網(wǎng)站是專(zhuān)攻桌面 Java 開(kāi)發(fā)的門(mén)戶(hù)網(wǎng)站。ClientJava.com 頻繁出現于 Web 上,從 JavaBlogs 一直到 Sun Microsystems 的網(wǎng)站。

Understanding how Eclipse plug-ins work with OSGi

Eclipse and OSGi from A to Z, or in this case, plugin.xml to manifest.mf

Document options

Print this page

E-mail this page


Rate this page

Help us improve this content


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.

What is a plug-in?

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.



Back to top


The evolution to OSGi

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.



Back to top


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
<?xml version="1.0" encoding="UTF-8"?>                        <?eclipse version="3.0"?>                        <plugin                        id="org.eclipse.pde.ui"                        name="%name"                        version="3.0.2"                        provider-name="%provider-name"                        class="org.eclipse.pde.internal.ui.PDEPlugin">                        <runtime>                        <library name="pdeui.jar">                        <export name="*"/>                        </library>                        </runtime>                        <requires>                        <import plugin="org.eclipse.core.runtime.compatibility"/>                        <import plugin="org.eclipse.ui.ide"/>                        <import plugin="org.eclipse.ui.views"/>                        <import plugin="org.eclipse.jface.text"/>                        <import plugin="org.eclipse.ui.workbench.texteditor"/>                        <import plugin="org.eclipse.ui.editors"/>                        <import plugin="org.eclipse.ant.core"/>                        <import plugin="org.eclipse.core.resources"/>                        <import plugin="org.eclipse.debug.core"/>                        <import plugin="org.eclipse.debug.ui"/>                        <import plugin="org.eclipse.help.base"/>                        <import plugin="org.eclipse.jdt.core"/>                        <import plugin="org.eclipse.jdt.debug.ui"/>                        <import plugin="org.eclipse.jdt.launching"/>                        <import plugin="org.eclipse.jdt.ui"/>                        <import plugin="org.eclipse.pde"/>                        <import plugin="org.eclipse.pde.build"/>                        <import plugin="org.eclipse.search"/>                        <import plugin="org.eclipse.team.core"/>                        <import plugin="org.eclipse.ui"/>                        <import plugin="org.eclipse.update.core"/>                        <import plugin="org.eclipse.ui.forms"/>                        <import plugin="org.eclipse.ant.ui"/>                        <import plugin="org.eclipse.jdt.junit"/>                        <import plugin="org.eclipse.ui.intro"/>                        <import plugin="org.eclipse.ui.cheatsheets"/>                        </requires>                        <!-- Extension points -->                        <extension-point id="pluginContent"                        name="%expoint.pluginContent.name"                        schema="schema/pluginContent.exsd"/>                        <extension-point id="newExtension"                        name="%expoint.newExtension.name"                        schema="schema/newExtension.exsd"/>                        <extension-point id="templates"                        name="%expoint.templates.name"                        schema="schema/templates.exsd"/>                        <extension-point id="samples"                        name="%expoint.samples.name"                        schema="schema/samples.exsd"/>                        <!-- Extensions -->                        <extension                        point="org.eclipse.ui.perspectives">                        <perspective                        name="%perspective.name"                        icon="icons/eview16/plugins.gif"                        class="org.eclipse.pde.internal.ui.PDEPerspective"                        id="org.eclipse.pde.ui.PDEPerspective">                        </perspective>                        </extension>                        

The <export name="*"/> declaration exposes all packages in the plug-in for use in other plug-ins. The plug-in dependency imports section lists the prerequisite plug-ins the org.eclipse.pde.ui plug-in requires.

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
<?xml version="1.0" encoding="UTF-8"?>                        <?eclipse version="3.0"?>                        <plugin>                        <!-- Extension points -->                        <extension-point id="pluginContent"                        name="%expoint.pluginContent.name"                        schema="schema/pluginContent.exsd"/>                        <extension-point id="newExtension"                        name="%expoint.newExtension.name"                        schema="schema/newExtension.exsd"/>                        <extension-point id="templates"                        name="%expoint.templates.name"                        schema="schema/templates.exsd"/>                        <extension-point id="samples"                        name="%expoint.samples.name"                        schema="schema/samples.exsd"/>                        <!-- Extensions -->                        <extension                        point="org.eclipse.ui.perspectives">                        <perspective                        name="%perspective.name"                        icon="icons/eview16/plugins.gif"                        class="org.eclipse.pde.internal.ui.PDEPerspective"                        id="org.eclipse.pde.ui.PDEPerspective">                        </perspective>                        

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
Manifest-Version: 1.0                        Bundle-Name: %name                        Bundle-SymbolicName: org.eclipse.pde.ui; singleton:=true                        Bundle-Version: 3.1.0                        Bundle-ClassPath: org.eclipse.pde.ui_3.1.0.jar                        Bundle-Activator: org.eclipse.pde.internal.ui.PDEPlugin                        Bundle-Vendor: %provider-name                        Bundle-Localization: plugin                        Require-Bundle: org.eclipse.core.runtime,                        org.eclipse.ui.ide,                        org.eclipse.ui.views,                        org.eclipse.jface.text,                        org.eclipse.ui.workbench.texteditor,                        org.eclipse.ui.editors,                        org.eclipse.ant.core,                        org.eclipse.core.resources,                        org.eclipse.debug.core,                        org.eclipse.debug.ui,                        org.eclipse.jdt.core,                        org.eclipse.jdt.debug.ui,                        org.eclipse.jdt.launching,                        org.eclipse.jdt.ui,                        org.eclipse.pde,                        org.eclipse.pde.build,                        org.eclipse.search,                        org.eclipse.team.core,                        org.eclipse.ui,                        org.eclipse.update.core,                        org.eclipse.ui.forms,                        org.eclipse.ant.ui,                        org.eclipse.jdt.junit,                        org.eclipse.ui.intro,                        org.eclipse.ui.cheatsheets,                        org.eclipse.update.configurator,                        org.eclipse.help.base                        Bundle-ManifestVersion: 2                        Eclipse-AutoStart: true                        Export-Package: org.eclipse.pde.internal.ui;x-internal:=true,                        org.eclipse.pde.internal.ui.build;x-internal:=true,                        . . .                        org.eclipse.pde.ui,                        org.eclipse.pde.ui.internal.samples;x-internal:=true,                        org.eclipse.pde.ui.templates                        

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 <export name="*"/>, which had existed in earlier versions of Eclipse. There are many reasons for this omission, however. The most important reason is the speed gains from having explicit import and exports. Previous versions of Eclipse had to open and scan each plug-in jar file to determine which classes it contained. Not including a * export also provides a level of protection against plug-ins exposing unwanted classes. Plug-in developers must make a conscious choice to make functionality in a plug-in available for outside use. This restriction allows internal packages to stay internal.



Back to top


OSGi manifest options

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:

Bundle-Activator
This class is used to start and stop the bundle. In the example plug-in above, the org.eclipse.pde.internal.ui.PDEPlugin class is specified. This class extends org.eclipse.core.runtime.Plugin, which implements the BundleActivator interface.
Bundle-ClassPath
This property specifies the CLASSPATH to use for the bundle. The property may contain references to directories or jar files inside the bundle jar file. You can use the period to indicate the bundle's root. In the case of the example Eclipse PDE bundle, org.eclipse.pde.ui_3.1.0.jar in the bundle jar file is specified. If you import the source version of the plug-in into your workspace, the import process changes the bundle CLASSPATH to appear as Bundle-ClassPath:, which allows the development version of the plug-in to pick up the compiled bundle classes.
Bundle-Version
This property specifies the version number of the bundle. Package imports and required bundle specifications may include a bundle version number.
Export-Package
This property specifies all the packages to publicly expose to other plug-ins.
Import-Package
This property specifies all the packages to explicitly import from required plug-ins. By default, all packages must be resolved for a bundle to start. You can also specify package imports as optional to support cases in which a package may not exist. Explicitly imported classes are resolved before packages from Require-Bundle plug-ins.
Require-Bundle
This property specifies which bundles and their exported packages to import for use in the given bundle. Specified bundles are evaluated after explicit package imports.

Additional manifest options provided by Eclipse

Buddy class loader options

Begin by creating a plug-in for Hibernate. Then create a plug-in containing the domain-specific classes with a dependency on Hibernate. Add the following line to the Hibernate plug-in manifest: Eclipse-BuddyPolicy: registered.

Add the following manifest property to the manifest of the plug-in containing the domain-specific classes or resources: Eclipse-RegisterBuddy: hibernate.

This line allows plug-ins to declaratively expose themselves to the Hibernate plug-in without it knowing about them beforehand. Now the Hibernate plug-in can see the needed classes even though it did not specifically import them.

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):

Export-Package Header Extensions
Eclipse has two OSGi resolver methods -- default and strict -- that the osgi.resolver property can specify. Eclipse also includes two extensions to the Export-Package property -- x-internal and x-friends -- both of which are enforced when you enable Strict mode.
x-internal
The default value for this property is false. When internal packages are specified as true using this option, the Eclipse PDE discourages their use.
x-friends
This option is similar to x-internal, but allows certain bundles to use the exported packages that have this option. Others bundles are discouraged. The x-internal option takes precedence over x-friends.
Eclipse-AutoStart
By default, Eclipse loads bundles on demand. Therefore, bundles are loaded when the first class they contain is required by a bundle that imports it. Specifying this value as ?? causes Eclipse to load the bundle at startup. You can also specify a list of exceptions, which are classes, and resources, and which may be loaded without starting their containing bundle.
Eclipse-PlatformFilter
This property allows you to specify conditions that must evaluate to true for a bundle to start. You can include the following information in the expression you specify:
  • osgi.nl for language
  • osgi.os for operating system
  • osgi.arch for architecture
  • osgi.ws for windowing system
An example of how to use this property is to verify whether the operating system is not Mac OS X before starting a plug-in that uses the SWT_AWT bridge. (The Mac OS X implementation of the Standard Widget Toolkit (SWT) doesn't currently support this feature.)
Eclipse-BuddyPolicy
This option specifies the class loading policy of the bundle. Typically, bundles only have visibility in their internal classes and those imported from dependent bundles. The popular example that has been used in the Eclipse newsgroups to explain buddy class loading is Hibernate. The Hibernate framework must see classes and resources that are user-created and not part of Hibernate itself. One such case is when you use a project to dynamically fill a class from a Hibernate Query Language (HQL) query. By default, Hibernate won't be able to see classes outside the plug-in containing the Hibernate jar files, and requiring modification of the Hibernate plug-in for the creation of each plug-in containing classes that Hibernate maps is not acceptable. Fortunately, you can solve this using the buddy class loader options explained in the Buddy class loader options section.


Back to top


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.



Resources

Learn

Get products and technologies
  • Innovate your next open source development project with IBM trial software, available for download or on DVD.


Discuss

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Eclipse插件開(kāi)發(fā)之基礎篇(2) 第一個(gè)Eclipse插件
基于OSGI的Cache組件的實(shí)現
CSDN技術(shù)中心 教您如何創(chuàng )建、調試和安裝Eclipse插件
org.eclipse.ui.plugin.AbstractUIPlugin 插件
eclipse入門(mén):開(kāi)發(fā)eclipse插件
Equinox Launcher的啟動(dòng)配置參數
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久