Osgi Intro

2,056 views
1,980 views

Published on

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,056
On SlideShare
0
From Embeds
0
Number of Embeds
236
Actions
Shares
0
Downloads
61
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Osgi Intro

  1. 1. Ching Yi, Chan (qrtt1) OSGi 入門與簡介
  2. 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. 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. 4. WHAT IS OSGi ?
  5. 5. 今日焦點 <ul><li>定義少一點 </li></ul><ul><li>心情好一點 </li></ul><ul><li>範例懂一點 </li></ul><ul><li>知識長一點 </li></ul>略懂
  6. 6. OSGi is … <ul><li>Service Platform </li></ul>
  7. 7. Service Platform <ul><li>以「服務」「模組」建構系統 </li></ul><ul><li>適合開發服務導向架構 (SOA) 的平台 </li></ul><ul><li>您可以使用 OSGi </li></ul><ul><ul><li>發佈「服務」 </li></ul></ul><ul><ul><li>使用「服務」 </li></ul></ul><ul><ul><li>尋找「服務」 </li></ul></ul>
  8. 8. OSGi Services Example <ul><li>縮短網址的服務 </li></ul><ul><ul><li>以共通的 interface 發佈服務 </li></ul></ul><ul><ul><li>允許同時存在不同的實作 </li></ul></ul><ul><ul><li>可以即時選擇要使用的服務 </li></ul></ul><ul><ul><li>可以知道服務的狀態 </li></ul></ul>
  9. 9. Where is Service ? <ul><li>Service 建構在 Module 層之上 </li></ul><ul><li>受 Module Life cycle 影響 </li></ul><ul><li>在 Module 內使用 ServiceRegistry 註冊 Service </li></ul><ul><li>那麼, Bundle(App) 就能使用 Service </li></ul>
  10. 10. Quick Tutorial In Bundle <ul><li>OSGi 模組的基本單位 </li></ul><ul><li>建立 Bundle 的步驟 </li></ul><ul><ul><li>寫一些類別 ! ? </li></ul></ul><ul><ul><li>在 META/MANIFEST.MF 填些什麼? </li></ul></ul><ul><ul><li>包成 JAR ! ? </li></ul></ul>
  11. 11. Hello Bundle Activator & MANIFEST.MF <ul><li>Activator/hook method </li></ul><ul><ul><li>Start </li></ul></ul><ul><ul><li>Stop </li></ul></ul><ul><li>MANIFEST.MF </li></ul>Make jug.hello bundle with Activator print hello & bye. Run and use start/stop.
  12. 12. Bundle - visibility <ul><li>預設的 Bundle 是個黑盒子 </li></ul><ul><ul><li>完整地保護 </li></ul></ul><ul><ul><li>無法窺視內部 </li></ul></ul><ul><ul><ul><li>Reflection </li></ul></ul></ul><ul><ul><ul><li>Classloader trickery </li></ul></ul></ul>
  13. 13. Bundle - visibility <ul><li>透過 Export-Package 分享 package </li></ul><ul><ul><li>Export-Package: jug.hello.api </li></ul></ul>jug.hello.api
  14. 14. Bundle - visibility <ul><li>透過 Import-Package 使用 package </li></ul><ul><ul><li>Import-Package: jug.hello.api </li></ul></ul>B A import jug.hello.api .HelloWorld; class Foo{ public void hello(){ new HelloWorld().hello(); } } jug.hello.api
  15. 15. Bundle – package propety <ul><li>Bundle: jug.hello.api.v1 </li></ul><ul><ul><li>Export-Package: </li></ul></ul><ul><ul><ul><li>jug.hello.api; version =&quot;1.0.0&quot; </li></ul></ul></ul><ul><li>Bundle: jug.hello.api.v2 </li></ul><ul><ul><li>Export-Package: </li></ul></ul><ul><ul><ul><li>jug.hello.api; version =“2.0.0&quot; </li></ul></ul></ul>
  16. 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. 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. 18. Life Cycle Demo hello.api later INSTALLED RESOLVED UNINSTALLED ACTIVE STOPPING STARTING start stop
  19. 19. Life Cycle <ul><li>Change </li></ul><ul><li>System Behavior </li></ul><ul><li>In Runtime </li></ul>Demo remote Demo jug.hello.api (change import version)
  20. 20. OSGi Service & URL Shorten Bundle
  21. 21. Service Bundle <ul><li>shorten.api </li></ul><ul><ul><li>僅提供 interface </li></ul></ul><ul><li>shorten.client </li></ul><ul><ul><li>Swing UI 使用者互動介面 </li></ul></ul><ul><li>shorten.orz </li></ul><ul><ul><li>0rz.tw 短址服服 </li></ul></ul>
  22. 22. Service Registry <ul><li>BundleContext. registerService() </li></ul>ServiceRegistration registration ; Properties prop = new Properties(); prop.setProperty(&quot;name&quot;, &quot;0rz.tw&quot;); registration = context.registerService ( IShortenURL.class.getName() , new OrzURL(), prop);
  23. 23. Find All Service <ul><li>BundleContext. getServiceReferences () </li></ul>ServiceReference[] refs = context.getServiceReferences ( IShortenURL.class.getName() , null );
  24. 24. Service Filter (LDAP Filter) context. getAllServiceReferences ( IShortenURL.class.getName(), String. format ( &quot;(name=%s)&quot; , name) ); “ (name=0rz.tw)” “ (name=tinyurl)” 過濾含有屬性 name ,且值為 0rz.tw 的 Service 過濾含有屬性 name ,且值為 tinyurl 的 Service
  25. 25. Service Filter (LDAP Filter) 選擇具有 cn 屬性且值為 Babs Jensen 者 &quot;(cn=Babs Jensen)&quot; 以 ! 排除具有 cn 屬性且值為 Tim Howes 者 &quot;(!(cn=Tim Howes))&quot; 選擇具有 objectclass 屬性且值為 Persion 並且不含屬性 sn 值 Jensen 與屬性 cn 且值為 Babs J 開頭者 &quot;(& (&quot; + Constants.OBJECTCLASS + &quot;=Person) (|(sn=Jensen)(cn=Babs J*)) )&quot; &quot;(o=univ*of*mich*)&quot;
  26. 26. Service Tracker <ul><li>Monitor Service come and go </li></ul><ul><li>ServiceTracker </li></ul><ul><ul><li>addingService() </li></ul></ul><ul><ul><ul><li>當 Service 被發佈時觸發 </li></ul></ul></ul><ul><ul><li>removedService() </li></ul></ul><ul><ul><ul><li>當 Service 被終止時觸發 </li></ul></ul></ul>
  27. 27. tracker = new ServiceTracker(context, IShortenURL.class.getName(), null) { public Object addingService (ServiceReference reference) { ui.resetAvailableServiceList(context); ui.updateStatus(String.format(&quot;Service [%s] is available.&quot;, reference.getProperty(&quot;name&quot;))); return super.addingService(reference); } public void removedService (ServiceReference reference, Object service) { ui.resetAvailableServiceList(context); ui.updateStatus(String.format(&quot;Service [%s] is unavailable.&quot;, reference.getProperty(&quot;name&quot;))); super.removedService(reference, service); } }; tracker. open ();
  28. 28. Demo Service Gone
  29. 29. Demo Service Back
  30. 30. 你需要 OSGi 嗎? <ul><li>你的系統需要執行期改變行為嗎? </li></ul><ul><li>你的系統提供的服務經常變動嗎? </li></ul>
  31. 31. 導入 OSGi 的問題 <ul><li>沿用 META/MANIFEST.MF, BUT … </li></ul><ul><ul><li>「手工」撰寫 MANIFEST.MF 是自虐。 </li></ul></ul><ul><ul><li>有單行長度限制 </li></ul></ul><ul><ul><li>最後一行需要空行 </li></ul></ul><ul><li>Metadata 需要填的內容可能很多 </li></ul><ul><ul><li>Import-Package </li></ul></ul><ul><ul><li>Export-Package </li></ul></ul><ul><ul><li>… </li></ul></ul>
  32. 32. Manifest-Version: 1.0 Created-By: 1.5.0_12 (Sun Microsystems Inc.) Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Import-Package: com.thoughtworks.xstream;version=&quot;[1.2.2, 2.0.0)&quot;;reso lution:=optional,com.thoughtworks.xstream.annotations;version=&quot;[1.2.2 , 2.0.0)&quot;;resolution:=optional,com.thoughtworks.xstream.converters;ve rsion=&quot;[1.2.2, 2.0.0)&quot;;resolution:=optional,com.thoughtworks.xstream. io;version=&quot;[1.2.2, 2.0.0)&quot;;resolution:=optional,com.thoughtworks.xst ream.io.json;version=&quot;[1.2.2, 2.0.0)&quot;;resolution:=optional,com.though tworks.xstream.io.xml;version=&quot;[1.2.2, 2.0.0)&quot;;resolution:=optional,j avax.jms;version=&quot;[1.1.0, 2.0.0)&quot;,javax.management,javax.management.j 2ee.statistics;version=&quot;[1.0.1, 2.0.0)&quot;,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=&quot;[1.0.1, 2.0.0)&quot;;resolu tion:=optional,javax.transaction.xa;version=&quot;[1.0.1, 2.0.0)&quot;;resoluti on:=optional,javax.xml.parsers,org.apache.activeio.journal;version=&quot;[ 3.1.0, 4.0.0)&quot;;resolution:=optional,org.apache.activeio.journal.activ e;version=&quot;[3.1.0, 4.0.0)&quot;;resolution:=optional,org.apache.activeio.p acket;version=&quot;[3.1.0, 4.0.0)&quot;;resolution:=optional,org.apache.camel; version=&quot;[1.3.0, 2.0.0)&quot;;resolution:=optional,org.apache.camel.compon
  33. 33. 導入 OSGi 的問題 <ul><li>Bundle ClassLoader 真麻煩 !? </li></ul><ul><ul><li>我想要把舊的專案搬到 OSGi 上? </li></ul></ul><ul><ul><ul><li>這是可行的 </li></ul></ul></ul><ul><ul><ul><li>但是,所有使用到的 Library 都必需轉成 Bundle 並且設 Export-Package </li></ul></ul></ul><ul><ul><li>然後,你會開始抱怨。 </li></ul></ul><ul><ul><ul><li>OSGi Sucks. </li></ul></ul></ul><ul><ul><ul><li>Daxx it. OSGi. </li></ul></ul></ul>
  34. 34. 導入 OSGi 的問題 <ul><li>JARs  Bundles </li></ul><ul><ul><li>使用到的 Library 都已經轉成 OSGi Bundle 了嗎? </li></ul></ul><ul><ul><ul><li>http:// www.springsource.com /repository/app </li></ul></ul></ul><ul><ul><li>可能將它們的轉成 OSGi Bundle 嗎? </li></ul></ul><ul><ul><ul><li>http://aqute.biz/Code/Bnd </li></ul></ul></ul><ul><ul><ul><li>http://wiki.ops4j.org/confluence/display/ops4j/Pax </li></ul></ul></ul><ul><ul><li>能否單純以 JAR 的型式使用 </li></ul></ul><ul><ul><ul><li>Bundle-ClassPath </li></ul></ul></ul><ul><ul><ul><li>Bundle-ClassPath + Façade Pattern  OSGi Service </li></ul></ul></ul>
  35. 35. 導入 OSGi 的問題 <ul><li>問題在設計 </li></ul><ul><ul><li>以 Façade Pattern 隔離底層不需共用的 JARs </li></ul></ul><ul><ul><li>管理 Bundle Dependency ,管制共用的 Bundle </li></ul></ul><ul><ul><ul><li>org.apache.commons.* </li></ul></ul></ul><ul><ul><ul><li>Spring </li></ul></ul></ul><ul><ul><li>採介面 / 實作分離的設計,以 Service 建構系統。 </li></ul></ul>
  36. 36. 導入 OSGi 的問題 <ul><li>OSGi Run many app in single vm </li></ul><ul><ul><li>需要禁止任何終止 VM 活動的程式 </li></ul></ul><ul><li>使用 SecurityManager ( 啟動 Security Layer 防護功能 ) </li></ul>System.exit(0);
  37. 37. <ul><li>Thank You. </li></ul><ul><li>Q & A </li></ul>

×