Skip to content

Commit

Permalink
Fixed NPE while calculating storage size
Browse files Browse the repository at this point in the history
fixes #640
  • Loading branch information
amitjoy committed Jul 13, 2023
1 parent 09b8c74 commit 5438a1b
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.osgi.framework.namespace.HostNamespace.HOST_NAMESPACE;
import static org.osgi.framework.wiring.BundleRevision.PACKAGE_NAMESPACE;

import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -106,7 +107,7 @@ public static XBundleDTO toDTO(final Bundle bundle, final BundleStartTimeCalcula
dto.category = getHeader(bundle, BUNDLE_CATEGORY);
dto.isFragment = getHeader(bundle, FRAGMENT_HOST) != null;
dto.lastModified = bundle.getLastModified();
dto.dataFolderSize = bundle.getBundleContext().getDataFile("").length();
dto.dataFolderSize = getStorageSize(bundle);
dto.documentation = getHeader(bundle, BUNDLE_DOCURL);
dto.vendor = getHeader(bundle, BUNDLE_VENDOR);
dto.description = getHeader(bundle, BUNDLE_DESCRIPTION);
Expand Down Expand Up @@ -135,6 +136,18 @@ public static XBundleDTO toDTO(final Bundle bundle, final BundleStartTimeCalcula
return dto;
}

private static long getStorageSize(final Bundle bundle) {
final BundleContext bundleContext = bundle.getBundleContext();
if (bundleContext == null) {
return -1;
}
final File storage = bundleContext.getDataFile("");
if (storage == null) {
return -1;
}
return storage.length();
}

/**
* This has been introduced to catch any Exception that might occur while
* adapting a bundle instance which is not valid anymore, for example, if the
Expand Down

0 comments on commit 5438a1b

Please sign in to comment.