Osgi Intro

Post on 30-Aug-2014

2.786 views 2 download

Tags:

description

 

Transcript of Osgi Intro

1

Ching Yi, Chan (qrtt1)

OSGi 入門與簡介

2

WHAT IS OSGi ?

The OSGi™ specifications define a standardized, component-oriented, computing environment for networked services that is the foundation of an enhanced service-oriented architecture. Adding an OSGi Service Platform to a networked device (embedded as well as servers), adds the capability to manage the lifecycle of the software components in the device from anywhere in the network. Software components can be installed, updated, or removed on the fly without ever having to disrupt the operation of the device.

3

WHAT IS OSGi ?

The OSGi™ specifications define a standardized, component-oriented, computing environment for networked services that is the foundation of an enhanced service-oriented architecture. Adding an OSGi Service Platform to a networked device (embedded as well as servers), adds the capability to manage the lifecycle of the software components in the device from anywhere in the network. Software components can be installed, updated, or removed on the fly without ever having to disrupt the operation of the device.

WHO CARE ?

4

WHAT IS OSGi ?

5

今日焦點• 定義少一點• 心情好一點• 範例懂一點• 知識長一點

略懂

6

OSGi is …

Service Platform

7

Service Platform

• 以「服務」「模組」建構系統• 適合開發服務導向架構 (SOA) 的平台• 您可以使用 OSGi

– 發佈「服務」– 使用「服務」– 尋找「服務」

8

OSGi Services Example

• 縮短網址的服務– 以共通的 interface 發佈服務– 允許同時存在不同的實作– 可以即時選擇要使用的服務– 可以知道服務的狀態

9

Where is Service ?• Service 建構在 Module 層之上• 受 Module Life cycle 影響• 在 Module 內使用 ServiceRegistry 註冊 Service• 那麼, Bundle(App) 就能使用 Service

10

Quick Tutorial In Bundle

• OSGi 模組的基本單位• 建立 Bundle 的步驟

– 寫一些類別 ! ?– 在 META/MANIFEST.MF 填些什麼?– 包成 JAR ! ?

11

Hello BundleActivator & MANIFEST.MF

• Activator/hook method– Start– Stop

• MANIFEST.MF

Make jug.hello bundle with Activator print hello & bye. Run and use start/stop.

12

Bundle - visibility

• 預設的 Bundle 是個黑盒子– 完整地保護– 無法窺視內部

• Reflection• Classloader trickery

13

Bundle - visibility

• 透過 Export-Package 分享 package– Export-Package: jug.hello.api

jug.hello.api

14

Bundle - visibility

• 透過 Import-Package 使用 package– Import-Package: jug.hello.api

jug.hello.api

BA

import jug.hello.api.HelloWorld;

class Foo{

public void hello(){

new HelloWorld().hello();

}

}

15

Bundle – package propety

• Bundle: jug.hello.api.v1– Export-Package:

• jug.hello.api;version="1.0.0"

• Bundle: jug.hello.api.v2– Export-Package:

• jug.hello.api;version=“2.0.0"

16

Export-Package:jug.hello.api;version=“1.0.0”

Export-Package: jug.hello.api;version=“2.0.0”

jug.hello.api.v1

jug.hello.api.v2

jug.hello

Import-Package: jug.hello.api

版本高者優先

Demo hello.api later

17

Export-Package:jug.hello.api;version=“1.0.0”

Export-Package: jug.hello.api;version=“2.0.0”

jug.hello.api.v1

jug.hello.api.v2

jug.hello

Import-Package:jug.hello.api;version=“[1.0.0,1.5.0]”

選則偏好的版本

Demo hello.api later

18

Life Cycle

INSTALLED

RESOLVED

UNINSTALLED

ACTIVE

STOPPING

STARTING

start

stop

Demo hello.api later

19

Life Cycle

Change System Behavior In Runtime

Demo remote

Demo jug.hello.api (change import version)

20

OSGi Service & URL Shorten Bundle

21

Service Bundle

• shorten.api– 僅提供 interface

• shorten.client– Swing UI 使用者互動介面

• shorten.orz– 0rz.tw 短址服服

22

Service Registry

• BundleContext.registerService()

ServiceRegistration registration;

Properties prop = new Properties();prop.setProperty("name", "0rz.tw");

registration = context.registerService(IShortenURL.class.getName(), new OrzURL(), prop);

23

Find All Service

ServiceReference[] refs = context.getServiceReferences( IShortenURL.class.getName(), null );

• BundleContext.getServiceReferences()

24

Service Filter (LDAP Filter)context.getAllServiceReferences( IShortenURL.class.getName(), String.format("(name=%s)", name) );

“(name=0rz.tw)”

“(name=tinyurl)”

過濾含有屬性 name ,且值為 0rz.tw 的 Service

過濾含有屬性 name ,且值為 tinyurl 的 Service

25

