Freud's Blog

Stay hungry, stay foolish. 少年辛苦终身事,莫向光阴惰寸功。

JAVA OSGi(六)-component注册自定义控制台命令实现

Posted on By Freud Kang

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

mainefest file configuration

  • 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

new component definition

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

services configuration

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

say freud

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

bundle use service

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

helper info


参考资料

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