基于Eclipse的Equinox框架开发OSGi Bundle应用

一、创建Plug-in项目:osgiexample
package osgi.example.service;

/*
 * 定义服务接口<br/>
 * 为了将服务接口和服务实现分离,方便其它Bundle引用该服务,我们通常需要将该服务接口单独放在一个包内。
 */
public interface QueryService {

	public boolean queryUserName(String userName);

}


package osgi.example.activator;

import osgi.example.service.QueryService;

/*
 * 服务接口实现
 */
public class QueryServiceImp implements QueryService {

	private String[] userNames = { "Marry", "John", "David", "Rachel", "Ross" };

	public boolean queryUserName(String userName) {
		for (String tempUserName : userNames) {
			if (tempUserName.equals(userName)) {
				return true;
			}
		}
		return false;
	}

}


package osgi.example.activator;

import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import osgi.example.service.QueryService;

/*
 * 该类用来启动和停止Bundle应用。
 */
public class Activator implements BundleActivator {

	/*
	 * 利用BundleContext注册一个查询服务,并且为该服务设置相关属性。
	 */
	public void start(BundleContext context) throws Exception {
		Properties props = new Properties();
		props.put("ClassRoom", "ClassOne");
		context.registerService(QueryService.class.getName(),
				new QueryServiceImp(), props);
	}

	public void stop(BundleContext context) throws Exception {
	}

}


MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OsgiExample Bundle
Bundle-SymbolicName: osgiexample
Bundle-Version: 1.0.0
Bundle-Activator: osgi.example.activator.Activator
Import-Package: org.osgi.framework;version="1.3.0"
Eclipse-LazyStart: true
Bundle-Localization: plugin
Export-Package: osgi.example.service



二、创建Plug-in项目:osgiexampleclient
package osgi.exampleclient.activator;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import osgi.example.service.QueryService;

/*
 * 在OSGi平台上查找并引用OsgiExample Bundle应用已经注册的查询服务。
 */
public class Activator implements BundleActivator {

	public void start(BundleContext context) throws Exception {
		ServiceReference[] serviceReferences = context.getServiceReferences(
				QueryService.class.getName(), "(ClassRoom=*)");
		if (serviceReferences != null) {
			System.out.println("Enter a blank line to exit.");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(System.in));
			String userName = "";
			while (true) {
				System.out.print("Enter a Name: ");
				userName = bufferedReader.readLine();
				if (userName.length() == 0) {
					break;
				}
				QueryService queryService = (QueryService) context
						.getService(serviceReferences[0]);
				if (queryService.queryUserName(userName)) {
					System.out.println("The Name is Correct.");
				} else {
					System.out.println("The Name is Incorrect.");
				}
				context.ungetService(serviceReferences[0]);
			}
		} else {
			System.out.println("Couldn't find any query service.");
		}
	}

	public void stop(BundleContext context) throws Exception {
	}

}


MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Osgiexampleclient Bundle
Bundle-SymbolicName: osgiexampleclient
Bundle-Version: 1.0.0
Bundle-Activator: osgi.exampleclient.activator.Activator
Import-Package: osgi.example.service,org.osgi.framework;version="1.3.0"
Eclipse-LazyStart: true
Bundle-Localization: plugin



三、部署并运行Bundle应用
将开发的osgiexample和osgiexampleclient两个Bundle应用导出成Jar文件并启动OSGi服务平台。

Console Output:

osgi> ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.3.2.R33x_v20080105
443	ACTIVE      osgiexample_1.0.0

osgi> install file:///C:\Documents%20and%20Settings\Administrator\桌面\新建文件夹\osgiexampleclient.jar
Bundle id is 1204

osgi> ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.3.2.R33x_v20080105
443	ACTIVE      osgiexample_1.0.0
1204	INSTALLED   osgiexampleclient_1.0.0

osgi> start 1204
Enter a blank line to exit.
Enter a Name: test
The Name is Incorrect.
Enter a Name: Ross
The Name is Correct.
Enter a Name: 