Service Filter (LDAP Filter)選擇具有 cn 屬性且值為 Babs Jensen 者"(cn=Babs Jensen)"

以 ! 排除具有 cn 屬性且值為 Tim Howes 者"(!(cn=Tim Howes))"

選擇具有 objectclass 屬性且值為 Persion 並且不含屬性 sn 值 Jensen 與屬性 cn 且值為 Babs J 開頭者"(& (" + Constants.OBJECTCLASS + "=Person) (|(sn=Jensen)(cn=Babs J*)))"

"(o=univ*of*mich*)"

26

Service Tracker• Monitor Service come and go• ServiceTracker

– addingService()• 當 Service 被發佈時觸發

– removedService()• 當 Service 被終止時觸發

27

tracker = new ServiceTracker(context, IShortenURL.class.getName(), null){ public Object addingService(ServiceReference reference) { ui.resetAvailableServiceList(context); ui.updateStatus(String.format("Service [%s] is available.", reference.getProperty("name"))); return super.addingService(reference); } public void removedService(ServiceReference reference, Object service) { ui.resetAvailableServiceList(context); ui.updateStatus(String.format("Service [%s] is unavailable.", reference.getProperty("name"))); super.removedService(reference, service); }};tracker.open();

28

Demo Service Gone

29

Demo Service Back

30

你需要 OSGi 嗎?• 你的系統需要執行期改變行為嗎?• 你的系統提供的服務經常變動嗎?

31

導入 OSGi 的問題• 沿用 META/MANIFEST.MF, BUT …

– 「手工」撰寫 MANIFEST.MF 是自虐。– 有單行長度限制– 最後一行需要空行

• Metadata 需要填的內容可能很多– Import-Package– Export-Package– …

32

Manifest-Version: 1.0Created-By: 1.5.0_12 (Sun Microsystems Inc.)Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txtImport-Package: com.thoughtworks.xstream;version="[1.2.2, 2.0.0)";reso lution:=optional,com.thoughtworks.xstream.annotations;version="[1.2.2 , 2.0.0)";resolution:=optional,com.thoughtworks.xstream.converters;ve rsion="[1.2.2, 2.0.0)";resolution:=optional,com.thoughtworks.xstream. io;version="[1.2.2, 2.0.0)";resolution:=optional,com.thoughtworks.xst ream.io.json;version="[1.2.2, 2.0.0)";resolution:=optional,com.though tworks.xstream.io.xml;version="[1.2.2, 2.0.0)";resolution:=optional,j avax.jms;version="[1.1.0, 2.0.0)",javax.management,javax.management.j 2ee.statistics;version="[1.0.1, 2.0.0)",javax.management.openmbean,ja vax.management.remote,javax.naming,javax.naming.directory,javax.namin g.event,javax.naming.spi,javax.net,javax.net.ssl,javax.security.auth, javax.security.auth.callback,javax.security.auth.login,javax.security .auth.spi,javax.sql,javax.transaction;version="[1.0.1, 2.0.0)";resolu tion:=optional,javax.transaction.xa;version="[1.0.1, 2.0.0)";resoluti on:=optional,javax.xml.parsers,org.apache.activeio.journal;version="[ 3.1.0, 4.0.0)";resolution:=optional,org.apache.activeio.journal.activ e;version="[3.1.0, 4.0.0)";resolution:=optional,org.apache.activeio.p acket;version="[3.1.0, 4.0.0)";resolution:=optional,org.apache.camel; version="[1.3.0, 2.0.0)";resolution:=optional,org.apache.camel.compon

33

導入 OSGi 的問題• Bundle ClassLoader 真麻煩 !?

– 我想要把舊的專案搬到 OSGi 上?• 這是可行的• 但是,所有使用到的 Library 都必需轉成 Bundle 並且設 Export-Package

–然後,你會開始抱怨。• OSGi Sucks.• Daxx it. OSGi.

34

導入 OSGi 的問題• JARs Bundles

– 使用到的 Library 都已經轉成 OSGi Bundle 了嗎?• http://www.springsource.com/repository/app

– 可能將它們的轉成 OSGi Bundle 嗎?• http://aqute.biz/Code/Bnd• http://wiki.ops4j.org/confluence/display/ops4j/Pax

– 能否單純以 JAR 的型式使用• Bundle-ClassPath• Bundle-ClassPath + Façade Pattern OSGi Service

35

導入 OSGi 的問題• 問題在設計

– 以 Façade Pattern 隔離底層不需共用的 JARs

–管理 Bundle Dependency ,管制共用的 Bundle

• org.apache.commons.*• Spring

–採介面 / 實作分離的設計,以 Service 建構系統。

36

導入 OSGi 的問題• OSGi Run many app in single vm

– 需要禁止任何終止 VM 活動的程式

• 使用 SecurityManager (啟動 Security Layer 防護功能 )

System.exit(0);

37

Thank You.

Q & A