基於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
該屬性指定要在給定綁定包中導入使用的綁定包及其已導出的包。指定的綁定包在顯式包導入之後解析。
發佈了17 篇原創文章 · 獲贊 1 · 訪問量 2721
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章