我必须将 Eclipse/Equinox OSGi 框架嵌入到现有应用程序中。
我找到了本教程http://njbartlett.name/2011/03/07/embedding-osgi.html并尝试使用 FRAMEWORK_SYSTEMPACKAGES_EXTRA 常量。
所以我尝试通过系统包将一个简单的java类导出到正在运行的框架中,但是我从我的激活器包中收到以下错误
Exception in thread "main" org.osgi.framework.BundleException: The activator com.bundleactivator.Activator for bundle com.bundleactivator is invalid
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:156)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:751)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:370)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
at RunNewEquinoxOsgi.main(RunNewEquinoxOsgi.java:74)
Caused by: java.lang.Error: Unresolved compilation problem:
The import example cannot be resolved
at com.bundleactivator.Activator.<init>(Activator.java:8)
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1505)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:151)
... 5 more
在我从激活器的系统包中导入 example.osgi 包的地方引发异常
import example.osgi.Example;
我在其他 java 类中通过 FrameworkFactory 启动 Equinox
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import example.osgi.*;
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"example.osgi;version=\"1.0.0\"");
try {
framework.start();
System.out.println("Framework start");
} catch (BundleException e) {
e.printStackTrace();
}
BundleContext ctx = framework.getBundleContext();
Bundle b3 = ctx.installBundle("file:C:\\equinoxtest\\ds.jar");
System.out.println(b3.getBundleId() + " installed");
Bundle b4 = ctx.installBundle("file:C:\\equinoxtest\\services.jar");
System.out.println(b4.getBundleId() + " installed");
Bundle b5 = ctx.installBundle("file:C:\\equinoxtest\\util.jar");
System.out.println(b5.getBundleId() + " installed");
Bundle b6 = ctx.installBundle("file:C:\\equinoxtest\\service.jar");
System.out.println(b6.getBundleId() + " installed");
Bundle b7 = ctx.installBundle("file:C:\\equinoxtest\\rule1.jar");
System.out.println(b7.getBundleId() + " installed");
Bundle b8 = ctx.installBundle("file:C:\\equinoxtest\\rule2.jar");
System.out.println(b8.getBundleId() + " installed");
Bundle b9 = ctx.installBundle("file:C:\\equinoxtest\\rule3.jar");
System.out.println(b9.getBundleId() + " installed");
Bundle b10 = ctx.installBundle("file:C:\\equinoxtest\\activator.jar");
System.out.println(b10.getBundleId() + " installed");
b3.start();
b4.start();
b5.start();
b6.start();
b7.start();
b8.start();
b9.start();
b10.start(); // here i start the activator
这是激活器类的清单
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: BundleActivator
Bundle-SymbolicName: com.bundleactivator
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.bundleactivator.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: com.bundleservice,
example.osgi;version="1.0.0";resolution:=optional,
org.osgi.framework;version="1.3.0",
org.osgi.util.tracker;version="1.4.0"
Bundle-ActivationPolicy: lazy
Export-Package: com.bundleactivator
在 OSGi 控制台中,我在系统包导出中看到了这个包(example.osgi),但导入类(激活器)抛出异常。我还应该做什么?
>osgi packages 0
...
example.osgi; version="1.0.0"<org.eclipse.osgi_3.6.2.R36x_v20110210 [0]>