From dda4ee63b6587bd6a3f3152446fd063588e97aa2 Mon Sep 17 00:00:00 2001 From: Amit Kumar Mondal Date: Mon, 10 Jul 2023 10:08:04 +0200 Subject: [PATCH] Fixed NPE while calculating storage size fixes #640 --- .../console/agent/admin/XBundleAdmin.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/com.osgifx.console.agent/src/main/java/com/osgifx/console/agent/admin/XBundleAdmin.java b/com.osgifx.console.agent/src/main/java/com/osgifx/console/agent/admin/XBundleAdmin.java index 667e6773..c4f29827 100644 --- a/com.osgifx.console.agent/src/main/java/com/osgifx/console/agent/admin/XBundleAdmin.java +++ b/com.osgifx.console.agent/src/main/java/com/osgifx/console/agent/admin/XBundleAdmin.java @@ -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; @@ -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); @@ -135,6 +136,21 @@ public static XBundleDTO toDTO(final Bundle bundle, final BundleStartTimeCalcula return dto; } + private static long getStorageSize(final Bundle bundle) { + final BundleContext bundleContext = bundle.getBundleContext(); + final String bsn = bundle.getSymbolicName(); + if (bundleContext == null) { + logger.atDebug().msg("Bundle context is null for '{}'").arg(bsn).log(); + return -1; + } + final File storage = bundleContext.getDataFile(""); + if (storage == null) { + logger.atDebug().msg("Bundle storage is null for '{}'").arg(bsn).log(); + 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