上一篇文章,我们谈到了基础的bundle依赖关系,以及当依赖的bundle出现问题的时候的情况。
这里看一个更常见的情况,当依赖的多个插件同时Export了相同的package的时候,bundle如何处理呢?
先预测一下:
据依赖中的顺序来的,优先使用顺序靠前的bundle中的package
还是简单的搭建一个场景测试一下。
测试环境:Eclipse 3.6 、Equinox
准备Bundle
老规矩,新建3个插件,分别为:
- org.salever.osgi.depends.sdk1
- org.salever.osgi.depends.sdk2
- org.salever.osgi.depends.sdk-client
其中,sdk1和sdk2同时拥有org.salever.osgi.depends.service这个package,分别新建SDKService.java,内容一样为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package
org.salever.osgi.depends.service;
import
org.salever.osgi.depends.sdk1.Activator;
/**
* @author LiXP
*
*/
public
class
SDKService {
public
static
void
service() {
System.out.println(Activator.
class
.toString() +
": service!"
);
}
}
|
启动OSGI Framework
查看bundle的状态:
1
2
3
4
5
6
7
8
9
10
11
|
osgi> ss
Framework is launched.
id State Bundle
0
ACTIVE org.eclipse.osgi_3.
6.2
.R36x_v20110210
1
<<LAZY>> org.salever.osgi.depends.sdk1_1.
0.0
.qualifier
2
<<LAZY>> org.salever.osgi.depends.sdk-client_1.
0.0
.qualifier
3
<<LAZY>> org.salever.osgi.depends.sdk2_1.
0.0
.qualifier
osgi>
|
建立依赖关系
按照下面的步骤:
首先同时在sdk1和sdk2的Runtime,添加org.salever.osgi.depends.service的
1
|
Export-
package
Export-Package: org.salever.osgi.depends.service
|
然后在sdk-client中同时添加sdk1和sdk2的依赖
1
|
Require-Bundle: org.salever.osgi.depends.sdk2;bundle-version=
"1.0.0"
,org.salever.osgi.depends.sdk1;bundle-version=
"1.0.0"
|
注意这时候的顺序
然后在org.salever.osgi.depends.sdk_client.Activator中使用SDKService:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package
org.salever.osgi.depends.sdk_client;
import
org.osgi.framework.BundleActivator;
import
org.osgi.framework.BundleContext;
import
org.salever.osgi.depends.service.SDKService;
public
class
Activator
implements
BundleActivator {
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
* )
*/
public void start(BundleContext context) throws Exception {
SDKService.service();
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public
void
stop(BundleContext context)
throws
Exception {
}
}
|
重新启动一下OSGI Framework,测试一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
osgi> ss
Framework is launched.
id State Bundle
0
ACTIVE org.eclipse.osgi_3.
6.2
.R36x_v20110210
1
<<LAZY>> org.salever.osgi.depends.sdk1_1.
0.0
.qualifier
2
<<LAZY>> org.salever.osgi.depends.sdk-client_1.
0.0
.qualifier
3
<<LAZY>> org.salever.osgi.depends.sdk2_1.
0.0
.qualifier
osgi> start
2
class
org.salever.osgi.depends.sdk1.Activator: service!
osgi>
|
这时候使用了org.salever.osgi.depends.sdk1中的package。基本验证了我们的猜测。
修改一下依赖的顺序,再次测试一下:
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
osgi> ss
Framework is launched.
id State Bundle
0
ACTIVE org.eclipse.osgi_3.
6.2
.R36x_v20110210
1
<<LAZY>> org.salever.osgi.depends.sdk1_1.
0.0
.qualifier
2
<<LAZY>> org.salever.osgi.depends.sdk-client_1.
0.0
.qualifier
3
<<LAZY>> org.salever.osgi.depends.sdk2_1.
0.0
.qualifier
osgi> start
2
class
org.salever.osgi.depends.sdk2.Activator: service!
osgi>
|
这次使用了sdk2的。测试结束!