轉(zhuǎn):Osgi實(shí)戰(zhàn)中的問題
本文關(guān)鍵詞:OSGi實(shí)戰(zhàn),由筆耕文化傳播整理發(fā)布。
對osgi有了一個(gè)初步的了解之后,準(zhǔn)備寫段代碼跑跑,一試身手,
先下載了一份Bluedavy 的《OSGi實(shí)戰(zhàn)》
里邊有可以直接運(yùn)行的代碼,雙擊run.bat運(yùn)行正常,暗爽!
開始練習(xí)《OSGi實(shí)戰(zhàn)》中用戶登錄驗(yàn)證模塊,一行一行敲代碼,第一個(gè)變化就是工程之間相互引用不能在Build path里添加工程引用了,改成了在MANIFEST.MF當(dāng)中添加Import-Package
在學(xué)習(xí)過程當(dāng)中還是遇到了不少問題,記錄下來,幫助遇到和我同樣樣問題的少走彎路。
我用的是eclipse3.4 jdk1.6
1.Import-Package時(shí)org.eclipse.equinox.servlet.api這個(gè)包死活找不到。
在eclipse3.4已經(jīng)不存在了直接導(dǎo)入javax.servlet_2.4.0.v200806031604.jar就可以了
如果沒有添加javax.servlet會出現(xiàn) INSTALLED UserValidatorWebBundle_1.0.0
強(qiáng)行啟動(dòng)會拋出以下異常
org.osgi.framework.BundleException:
The bundle could not be resolved. Reason: Missing Constraint:
Require-Bundle: javax.servlet; bundle-version="2.4.0"
2.如果使用了Equinox OSGI Declarative Service需要下載 eclipse-equinox-SDK-3.4.2.zip
因?yàn)镈eclarative Service實(shí)現(xiàn)并沒有包含在Eclipse的默認(rèn)軟件包中,需要單獨(dú)從 Eclipse 的的網(wǎng)站上獲得,下載包當(dāng)中的plugins和features復(fù)制到eclipse當(dāng)中重啟
org.eclipse.equinox.ds_1.0.0.v20080427-0830.jar是運(yùn)行時(shí)用到的bundle
org.eclipse.equinox.ds用到了org.eclipse.equinox.util_1.0.0.v20080414.jar
都要在config.ini當(dāng)中添加并啟動(dòng)
3.一切看似啟動(dòng)正常了,log當(dāng)中還是有以下異常
java.lang.IllegalStateException:
Unable to acquire application service. Ensure that the
org.eclipse.core.runtime bundle is resolved and started (see
config.ini).
還得啟動(dòng) org.eclipse.osgi.util_3.1.300.v20080303.jar 這個(gè)bundle
4.如果 這個(gè)頁面不能訪問可能org.eclipse.equinox.http_1.0.200.v20080421-2006.jar沒有啟動(dòng),如何htm能訪問了 不能訪問 可能org.eclipse.equinox.http.servlet_1.0.100.v20080427-0830.jar沒有啟動(dòng)
總結(jié)一下用戶登錄驗(yàn)證模塊要啟動(dòng)的bundle
id State Bundle
0 ACTIVE org.eclipse.osgi_3.4.2.R34x_v20080826-1230
1 ACTIVE ConfigFileValidatorBundle_1.0.0
2 ACTIVE DBValidatorBundle_1.0.0
4 ACTIVE UserValidatorBundle_1.0.0
5 ACTIVE LDAPValidatorBundle_1.0.0
9 ACTIVE UserValidatorWebBundle_1.0.0
10 ACTIVE org.eclipse.equinox.util_1.0.0.v20080414
11 ACTIVE org.eclipse.equinox.ds_1.0.0.v20080427-0830
12 ACTIVE javax.servlet_2.4.0.v200806031604
13 ACTIVE org.eclipse.osgi.services_3.1.200.v20071203
14 ACTIVE org.eclipse.equinox.http.servlet_1.0.100.v20080427-0830
15 ACTIVE org.eclipse.equinox.http_1.0.200.v20080421-2006
17 ACTIVE org.eclipse.osgi.util_3.1.300.v20080303
當(dāng)缺少什么bundle時(shí),自己去網(wǎng)上下就行,然后用導(dǎo)入Plug- ins and Fragments的方式將bundle導(dǎo)入,便可以引用了,所以別當(dāng)成什么高深的東西,別忘了在運(yùn)行時(shí)用"Add Required Bundles",它幫我們加入會用的的bundles,這個(gè)就幫我們很大的忙了.
在第一個(gè)例子(源碼中的classic)中,用代碼注冊的方式來注冊服務(wù),即在每個(gè)實(shí)現(xiàn)服務(wù)接口的bundle里的Activator注冊自己的服務(wù)到指定服務(wù)名下,如
Java代碼
package org.riawork.demo.user.validator; /* * RIAWork.org * * OSGI Opendoc Demo */ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.riawork.demo.service.user.Validator; import org.riawork.demo.service.user.impl.ConfigFileValidatorImpl; /** * desc: ConfigFileBundle Activator,采用傳統(tǒng)的方式完成服務(wù)的注冊 * * @author jerry */ public class Activator implements BundleActivator { // --------------------------------------------Instance Variables private ServiceRegistration serviceReg=null; // --------------------------------------------Public Method /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ //實(shí)現(xiàn)Validator的Bunble都會在Activator里將自己的新的實(shí)現(xiàn)注冊到Validator.class.getName()名下,這樣Validator.class.getName()為名的服務(wù)就有多個(gè)了 public void start(BundleContext context) throws Exception { serviceReg=context.registerService(Validator.class.getName(), new ConfigFileValidatorImpl(), null); } /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { if(serviceReg!=null) serviceReg.unregister(); } }在第二個(gè)例子(源碼中的ds),采用的是在配置文件里聲明的方式來發(fā)布一個(gè)bundle里的服務(wù)的,即在 ConfigFileValidatorBundle,DBValidatorBundle,LDAPValidatorBundle這三個(gè)實(shí)現(xiàn) UserValidatorBundle接口的bundle里沒有Activator來注冊它們的實(shí)現(xiàn)服務(wù),而是在MANIFEST.MF配置文件里加入 Service-Component: OSGI-INF/component.xml來發(fā)布聲明在項(xiàng)目目錄下的OSGI-INF/component.xml配置文件的服務(wù)。
打包時(shí),按著文檔來做是不行的,打包的時(shí)候,org.eclipse.osgi的jar包和run.bat是在一個(gè)目錄里,而在它們目錄下再建 configuration目錄(存放config.ini文件)和bundles目錄(存放自己打包出來的bundle和它們運(yùn)行依賴的 bundle),還有一點(diǎn)在注意的是拷貝文檔里的config.ini內(nèi)容到config.ini文件是不能運(yùn)行的,可以是有中文字符在里面。
我這里能運(yùn)行的如下:
Xml代碼
osgi.noShutdown=true # 當(dāng)前系統(tǒng)下運(yùn)行的 Bundle,可以在此指定 Bundle 的啟動(dòng)順序,,在后續(xù)的 # StartLevel Service章節(jié)中會詳細(xì)的介紹 #避免Unable to acquire application service. Ensure that the org.eclipse.core.runtime錯(cuò)誤 eclipse.ignoreApp=true #osgi.bundles=reference\:file\:bundles/ConfigFileValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/DBValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/LDAPValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,reference\:file\:bundles/javax.servlet_2.5.0.v200806031605.jar@start,reference\:file\:bundles/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,reference\:file\:bundles/UserValidatorBundle_1.0.0.jar@start, reference\:file\:bundles/UserValidatorWebBundle_1.0.0.jar@start osgi.bundles=plugins/ConfigFileValidatorBundle_1.0.0.jar@start,\ plugins/DBValidatorBundle_1.0.0.jar@start,\ plugins/LDAPValidatorBundle_1.0.0.jar@start,\ plugins/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,\ plugins/javax.servlet_2.5.0.v200806031605.jar@start,\ plugins/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,\ plugins/UserValidatorBundle_1.0.0.jar@start,\ plugins/UserValidatorWebBundle_1.0.0.jar@start,\ plugins/org.eclipse.equinox.ds_1.1.1.R35x_v20090806.jar@start,\ plugins/org.eclipse.equinox.util_1.0.100.v20090520-1800.jar@start osgi.bundles.defaultStartLevel=4這里不要忘記加入ds的bundle不然用Service-Component: OSGI-INF/component.xml配置就不起作用了,發(fā)布不了服務(wù)的,因?yàn)槭怯蒬s的bundle來掃描發(fā)現(xiàn)相應(yīng)的服務(wù),并加于管理的。
本文關(guān)鍵詞:OSGi實(shí)戰(zhàn),由筆耕文化傳播整理發(fā)布。
本文編號:73906
本文鏈接:http://sikaile.net/wenshubaike/mishujinen/73906.html