30
30
import java .net .URL ;
31
31
import java .nio .charset .Charset ;
32
32
import java .security .CodeSource ;
33
+ import java .security .ProtectionDomain ;
33
34
import java .util .List ;
34
35
import java .util .jar .Attributes ;
35
36
import java .util .jar .Manifest ;
@@ -106,11 +107,9 @@ private static boolean nameMatches(final String name, final String... prefixes)
106
107
107
108
private static String getDriverVersion () {
108
109
String driverVersion = "unknown" ;
109
-
110
- try {
111
- CodeSource codeSource = InternalStreamConnectionInitializer .class .getProtectionDomain ().getCodeSource ();
112
- if (codeSource != null ) {
113
- String path = codeSource .getLocation ().getPath ();
110
+ String path = getCodeSourcePath ();
111
+ if (path != null ) {
112
+ try {
114
113
URL jarUrl = path .endsWith (".jar" ) ? new URL ("jar:file:" + path + "!/" ) : null ;
115
114
if (jarUrl != null ) {
116
115
JarURLConnection jarURLConnection = (JarURLConnection ) jarUrl .openConnection ();
@@ -120,14 +119,28 @@ private static String getDriverVersion() {
120
119
driverVersion = version ;
121
120
}
122
121
}
122
+ } catch (IOException e ) {
123
+ // do nothing
123
124
}
124
- } catch (SecurityException e ) {
125
- // do nothing
126
- } catch (IOException e ) {
127
- // do nothing
128
125
}
129
126
return driverVersion ;
130
127
}
128
+
129
+ private static String getCodeSourcePath () {
130
+ String path = null ;
131
+ ProtectionDomain protectionDomain = InternalStreamConnectionInitializer .class .getProtectionDomain ();
132
+ if (protectionDomain != null ) {
133
+ CodeSource codeSource = protectionDomain .getCodeSource ();
134
+ if (codeSource != null ) {
135
+ URL location = codeSource .getLocation ();
136
+ if (location != null ) {
137
+ path = location .getPath ();
138
+ }
139
+ }
140
+ }
141
+ return path ;
142
+ }
143
+
131
144
static BsonDocument createClientMetadataDocument (final String applicationName ) {
132
145
return createClientMetadataDocument (applicationName , null );
133
146
}
0 commit comments