2013-08-22 52 views
0

我正在嘗試安裝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文件,然後安裝呢?

任何人都可以用這個簡單的例子基礎上面的代碼來幫助我嗎?謝謝您的幫助。

+0

你試圖使用'#BundleContext的installBundle(字符串,InputStream的)',是這樣的:'context.installBundle(文件名,新的ByteArrayInputStream(b));' – Katona

+0

還沒有..你能給我一個例子如何做到這一點?謝謝 – AKIWEB

+0

我認爲你必須用'context.installBundle(fileName,new ByteArrayInputStream(b))'來替換'context.installBundle(localFilename)'',它應該編譯,如果工作正常,那麼你可以擺脫複製到本地文件夾 – Katona

回答

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