使用字节数组而不是文件位置安装osgi包?

时间:2013-08-22 20:28:08

标签: java osgi apache-felix osgi-bundle

我正在尝试安装OSGi包。我能够成功地做到这一点。现在我正在做的是,在我们公司,我们有一些存储,我们存储所有的OSGi包jar。所以我将这些OSGi包jar下载到一些本地目录中,然后尝试从我的存储库下载的本地位置安装这些包。

以下方法只接受文件位置 - 所以我提供了完整的本地文件路径。

context.installBundle(localFilename)

有没有办法,我可以使用byte[]安装它。基本上我试图避免将jar文件从我的存储位置下载到某个本地文件夹,然后使用该本地位置来安装捆绑包。

    private static void callMethod() throws BundleException {


    final IStorageServiceClient client = StorageServiceConsumerProvider.getStorageServiceClient(envType);

    final StorageObjectIdentifier objIdentifierDir = new StorageObjectIdentifier(name, version, null);

    final List<Map<String, String>> dirs = new ArrayList<Map<String, String>>();
    client.listDirectory(objIdentifierDir, dirs);

    final String filename = name + Constants.DASH + version + Constants.DOTJAR;
    final String localFilename = basePath + File.separatorChar + filename;

    // first of all, I am trying to delete the jar file from the local folder, if it is already there
    new File(localFilename).delete();

    final StorageObjectIdentifier objIdentifier = new StorageObjectIdentifier(name, version, filename);

    // now I get the byte array of the jar file here.
    final byte[] b = client.retrieveObject(objIdentifier);

    // now I am writing that jar file to that local folder again using the byte array.
    final FileOutputStream fos = new FileOutputStream(localFilename);

    fos.write(b);
    fos.close();

    // now the jar file is there in that location, and now I am using the full path of the jar file to intall it.
    BundleContext context = framework.getBundleContext();
    List<Bundle> installedBundles = new LinkedList<Bundle>();

    installedBundles.add(context.installBundle(localFilename));

    for (Bundle bundle : installedBundles) {
        bundle.start();
    }
}

如果没有将jar文件从我的存储位置复制到我的本地文件夹然后使用我的本地jar文件的完整路径然后安装它,有没有办法做这件事?

任何人都可以通过上述代码的简单示例帮助我吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

我没有尝试过,但BundleContext#installBundle(String, InputStream)应该适合这个。使用上面提到的方法,您的代码会喜欢这个(本地文件创建已被删除):

private static void callMethod() throws BundleException {

    final IStorageServiceClient client = StorageServiceConsumerProvider.getStorageServiceClient(envType);

    final StorageObjectIdentifier objIdentifierDir = new StorageObjectIdentifier(name, version, null);

    final List<Map<String, String>> dirs = new ArrayList<Map<String, String>>();
    client.listDirectory(objIdentifierDir, dirs);

    final String filename = name + Constants.DASH + version + Constants.DOTJAR;

    final StorageObjectIdentifier objIdentifier = new StorageObjectIdentifier(name, version, filename);

    // now I get the byte array of the jar file here.
    final byte[] b = client.retrieveObject(objIdentifier);

    // now the jar file is there in that location, and now I am using the full path of the jar file to intall it.
    BundleContext context = framework.getBundleContext();
    List<Bundle> installedBundles = new LinkedList<Bundle>();

    installedBundles.add(context.installBundle(fileName, new ByteArrayInputStream(b)));

    for (Bundle bundle : installedBundles) {
        bundle.start();
    }
}