- 在
第五章的基础上,将OSGi_equinox_consumer项目下的META-INF/MANIFEST.MF文件中的Bundle-Activator项删除 - 在
MANIFEST.MF中添加org.eclipse.osgi.framework.consoleBundle的支持

- 在
com.freud.osgi.consumer目录下新建一个PracticeService的类,内容为
package com.freud.osgi.consumer;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import com.freud.osgi.HelloWorldService;
public class PracticeService implements CommandProvider {
private HelloWorldService helloWorldService;
public void _say(CommandInterpreter ci) {
ci.println(helloWorldService.sayHello(ci.nextArgument()));
}
public synchronized void unsetHelloWorldService() {
this.helloWorldService = null;
}
public synchronized void setHelloWorldService(
HelloWorldService helloWorldService) {
if (this.helloWorldService != helloWorldService) {
this.helloWorldService = helloWorldService;
}
}
@Override
public String getHelp() {
String ret = "pring the help info for <say>";
System.out.println(ret);
return ret;
}
}- 在项目下新建一个文件夹
OSGI-INF - 在
osgi-INF目录右键,新建一个Component Definition,修改Class为om.freud.osgi.consumer.PracticeService

- 打开
component文件,切到Services的tab下,在Referenced Services上Add添加一个新的Reference Service为“HelloWorldService”,然后点击Edit编辑如下内容 - 在
Provided Services中点击Add添加org.eclipse.osgi.framework.console.CommandProvider - 上两步的
截图如下

- 重新
运行 - 在控制台中尝试输入
say freud

- 在控制台输入
services *HelloWorldService, 输出为这个Service的Bundle使用状态。 - 在控制台中尝试输入
say freud

- 在控制台输入
help say输出say的Helper info

参考资料
视频教程 : http://v.youku.com/v_show/id_XNDE1NzU0OTY0.html
Equinox OSGi官网 : http://www.eclipse.org/equinox/
林昊 : 《OSGi原理与最佳实践》
Richard S. Hall : 《OSGi实战》