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);
  }
 List<BundleStorageState> getBundleStorageStates() throws IOException {
   List<BundleStorageState> states = new ArrayList<BundleStorageState>();
   File[] storageDirs = getStorageArea().listFiles();
   if (storageDirs != null) {
     for (File storageDir : storageDirs) {
       log.debugf("Creating storage state from: %s", storageDir);
       BundleStorageState storageState = BundleStorageState.createFromStorage(storageDir);
       states.add(storageState);
     }
   }
   return Collections.unmodifiableList(states);
 }