|
25 | 25 | import org.bson.codecs.EncoderContext;
|
26 | 26 | import org.bson.io.BasicOutputBuffer;
|
27 | 27 |
|
| 28 | +import java.io.IOException; |
28 | 29 | import java.net.JarURLConnection;
|
29 | 30 | import java.net.URL;
|
30 | 31 | import java.nio.charset.Charset;
|
| 32 | +import java.security.CodeSource; |
31 | 33 | import java.util.List;
|
32 | 34 | import java.util.jar.Attributes;
|
33 | 35 | import java.util.jar.Manifest;
|
@@ -106,17 +108,22 @@ private static String getDriverVersion() {
|
106 | 108 | String driverVersion = "unknown";
|
107 | 109 |
|
108 | 110 | 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 | + } |
117 | 122 | }
|
118 | 123 | }
|
119 |
| - } catch (Exception e) { |
| 124 | + } catch (SecurityException e) { |
| 125 | + // do nothing |
| 126 | + } catch (IOException e) { |
120 | 127 | // do nothing
|
121 | 128 | }
|
122 | 129 | return driverVersion;
|
|
0 commit comments