我正在尝试在 OSGi 容器中安装 OSGi bundle 。我的一个文件夹中有一个 jar 文件。我将该 jar 文件读入 ByteArray 中,然后使用此 ByteArray 来安装 Framework > 捆绑在 OSGi 容器中。下面是代码..
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
framework = frameworkFactory.newFramework(new HashMap<String, String>());
framework.start();
final String basePath = "C:\\LocalStorage";
final BundleContext bundleContext = framework.getBundleContext();
final List<Bundle> installedBundles = new LinkedList<Bundle>();
String filename = "Framework" + "-" + "1.0.0" + ".jar";
String localFilename = basePath+ File.separatorChar + filename;
File file = new File(localFilename);
byte [] fileData = new byte[(int)file.length()];
DataInputStream dis = new DataInputStream((new FileInputStream(file)));
dis.readFully(fileData);
dis.close();
// But below line gives me exception always-
installedBundles.add(bundleContext.installBundle(filename, new ByteArrayInputStream(fileData)));
for (Bundle bundle : installedBundles) {
bundle.start();
}
下面是异常(exception),我总是得到-
org.osgi.framework.BundleException: Bundle symbolic name and version are not unique: Framework:1.0.0
谁能告诉我我做错了什么?我需要使用 ByteArray,因为在另一个类的一些代码中,我使用 ByteArray,所以我需要将 jars 文件的 ByteArray 传递给这些方法..
更新:-
但是如果我像这样安装它,那么它就可以正常工作。如果我通过 ByteArray 安装它,它不起作用..
final String basePath = "C:\\LocalStorage";
final BundleContext bundleContext = framework.getBundleContext();
final List<Bundle> installedBundles = new LinkedList<Bundle>();
String filename = "Framework" + "-" + "1.0.0" + ".jar";
String localFilename = Constants.FILE_PROTOCOL + basePath+ File.separatorChar + filename;
installedBundles.add(bundleContext.installBundle(localFilename));
for (Bundle bundle : installedBundles) {
bundle.start();
}
我可能对 ByteArray 做错了什么?有什么想法吗?
最佳答案
该错误表明您已经拥有具有相同符号名称和版本的 bundle 。这两个一起就像每个包的“主键”;您可以拥有同一 bundle 的两个版本,但不能拥有两个具有相同名称和版本的 bundle 。
我仍然不明白为什么你需要传递字节数组...为什么不传递 FileInputStream,因为你已经有了它?
无论如何,我会让您自己弄清楚为什么要加载同一个包两次。注意,文件名并不重要,只有 MANIFEST.MF 中的 Bundle-SymbolicName
和 Bundle-Version
条目。
关于java - 使用 ByteArray 安装 OSGi Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950118/