OSGi实战的问题(转)
对osgi有了一个初步的了解之后,准备写段代码跑跑,一试身手,
先下载了一份Bluedavy 的《OSGI实战》
里边有可以直接运行的代码,双击run.bat运行正常,暗爽!
开始练习《OSGI实战》中用户登录验证模块,一行一行敲代码,第一个变化就是工程之间相互引用不能在Build path里添加工程引用了,改成了在MANIFEST.MF当中添加Import-Package
在学习过程当中还是遇到了不少问题,记录下来,帮助遇到和我同样样问题的少走弯路。
我用的是eclipse3.4 jdk1.6?
1.Import-Package时org.eclipse.equinox.servlet.api这个包死活找不到。
在eclipse3.4已经不存在了直接导入javax.servlet_2.4.0.v200806031604.jar就可以了
如果没有添加javax.servlet会出现? INSTALLED?? UserValidatorWebBundle_1.0.0
强行启动会抛出以下异常
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?
因为Declarative Service实现并没有包含在Eclipse的默认软件包中,需要单独从 Eclipse 的的网站上获得,下载包当中的plugins和features复制到eclipse当中重启
org.eclipse.equinox.ds_1.0.0.v20080427-0830.jar是运行时用到的bundle
org.eclipse.equinox.ds用到了org.eclipse.equinox.util_1.0.0.v20080414.jar
都要在config.ini当中添加并启动
3.一切看似启动正常了,log当中还是有以下异常
java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).
还得启动 org.eclipse.osgi.util_3.1.300.v20080303.jar 这个bundle
4.如果http://localhost/demo/page/login.htm 这个页面不能访问可能org.eclipse.equinox.http_1.0.200.v20080421-2006.jar没有启动,如何htm能访问了http://localhost/demo/login 不能访问 可能org.eclipse.equinox.http.servlet_1.0.100.v20080427-0830.jar没有启动
总结一下用户登录验证模块要启动的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
?
?
当缺少什么bundle时,自己去网上下就行,然后用导入Plug-ins and Fragments的方式将bundle导入,便可以引用了,所以别当成什么高深的东西,别忘了在运行时用"Add Required Bundles",它帮我们加入会用的的bundles,这个就帮我们很大的忙了.
?
在第一个例子(源码中的classic)中,用代码注册的方式来注册服务,即在每个实现服务接口的bundle里的Activator注册自己的服务到指定服务名下,如
osgi.noShutdown=true# 当前系统下运行的 Bundle,可以在此指定 Bundle 的启动顺序,在后续的# StartLevel Service章节中会详细的介绍#避免Unable to acquire application service. Ensure that the org.eclipse.core.runtime错误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@startosgi.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@startosgi.bundles.defaultStartLevel=4
?
这里不要忘记加入ds的bundle不然用Service-Component: OSGI-INF/component.xml配置就不起作用了,发布不了服务的,因为是由ds的bundle来扫描发现相应的服务,并加于管理的。