Skip to content

Commit b78aa3f

Browse files
committed
JAVA-2308: Improve exception handling getting the driver version from the manifest
1 parent b1ea80b commit b78aa3f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

driver-core/src/main/com/mongodb/connection/ClientMetadataHelper.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import org.bson.codecs.EncoderContext;
2626
import org.bson.io.BasicOutputBuffer;
2727

28+
import java.io.IOException;
2829
import java.net.JarURLConnection;
2930
import java.net.URL;
3031
import java.nio.charset.Charset;
32+
import java.security.CodeSource;
3133
import java.util.List;
3234
import java.util.jar.Attributes;
3335
import java.util.jar.Manifest;
@@ -106,17 +108,22 @@ private static String getDriverVersion() {
106108
String driverVersion = "unknown";
107109

108110
try {
109-
String path = InternalStreamConnectionInitializer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
110-
URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
111-
if (jarUrl != null) {
112-
JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
113-
Manifest manifest = jarURLConnection.getManifest();
114-
String version = (String) manifest.getMainAttributes().get(new Attributes.Name("Build-Version"));
115-
if (version != null) {
116-
driverVersion = version;
111+
CodeSource codeSource = InternalStreamConnectionInitializer.class.getProtectionDomain().getCodeSource();
112+
if (codeSource != null) {
113+
String path = codeSource.getLocation().getPath();
114+
URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
115+
if (jarUrl != null) {
116+
JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
117+
Manifest manifest = jarURLConnection.getManifest();
118+
String version = (String) manifest.getMainAttributes().get(new Attributes.Name("Build-Version"));
119+
if (version != null) {
120+
driverVersion = version;
121+
}
117122
}
118123
}
119-
} catch (Exception e) {
124+
} catch (SecurityException e) {
125+
// do nothing
126+
} catch (IOException e) {
120127
// do nothing
121128
}
122129
return driverVersion;

0 commit comments

Comments
 (0)