osgi> ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.3.2.R33x_v20080105
443	ACTIVE      osgiexample_1.0.0
1204	ACTIVE      osgiexampleclient_1.0.0

osgi> stop 1204

osgi> ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.3.2.R33x_v20080105
443	ACTIVE      osgiexample_1.0.0
1204	RESOLVED    osgiexampleclient_1.0.0



附:OSGi Console Command
Valid commands:
---Controlling the OSGi framework---
	launch - start the OSGi Framework
	shutdown - shutdown the OSGi Framework
	close - shutdown and exit
	exit - exit immediately (System.exit)
	init - uninstall all bundles
	setprop <key>=<value> - set the OSGi property
---Controlling Bundles---
	install - install and optionally start bundle from the given URL
	uninstall - uninstall the specified bundle(s)
	start - start the specified bundle(s)
	stop - stop the specified bundle(s)
	refresh - refresh the packages of the specified bundles
	update - update the specified bundle(s)
---Displaying Status---
	status [-s [<comma separated list of bundle states>]  [<segment of bsn>]] - display installed bundles and registered services
	ss [-s [<comma separated list of bundle states>]  [<segment of bsn>]] - display installed bundles (short status)
	services {filter} - display registered service details
	packages {<pkgname>|<id>|<location>} - display imported/exported package details
	bundles [-s [<comma separated list of bundle states>]  [<segment of bsn>]] - display details for all installed bundles
	bundle (<id>|<location>) - display details for the specified bundle(s)
	headers (<id>|<location>) - print bundle headers
	log (<id>|<location>) - display log entries
---Extras---
	exec <command> - execute a command in a separate process and wait
	fork <command> - execute a command in a separate process
	gc - perform a garbage collection
	getprop  { name } - displays the system properties with the given name, or all of them.
---Controlling Start Level---
	sl {(<id>|<location>)} - display the start level for the specified bundle, or for the framework if no bundle specified
	setfwsl <start level> - set the framework start level
	setbsl <start level> (<id>|<location>) - set the start level for the bundle(s)
	setibsl <start level> - set the initial bundle start level
---Controlling the Profiling---
	profilelog - Display & flush the profile log messages

---Eclipse Runtime commands.---
	diag - Displays unsatisfied constraints for the specified bundle(s).
---Controlling the Console---
	more - More prompt for console output


OSGi manifest.mf 文件选项:
Bundle-Activator 
该类用于启动和停止绑定包。在上面的示例插件中,指定了 org.eclipse.pde.internal.ui.PDEPlugin 类。该类扩展 org.eclipse.core.runtime.Plugin,实现了 BundleActivator 接口。
Bundle-ClassPath 
该属性指定要用于绑定包的 CLASSPATH。该属性可以包含对绑定包 jar 文件中目录或 jar 文件的引用。可以使用句点指明绑定包的根。在示例 Eclipse PDE 绑定包中,指定了绑定包 jar 文件中的 org.eclipse.pde.ui_3.1.0.jar。如果将插件的源版本导入工作区中,导入过程将更改绑定包 CLASSPATH 以显示为 Bundle-ClassPath:,这允许插件的开发版本挑选已编译的绑定包类。
Bundle-Version 
该属性指定绑定包的版本号。包导入和必需的绑定包规范可以包括绑定包版本号。
Export-Package 
该属性指定要公共暴露给其他插件的所有包。
Import-Package 
该属性指定要从必需插件中显式导入的所有包。默认情况下,必须为要启动的绑定包解析所有包。还可以将包导入指定为可选项,以支持包不存在的情况。显式导入的类在 Require-Bundle 插件中的包之前解析。
Require-Bundle 
该属性指定要在给定绑定包中导入使用的绑定包及其已导出的包。指定的绑定包在显式包导入之后解析。

你可能感兴趣的:(eclipse,UI,框架,osgi)