BundleStorageState createStorageState(long bundleId, String location, VirtualFile rootFile) throws IOException { if (location == null) throw new IllegalArgumentException("Null location"); // Make the bundle's storage dir String bundlePath = getStorageDir(bundleId).getAbsolutePath(); File bundleDir = new File(bundlePath); bundleDir.mkdirs(); Properties props = BundleStorageState.loadProperties(bundleDir); String previousRev = props.getProperty(BundleStorageState.PROPERTY_BUNDLE_REV); int revision = (previousRev != null ? Integer.parseInt(previousRev) + 1 : 0); if (rootFile != null) { File revFile = new File(bundlePath + "/bundle-" + bundleId + "-rev-" + revision + ".jar"); FileOutputStream output = new FileOutputStream(revFile); InputStream input = rootFile.openStream(); try { VFSUtils.copyStream(input, output); } finally { input.close(); output.close(); } props.put(BundleStorageState.PROPERTY_BUNDLE_FILE, revFile.getName()); } // Write the bundle properties props.put(BundleStorageState.PROPERTY_BUNDLE_LOCATION, location); props.put(BundleStorageState.PROPERTY_BUNDLE_ID, new Long(bundleId).toString()); props.put(BundleStorageState.PROPERTY_BUNDLE_REV, new Integer(revision).toString()); props.put( BundleStorageState.PROPERTY_LAST_MODIFIED, new Long(System.currentTimeMillis()).toString()); return BundleStorageState.createBundleStorageState(bundleDir, rootFile, props); }
@Test public void testBundleStorageForExternalFile() throws Exception { BundleStoragePlugin storagePlugin = getFrameworkState().getBundleStoragePlugin(); assertNotNull("BundleStoragePlugin not null", storagePlugin); File file = new File(storagePlugin.getStorageDir(0) + "/testBundleExternalFile.jar"); FileOutputStream fos = new FileOutputStream(file); VFSUtils.copyStream(toInputStream(getArchive()), fos); fos.close(); VirtualFile rootFile = AbstractVFS.toVirtualFile(file.toURI().toURL()); InternalStorageState storageState = storagePlugin.createStorageState(1, file.getAbsolutePath(), 1, rootFile); assertStorageState(storageState); storagePlugin.deleteStorageState(storageState); File storageDir = storageState.getStorageDir(); assertFalse("Storage dir deleted", storageDir.exists()); }