Eclipse 3.0+完全基於OSGi(Open Services Gateway Initiative)重構:插件即Bundle,Equinox是Eclipse的OSGi參考實現。控制台是運行時動態管理的核心入口。
Run → Run Configurations → OSGi Frameworkosgi>提示符eclipse -console
# 或
eclipse -console 5555 # Telnet端口遠程連接
| 命令 | 功能 | 示例 |
|---|---|---|
| ss | 列出所有Bundle狀態簡表 | ss |
| start <id|name> | 啟動指定Bundle | start 12 |
| stop <id|name> | 停止指定Bundle | stop com.example.bundle |
| install <url> | 安裝Bundle(文件/http) | install file:/tmp/bundle.jar |
| uninstall <url> | 卸載Bundle | uninstall file:/tmp/bundle.jar |
| update <url> | 更新Bundle | update file:/tmp/bundle.jar |
| active | 列出活躍Bundle | active |
| headers <id|name> | 顯示Bundle清單頭信息 | headers 12 |
| services [filter] | 列出服務註冊信息 | services (objectClass=*) |
| diag <id|name> | 診斷Bundle啟動失敗原因 | diag 12 |
| help | 顯示所有命令幫助 | help |
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.2.0
1 ACTIVE com.example.hello_1.0.0
osgi> stop 1
osgi> ss
id State Bundle
1 RESOLVED com.example.hello_1.0.0
osgi> start 1
osgi> ss
id State Bundle
1 ACTIVE com.example.hello_1.0.0
osgi> install file:/home/user/new-bundle.jar
Bundle id is 15
osgi> start 15
osgi> update 15 file:/home/user/new-bundle-v2.jar
osgi> uninstall 15
全程無需重啟JVM,體現OSGi動態模塊化核心價值。
osgi> start 20
org.osgi.framework.BundleException: Missing constraint: Import-Package: com.missing.package
osgi> diag 20
com.example.broken_1.0.0 [20]
Missing constraint: Import-Package: com.missing.package
精確定位缺失依賴包,避免盲目排查。
CommandProvider接口context.registerService(CommandProvider.class.getName(), this, props)_commandName(CommandInterpreter ci)getHelp()返回幫助文本public class Activator implements BundleActivator, CommandProvider {
private BundleContext context;
public void start(BundleContext context) {
this.context = context;
Hashtable props = new Hashtable();
context.registerService(CommandProvider.class.getName(), this, props);
}
public String getHelp() {
return "\tuname - returns framework information\n";
}
public void _uname(CommandInterpreter ci) {
String vendor = context.getProperty(Constants.FRAMEWORK_VENDOR);
String version = context.getProperty(Constants.FRAMEWORK_VERSION);
String osName = context.getProperty(Constants.FRAMEWORK_OS_NAME);
String osVersion = context.getProperty(Constants.FRAMEWORK_OS_VERSION);
System.out.println("\n " + vendor + " " + version + " (" + osName + " " + osVersion + ")");
}
// stop方法省略
}
效果:
osgi> uname
Eclipse Foundation 3.2.0 (Win32 5.1)
public String getHelp() {
return "\twhatami - returns whether the bundle is a plug-in or not\n" +
"\tuname - returns framework information\n";
}
public void _whatami(CommandInterpreter ci) {
try {
long id = Long.parseLong(ci.nextArgument());
Bundle bundle = context.getBundle(id);
URL url = bundle.getEntry("plugin.xml"); // Eclipse插件標識
if (url != null) {
System.out.println("\n I'm (" + bundle.getSymbolicName() + ") a plug-in");
} else {
System.out.println("\n I'm (" + bundle.getSymbolicName() + ") not a plug-in");
}
} catch (NumberFormatException e) {
System.out.println("\n Error processing command");
}
}
效果:
osgi> whatami 1
I'm (com.example.hello) not a plug-in
osgi> whatami 0
I'm (org.eclipse.osgi) a plug-in
| 維度 | Bundle | Plugin |
|---|---|---|
| 定義 | OSGi規範模塊單元 | Eclipse擴展註冊表增強的Bundle |
| 標識 | MANIFEST.MF | 根目錄有plugin.xml |
| 關係 | 所有Plugin都是Bundle | Bundle不一定是Plugin |
OSGi控制台是運行時微內核管理儀表板:
掌握控制台,即掌握了Eclipse/Equinox運行時的「上帝模式」。