Установить пакет osgi, используя массив байтов вместо местоположения файла?

Я пытаюсь установить пакеты OSGi. Я умею делать это успешно. Сейчас я занимаюсь тем, что в нашей компании есть какое-то хранилище, где мы храним все jar-файлы пакетов OSGi. Итак, я иду и загружаю эти jar-файлы пакетов OSGi в какой-то локальный каталог, а затем пытаюсь установить эти пакеты из локального места, где они были загружены из моего репозитория.

И ниже метод принимает только местоположение файла. Поэтому я предоставляю свой полный локальный путь к этому файлу.

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, а затем установить его?

Может ли кто-нибудь помочь мне с этим с помощью простого примера на основе приведенного выше кода? Спасибо за помощь.


person AKIWEB    schedule 22.08.2013    source источник
comment
вы пробовали использовать BundleContext#installBundle(String, InputStream), что-то вроде этого: context.installBundle(fileName, new ByteArrayInputStream(b));   -  person Katona    schedule 23.08.2013
comment
еще нет .. Можете ли вы привести пример, как это сделать? Спасибо   -  person AKIWEB    schedule 23.08.2013
comment
Думаю надо заменить context.installBundle(localFilename) на context.installBundle(fileName, new ByteArrayInputStream(b)), должно компилироваться, а если так же работает, то можно избавиться от копирования в локальную папку   -  person Katona    schedule 23.08.2013
comment
какое здесь будет имя файла? Простое имя файла jar?   -  person AKIWEB    schedule 23.08.2013


Ответы (1)


Я не пробовал, но 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();
    }
}
person Katona    schedule 22.08.2013