AKIWEB
AKIWEB

Reputation: 19612

Install the osgi bundle using the byte array instead of file location?

I am trying to install OSGi bundles. I am able to do it successfully. Right now what I am doing is, in our company we have some sort of storage where we are storing all the OSGi bundles jar. So I go and download those OSGi bundles jar into some local directory and then I try to install those bundles from the local location where it got downloaded from my storage repository.

And below method accepts only file location- So I provide my full local file path to this.

context.installBundle(localFilename)

Is there any way, I can install it using the byte[]. Basically I am trying to avoid downloading the jar file from my storage location to some local folder and then use that local location to install the bundles.

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

Is there any way to do this thing without copying the jar file from my storage location to my local folder and then use the full path to my local jar file and then install it?

Can anyone help me with this with a simple example basis on above code? Thanks for the help.

Upvotes: 0

Views: 317

Answers (1)

Katona
Katona

Reputation: 4901

I didn't try it, but BundleContext#installBundle(String, InputStream) should be suitable for this. Using the mentioned method, your code would like this (local file creation has been removed):

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

Upvotes: 2

Related Questions