使用ByteArray安装OSGi Bundle

时间:2013-09-23 00:17:49

标签: java osgi osgi-bundle

我正在尝试在OSGi容器中安装OSGi包。我在我的一个文件夹中有一个jar文件..我将该jar文件读入ByteArray,然后我使用此ByteArray在OSGi容器中安装Framework包。以下是代码..

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();
}

以下是例外,我总是得到 -

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做错事吗?有什么想法吗?

3 个答案:

答案 0 :(得分:1)

错误表示您已经拥有一个具有相同符号名称和版本的包。这两者一起就像每个捆绑的“主键”;您可以拥有同一个捆绑包的两个版本,但不能有两个具有相同名称​​和版本的捆绑包。

我仍然不明白为什么你需要传递字节数组...为什么不传递FileInputStream,因为你已经有了?

无论如何,我会留下让你弄清楚为什么你要加载两次相同的捆绑包。注意,文件名不重要,只有MANIFEST.MF中的Bundle-SymbolicNameBundle-Version条目。

答案 1 :(得分:0)

听起来你正在安装的jar的MANIFEST.MF中缺少一些OSGi标头。尝试添加一些这样的元数据条目:

Bundle-SymbolicName: your.name.Class

Bundle-Version: 1.0.0

查看wikipedia entry

答案 2 :(得分:0)

您尝试手动安装OSGi框架捆绑包到已由FrameworkFactory初始化的Framework系统捆绑包(这是相同的捆绑/文件)。这是您的异常的原因(阅读详细信息here)。