0

我正在尝试安装 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 文件的完整路径然后安装它?

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

4

1 回答 1

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();
    }
}
于 2013-08-22T21:15:39.377 回答